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); } }