TransferLogicTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace App\Module\Transfer\Tests\Unit;
  3. use App\Module\Transfer\Logics\TransferLogic;
  4. use App\Module\Transfer\Models\TransferApp;
  5. use App\Module\Transfer\Models\TransferOrder;
  6. use App\Module\Transfer\Enums\TransferStatus;
  7. use App\Module\Transfer\Enums\TransferType;
  8. use Tests\TestCase;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Foundation\Testing\WithFaker;
  11. /**
  12. * 划转逻辑测试
  13. */
  14. class TransferLogicTest extends TestCase
  15. {
  16. use RefreshDatabase, WithFaker;
  17. /**
  18. * 设置测试环境
  19. */
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. // 创建测试应用
  24. $this->testApp = TransferApp::create([
  25. 'keyname' => 'test_app',
  26. 'title' => '测试应用',
  27. 'description' => '用于单元测试的应用',
  28. 'out_id' => 1001,
  29. 'currency_id' => 1,
  30. 'fund_id' => 1,
  31. 'exchange_rate' => 1.0000,
  32. 'is_enabled' => true,
  33. ]);
  34. }
  35. /**
  36. * 测试创建转出订单
  37. */
  38. public function testCreateTransferOut(): void
  39. {
  40. $data = [
  41. 'transfer_app_id' => $this->testApp->id,
  42. 'business_id' => 'test_out_' . time(),
  43. 'user_id' => 1001,
  44. 'amount' => '100.00',
  45. 'remark' => '测试转出',
  46. ];
  47. $order = TransferLogic::createTransferOutFromArray($data);
  48. $this->assertInstanceOf(TransferOrder::class, $order);
  49. $this->assertEquals(TransferType::OUT, $order->type);
  50. $this->assertEquals(TransferStatus::CREATED, $order->status);
  51. $this->assertEquals('100.00', $order->amount);
  52. $this->assertEquals('100.00', $order->out_amount);
  53. $this->assertEquals(1.0000, $order->exchange_rate);
  54. }
  55. /**
  56. * 测试创建转入订单
  57. */
  58. public function testCreateTransferIn(): void
  59. {
  60. $data = [
  61. 'transfer_app_id' => $this->testApp->id,
  62. 'business_id' => 'test_in_' . time(),
  63. 'user_id' => 1001,
  64. 'amount' => '200.00',
  65. 'remark' => '测试转入',
  66. ];
  67. $order = TransferLogic::createTransferInFromArray($data);
  68. $this->assertInstanceOf(TransferOrder::class, $order);
  69. $this->assertEquals(TransferType::IN, $order->type);
  70. $this->assertEquals(TransferStatus::CREATED, $order->status);
  71. $this->assertEquals('200.00', $order->out_amount);
  72. $this->assertEquals('200.00', $order->amount);
  73. $this->assertEquals(1.0000, $order->exchange_rate);
  74. }
  75. /**
  76. * 测试汇率转换
  77. */
  78. public function testExchangeRateConversion(): void
  79. {
  80. // 设置汇率为2.0
  81. $this->testApp->update(['exchange_rate' => 2.0000]);
  82. $data = [
  83. 'transfer_app_id' => $this->testApp->id,
  84. 'business_id' => 'test_rate_' . time(),
  85. 'user_id' => 1001,
  86. 'amount' => '100.00',
  87. ];
  88. // 测试转出(内部金额转外部金额)
  89. $outOrder = TransferLogic::createTransferOutFromArray($data);
  90. $this->assertEquals('100.00', $outOrder->amount);
  91. $this->assertEquals('200.00', $outOrder->out_amount);
  92. // 测试转入(外部金额转内部金额)
  93. $data['business_id'] = 'test_rate_in_' . time();
  94. $inOrder = TransferLogic::createTransferInFromArray($data);
  95. $this->assertEquals('100.00', $inOrder->out_amount);
  96. $this->assertEquals('50.00', $inOrder->amount);
  97. }
  98. /**
  99. * 测试业务ID唯一性
  100. */
  101. public function testBusinessIdUniqueness(): void
  102. {
  103. $businessId = 'unique_test_' . time();
  104. $data = [
  105. 'transfer_app_id' => $this->testApp->id,
  106. 'business_id' => $businessId,
  107. 'user_id' => 1001,
  108. 'amount' => '100.00',
  109. ];
  110. // 第一次创建应该成功
  111. $order1 = TransferLogic::createTransferOutFromArray($data);
  112. $this->assertInstanceOf(TransferOrder::class, $order1);
  113. // 第二次创建相同业务ID应该抛出异常
  114. $this->expectException(\Exception::class);
  115. $this->expectExceptionMessage('业务订单ID已存在');
  116. TransferLogic::createTransferOutFromArray($data);
  117. }
  118. /**
  119. * 测试应用不存在的情况
  120. */
  121. public function testAppNotFound(): void
  122. {
  123. $data = [
  124. 'transfer_app_id' => 99999, // 不存在的应用ID
  125. 'business_id' => 'test_not_found_' . time(),
  126. 'user_id' => 1001,
  127. 'amount' => '100.00',
  128. ];
  129. $this->expectException(\Exception::class);
  130. $this->expectExceptionMessage('划转应用不存在');
  131. TransferLogic::createTransferOutFromArray($data);
  132. }
  133. /**
  134. * 测试应用已禁用的情况
  135. */
  136. public function testAppDisabled(): void
  137. {
  138. // 禁用应用
  139. $this->testApp->update(['is_enabled' => false]);
  140. $data = [
  141. 'transfer_app_id' => $this->testApp->id,
  142. 'business_id' => 'test_disabled_' . time(),
  143. 'user_id' => 1001,
  144. 'amount' => '100.00',
  145. ];
  146. $this->expectException(\Exception::class);
  147. $this->expectExceptionMessage('划转应用已禁用');
  148. TransferLogic::createTransferOutFromArray($data);
  149. }
  150. /**
  151. * 测试金额验证
  152. */
  153. public function testAmountValidation(): void
  154. {
  155. $data = [
  156. 'transfer_app_id' => $this->testApp->id,
  157. 'business_id' => 'test_amount_' . time(),
  158. 'user_id' => 1001,
  159. 'amount' => '0', // 无效金额
  160. ];
  161. $this->expectException(\Exception::class);
  162. $this->expectExceptionMessage('金额必须大于0');
  163. TransferLogic::createTransferOutFromArray($data);
  164. }
  165. /**
  166. * 测试内部模式处理
  167. */
  168. public function testInternalMode(): void
  169. {
  170. // 创建内部模式应用(所有API URL为空)
  171. $internalApp = TransferApp::create([
  172. 'keyname' => 'internal_app',
  173. 'title' => '内部应用',
  174. 'out_id' => 1002,
  175. 'currency_id' => 1,
  176. 'fund_id' => 1,
  177. 'exchange_rate' => 1.0000,
  178. 'is_enabled' => true,
  179. // 所有API URL都为空,表示内部模式
  180. ]);
  181. $data = [
  182. 'transfer_app_id' => $internalApp->id,
  183. 'business_id' => 'test_internal_' . time(),
  184. 'user_id' => 1001,
  185. 'amount' => '100.00',
  186. ];
  187. $order = TransferLogic::createTransferOutFromArray($data);
  188. // 内部模式应该直接完成
  189. $this->assertEquals(TransferStatus::COMPLETED, $order->status);
  190. }
  191. /**
  192. * 测试外部模式处理
  193. */
  194. public function testExternalMode(): void
  195. {
  196. // 设置外部API URL
  197. $this->testApp->update([
  198. 'order_out_create_url' => 'https://api.example.com/transfer/out',
  199. 'order_callback_url' => 'https://api.example.com/callback',
  200. ]);
  201. $data = [
  202. 'transfer_app_id' => $this->testApp->id,
  203. 'business_id' => 'test_external_' . time(),
  204. 'user_id' => 1001,
  205. 'amount' => '100.00',
  206. ];
  207. $order = TransferLogic::createTransferOutFromArray($data);
  208. // 外部模式应该是处理中状态
  209. $this->assertEquals(TransferStatus::PROCESSING, $order->status);
  210. }
  211. /**
  212. * 测试回调数据处理
  213. */
  214. public function testCallbackDataHandling(): void
  215. {
  216. $callbackData = [
  217. 'custom_field' => 'test_value',
  218. 'user_info' => ['name' => 'Test User'],
  219. ];
  220. $data = [
  221. 'transfer_app_id' => $this->testApp->id,
  222. 'business_id' => 'test_callback_' . time(),
  223. 'user_id' => 1001,
  224. 'amount' => '100.00',
  225. 'callback_data' => $callbackData,
  226. ];
  227. $order = TransferLogic::createTransferOutFromArray($data);
  228. $this->assertEquals($callbackData, $order->callback_data);
  229. }
  230. /**
  231. * 测试新的参数化API - 转出订单
  232. */
  233. public function testNewParameterizedCreateTransferOut(): void
  234. {
  235. // 使用新的明确参数API
  236. $order = TransferLogic::createTransferOut(
  237. transferAppId: $this->testApp->id,
  238. userId: 1001,
  239. amount: '150.75',
  240. password: 'test_password',
  241. googleCode: '123456',
  242. outUserId: 'ext_user_123',
  243. remark: '新API测试转出',
  244. callbackData: ['test' => 'new_api']
  245. );
  246. $this->assertInstanceOf(TransferOrder::class, $order);
  247. $this->assertEquals($this->testApp->id, $order->transfer_app_id);
  248. $this->assertEquals(1001, $order->user_id);
  249. $this->assertEquals('150.75', $order->out_amount);
  250. $this->assertEquals('150.75', $order->amount);
  251. $this->assertEquals(TransferType::OUT, $order->type);
  252. $this->assertEquals('ext_user_123', $order->out_user_id);
  253. $this->assertEquals('新API测试转出', $order->remark);
  254. $this->assertEquals(['test' => 'new_api'], $order->callback_data);
  255. }
  256. /**
  257. * 测试新的参数化API - 转入订单
  258. */
  259. public function testNewParameterizedCreateTransferIn(): void
  260. {
  261. // 使用新的明确参数API
  262. $order = TransferLogic::createTransferIn(
  263. transferAppId: $this->testApp->id,
  264. userId: 1001,
  265. businessId: 'NEW_API_' . time(),
  266. amount: '250.25',
  267. outUserId: 'ext_user_456',
  268. remark: '新API测试转入',
  269. callbackData: ['source' => 'new_api_test']
  270. );
  271. $this->assertInstanceOf(TransferOrder::class, $order);
  272. $this->assertEquals($this->testApp->id, $order->transfer_app_id);
  273. $this->assertEquals(1001, $order->user_id);
  274. $this->assertEquals('250.25', $order->out_amount);
  275. $this->assertEquals('250.25', $order->amount);
  276. $this->assertEquals(TransferType::IN, $order->type);
  277. $this->assertEquals('ext_user_456', $order->out_user_id);
  278. $this->assertEquals('新API测试转入', $order->remark);
  279. $this->assertEquals(['source' => 'new_api_test'], $order->callback_data);
  280. }
  281. /**
  282. * 测试新API的可选参数
  283. */
  284. public function testNewApiOptionalParameters(): void
  285. {
  286. // 只使用必需参数
  287. $order = TransferLogic::createTransferOut(
  288. transferAppId: $this->testApp->id,
  289. userId: 1001,
  290. amount: '100.00',
  291. password: 'test_password'
  292. );
  293. $this->assertInstanceOf(TransferOrder::class, $order);
  294. $this->assertNull($order->out_user_id);
  295. $this->assertNull($order->remark);
  296. $this->assertEquals([], $order->callback_data);
  297. }
  298. /**
  299. * 清理测试环境
  300. */
  301. protected function tearDown(): void
  302. {
  303. parent::tearDown();
  304. }
  305. }