TradeServiceTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit\Services;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Client;
  6. use SixShop\Wangdian\Response\ApiResponse;
  7. use SixShop\Wangdian\Services\TradeService;
  8. class TradeServiceTest extends TestCase
  9. {
  10. private TradeService $service;
  11. private Client $client;
  12. protected function setUp(): void
  13. {
  14. $this->client = $this->getMockBuilder(Client::class)
  15. ->disableOriginalConstructor()
  16. ->getMock();
  17. $this->service = new TradeService($this->client);
  18. }
  19. public function testPushWithValidData(): void
  20. {
  21. $tradeData = [
  22. 'shop_no' => 'test_shop',
  23. 'trade_list' => [
  24. ['trade_no' => '123', 'amount' => 100]
  25. ]
  26. ];
  27. $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
  28. $this->client
  29. ->expects($this->once())
  30. ->method('call')
  31. ->with(
  32. 'trade_push.php',
  33. [
  34. 'shop_no' => 'test_shop',
  35. 'switch' => 0,
  36. 'trade_list' => json_encode($tradeData['trade_list'], JSON_UNESCAPED_UNICODE)
  37. ]
  38. )
  39. ->willReturn($expectedResponse);
  40. $result = $this->service->push($tradeData);
  41. $this->assertSame($expectedResponse, $result);
  42. }
  43. public function testPushWithSwitchParameter(): void
  44. {
  45. $tradeData = [
  46. 'shop_no' => 'test_shop',
  47. 'switch' => 1,
  48. 'trade_list' => [
  49. ['trade_no' => '123']
  50. ]
  51. ];
  52. $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
  53. $this->client
  54. ->expects($this->once())
  55. ->method('call')
  56. ->with(
  57. 'trade_push.php',
  58. [
  59. 'shop_no' => 'test_shop',
  60. 'switch' => 1,
  61. 'trade_list' => json_encode($tradeData['trade_list'], JSON_UNESCAPED_UNICODE)
  62. ]
  63. )
  64. ->willReturn($expectedResponse);
  65. $result = $this->service->push($tradeData);
  66. $this->assertSame($expectedResponse, $result);
  67. }
  68. public function testPushMissingShopNo(): void
  69. {
  70. $tradeData = [
  71. 'trade_list' => [['trade_no' => '123']]
  72. ];
  73. $this->expectException(\InvalidArgumentException::class);
  74. $this->expectExceptionMessage("Required parameter 'shop_no' is missing or empty");
  75. $this->service->push($tradeData);
  76. }
  77. public function testPushMissingTradeList(): void
  78. {
  79. $tradeData = [
  80. 'shop_no' => 'test_shop'
  81. ];
  82. $this->expectException(\InvalidArgumentException::class);
  83. $this->expectExceptionMessage("Required parameter 'trade_list' is missing or empty");
  84. $this->service->push($tradeData);
  85. }
  86. public function testQuery(): void
  87. {
  88. $params = ['trade_no' => '123'];
  89. $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
  90. $this->client
  91. ->expects($this->once())
  92. ->method('call')
  93. ->with('trade_query.php', $params)
  94. ->willReturn($expectedResponse);
  95. $result = $this->service->query($params);
  96. $this->assertSame($expectedResponse, $result);
  97. }
  98. public function testQueryWithEmptyParams(): void
  99. {
  100. $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
  101. $this->client
  102. ->expects($this->once())
  103. ->method('call')
  104. ->with('trade_query.php', [])
  105. ->willReturn($expectedResponse);
  106. $result = $this->service->query();
  107. $this->assertSame($expectedResponse, $result);
  108. }
  109. public function testQueryLogisticsSync(): void
  110. {
  111. $params = ['shop_no' => 'test_shop'];
  112. $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
  113. $this->client
  114. ->expects($this->once())
  115. ->method('call')
  116. ->with('logistics_sync_query.php', $params)
  117. ->willReturn($expectedResponse);
  118. $result = $this->service->queryLogisticsSync($params);
  119. $this->assertSame($expectedResponse, $result);
  120. }
  121. public function testAckLogisticsSync(): void
  122. {
  123. $logisticsIds = ['log1', 'log2', 'log3'];
  124. $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
  125. $this->client
  126. ->expects($this->once())
  127. ->method('call')
  128. ->with(
  129. 'logistics_sync_ack.php',
  130. ['logistics_ids' => json_encode($logisticsIds, JSON_UNESCAPED_UNICODE)]
  131. )
  132. ->willReturn($expectedResponse);
  133. $result = $this->service->ackLogisticsSync($logisticsIds);
  134. $this->assertSame($expectedResponse, $result);
  135. }
  136. public function testAckLogisticsSyncMissingIds(): void
  137. {
  138. $this->expectException(\InvalidArgumentException::class);
  139. $this->expectExceptionMessage("Required parameter 'logistics_ids' is missing or empty");
  140. // This should now trigger validation error due to empty array
  141. $this->service->ackLogisticsSync([]);
  142. }
  143. public function testQueryGoodsStockChange(): void
  144. {
  145. $params = ['shop_no' => 'test_shop'];
  146. $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
  147. $this->client
  148. ->expects($this->once())
  149. ->method('call')
  150. ->with('api_goods_stock_change_query.php', $params)
  151. ->willReturn($expectedResponse);
  152. $result = $this->service->queryGoodsStockChange($params);
  153. $this->assertSame($expectedResponse, $result);
  154. }
  155. public function testAckGoodsStockChange(): void
  156. {
  157. $changeIds = ['change1', 'change2'];
  158. $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
  159. $this->client
  160. ->expects($this->once())
  161. ->method('call')
  162. ->with(
  163. 'api_goods_stock_change_ack.php',
  164. ['change_ids' => json_encode($changeIds, JSON_UNESCAPED_UNICODE)]
  165. )
  166. ->willReturn($expectedResponse);
  167. $result = $this->service->ackGoodsStockChange($changeIds);
  168. $this->assertSame($expectedResponse, $result);
  169. }
  170. public function testQueryStockoutOrder(): void
  171. {
  172. $params = ['shop_no' => 'test_shop'];
  173. $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
  174. $this->client
  175. ->expects($this->once())
  176. ->method('call')
  177. ->with('stockout_order_query_trade.php', $params)
  178. ->willReturn($expectedResponse);
  179. $result = $this->service->queryStockoutOrder($params);
  180. $this->assertSame($expectedResponse, $result);
  181. }
  182. }