TransferFundFlowTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Tests\Unit\Transfer;
  3. use App\Module\Fund\Services\FundService;
  4. use App\Module\Fund\Services\AccountService;
  5. use App\Module\Fund\Enums\FUND_TYPE;
  6. use App\Module\Transfer\Services\TransferService;
  7. use App\Module\Transfer\Models\TransferApp;
  8. use Tests\TestCase;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * Transfer模块fund_to_uid和fund_in_uid字段功能测试
  12. */
  13. class TransferFundFlowTest extends TestCase
  14. {
  15. protected $testUserId = 1001;
  16. protected $targetUserId = 1002;
  17. protected $sourceUserId = 1003;
  18. protected $fundId = 1; // FUND1
  19. public function setUp(): void
  20. {
  21. parent::setUp();
  22. // 确保测试用户账户存在
  23. AccountService::check4user($this->testUserId);
  24. AccountService::check4user($this->targetUserId);
  25. AccountService::check4user($this->sourceUserId);
  26. // 为测试用户充值
  27. $testFund = new FundService($this->testUserId, $this->fundId);
  28. if ($testFund->balance() < 1000) {
  29. $testFund->admin_operate(1, FUND_TYPE::FUND1, 1000, 'TEST充值');
  30. }
  31. // 为来源用户充值
  32. $sourceFund = new FundService($this->sourceUserId, $this->fundId);
  33. if ($sourceFund->balance() < 1000) {
  34. $sourceFund->admin_operate(1, FUND_TYPE::FUND1, 1000, 'TEST充值');
  35. }
  36. }
  37. /**
  38. * 测试转出时fund_to_uid字段的作用
  39. */
  40. public function test_transfer_out_with_fund_to_uid()
  41. {
  42. // 创建测试应用配置,设置fund_to_uid
  43. $app = new TransferApp();
  44. $app->keyname = 'test_transfer_out';
  45. $app->title = '测试转出应用';
  46. $app->fund_id = $this->fundId;
  47. $app->fund_to_uid = $this->targetUserId; // 设置转入目标账户
  48. $app->fund_in_uid = 0;
  49. $app->mode = 1; // 内部模式
  50. $app->status = 1;
  51. $app->save();
  52. // 记录转账前的余额
  53. $testFundBefore = new FundService($this->testUserId, $this->fundId);
  54. $targetFundBefore = new FundService($this->targetUserId, $this->fundId);
  55. $testBalanceBefore = $testFundBefore->balance();
  56. $targetBalanceBefore = $targetFundBefore->balance();
  57. // 执行转出操作
  58. $amount = 100.0;
  59. $result = TransferService::createTransferOut([
  60. 'app_key' => 'test_transfer_out',
  61. 'user_id' => $this->testUserId,
  62. 'amount' => $amount,
  63. 'out_order_id' => 'test_' . time()
  64. ]);
  65. // 验证转账结果
  66. $this->assertNotNull($result);
  67. $this->assertIsObject($result);
  68. // 验证余额变化
  69. $testFundAfter = new FundService($this->testUserId, $this->fundId);
  70. $targetFundAfter = new FundService($this->targetUserId, $this->fundId);
  71. $testBalanceAfter = $testFundAfter->balance();
  72. $targetBalanceAfter = $targetFundAfter->balance();
  73. // 测试用户余额应该减少
  74. $this->assertEquals($testBalanceBefore - $amount, $testBalanceAfter, '测试用户余额应该减少');
  75. // 目标用户余额应该增加
  76. $this->assertEquals($targetBalanceBefore + $amount, $targetBalanceAfter, '目标用户余额应该增加');
  77. echo "转出测试完成:\n";
  78. echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter}\n";
  79. echo "目标用户余额变化: {$targetBalanceBefore} -> {$targetBalanceAfter}\n";
  80. // 清理测试数据
  81. $app->delete();
  82. }
  83. /**
  84. * 测试转入时fund_in_uid字段的作用
  85. */
  86. public function test_transfer_in_with_fund_in_uid()
  87. {
  88. // 创建测试应用配置,设置fund_in_uid
  89. $app = new TransferApp();
  90. $app->keyname = 'test_transfer_in';
  91. $app->title = '测试转入应用';
  92. $app->fund_id = $this->fundId;
  93. $app->fund_to_uid = 0;
  94. $app->fund_in_uid = $this->sourceUserId; // 设置转入来源账户
  95. $app->mode = 1; // 内部模式
  96. $app->status = 1;
  97. $app->save();
  98. // 记录转账前的余额
  99. $testFundBefore = new FundService($this->testUserId, $this->fundId);
  100. $sourceFundBefore = new FundService($this->sourceUserId, $this->fundId);
  101. $testBalanceBefore = $testFundBefore->balance();
  102. $sourceBalanceBefore = $sourceFundBefore->balance();
  103. // 执行转入操作
  104. $amount = 100.0;
  105. $result = TransferService::createTransferIn([
  106. 'app_key' => 'test_transfer_in',
  107. 'user_id' => $this->testUserId,
  108. 'amount' => $amount,
  109. 'out_order_id' => 'test_' . time()
  110. ]);
  111. // 验证转账结果
  112. $this->assertNotNull($result);
  113. $this->assertIsObject($result);
  114. // 验证余额变化
  115. $testFundAfter = new FundService($this->testUserId, $this->fundId);
  116. $sourceFundAfter = new FundService($this->sourceUserId, $this->fundId);
  117. $testBalanceAfter = $testFundAfter->balance();
  118. $sourceBalanceAfter = $sourceFundAfter->balance();
  119. // 测试用户余额应该增加
  120. $this->assertEquals($testBalanceBefore + $amount, $testBalanceAfter, '测试用户余额应该增加');
  121. // 来源用户余额应该减少
  122. $this->assertEquals($sourceBalanceBefore - $amount, $sourceBalanceAfter, '来源用户余额应该减少');
  123. echo "转入测试完成:\n";
  124. echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter}\n";
  125. echo "来源用户余额变化: {$sourceBalanceBefore} -> {$sourceBalanceAfter}\n";
  126. // 清理测试数据
  127. $app->delete();
  128. }
  129. /**
  130. * 测试转出时fund_to_uid为空的情况(资金消失)
  131. */
  132. public function test_transfer_out_without_fund_to_uid()
  133. {
  134. // 创建测试应用配置,不设置fund_to_uid
  135. $app = new TransferApp();
  136. $app->keyname = 'test_transfer_out_empty';
  137. $app->title = '测试转出应用(无目标)';
  138. $app->fund_id = $this->fundId;
  139. $app->fund_to_uid = 0; // 不设置转入目标账户
  140. $app->fund_in_uid = 0;
  141. $app->mode = 1; // 内部模式
  142. $app->status = 1;
  143. $app->save();
  144. // 记录转账前的余额
  145. $testFundBefore = new FundService($this->testUserId, $this->fundId);
  146. $testBalanceBefore = $testFundBefore->balance();
  147. // 执行转出操作
  148. $amount = 50.0;
  149. $result = TransferService::createTransferOut([
  150. 'app_key' => 'test_transfer_out_empty',
  151. 'user_id' => $this->testUserId,
  152. 'amount' => $amount,
  153. 'out_order_id' => 'test_' . time()
  154. ]);
  155. // 验证转账结果
  156. $this->assertNotNull($result);
  157. // 验证余额变化
  158. $testFundAfter = new FundService($this->testUserId, $this->fundId);
  159. $testBalanceAfter = $testFundAfter->balance();
  160. // 测试用户余额应该减少(资金消失)
  161. $this->assertEquals($testBalanceBefore - $amount, $testBalanceAfter, '测试用户余额应该减少');
  162. echo "转出测试(无目标)完成:\n";
  163. echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter} (资金消失)\n";
  164. // 清理测试数据
  165. $app->delete();
  166. }
  167. /**
  168. * 测试转入时fund_in_uid为空的情况(资金凭空产生)
  169. */
  170. public function test_transfer_in_without_fund_in_uid()
  171. {
  172. // 创建测试应用配置,不设置fund_in_uid
  173. $app = new TransferApp();
  174. $app->keyname = 'test_transfer_in_empty';
  175. $app->title = '测试转入应用(无来源)';
  176. $app->fund_id = $this->fundId;
  177. $app->fund_to_uid = 0;
  178. $app->fund_in_uid = 0; // 不设置转入来源账户
  179. $app->mode = 1; // 内部模式
  180. $app->status = 1;
  181. $app->save();
  182. // 记录转账前的余额
  183. $testFundBefore = new FundService($this->testUserId, $this->fundId);
  184. $testBalanceBefore = $testFundBefore->balance();
  185. // 执行转入操作
  186. $amount = 50.0;
  187. $result = TransferService::createTransferIn([
  188. 'app_key' => 'test_transfer_in_empty',
  189. 'user_id' => $this->testUserId,
  190. 'amount' => $amount,
  191. 'out_order_id' => 'test_' . time()
  192. ]);
  193. // 验证转账结果
  194. $this->assertNotNull($result);
  195. // 验证余额变化
  196. $testFundAfter = new FundService($this->testUserId, $this->fundId);
  197. $testBalanceAfter = $testFundAfter->balance();
  198. // 测试用户余额应该增加(资金凭空产生)
  199. $this->assertEquals($testBalanceBefore + $amount, $testBalanceAfter, '测试用户余额应该增加');
  200. echo "转入测试(无来源)完成:\n";
  201. echo "测试用户余额变化: {$testBalanceBefore} -> {$testBalanceAfter} (资金凭空产生)\n";
  202. // 清理测试数据
  203. $app->delete();
  204. }
  205. }