| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace App\Module\Mex\Tests;
- use App\Module\AppGame\Validations\MatchexchangeAddValidation;
- use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
- use App\Module\Mex\Logic\FundLogic;
- use App\Module\Fund\Services\FundService;
- use Uraus\Kku\Common\MEX_DIRECTION;
- /**
- * AddHandler集成测试
- *
- * 测试完整的AddHandler流程,包括验证和服务调用
- */
- class AddHandlerIntegrationTest
- {
- /**
- * 测试用户资金余额查询(不同币种)
- */
- public static function testUserFundBalance(): array
- {
- $results = [];
- $testUserId = 1001; // 测试用户ID
-
- try {
- // 测试钻石币种余额
- $diamondCurrency = FUND_CURRENCY_TYPE::ZUANSHI;
- $diamondAccountType = FundLogic::getAvailableAccountType($diamondCurrency);
-
- if ($diamondAccountType) {
- $diamondFundService = new FundService($testUserId, $diamondAccountType->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'),
- ];
- }
- }
|