AddHandlerIntegrationTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Module\Mex\Tests;
  3. use App\Module\AppGame\Validations\MatchexchangeAddValidation;
  4. use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
  5. use App\Module\Mex\Logic\FundLogic;
  6. use App\Module\Fund\Services\FundService;
  7. use Uraus\Kku\Common\MEX_DIRECTION;
  8. /**
  9. * AddHandler集成测试
  10. *
  11. * 测试完整的AddHandler流程,包括验证和服务调用
  12. */
  13. class AddHandlerIntegrationTest
  14. {
  15. /**
  16. * 测试用户资金余额查询(不同币种)
  17. */
  18. public static function testUserFundBalance(): array
  19. {
  20. $results = [];
  21. $testUserId = 1001; // 测试用户ID
  22. try {
  23. // 测试钻石币种余额
  24. $diamondCurrency = FUND_CURRENCY_TYPE::ZUANSHI;
  25. $diamondAccountType = FundLogic::getAvailableAccountType($diamondCurrency);
  26. if ($diamondAccountType) {
  27. $diamondFundService = new FundService($testUserId, $diamondAccountType->value);
  28. $diamondBalance = $diamondFundService->balance();
  29. $diamondPrecision = $diamondCurrency->getPrecision();
  30. $diamondDisplayBalance = bcdiv($diamondBalance, bcpow('10', $diamondPrecision), $diamondPrecision);
  31. $results['diamond_balance'] = [
  32. 'currency' => $diamondCurrency->name,
  33. 'raw_balance' => $diamondBalance,
  34. 'display_balance' => $diamondDisplayBalance,
  35. 'precision' => $diamondPrecision,
  36. 'account_type' => $diamondAccountType->value,
  37. 'success' => true
  38. ];
  39. }
  40. // 测试金币币种余额
  41. $goldCurrency = FUND_CURRENCY_TYPE::JINBI;
  42. $goldAccountType = FundLogic::getAvailableAccountType($goldCurrency);
  43. if ($goldAccountType) {
  44. $goldFundService = new FundService($testUserId, $goldAccountType->value);
  45. $goldBalance = $goldFundService->balance();
  46. $goldPrecision = $goldCurrency->getPrecision();
  47. $goldDisplayBalance = bcdiv($goldBalance, bcpow('10', $goldPrecision), $goldPrecision);
  48. $results['gold_balance'] = [
  49. 'currency' => $goldCurrency->name,
  50. 'raw_balance' => $goldBalance,
  51. 'display_balance' => $goldDisplayBalance,
  52. 'precision' => $goldPrecision,
  53. 'account_type' => $goldAccountType->value,
  54. 'success' => true
  55. ];
  56. }
  57. } catch (\Exception $e) {
  58. $results['error'] = [
  59. 'success' => false,
  60. 'message' => $e->getMessage()
  61. ];
  62. }
  63. return $results;
  64. }
  65. /**
  66. * 测试资金充足性验证逻辑
  67. */
  68. public static function testFundSufficiencyCheck(): array
  69. {
  70. $results = [];
  71. $testUserId = 1001;
  72. try {
  73. // 测试钻石币种的资金充足性
  74. $diamondCurrency = FUND_CURRENCY_TYPE::ZUANSHI;
  75. $diamondAccountType = FundLogic::getAvailableAccountType($diamondCurrency);
  76. if ($diamondAccountType) {
  77. $diamondFundService = new FundService($testUserId, $diamondAccountType->value);
  78. $diamondBalance = $diamondFundService->balance();
  79. $diamondPrecision = $diamondCurrency->getPrecision();
  80. // 测试不同金额的充足性
  81. $testAmounts = [1.0, 10.0, 100.0, 1000.0];
  82. foreach ($testAmounts as $testAmount) {
  83. $requiredAmountInStorage = (int)bcmul($testAmount, bcpow('10', $diamondPrecision), 0);
  84. $isSufficient = $diamondBalance >= $requiredAmountInStorage;
  85. $results['diamond_sufficiency'][] = [
  86. 'test_amount' => $testAmount,
  87. 'required_storage' => $requiredAmountInStorage,
  88. 'user_balance' => $diamondBalance,
  89. 'is_sufficient' => $isSufficient
  90. ];
  91. }
  92. }
  93. // 测试金币币种的资金充足性
  94. $goldCurrency = FUND_CURRENCY_TYPE::JINBI;
  95. $goldAccountType = FundLogic::getAvailableAccountType($goldCurrency);
  96. if ($goldAccountType) {
  97. $goldFundService = new FundService($testUserId, $goldAccountType->value);
  98. $goldBalance = $goldFundService->balance();
  99. $goldPrecision = $goldCurrency->getPrecision();
  100. // 测试不同金额的充足性
  101. $testAmounts = [1, 10, 100, 1000]; // 金币是整数
  102. foreach ($testAmounts as $testAmount) {
  103. $requiredAmountInStorage = (int)bcmul($testAmount, bcpow('10', $goldPrecision), 0);
  104. $isSufficient = $goldBalance >= $requiredAmountInStorage;
  105. $results['gold_sufficiency'][] = [
  106. 'test_amount' => $testAmount,
  107. 'required_storage' => $requiredAmountInStorage,
  108. 'user_balance' => $goldBalance,
  109. 'is_sufficient' => $isSufficient
  110. ];
  111. }
  112. }
  113. } catch (\Exception $e) {
  114. $results['error'] = [
  115. 'success' => false,
  116. 'message' => $e->getMessage()
  117. ];
  118. }
  119. return $results;
  120. }
  121. /**
  122. * 测试验证流程的完整性
  123. */
  124. public static function testValidationFlow(): array
  125. {
  126. $results = [];
  127. try {
  128. // 模拟AddHandler接收到的数据
  129. $mockRequestData = [
  130. 'user_id' => 1001,
  131. 'itemId' => 10001,
  132. 'price' => 2.5,
  133. 'num' => 10,
  134. 'direction' => 'BUY'
  135. ];
  136. // 测试数据预处理逻辑
  137. $direction = $mockRequestData['direction'];
  138. $directionValue = match(strtoupper($direction)) {
  139. 'SELL' => MEX_DIRECTION::SELL,
  140. 'BUY' => MEX_DIRECTION::BUY,
  141. 'DIRECTION_NONE' => MEX_DIRECTION::DIRECTION_NONE,
  142. default => $direction
  143. };
  144. $results['direction_preprocessing'] = [
  145. 'input' => $direction,
  146. 'output' => $directionValue,
  147. 'success' => is_int($directionValue)
  148. ];
  149. // 测试币种添加逻辑
  150. $defaultCurrency = FundLogic::getDefaultCurrency();
  151. $mockRequestData['currency_type'] = $defaultCurrency->value;
  152. $results['currency_addition'] = [
  153. 'added_currency' => $defaultCurrency->value,
  154. 'currency_name' => $defaultCurrency->name,
  155. 'success' => true
  156. ];
  157. // 测试买入订单的资金需求计算
  158. if ($directionValue === MEX_DIRECTION::BUY) {
  159. $totalCost = $mockRequestData['num'] * $mockRequestData['price'];
  160. $precision = $defaultCurrency->getPrecision();
  161. $requiredAmountInStorage = (int)bcmul($totalCost, bcpow('10', $precision), 0);
  162. $results['buy_order_calculation'] = [
  163. 'quantity' => $mockRequestData['num'],
  164. 'price' => $mockRequestData['price'],
  165. 'total_cost' => $totalCost,
  166. 'precision' => $precision,
  167. 'storage_amount' => $requiredAmountInStorage,
  168. 'success' => true
  169. ];
  170. }
  171. } catch (\Exception $e) {
  172. $results['error'] = [
  173. 'success' => false,
  174. 'message' => $e->getMessage()
  175. ];
  176. }
  177. return $results;
  178. }
  179. /**
  180. * 运行所有集成测试
  181. */
  182. public static function runAllTests(): array
  183. {
  184. return [
  185. 'user_fund_balance' => self::testUserFundBalance(),
  186. 'fund_sufficiency_check' => self::testFundSufficiencyCheck(),
  187. 'validation_flow' => self::testValidationFlow(),
  188. 'test_time' => date('Y-m-d H:i:s'),
  189. ];
  190. }
  191. }