DiamondApiTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Module\OpenAPI\Tests;
  3. use App\Module\OpenAPI\Services\DeveloperAccountService;
  4. use App\Module\OpenAPI\Models\OpenApiApp;
  5. use App\Module\Fund\Services\FundService;
  6. use App\Module\Fund\Enums\FUND_TYPE;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * 钻石API测试类
  10. *
  11. * 测试钻石充值和提取的API接口
  12. */
  13. class DiamondApiTest extends TestCase
  14. {
  15. /**
  16. * 测试开发者账户创建
  17. */
  18. public function testCreateDeveloperAccounts()
  19. {
  20. // 创建测试应用(不保存到数据库)
  21. $app = new OpenApiApp();
  22. $app->id = 1;
  23. $app->name = '测试应用';
  24. $app->app_id = str_repeat('a', 32);
  25. $app->app_secret = str_repeat('b', 64);
  26. $app->status = 'ACTIVE';
  27. $app->user_id = 1;
  28. // 测试账户ID生成
  29. $rechargeUserId = DeveloperAccountService::getRechargeUserId($app->id);
  30. $withdrawUserId = DeveloperAccountService::getWithdrawUserId($app->id);
  31. $this->assertEquals(100001, $rechargeUserId);
  32. $this->assertEquals(200001, $withdrawUserId);
  33. }
  34. /**
  35. * 测试账户检查功能
  36. */
  37. public function testCheckDeveloperAccounts()
  38. {
  39. $app = new OpenApiApp();
  40. $app->id = 2;
  41. $app->name = '测试应用2';
  42. // 检查不存在的账户
  43. $check = DeveloperAccountService::checkDeveloperAccounts($app);
  44. $this->assertFalse($check['recharge_account_exists']);
  45. $this->assertFalse($check['withdraw_account_exists']);
  46. $this->assertEquals(100002, $check['recharge_user_id']);
  47. $this->assertEquals(200002, $check['withdraw_user_id']);
  48. }
  49. /**
  50. * 测试钻石精度处理
  51. */
  52. public function testDiamondPrecision()
  53. {
  54. $precision = \App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI->getPrecision();
  55. $this->assertEquals(10, $precision);
  56. // 测试精度格式化
  57. $testAmounts = [
  58. 123.123456789012345 => '123.1234567890',
  59. 100.0 => '100.0000000000',
  60. 0.0000000001 => '0.0000000001',
  61. 999999999.9999999999 => '999999999.9999999999',
  62. ];
  63. foreach ($testAmounts as $input => $expected) {
  64. $formatted = number_format($input, $precision, '.', '');
  65. $this->assertEquals($expected, $formatted, "金额 {$input} 格式化失败");
  66. }
  67. }
  68. /**
  69. * 测试用户ID范围
  70. */
  71. public function testUserIdRanges()
  72. {
  73. // 测试多个应用ID的用户ID生成
  74. $testCases = [
  75. 1 => ['recharge' => 100001, 'withdraw' => 200001],
  76. 100 => ['recharge' => 100100, 'withdraw' => 200100],
  77. 999 => ['recharge' => 100999, 'withdraw' => 200999],
  78. 9999 => ['recharge' => 109999, 'withdraw' => 209999],
  79. ];
  80. foreach ($testCases as $appId => $expected) {
  81. $rechargeUserId = DeveloperAccountService::getRechargeUserId($appId);
  82. $withdrawUserId = DeveloperAccountService::getWithdrawUserId($appId);
  83. $this->assertEquals($expected['recharge'], $rechargeUserId, "应用ID {$appId} 的充值用户ID不正确");
  84. $this->assertEquals($expected['withdraw'], $withdrawUserId, "应用ID {$appId} 的提取用户ID不正确");
  85. }
  86. }
  87. /**
  88. * 测试账户类型映射
  89. */
  90. public function testAccountTypeMapping()
  91. {
  92. // 测试钻石账户类型
  93. $diamondFundType = FUND_TYPE::FUND2;
  94. $this->assertEquals(2, $diamondFundType->value);
  95. // 测试钻石冻结账户类型
  96. $diamondFrozenFundType = FUND_TYPE::FUND3;
  97. $this->assertEquals(3, $diamondFrozenFundType->value);
  98. // 测试币种映射
  99. $diamondCurrency = FUND_TYPE::type2Currency($diamondFundType);
  100. $this->assertEquals(\App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI, $diamondCurrency);
  101. $diamondFrozenCurrency = FUND_TYPE::type2Currency($diamondFrozenFundType);
  102. $this->assertEquals(\App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI, $diamondFrozenCurrency);
  103. }
  104. /**
  105. * 测试错误边界情况
  106. */
  107. public function testErrorBoundaries()
  108. {
  109. // 测试极端应用ID
  110. $extremeCases = [
  111. 0 => ['recharge' => 100000, 'withdraw' => 200000],
  112. -1 => ['recharge' => 99999, 'withdraw' => 199999],
  113. PHP_INT_MAX => ['recharge' => 100000 + PHP_INT_MAX, 'withdraw' => 200000 + PHP_INT_MAX],
  114. ];
  115. foreach ($extremeCases as $appId => $expected) {
  116. $rechargeUserId = DeveloperAccountService::getRechargeUserId($appId);
  117. $withdrawUserId = DeveloperAccountService::getWithdrawUserId($appId);
  118. $this->assertEquals($expected['recharge'], $rechargeUserId, "极端应用ID {$appId} 的充值用户ID不正确");
  119. $this->assertEquals($expected['withdraw'], $withdrawUserId, "极端应用ID {$appId} 的提取用户ID不正确");
  120. }
  121. }
  122. /**
  123. * 测试金额验证边界
  124. */
  125. public function testAmountValidationBoundaries()
  126. {
  127. // 测试有效金额
  128. $validAmounts = [
  129. 0.0000000001, // 最小有效金额
  130. 1.0,
  131. 100.123456789,
  132. 999999999.9999999999, // 接近最大值
  133. ];
  134. foreach ($validAmounts as $amount) {
  135. $this->assertTrue(is_numeric($amount), "金额 {$amount} 应该是数字");
  136. $this->assertGreaterThan(0, $amount, "金额 {$amount} 应该大于0");
  137. }
  138. // 测试无效金额
  139. $invalidAmounts = [
  140. 0,
  141. -1,
  142. -100.5,
  143. 'abc',
  144. null,
  145. false,
  146. ];
  147. foreach ($invalidAmounts as $amount) {
  148. if ($amount !== null && $amount !== false) {
  149. $this->assertTrue(!is_numeric($amount) || $amount <= 0, "金额 {$amount} 应该是无效的");
  150. }
  151. }
  152. }
  153. /**
  154. * 测试并发安全性
  155. */
  156. public function testConcurrencySafety()
  157. {
  158. // 测试相同应用ID多次调用是否返回相同结果
  159. $appId = 123;
  160. $results = [];
  161. for ($i = 0; $i < 10; $i++) {
  162. $results[] = [
  163. 'recharge' => DeveloperAccountService::getRechargeUserId($appId),
  164. 'withdraw' => DeveloperAccountService::getWithdrawUserId($appId),
  165. ];
  166. }
  167. // 所有结果应该相同
  168. $firstResult = $results[0];
  169. foreach ($results as $result) {
  170. $this->assertEquals($firstResult, $result, '并发调用应该返回相同结果');
  171. }
  172. }
  173. }