testUserId); AccountService::check4user($this->targetUserId); AccountService::check4user($this->sourceUserId); // 为测试用户充值 $testFund = new FundService($this->testUserId, $this->fundId); if ($testFund->balance() < 1000) { $testFund->admin_operate(1, FUND_TYPE::FUND1, 1000, 'TEST充值'); } // 为来源用户充值 $sourceFund = new FundService($this->sourceUserId, $this->fundId); if ($sourceFund->balance() < 1000) { $sourceFund->admin_operate(1, FUND_TYPE::FUND1, 1000, 'TEST充值'); } } /** * 测试转出时fund_to_uid字段的作用 */ public function test_transfer_out_with_fund_to_uid() { // 创建测试应用配置,设置fund_to_uid $app = new TransferApp(); $app->keyname = 'test_transfer_out'; $app->title = '测试转出应用'; $app->fund_id = $this->fundId; $app->fund_to_uid = $this->targetUserId; // 设置转入目标账户 $app->fund_in_uid = 0; $app->mode = 1; // 内部模式 $app->status = 1; $app->save(); // 记录转账前的余额 $testFundBefore = new FundService($this->testUserId, $this->fundId); $targetFundBefore = new FundService($this->targetUserId, $this->fundId); $testBalanceBefore = $testFundBefore->balance(); $targetBalanceBefore = $targetFundBefore->balance(); // 执行转出操作 $amount = 100.0; $result = TransferService::createTransferOut([ 'app_key' => 'test_transfer_out', 'user_id' => $this->testUserId, 'amount' => $amount, 'out_order_id' => 'test_' . time() ]); // 验证转账结果 $this->assertNotNull($result); $this->assertIsObject($result); // 验证余额变化 $testFundAfter = new FundService($this->testUserId, $this->fundId); $targetFundAfter = new FundService($this->targetUserId, $this->fundId); $testBalanceAfter = $testFundAfter->balance(); $targetBalanceAfter = $targetFundAfter->balance(); // 测试用户余额应该减少 $this->assertEquals($testBalanceBefore - $amount, $testBalanceAfter, '测试用户余额应该减少'); // 目标用户余额应该增加 $this->assertEquals($targetBalanceBefore + $amount, $targetBalanceAfter, '目标用户余额应该增加'); echo "转出测试完成:\n"; echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter}\n"; echo "目标用户余额变化: {$targetBalanceBefore} -> {$targetBalanceAfter}\n"; // 清理测试数据 $app->delete(); } /** * 测试转入时fund_in_uid字段的作用 */ public function test_transfer_in_with_fund_in_uid() { // 创建测试应用配置,设置fund_in_uid $app = new TransferApp(); $app->keyname = 'test_transfer_in'; $app->title = '测试转入应用'; $app->fund_id = $this->fundId; $app->fund_to_uid = 0; $app->fund_in_uid = $this->sourceUserId; // 设置转入来源账户 $app->mode = 1; // 内部模式 $app->status = 1; $app->save(); // 记录转账前的余额 $testFundBefore = new FundService($this->testUserId, $this->fundId); $sourceFundBefore = new FundService($this->sourceUserId, $this->fundId); $testBalanceBefore = $testFundBefore->balance(); $sourceBalanceBefore = $sourceFundBefore->balance(); // 执行转入操作 $amount = 100.0; $result = TransferService::createTransferIn([ 'app_key' => 'test_transfer_in', 'user_id' => $this->testUserId, 'amount' => $amount, 'out_order_id' => 'test_' . time() ]); // 验证转账结果 $this->assertNotNull($result); $this->assertIsObject($result); // 验证余额变化 $testFundAfter = new FundService($this->testUserId, $this->fundId); $sourceFundAfter = new FundService($this->sourceUserId, $this->fundId); $testBalanceAfter = $testFundAfter->balance(); $sourceBalanceAfter = $sourceFundAfter->balance(); // 测试用户余额应该增加 $this->assertEquals($testBalanceBefore + $amount, $testBalanceAfter, '测试用户余额应该增加'); // 来源用户余额应该减少 $this->assertEquals($sourceBalanceBefore - $amount, $sourceBalanceAfter, '来源用户余额应该减少'); echo "转入测试完成:\n"; echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter}\n"; echo "来源用户余额变化: {$sourceBalanceBefore} -> {$sourceBalanceAfter}\n"; // 清理测试数据 $app->delete(); } /** * 测试转出时fund_to_uid为空的情况(资金消失) */ public function test_transfer_out_without_fund_to_uid() { // 创建测试应用配置,不设置fund_to_uid $app = new TransferApp(); $app->keyname = 'test_transfer_out_empty'; $app->title = '测试转出应用(无目标)'; $app->fund_id = $this->fundId; $app->fund_to_uid = 0; // 不设置转入目标账户 $app->fund_in_uid = 0; $app->mode = 1; // 内部模式 $app->status = 1; $app->save(); // 记录转账前的余额 $testFundBefore = new FundService($this->testUserId, $this->fundId); $testBalanceBefore = $testFundBefore->balance(); // 执行转出操作 $amount = 50.0; $result = TransferService::createTransferOut([ 'app_key' => 'test_transfer_out_empty', 'user_id' => $this->testUserId, 'amount' => $amount, 'out_order_id' => 'test_' . time() ]); // 验证转账结果 $this->assertNotNull($result); // 验证余额变化 $testFundAfter = new FundService($this->testUserId, $this->fundId); $testBalanceAfter = $testFundAfter->balance(); // 测试用户余额应该减少(资金消失) $this->assertEquals($testBalanceBefore - $amount, $testBalanceAfter, '测试用户余额应该减少'); echo "转出测试(无目标)完成:\n"; echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter} (资金消失)\n"; // 清理测试数据 $app->delete(); } /** * 测试转入时fund_in_uid为空的情况(资金凭空产生) */ public function test_transfer_in_without_fund_in_uid() { // 创建测试应用配置,不设置fund_in_uid $app = new TransferApp(); $app->keyname = 'test_transfer_in_empty'; $app->title = '测试转入应用(无来源)'; $app->fund_id = $this->fundId; $app->fund_to_uid = 0; $app->fund_in_uid = 0; // 不设置转入来源账户 $app->mode = 1; // 内部模式 $app->status = 1; $app->save(); // 记录转账前的余额 $testFundBefore = new FundService($this->testUserId, $this->fundId); $testBalanceBefore = $testFundBefore->balance(); // 执行转入操作 $amount = 50.0; $result = TransferService::createTransferIn([ 'app_key' => 'test_transfer_in_empty', 'user_id' => $this->testUserId, 'amount' => $amount, 'out_order_id' => 'test_' . time() ]); // 验证转账结果 $this->assertNotNull($result); // 验证余额变化 $testFundAfter = new FundService($this->testUserId, $this->fundId); $testBalanceAfter = $testFundAfter->balance(); // 测试用户余额应该增加(资金凭空产生) $this->assertEquals($testBalanceBefore + $amount, $testBalanceAfter, '测试用户余额应该增加'); echo "转入测试(无来源)完成:\n"; echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter} (资金凭空产生)\n"; // 清理测试数据 $app->delete(); } }