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, '并发调用应该返回相同结果'); } } }