| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Module\OpenAPI\Tests;
- use App\Module\OpenAPI\Services\DeveloperAccountService;
- use App\Module\OpenAPI\Models\OpenApiApp;
- use App\Module\Fund\Services\FundService;
- use App\Module\Fund\Enums\FUND_TYPE;
- use PHPUnit\Framework\TestCase;
- /**
- * 钻石API测试类
- *
- * 测试钻石充值和提取的API接口
- */
- class DiamondApiTest extends TestCase
- {
- /**
- * 测试开发者账户创建
- */
- public function testCreateDeveloperAccounts()
- {
- // 创建测试应用(不保存到数据库)
- $app = new OpenApiApp();
- $app->id = 1;
- $app->name = '测试应用';
- $app->app_id = str_repeat('a', 32);
- $app->app_secret = str_repeat('b', 64);
- $app->status = 'ACTIVE';
- $app->user_id = 1;
- // 测试账户ID生成
- $rechargeUserId = DeveloperAccountService::getRechargeUserId($app->id);
- $withdrawUserId = DeveloperAccountService::getWithdrawUserId($app->id);
- $this->assertEquals(100001, $rechargeUserId);
- $this->assertEquals(200001, $withdrawUserId);
- }
- /**
- * 测试账户检查功能
- */
- public function testCheckDeveloperAccounts()
- {
- $app = new OpenApiApp();
- $app->id = 2;
- $app->name = '测试应用2';
-
- // 检查不存在的账户
- $check = DeveloperAccountService::checkDeveloperAccounts($app);
-
- $this->assertFalse($check['recharge_account_exists']);
- $this->assertFalse($check['withdraw_account_exists']);
- $this->assertEquals(100002, $check['recharge_user_id']);
- $this->assertEquals(200002, $check['withdraw_user_id']);
- }
- /**
- * 测试钻石精度处理
- */
- public function testDiamondPrecision()
- {
- $precision = \App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI->getPrecision();
- $this->assertEquals(10, $precision);
-
- // 测试精度格式化
- $testAmounts = [
- 123.123456789012345 => '123.1234567890',
- 100.0 => '100.0000000000',
- 0.0000000001 => '0.0000000001',
- 999999999.9999999999 => '999999999.9999999999',
- ];
-
- foreach ($testAmounts as $input => $expected) {
- $formatted = number_format($input, $precision, '.', '');
- $this->assertEquals($expected, $formatted, "金额 {$input} 格式化失败");
- }
- }
- /**
- * 测试用户ID范围
- */
- public function testUserIdRanges()
- {
- // 测试多个应用ID的用户ID生成
- $testCases = [
- 1 => ['recharge' => 100001, 'withdraw' => 200001],
- 100 => ['recharge' => 100100, 'withdraw' => 200100],
- 999 => ['recharge' => 100999, 'withdraw' => 200999],
- 9999 => ['recharge' => 109999, 'withdraw' => 209999],
- ];
-
- foreach ($testCases as $appId => $expected) {
- $rechargeUserId = DeveloperAccountService::getRechargeUserId($appId);
- $withdrawUserId = DeveloperAccountService::getWithdrawUserId($appId);
-
- $this->assertEquals($expected['recharge'], $rechargeUserId, "应用ID {$appId} 的充值用户ID不正确");
- $this->assertEquals($expected['withdraw'], $withdrawUserId, "应用ID {$appId} 的提取用户ID不正确");
- }
- }
- /**
- * 测试账户类型映射
- */
- public function testAccountTypeMapping()
- {
- // 测试钻石账户类型
- $diamondFundType = FUND_TYPE::FUND2;
- $this->assertEquals(2, $diamondFundType->value);
-
- // 测试钻石冻结账户类型
- $diamondFrozenFundType = FUND_TYPE::FUND3;
- $this->assertEquals(3, $diamondFrozenFundType->value);
-
- // 测试币种映射
- $diamondCurrency = FUND_TYPE::type2Currency($diamondFundType);
- $this->assertEquals(\App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI, $diamondCurrency);
-
- $diamondFrozenCurrency = FUND_TYPE::type2Currency($diamondFrozenFundType);
- $this->assertEquals(\App\Module\Fund\Enums\FUND_CURRENCY_TYPE::ZUANSHI, $diamondFrozenCurrency);
- }
- /**
- * 测试错误边界情况
- */
- public function testErrorBoundaries()
- {
- // 测试极端应用ID
- $extremeCases = [
- 0 => ['recharge' => 100000, 'withdraw' => 200000],
- -1 => ['recharge' => 99999, 'withdraw' => 199999],
- PHP_INT_MAX => ['recharge' => 100000 + PHP_INT_MAX, 'withdraw' => 200000 + PHP_INT_MAX],
- ];
-
- foreach ($extremeCases as $appId => $expected) {
- $rechargeUserId = DeveloperAccountService::getRechargeUserId($appId);
- $withdrawUserId = DeveloperAccountService::getWithdrawUserId($appId);
-
- $this->assertEquals($expected['recharge'], $rechargeUserId, "极端应用ID {$appId} 的充值用户ID不正确");
- $this->assertEquals($expected['withdraw'], $withdrawUserId, "极端应用ID {$appId} 的提取用户ID不正确");
- }
- }
- /**
- * 测试金额验证边界
- */
- public function testAmountValidationBoundaries()
- {
-
- // 测试有效金额
- $validAmounts = [
- 0.0000000001, // 最小有效金额
- 1.0,
- 100.123456789,
- 999999999.9999999999, // 接近最大值
- ];
-
- foreach ($validAmounts as $amount) {
- $this->assertTrue(is_numeric($amount), "金额 {$amount} 应该是数字");
- $this->assertGreaterThan(0, $amount, "金额 {$amount} 应该大于0");
- }
-
- // 测试无效金额
- $invalidAmounts = [
- 0,
- -1,
- -100.5,
- 'abc',
- null,
- false,
- ];
-
- foreach ($invalidAmounts as $amount) {
- if ($amount !== null && $amount !== false) {
- $this->assertTrue(!is_numeric($amount) || $amount <= 0, "金额 {$amount} 应该是无效的");
- }
- }
- }
- /**
- * 测试并发安全性
- */
- public function testConcurrencySafety()
- {
- // 测试相同应用ID多次调用是否返回相同结果
- $appId = 123;
-
- $results = [];
- for ($i = 0; $i < 10; $i++) {
- $results[] = [
- 'recharge' => DeveloperAccountService::getRechargeUserId($appId),
- 'withdraw' => DeveloperAccountService::getWithdrawUserId($appId),
- ];
- }
-
- // 所有结果应该相同
- $firstResult = $results[0];
- foreach ($results as $result) {
- $this->assertEquals($firstResult, $result, '并发调用应该返回相同结果');
- }
- }
- }
|