FeeServiceTransferTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Tests\Unit\Transfer;
  3. use Tests\TestCase;
  4. use App\Module\Transfer\Services\FeeService;
  5. use App\Module\Transfer\Models\TransferApp;
  6. use App\Module\User\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * FeeService转移方法测试
  11. */
  12. class FeeServiceTransferTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. // 创建测试数据
  19. $this->createTestData();
  20. }
  21. /**
  22. * 创建测试数据
  23. */
  24. private function createTestData()
  25. {
  26. // 创建测试应用
  27. DB::table('kku_transfer_apps')->insert([
  28. 'id' => 1,
  29. 'keyname' => 'test_app',
  30. 'title' => '测试应用',
  31. 'description' => '用于测试的应用',
  32. 'out_id' => 1001,
  33. 'currency_id' => 1,
  34. 'fund_id' => 1,
  35. 'exchange_rate' => 1.0000,
  36. 'is_enabled' => true,
  37. 'allow_transfer_in' => true,
  38. 'allow_transfer_out' => true,
  39. 'fee_in_rate' => 0.0050,
  40. 'fee_out_rate' => 0.0100,
  41. 'fee_in_min' => 0.1000,
  42. 'fee_in_max' => 10.0000,
  43. 'fee_out_min' => 0.2000,
  44. 'fee_out_max' => 20.0000,
  45. 'fee_account_uid' => 1,
  46. 'created_at' => now(),
  47. 'updated_at' => now(),
  48. ]);
  49. // 创建测试用户
  50. DB::table('kku_users')->insert([
  51. 'id' => 1,
  52. 'username' => 'fee_account',
  53. 'email' => 'fee@test.com',
  54. 'password' => bcrypt('password'),
  55. 'created_at' => now(),
  56. 'updated_at' => now(),
  57. ]);
  58. DB::table('kku_users')->insert([
  59. 'id' => 2,
  60. 'username' => 'target_user',
  61. 'email' => 'target@test.com',
  62. 'password' => bcrypt('password'),
  63. 'created_at' => now(),
  64. 'updated_at' => now(),
  65. ]);
  66. // 创建资金账户
  67. DB::table('kku_fund')->insert([
  68. 'user_id' => 1,
  69. 'fund_id' => 1,
  70. 'balance' => 1000.0000,
  71. 'create_time' => time(),
  72. 'update_time' => time(),
  73. ]);
  74. DB::table('kku_fund')->insert([
  75. 'user_id' => 2,
  76. 'fund_id' => 1,
  77. 'balance' => 0.0000,
  78. 'create_time' => time(),
  79. 'update_time' => time(),
  80. ]);
  81. }
  82. /**
  83. * 测试成功转移手续费
  84. */
  85. public function testSuccessfulTransfer()
  86. {
  87. $result = FeeService::transfer(
  88. appId: 1,
  89. targetUserId: 2,
  90. amount: 10.0,
  91. orderId: 12345
  92. );
  93. $this->assertTrue($result, '手续费转移应该成功');
  94. }
  95. /**
  96. * 测试自定义转移类型
  97. */
  98. public function testCustomTransferType()
  99. {
  100. $result = FeeService::transfer(
  101. appId: 1,
  102. targetUserId: 2,
  103. amount: 10.0,
  104. orderId: 12345,
  105. transferType: 'custom_transfer'
  106. );
  107. $this->assertTrue($result, '自定义转移类型应该成功');
  108. }
  109. /**
  110. * 测试无效金额
  111. */
  112. public function testInvalidAmount()
  113. {
  114. $result = FeeService::transfer(
  115. appId: 1,
  116. targetUserId: 2,
  117. amount: 0,
  118. orderId: 12345
  119. );
  120. $this->assertEquals('转移金额必须大于0', $result);
  121. $result = FeeService::transfer(
  122. appId: 1,
  123. targetUserId: 2,
  124. amount: -10.0,
  125. orderId: 12345
  126. );
  127. $this->assertEquals('转移金额必须大于0', $result);
  128. }
  129. /**
  130. * 测试应用不存在
  131. */
  132. public function testAppNotExists()
  133. {
  134. $result = FeeService::transfer(
  135. appId: 999,
  136. targetUserId: 2,
  137. amount: 10.0,
  138. orderId: 12345
  139. );
  140. $this->assertEquals('应用不存在', $result);
  141. }
  142. /**
  143. * 测试目标用户不存在
  144. */
  145. public function testTargetUserNotExists()
  146. {
  147. $result = FeeService::transfer(
  148. appId: 1,
  149. targetUserId: 999,
  150. amount: 10.0,
  151. orderId: 12345
  152. );
  153. $this->assertEquals('目标用户不存在', $result);
  154. }
  155. /**
  156. * 测试余额不足
  157. */
  158. public function testInsufficientBalance()
  159. {
  160. // 清空手续费账户余额
  161. DB::table('kku_fund')
  162. ->where('user_id', 1)
  163. ->where('fund_id', 1)
  164. ->update(['balance' => 0.0000]);
  165. $result = FeeService::transfer(
  166. appId: 1,
  167. targetUserId: 2,
  168. amount: 10.0,
  169. orderId: 12345
  170. );
  171. $this->assertIsString($result);
  172. $this->assertStringContains('not-sufficient-funds', $result);
  173. }
  174. }