1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\OmsClient\Tests\V2; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use GuzzleHttp\RequestOptions; |
11
|
|
|
use Lamoda\OmsClient\Exception\OmsGeneralErrorException; |
12
|
|
|
use Lamoda\OmsClient\Exception\OmsRequestErrorException; |
13
|
|
|
use Lamoda\OmsClient\Serializer\SerializerInterface; |
14
|
|
|
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse; |
15
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICRequestLight; |
16
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICResponse; |
17
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse; |
18
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse; |
19
|
|
|
use Lamoda\OmsClient\V2\Extension; |
20
|
|
|
use Lamoda\OmsClient\V2\OmsApi; |
21
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
use Psr\Http\Message\RequestInterface; |
24
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @covers \Lamoda\OmsClient\V2\OmsApi |
28
|
|
|
*/ |
29
|
|
|
final class OmsApiTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
private const TOKEN = 'abcdefg12345678'; |
32
|
|
|
private const API_RESPONSE = '{stub_result}'; |
33
|
|
|
private const OMS_ID = '123456'; |
34
|
|
|
private const ORDER_ID = 'af7a55ae-83de-470b-a1bd-6bbf657106ef'; |
35
|
|
|
private const GTIN = '046000012345'; |
36
|
|
|
private const MARKING_CODE = "010467003301005321gJk6o54AQBJfX\u{001d}91ffd0\u{001d}92LGYcm3FRQrRdNOO+8t0pz78QTyxxBmYKhLXaAS03jKV7oy+DWGy1SeU+BZ8o7B8+hs9LvPdNA7B6NPGjrCm34A=="; |
37
|
|
|
private const LAST_BLOCK_ID = '123456'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClientInterface | MockObject |
41
|
|
|
*/ |
42
|
|
|
private $client; |
43
|
|
|
/** |
44
|
|
|
* @var SerializerInterface | MockObject |
45
|
|
|
*/ |
46
|
|
|
private $serializer; |
47
|
|
|
/** |
48
|
|
|
* @var OmsApi |
49
|
|
|
*/ |
50
|
|
|
private $api; |
51
|
|
|
|
52
|
|
|
protected function setUp() |
53
|
|
|
{ |
54
|
|
|
parent::setUp(); |
55
|
|
|
|
56
|
|
|
$this->client = $this->createMock(ClientInterface::class); |
|
|
|
|
57
|
|
|
$this->serializer = $this->createMock(SerializerInterface::class); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->api = new OmsApi( |
60
|
|
|
$this->client, |
|
|
|
|
61
|
|
|
$this->serializer |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testExceptionWithHttpCode(): void |
66
|
|
|
{ |
67
|
|
|
$this->client |
|
|
|
|
68
|
|
|
->method('request') |
69
|
|
|
->willThrowException(new BadResponseException('Bad response', $this->createMock(RequestInterface::class))); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->expectException(OmsRequestErrorException::class); |
72
|
|
|
$this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGeneralException(): void |
76
|
|
|
{ |
77
|
|
|
$this->client |
|
|
|
|
78
|
|
|
->method('request') |
79
|
|
|
->willThrowException(new \RuntimeException()); |
80
|
|
|
|
81
|
|
|
$this->expectException(OmsGeneralErrorException::class); |
82
|
|
|
$this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider dataCorrectUsageOfExtensions |
87
|
|
|
*/ |
88
|
|
|
public function testCorrectUsageOfExtensions(Extension $extension, string $extensionName): void |
89
|
|
|
{ |
90
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
91
|
|
|
->method('request') |
92
|
|
|
->with( |
93
|
|
|
$this->anything(), |
94
|
|
|
sprintf('api/v2/%s/buffer/status', $extensionName), |
95
|
|
|
$this->anything() |
96
|
|
|
) |
97
|
|
|
->willReturn( |
98
|
|
|
(new Response()) |
99
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], ''); |
103
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
104
|
|
|
->method('deserialize') |
105
|
|
|
->willReturn($expectedResult); |
106
|
|
|
|
107
|
|
|
$this->api->getICBufferStatus($extension, self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function dataCorrectUsageOfExtensions(): array |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
[ |
114
|
|
|
Extension::light(), |
115
|
|
|
'light', |
116
|
|
|
], |
117
|
|
|
[ |
118
|
|
|
Extension::pharma(), |
119
|
|
|
'pharma', |
120
|
|
|
], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testCreateOrderForEmissionIC(): void |
125
|
|
|
{ |
126
|
|
|
$createOrderForEmissionICRequestLight = new CreateOrderForEmissionICRequestLight( |
127
|
|
|
'', |
128
|
|
|
'', |
129
|
|
|
'', |
130
|
|
|
'', |
131
|
|
|
'', |
132
|
|
|
new \DateTimeImmutable(), |
133
|
|
|
[] |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$serializedRequest = '{"test": "value"}'; |
137
|
|
|
|
138
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
139
|
|
|
->method('serialize') |
140
|
|
|
->with( |
141
|
|
|
$createOrderForEmissionICRequestLight |
142
|
|
|
) |
143
|
|
|
->willReturn($serializedRequest); |
144
|
|
|
|
145
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
146
|
|
|
->method('request') |
147
|
|
|
->with( |
148
|
|
|
'POST', |
149
|
|
|
'api/v2/light/orders', |
150
|
|
|
[ |
151
|
|
|
RequestOptions::BODY => $serializedRequest, |
152
|
|
|
RequestOptions::HEADERS => [ |
153
|
|
|
'Content-Type' => 'application/json', |
154
|
|
|
'clientToken' => self::TOKEN, |
155
|
|
|
], |
156
|
|
|
RequestOptions::QUERY => [ |
157
|
|
|
'omsId' => self::OMS_ID, |
158
|
|
|
], |
159
|
|
|
RequestOptions::HTTP_ERRORS => true, |
160
|
|
|
] |
161
|
|
|
) |
162
|
|
|
->willReturn( |
163
|
|
|
(new Response()) |
164
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$expectedResult = new CreateOrderForEmissionICResponse(self::OMS_ID, self::ORDER_ID, 100); |
168
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
169
|
|
|
->method('deserialize') |
170
|
|
|
->with( |
171
|
|
|
CreateOrderForEmissionICResponse::class, |
172
|
|
|
self::API_RESPONSE |
173
|
|
|
) |
174
|
|
|
->willReturn($expectedResult); |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
$result = $this->api->createOrderForEmissionIC( |
178
|
|
|
Extension::light(), |
179
|
|
|
self::TOKEN, |
180
|
|
|
self::OMS_ID, |
181
|
|
|
$createOrderForEmissionICRequestLight |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->assertEquals($expectedResult, $result); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testGetICBufferStatus(): void |
188
|
|
|
{ |
189
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
190
|
|
|
->method('request') |
191
|
|
|
->with( |
192
|
|
|
'GET', |
193
|
|
|
'api/v2/light/buffer/status', |
194
|
|
|
[ |
195
|
|
|
RequestOptions::BODY => null, |
196
|
|
|
RequestOptions::HEADERS => [ |
197
|
|
|
'Content-Type' => 'application/json', |
198
|
|
|
'clientToken' => self::TOKEN, |
199
|
|
|
], |
200
|
|
|
RequestOptions::QUERY => [ |
201
|
|
|
'omsId' => self::OMS_ID, |
202
|
|
|
'orderId' => self::ORDER_ID, |
203
|
|
|
'gtin' => self::GTIN, |
204
|
|
|
], |
205
|
|
|
RequestOptions::HTTP_ERRORS => true, |
206
|
|
|
] |
207
|
|
|
) |
208
|
|
|
->willReturn( |
209
|
|
|
(new Response()) |
210
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], ''); |
214
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
215
|
|
|
->method('deserialize') |
216
|
|
|
->with( |
217
|
|
|
GetICBufferStatusResponse::class, |
218
|
|
|
self::API_RESPONSE |
219
|
|
|
) |
220
|
|
|
->willReturn($expectedResult); |
221
|
|
|
|
222
|
|
|
$result = $this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, |
223
|
|
|
self::GTIN); |
224
|
|
|
|
225
|
|
|
$this->assertEquals($expectedResult, $result); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testGetICsFromOrder(): void |
229
|
|
|
{ |
230
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
231
|
|
|
->method('request') |
232
|
|
|
->with( |
233
|
|
|
'GET', |
234
|
|
|
'api/v2/light/codes', |
235
|
|
|
[ |
236
|
|
|
RequestOptions::BODY => null, |
237
|
|
|
RequestOptions::HEADERS => [ |
238
|
|
|
'Content-Type' => 'application/json', |
239
|
|
|
'clientToken' => self::TOKEN, |
240
|
|
|
], |
241
|
|
|
RequestOptions::QUERY => [ |
242
|
|
|
'omsId' => self::OMS_ID, |
243
|
|
|
'orderId' => self::ORDER_ID, |
244
|
|
|
'gtin' => self::GTIN, |
245
|
|
|
'quantity' => 2, |
246
|
|
|
'lastBlockId' => '1', |
247
|
|
|
], |
248
|
|
|
RequestOptions::HTTP_ERRORS => true, |
249
|
|
|
] |
250
|
|
|
) |
251
|
|
|
->willReturn( |
252
|
|
|
(new Response()) |
253
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$expectedResult = new GetICsFromOrderResponse(self::OMS_ID, [self::MARKING_CODE], '2'); |
257
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
258
|
|
|
->method('deserialize') |
259
|
|
|
->with( |
260
|
|
|
GetICsFromOrderResponse::class, |
261
|
|
|
self::API_RESPONSE |
262
|
|
|
) |
263
|
|
|
->willReturn($expectedResult); |
264
|
|
|
|
265
|
|
|
$result = $this->api->getICsFromOrder(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN, |
266
|
|
|
2, '1'); |
267
|
|
|
|
268
|
|
|
$this->assertEquals($expectedResult, $result); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testCloseICArray(): void |
272
|
|
|
{ |
273
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
274
|
|
|
->method('request') |
275
|
|
|
->with( |
276
|
|
|
'POST', |
277
|
|
|
'api/v2/light/buffer/close', |
278
|
|
|
[ |
279
|
|
|
RequestOptions::BODY => null, |
280
|
|
|
RequestOptions::HEADERS => [ |
281
|
|
|
'Content-Type' => 'application/json', |
282
|
|
|
'clientToken' => self::TOKEN, |
283
|
|
|
], |
284
|
|
|
RequestOptions::QUERY => [ |
285
|
|
|
'omsId' => self::OMS_ID, |
286
|
|
|
'orderId' => self::ORDER_ID, |
287
|
|
|
'gtin' => self::GTIN, |
288
|
|
|
'lastBlockId' => self::LAST_BLOCK_ID, |
289
|
|
|
], |
290
|
|
|
RequestOptions::HTTP_ERRORS => true, |
291
|
|
|
] |
292
|
|
|
) |
293
|
|
|
->willReturn( |
294
|
|
|
(new Response()) |
295
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
296
|
|
|
); |
297
|
|
|
|
298
|
|
|
$expectedResult = new CloseICArrayResponse(self::OMS_ID); |
299
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
300
|
|
|
->method('deserialize') |
301
|
|
|
->with( |
302
|
|
|
CloseICArrayResponse::class, |
303
|
|
|
self::API_RESPONSE |
304
|
|
|
) |
305
|
|
|
->willReturn($expectedResult); |
306
|
|
|
|
307
|
|
|
$result = $this->api->closeICArray( |
308
|
|
|
Extension::light(), |
309
|
|
|
self::TOKEN, |
310
|
|
|
self::OMS_ID, |
311
|
|
|
self::ORDER_ID, |
312
|
|
|
self::GTIN, |
313
|
|
|
self::LAST_BLOCK_ID |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$this->assertEquals($expectedResult, $result); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..