value); $diamondBalance = $diamondFundService->balance(); $diamondPrecision = $diamondCurrency->getPrecision(); $diamondDisplayBalance = bcdiv($diamondBalance, bcpow('10', $diamondPrecision), $diamondPrecision); $results['diamond_balance'] = [ 'currency' => $diamondCurrency->name, 'raw_balance' => $diamondBalance, 'display_balance' => $diamondDisplayBalance, 'precision' => $diamondPrecision, 'account_type' => $diamondAccountType->value, 'success' => true ]; } // 测试金币币种余额 $goldCurrency = FUND_CURRENCY_TYPE::JINBI; $goldAccountType = FundLogic::getAvailableAccountType($goldCurrency); if ($goldAccountType) { $goldFundService = new FundService($testUserId, $goldAccountType->value); $goldBalance = $goldFundService->balance(); $goldPrecision = $goldCurrency->getPrecision(); $goldDisplayBalance = bcdiv($goldBalance, bcpow('10', $goldPrecision), $goldPrecision); $results['gold_balance'] = [ 'currency' => $goldCurrency->name, 'raw_balance' => $goldBalance, 'display_balance' => $goldDisplayBalance, 'precision' => $goldPrecision, 'account_type' => $goldAccountType->value, 'success' => true ]; } } catch (\Exception $e) { $results['error'] = [ 'success' => false, 'message' => $e->getMessage() ]; } return $results; } /** * 测试资金充足性验证逻辑 */ public static function testFundSufficiencyCheck(): array { $results = []; $testUserId = 1001; try { // 测试钻石币种的资金充足性 $diamondCurrency = FUND_CURRENCY_TYPE::ZUANSHI; $diamondAccountType = FundLogic::getAvailableAccountType($diamondCurrency); if ($diamondAccountType) { $diamondFundService = new FundService($testUserId, $diamondAccountType->value); $diamondBalance = $diamondFundService->balance(); $diamondPrecision = $diamondCurrency->getPrecision(); // 测试不同金额的充足性 $testAmounts = [1.0, 10.0, 100.0, 1000.0]; foreach ($testAmounts as $testAmount) { $requiredAmountInStorage = (int)bcmul($testAmount, bcpow('10', $diamondPrecision), 0); $isSufficient = $diamondBalance >= $requiredAmountInStorage; $results['diamond_sufficiency'][] = [ 'test_amount' => $testAmount, 'required_storage' => $requiredAmountInStorage, 'user_balance' => $diamondBalance, 'is_sufficient' => $isSufficient ]; } } // 测试金币币种的资金充足性 $goldCurrency = FUND_CURRENCY_TYPE::JINBI; $goldAccountType = FundLogic::getAvailableAccountType($goldCurrency); if ($goldAccountType) { $goldFundService = new FundService($testUserId, $goldAccountType->value); $goldBalance = $goldFundService->balance(); $goldPrecision = $goldCurrency->getPrecision(); // 测试不同金额的充足性 $testAmounts = [1, 10, 100, 1000]; // 金币是整数 foreach ($testAmounts as $testAmount) { $requiredAmountInStorage = (int)bcmul($testAmount, bcpow('10', $goldPrecision), 0); $isSufficient = $goldBalance >= $requiredAmountInStorage; $results['gold_sufficiency'][] = [ 'test_amount' => $testAmount, 'required_storage' => $requiredAmountInStorage, 'user_balance' => $goldBalance, 'is_sufficient' => $isSufficient ]; } } } catch (\Exception $e) { $results['error'] = [ 'success' => false, 'message' => $e->getMessage() ]; } return $results; } /** * 测试验证流程的完整性 */ public static function testValidationFlow(): array { $results = []; try { // 模拟AddHandler接收到的数据 $mockRequestData = [ 'user_id' => 1001, 'itemId' => 10001, 'price' => 2.5, 'num' => 10, 'direction' => 'BUY' ]; // 测试数据预处理逻辑 $direction = $mockRequestData['direction']; $directionValue = match(strtoupper($direction)) { 'SELL' => MEX_DIRECTION::SELL, 'BUY' => MEX_DIRECTION::BUY, 'DIRECTION_NONE' => MEX_DIRECTION::DIRECTION_NONE, default => $direction }; $results['direction_preprocessing'] = [ 'input' => $direction, 'output' => $directionValue, 'success' => is_int($directionValue) ]; // 测试币种添加逻辑 $defaultCurrency = FundLogic::getDefaultCurrency(); $mockRequestData['currency_type'] = $defaultCurrency->value; $results['currency_addition'] = [ 'added_currency' => $defaultCurrency->value, 'currency_name' => $defaultCurrency->name, 'success' => true ]; // 测试买入订单的资金需求计算 if ($directionValue === MEX_DIRECTION::BUY) { $totalCost = $mockRequestData['num'] * $mockRequestData['price']; $precision = $defaultCurrency->getPrecision(); $requiredAmountInStorage = (int)bcmul($totalCost, bcpow('10', $precision), 0); $results['buy_order_calculation'] = [ 'quantity' => $mockRequestData['num'], 'price' => $mockRequestData['price'], 'total_cost' => $totalCost, 'precision' => $precision, 'storage_amount' => $requiredAmountInStorage, 'success' => true ]; } } catch (\Exception $e) { $results['error'] = [ 'success' => false, 'message' => $e->getMessage() ]; } return $results; } /** * 运行所有集成测试 */ public static function runAllTests(): array { return [ 'user_fund_balance' => self::testUserFundBalance(), 'fund_sufficiency_check' => self::testFundSufficiencyCheck(), 'validation_flow' => self::testValidationFlow(), 'test_time' => date('Y-m-d H:i:s'), ]; } }