| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace Tests\Unit\Transfer;
- use App\Module\Fund\Services\FundService;
- use App\Module\Fund\Services\AccountService;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Transfer\Services\TransferService;
- use App\Module\Transfer\Models\TransferApp;
- use Tests\TestCase;
- use Illuminate\Support\Facades\DB;
- /**
- * Transfer模块fund_to_uid和fund_in_uid字段功能测试
- */
- class TransferFundFlowTest extends TestCase
- {
- protected $testUserId = 1001;
- protected $targetUserId = 1002;
- protected $sourceUserId = 1003;
- protected $fundId = 1; // FUND1
- public function setUp(): void
- {
- parent::setUp();
- // 确保测试用户账户存在
- AccountService::check4user($this->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();
- }
- }
|