| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Services;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Client;
- use SixShop\Wangdian\Response\ApiResponse;
- use SixShop\Wangdian\Services\TradeService;
- class TradeServiceTest extends TestCase
- {
- private TradeService $service;
- private Client $client;
- protected function setUp(): void
- {
- $this->client = $this->getMockBuilder(Client::class)
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->service = new TradeService($this->client);
- }
- public function testPushWithValidData(): void
- {
- $tradeData = [
- 'shop_no' => 'test_shop',
- 'trade_list' => [
- ['trade_no' => '123', 'amount' => 100]
- ]
- ];
- $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with(
- 'trade_push.php',
- [
- 'shop_no' => 'test_shop',
- 'switch' => 0,
- 'trade_list' => json_encode($tradeData['trade_list'], JSON_UNESCAPED_UNICODE)
- ]
- )
- ->willReturn($expectedResponse);
- $result = $this->service->push($tradeData);
- $this->assertSame($expectedResponse, $result);
- }
- public function testPushWithSwitchParameter(): void
- {
- $tradeData = [
- 'shop_no' => 'test_shop',
- 'switch' => 1,
- 'trade_list' => [
- ['trade_no' => '123']
- ]
- ];
- $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with(
- 'trade_push.php',
- [
- 'shop_no' => 'test_shop',
- 'switch' => 1,
- 'trade_list' => json_encode($tradeData['trade_list'], JSON_UNESCAPED_UNICODE)
- ]
- )
- ->willReturn($expectedResponse);
- $result = $this->service->push($tradeData);
- $this->assertSame($expectedResponse, $result);
- }
- public function testPushMissingShopNo(): void
- {
- $tradeData = [
- 'trade_list' => [['trade_no' => '123']]
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'shop_no' is missing or empty");
- $this->service->push($tradeData);
- }
- public function testPushMissingTradeList(): void
- {
- $tradeData = [
- 'shop_no' => 'test_shop'
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'trade_list' is missing or empty");
- $this->service->push($tradeData);
- }
- public function testQuery(): void
- {
- $params = ['trade_no' => '123'];
- $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with('trade_query.php', $params)
- ->willReturn($expectedResponse);
- $result = $this->service->query($params);
- $this->assertSame($expectedResponse, $result);
- }
- public function testQueryWithEmptyParams(): void
- {
- $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with('trade_query.php', [])
- ->willReturn($expectedResponse);
- $result = $this->service->query();
- $this->assertSame($expectedResponse, $result);
- }
- public function testQueryLogisticsSync(): void
- {
- $params = ['shop_no' => 'test_shop'];
- $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with('logistics_sync_query.php', $params)
- ->willReturn($expectedResponse);
- $result = $this->service->queryLogisticsSync($params);
- $this->assertSame($expectedResponse, $result);
- }
- public function testAckLogisticsSync(): void
- {
- $logisticsIds = ['log1', 'log2', 'log3'];
- $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with(
- 'logistics_sync_ack.php',
- ['logistics_ids' => json_encode($logisticsIds, JSON_UNESCAPED_UNICODE)]
- )
- ->willReturn($expectedResponse);
- $result = $this->service->ackLogisticsSync($logisticsIds);
- $this->assertSame($expectedResponse, $result);
- }
- public function testAckLogisticsSyncMissingIds(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'logistics_ids' is missing or empty");
- // This should now trigger validation error due to empty array
- $this->service->ackLogisticsSync([]);
- }
- public function testQueryGoodsStockChange(): void
- {
- $params = ['shop_no' => 'test_shop'];
- $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with('api_goods_stock_change_query.php', $params)
- ->willReturn($expectedResponse);
- $result = $this->service->queryGoodsStockChange($params);
- $this->assertSame($expectedResponse, $result);
- }
- public function testAckGoodsStockChange(): void
- {
- $changeIds = ['change1', 'change2'];
- $expectedResponse = new ApiResponse(['code' => 0, 'message' => 'Success']);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with(
- 'api_goods_stock_change_ack.php',
- ['change_ids' => json_encode($changeIds, JSON_UNESCAPED_UNICODE)]
- )
- ->willReturn($expectedResponse);
- $result = $this->service->ackGoodsStockChange($changeIds);
- $this->assertSame($expectedResponse, $result);
- }
- public function testQueryStockoutOrder(): void
- {
- $params = ['shop_no' => 'test_shop'];
- $expectedResponse = new ApiResponse(['code' => 0, 'data' => []]);
- $this->client
- ->expects($this->once())
- ->method('call')
- ->with('stockout_order_query_trade.php', $params)
- ->willReturn($expectedResponse);
- $result = $this->service->queryStockoutOrder($params);
- $this->assertSame($expectedResponse, $result);
- }
- }
|