TransferServiceTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Module\Transfer\Tests\Unit;
  3. use App\Module\Transfer\Services\TransferService;
  4. use App\Module\Transfer\Models\TransferApp;
  5. use App\Module\Transfer\Models\TransferOrder;
  6. use App\Module\Transfer\Enums\TransferType;
  7. use App\Module\Transfer\Enums\TransferStatus;
  8. use App\Module\Transfer\Exceptions\TransferException;
  9. use Tests\TestCase;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. /**
  12. * Transfer服务单元测试
  13. */
  14. class TransferServiceTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected TransferApp $testApp;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. // 创建测试应用
  22. $this->testApp = TransferApp::create([
  23. 'keyname' => 'test_app',
  24. 'title' => '测试应用',
  25. 'description' => '用于单元测试的应用',
  26. 'out_id' => 1001,
  27. 'currency_id' => 2,
  28. 'fund_id' => 2,
  29. 'exchange_rate' => 1.0000,
  30. 'is_enabled' => true,
  31. ]);
  32. }
  33. /**
  34. * 测试创建转入订单
  35. */
  36. public function test_create_transfer_in_order()
  37. {
  38. $data = [
  39. 'transfer_app_id' => $this->testApp->id,
  40. 'business_id' => 'TEST_IN_' . time(),
  41. 'user_id' => 1,
  42. 'amount' => '100.0000000000',
  43. 'remark' => '测试转入',
  44. ];
  45. $result = TransferService::createTransferIn($data);
  46. $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result);
  47. $this->assertEquals(TransferType::IN, $result->type);
  48. $this->assertEquals(TransferStatus::CREATED, $result->status);
  49. $this->assertEquals('100.0000000000', $result->amount);
  50. }
  51. /**
  52. * 测试创建转出订单
  53. */
  54. public function test_create_transfer_out_order()
  55. {
  56. $data = [
  57. 'transfer_app_id' => $this->testApp->id,
  58. 'user_id' => 1,
  59. 'amount' => '50.0000000000',
  60. 'password' => 'test123',
  61. 'remark' => '测试转出',
  62. ];
  63. $result = TransferService::createTransferOut($data);
  64. $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result);
  65. $this->assertEquals(TransferType::OUT, $result->type);
  66. $this->assertEquals(TransferStatus::CREATED, $result->status);
  67. $this->assertEquals('50.0000000000', $result->amount);
  68. }
  69. /**
  70. * 测试获取订单详情
  71. */
  72. public function test_get_order_detail()
  73. {
  74. // 创建测试订单
  75. $order = TransferOrder::create([
  76. 'transfer_app_id' => $this->testApp->id,
  77. 'out_order_id' => 'TEST_ORDER_' . time(),
  78. 'user_id' => 1,
  79. 'type' => TransferType::IN,
  80. 'status' => TransferStatus::COMPLETED,
  81. 'out_amount' => '100.0000000000',
  82. 'amount' => '100.0000000000',
  83. 'exchange_rate' => 1.0000,
  84. 'currency_id' => 2,
  85. 'fund_id' => 2,
  86. ]);
  87. $result = TransferService::getOrderDetail($order->id);
  88. $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result);
  89. $this->assertEquals($order->id, $result->id);
  90. $this->assertEquals($order->out_order_id, $result->outOrderId);
  91. }
  92. /**
  93. * 测试获取用户订单列表
  94. */
  95. public function test_get_user_orders()
  96. {
  97. $userId = 1;
  98. // 创建多个测试订单
  99. for ($i = 1; $i <= 3; $i++) {
  100. TransferOrder::create([
  101. 'transfer_app_id' => $this->testApp->id,
  102. 'out_order_id' => 'TEST_ORDER_' . $i,
  103. 'user_id' => $userId,
  104. 'type' => $i % 2 == 0 ? TransferType::OUT : TransferType::IN,
  105. 'status' => TransferStatus::COMPLETED,
  106. 'out_amount' => '100.0000000000',
  107. 'amount' => '100.0000000000',
  108. 'exchange_rate' => 1.0000,
  109. 'currency_id' => 2,
  110. 'fund_id' => 2,
  111. ]);
  112. }
  113. $result = TransferService::getUserOrders($userId);
  114. $this->assertIsArray($result);
  115. $this->assertCount(3, $result);
  116. $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result[0]);
  117. }
  118. /**
  119. * 测试应用不存在的情况
  120. */
  121. public function test_create_order_with_invalid_app()
  122. {
  123. $this->expectException(TransferException::class);
  124. $data = [
  125. 'transfer_app_id' => 99999, // 不存在的应用ID
  126. 'business_id' => 'TEST_INVALID',
  127. 'user_id' => 1,
  128. 'amount' => '100.0000000000',
  129. ];
  130. TransferService::createTransferIn($data);
  131. }
  132. /**
  133. * 测试重复业务ID的情况
  134. */
  135. public function test_create_order_with_duplicate_business_id()
  136. {
  137. $businessId = 'TEST_DUPLICATE_' . time();
  138. // 创建第一个订单
  139. $data1 = [
  140. 'transfer_app_id' => $this->testApp->id,
  141. 'business_id' => $businessId,
  142. 'user_id' => 1,
  143. 'amount' => '100.0000000000',
  144. ];
  145. TransferService::createTransferIn($data1);
  146. // 尝试创建重复业务ID的订单
  147. $this->expectException(TransferException::class);
  148. $data2 = [
  149. 'transfer_app_id' => $this->testApp->id,
  150. 'business_id' => $businessId,
  151. 'user_id' => 2,
  152. 'amount' => '200.0000000000',
  153. ];
  154. TransferService::createTransferIn($data2);
  155. }
  156. /**
  157. * 测试金额验证
  158. */
  159. public function test_create_order_with_invalid_amount()
  160. {
  161. $this->expectException(TransferException::class);
  162. $data = [
  163. 'transfer_app_id' => $this->testApp->id,
  164. 'business_id' => 'TEST_INVALID_AMOUNT',
  165. 'user_id' => 1,
  166. 'amount' => '-100.0000000000', // 负数金额
  167. ];
  168. TransferService::createTransferIn($data);
  169. }
  170. /**
  171. * 测试禁用应用的情况
  172. */
  173. public function test_create_order_with_disabled_app()
  174. {
  175. // 禁用应用
  176. $this->testApp->update(['is_enabled' => false]);
  177. $this->expectException(TransferException::class);
  178. $data = [
  179. 'transfer_app_id' => $this->testApp->id,
  180. 'business_id' => 'TEST_DISABLED_APP',
  181. 'user_id' => 1,
  182. 'amount' => '100.0000000000',
  183. ];
  184. TransferService::createTransferIn($data);
  185. }
  186. /**
  187. * 测试汇率计算
  188. */
  189. public function test_exchange_rate_calculation()
  190. {
  191. // 设置汇率为2.0
  192. $this->testApp->update(['exchange_rate' => 2.0000]);
  193. $data = [
  194. 'transfer_app_id' => $this->testApp->id,
  195. 'business_id' => 'TEST_EXCHANGE_RATE',
  196. 'user_id' => 1,
  197. 'amount' => '100.0000000000', // 外部金额
  198. ];
  199. $result = TransferService::createTransferIn($data);
  200. // 内部金额应该是外部金额除以汇率
  201. $this->assertEquals('50.0000000000', $result->amount);
  202. $this->assertEquals('100.0000000000', $result->outAmount);
  203. $this->assertEquals(2.0000, $result->exchangeRate);
  204. }
  205. /**
  206. * 测试获取应用配置
  207. */
  208. public function test_get_app_config()
  209. {
  210. $result = TransferService::getAppConfig($this->testApp->id);
  211. $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferAppDto::class, $result);
  212. $this->assertEquals($this->testApp->id, $result->id);
  213. $this->assertEquals($this->testApp->keyname, $result->keyname);
  214. $this->assertEquals($this->testApp->title, $result->title);
  215. }
  216. /**
  217. * 测试获取不存在的应用配置
  218. */
  219. public function test_get_invalid_app_config()
  220. {
  221. $this->expectException(TransferException::class);
  222. TransferService::getAppConfig(99999);
  223. }
  224. }