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\Exception\OmsSignerErrorException; |
14
|
|
|
use Lamoda\OmsClient\Serializer\SerializerInterface; |
15
|
|
|
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse; |
16
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICRequestLight; |
17
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICResponse; |
18
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse; |
19
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse; |
20
|
|
|
use Lamoda\OmsClient\V2\Extension; |
21
|
|
|
use Lamoda\OmsClient\V2\OmsApi; |
22
|
|
|
use Lamoda\OmsClient\V2\Signer\SignerInterface; |
23
|
|
|
use PHPUnit\Framework\Assert; |
24
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
use Psr\Http\Message\RequestInterface; |
27
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @covers \Lamoda\OmsClient\V2\OmsApi |
31
|
|
|
*/ |
32
|
|
|
final class OmsApiTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
private const TOKEN = 'abcdefg12345678'; |
35
|
|
|
private const API_RESPONSE = '{stub_result}'; |
36
|
|
|
private const OMS_ID = '123456'; |
37
|
|
|
private const ORDER_ID = 'af7a55ae-83de-470b-a1bd-6bbf657106ef'; |
38
|
|
|
private const GTIN = '046000012345'; |
39
|
|
|
private const MARKING_CODE = "010467003301005321gJk6o54AQBJfX\u{001d}91ffd0\u{001d}92LGYcm3FRQrRdNOO+8t0pz78QTyxxBmYKhLXaAS03jKV7oy+DWGy1SeU+BZ8o7B8+hs9LvPdNA7B6NPGjrCm34A=="; |
40
|
|
|
private const LAST_BLOCK_ID = '123456'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ClientInterface | MockObject |
44
|
|
|
*/ |
45
|
|
|
private $client; |
46
|
|
|
/** |
47
|
|
|
* @var SerializerInterface | MockObject |
48
|
|
|
*/ |
49
|
|
|
private $serializer; |
50
|
|
|
/** |
51
|
|
|
* @var OmsApi |
52
|
|
|
*/ |
53
|
|
|
private $api; |
54
|
|
|
|
55
|
|
|
protected function setUp() |
56
|
|
|
{ |
57
|
|
|
parent::setUp(); |
58
|
|
|
|
59
|
|
|
$this->client = $this->createMock(ClientInterface::class); |
|
|
|
|
60
|
|
|
$this->serializer = $this->createMock(SerializerInterface::class); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->api = new OmsApi( |
63
|
|
|
$this->client, |
|
|
|
|
64
|
|
|
$this->serializer |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testExceptionWithHttpCode(): void |
69
|
|
|
{ |
70
|
|
|
$this->client |
|
|
|
|
71
|
|
|
->method('request') |
72
|
|
|
->willThrowException(new BadResponseException('Bad response', $this->createMock(RequestInterface::class))); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->expectException(OmsRequestErrorException::class); |
75
|
|
|
$this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGeneralException(): void |
79
|
|
|
{ |
80
|
|
|
$this->client |
|
|
|
|
81
|
|
|
->method('request') |
82
|
|
|
->willThrowException(new \RuntimeException()); |
83
|
|
|
|
84
|
|
|
$this->expectException(OmsGeneralErrorException::class); |
85
|
|
|
$this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider dataCorrectUsageOfExtensions |
90
|
|
|
*/ |
91
|
|
|
public function testCorrectUsageOfExtensions(Extension $extension, string $extensionName): void |
92
|
|
|
{ |
93
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
94
|
|
|
->method('request') |
95
|
|
|
->with( |
96
|
|
|
$this->anything(), |
97
|
|
|
sprintf('api/v2/%s/buffer/status', $extensionName), |
98
|
|
|
$this->anything() |
99
|
|
|
) |
100
|
|
|
->willReturn( |
101
|
|
|
(new Response()) |
102
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], ''); |
106
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
107
|
|
|
->method('deserialize') |
108
|
|
|
->willReturn($expectedResult); |
109
|
|
|
|
110
|
|
|
$this->api->getICBufferStatus($extension, self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function dataCorrectUsageOfExtensions(): array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
[ |
117
|
|
|
Extension::light(), |
118
|
|
|
'light', |
119
|
|
|
], |
120
|
|
|
[ |
121
|
|
|
Extension::pharma(), |
122
|
|
|
'pharma', |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testCreateOrderForEmissionIC(): void |
128
|
|
|
{ |
129
|
|
|
$createOrderForEmissionICRequestLight = new CreateOrderForEmissionICRequestLight( |
130
|
|
|
'', |
131
|
|
|
'', |
132
|
|
|
'', |
133
|
|
|
'', |
134
|
|
|
'', |
135
|
|
|
new \DateTimeImmutable(), |
136
|
|
|
[] |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$serializedRequest = '{"test": "value"}'; |
140
|
|
|
|
141
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
142
|
|
|
->method('serialize') |
143
|
|
|
->with( |
144
|
|
|
$createOrderForEmissionICRequestLight |
145
|
|
|
) |
146
|
|
|
->willReturn($serializedRequest); |
147
|
|
|
|
148
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
149
|
|
|
->method('request') |
150
|
|
|
->with( |
151
|
|
|
'POST', |
152
|
|
|
'api/v2/light/orders', |
153
|
|
|
[ |
154
|
|
|
RequestOptions::BODY => $serializedRequest, |
155
|
|
|
RequestOptions::HEADERS => [ |
156
|
|
|
'Content-Type' => 'application/json', |
157
|
|
|
'clientToken' => self::TOKEN, |
158
|
|
|
], |
159
|
|
|
RequestOptions::QUERY => [ |
160
|
|
|
'omsId' => self::OMS_ID, |
161
|
|
|
], |
162
|
|
|
RequestOptions::HTTP_ERRORS => true, |
163
|
|
|
] |
164
|
|
|
) |
165
|
|
|
->willReturn( |
166
|
|
|
(new Response()) |
167
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$expectedResult = new CreateOrderForEmissionICResponse(self::OMS_ID, self::ORDER_ID, 100); |
171
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
172
|
|
|
->method('deserialize') |
173
|
|
|
->with( |
174
|
|
|
CreateOrderForEmissionICResponse::class, |
175
|
|
|
self::API_RESPONSE |
176
|
|
|
) |
177
|
|
|
->willReturn($expectedResult); |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
$result = $this->api->createOrderForEmissionIC( |
181
|
|
|
Extension::light(), |
182
|
|
|
self::TOKEN, |
183
|
|
|
self::OMS_ID, |
184
|
|
|
$createOrderForEmissionICRequestLight |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->assertEquals($expectedResult, $result); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testCreateOrderForEmissionICWithSignature(): void |
191
|
|
|
{ |
192
|
|
|
$createOrderForEmissionICRequestLight = new CreateOrderForEmissionICRequestLight( |
193
|
|
|
'', |
194
|
|
|
'', |
195
|
|
|
'', |
196
|
|
|
'', |
197
|
|
|
'', |
198
|
|
|
new \DateTimeImmutable(), |
199
|
|
|
[] |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$serializedRequest = '{"test": "value"}'; |
203
|
|
|
$signature = 'signature'; |
204
|
|
|
|
205
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
206
|
|
|
->method('serialize') |
207
|
|
|
->with( |
208
|
|
|
$createOrderForEmissionICRequestLight |
209
|
|
|
) |
210
|
|
|
->willReturn($serializedRequest); |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
214
|
|
|
->method('request') |
215
|
|
|
->with( |
216
|
|
|
'POST', |
217
|
|
|
'api/v2/light/orders', |
218
|
|
|
[ |
219
|
|
|
RequestOptions::BODY => $serializedRequest, |
220
|
|
|
RequestOptions::HEADERS => [ |
221
|
|
|
'Content-Type' => 'application/json', |
222
|
|
|
'clientToken' => self::TOKEN, |
223
|
|
|
'X-Signature' => $signature |
224
|
|
|
], |
225
|
|
|
RequestOptions::QUERY => [ |
226
|
|
|
'omsId' => self::OMS_ID, |
227
|
|
|
], |
228
|
|
|
RequestOptions::HTTP_ERRORS => true, |
229
|
|
|
] |
230
|
|
|
) |
231
|
|
|
->willReturn( |
232
|
|
|
(new Response()) |
233
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$expectedResult = new CreateOrderForEmissionICResponse(self::OMS_ID, self::ORDER_ID, 100); |
237
|
|
|
$this->serializer |
|
|
|
|
238
|
|
|
->method('deserialize') |
239
|
|
|
->willReturn($expectedResult); |
240
|
|
|
|
241
|
|
|
$signer = $this->createMock(SignerInterface::class); |
242
|
|
|
$signer->method('sign') |
243
|
|
|
->with(base64_encode($serializedRequest)) |
244
|
|
|
->willReturn($signature); |
245
|
|
|
|
246
|
|
|
$result = $this->api->createOrderForEmissionIC( |
247
|
|
|
Extension::light(), |
248
|
|
|
self::TOKEN, |
249
|
|
|
self::OMS_ID, |
250
|
|
|
$createOrderForEmissionICRequestLight, |
251
|
|
|
$signer |
|
|
|
|
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
$this->assertEquals($expectedResult, $result); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testCreateOrderForEmissionFinishedWithSignerException(): void |
258
|
|
|
{ |
259
|
|
|
$createOrderForEmissionICRequestLight = new CreateOrderForEmissionICRequestLight( |
260
|
|
|
'', |
261
|
|
|
'', |
262
|
|
|
'', |
263
|
|
|
'', |
264
|
|
|
'', |
265
|
|
|
new \DateTimeImmutable(), |
266
|
|
|
[] |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
$serializedRequest = '{"test": "value"}'; |
270
|
|
|
|
271
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
272
|
|
|
->method('serialize') |
273
|
|
|
->with( |
274
|
|
|
$createOrderForEmissionICRequestLight |
275
|
|
|
) |
276
|
|
|
->willReturn($serializedRequest); |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
$this->client->expects($this->never()) |
|
|
|
|
280
|
|
|
->method('request'); |
281
|
|
|
|
282
|
|
|
$signer = $this->createMock(SignerInterface::class); |
283
|
|
|
$signer->method('sign') |
284
|
|
|
->willThrowException(new \Exception('Something happened in signer')); |
285
|
|
|
|
286
|
|
|
$this->expectException(OmsSignerErrorException::class); |
287
|
|
|
$this->api->createOrderForEmissionIC( |
288
|
|
|
Extension::light(), |
289
|
|
|
self::TOKEN, |
290
|
|
|
self::OMS_ID, |
291
|
|
|
$createOrderForEmissionICRequestLight, |
292
|
|
|
$signer |
|
|
|
|
293
|
|
|
); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function testGetICBufferStatus(): void |
297
|
|
|
{ |
298
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
299
|
|
|
->method('request') |
300
|
|
|
->with( |
301
|
|
|
'GET', |
302
|
|
|
'api/v2/light/buffer/status', |
303
|
|
|
[ |
304
|
|
|
RequestOptions::BODY => null, |
305
|
|
|
RequestOptions::HEADERS => [ |
306
|
|
|
'Content-Type' => 'application/json', |
307
|
|
|
'clientToken' => self::TOKEN, |
308
|
|
|
], |
309
|
|
|
RequestOptions::QUERY => [ |
310
|
|
|
'omsId' => self::OMS_ID, |
311
|
|
|
'orderId' => self::ORDER_ID, |
312
|
|
|
'gtin' => self::GTIN, |
313
|
|
|
], |
314
|
|
|
RequestOptions::HTTP_ERRORS => true, |
315
|
|
|
] |
316
|
|
|
) |
317
|
|
|
->willReturn( |
318
|
|
|
(new Response()) |
319
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
320
|
|
|
); |
321
|
|
|
|
322
|
|
|
$expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], ''); |
323
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
324
|
|
|
->method('deserialize') |
325
|
|
|
->with( |
326
|
|
|
GetICBufferStatusResponse::class, |
327
|
|
|
self::API_RESPONSE |
328
|
|
|
) |
329
|
|
|
->willReturn($expectedResult); |
330
|
|
|
|
331
|
|
|
$result = $this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, |
332
|
|
|
self::GTIN); |
333
|
|
|
|
334
|
|
|
$this->assertEquals($expectedResult, $result); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testGetICsFromOrder(): void |
338
|
|
|
{ |
339
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
340
|
|
|
->method('request') |
341
|
|
|
->with( |
342
|
|
|
'GET', |
343
|
|
|
'api/v2/light/codes', |
344
|
|
|
[ |
345
|
|
|
RequestOptions::BODY => null, |
346
|
|
|
RequestOptions::HEADERS => [ |
347
|
|
|
'Content-Type' => 'application/json', |
348
|
|
|
'clientToken' => self::TOKEN, |
349
|
|
|
], |
350
|
|
|
RequestOptions::QUERY => [ |
351
|
|
|
'omsId' => self::OMS_ID, |
352
|
|
|
'orderId' => self::ORDER_ID, |
353
|
|
|
'gtin' => self::GTIN, |
354
|
|
|
'quantity' => 2, |
355
|
|
|
'lastBlockId' => '1', |
356
|
|
|
], |
357
|
|
|
RequestOptions::HTTP_ERRORS => true, |
358
|
|
|
] |
359
|
|
|
) |
360
|
|
|
->willReturn( |
361
|
|
|
(new Response()) |
362
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
363
|
|
|
); |
364
|
|
|
|
365
|
|
|
$expectedResult = new GetICsFromOrderResponse(self::OMS_ID, [self::MARKING_CODE], '2'); |
366
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
367
|
|
|
->method('deserialize') |
368
|
|
|
->with( |
369
|
|
|
GetICsFromOrderResponse::class, |
370
|
|
|
self::API_RESPONSE |
371
|
|
|
) |
372
|
|
|
->willReturn($expectedResult); |
373
|
|
|
|
374
|
|
|
$result = $this->api->getICsFromOrder(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN, |
375
|
|
|
2, '1'); |
376
|
|
|
|
377
|
|
|
$this->assertEquals($expectedResult, $result); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function testCloseICArray(): void |
381
|
|
|
{ |
382
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
383
|
|
|
->method('request') |
384
|
|
|
->with( |
385
|
|
|
'POST', |
386
|
|
|
'api/v2/light/buffer/close', |
387
|
|
|
[ |
388
|
|
|
RequestOptions::BODY => null, |
389
|
|
|
RequestOptions::HEADERS => [ |
390
|
|
|
'Content-Type' => 'application/json', |
391
|
|
|
'clientToken' => self::TOKEN, |
392
|
|
|
], |
393
|
|
|
RequestOptions::QUERY => [ |
394
|
|
|
'omsId' => self::OMS_ID, |
395
|
|
|
'orderId' => self::ORDER_ID, |
396
|
|
|
'gtin' => self::GTIN, |
397
|
|
|
'lastBlockId' => self::LAST_BLOCK_ID, |
398
|
|
|
], |
399
|
|
|
RequestOptions::HTTP_ERRORS => true, |
400
|
|
|
] |
401
|
|
|
) |
402
|
|
|
->willReturn( |
403
|
|
|
(new Response()) |
404
|
|
|
->withBody(stream_for(self::API_RESPONSE)) |
405
|
|
|
); |
406
|
|
|
|
407
|
|
|
$expectedResult = new CloseICArrayResponse(self::OMS_ID); |
408
|
|
|
$this->serializer->expects($this->once()) |
|
|
|
|
409
|
|
|
->method('deserialize') |
410
|
|
|
->with( |
411
|
|
|
CloseICArrayResponse::class, |
412
|
|
|
self::API_RESPONSE |
413
|
|
|
) |
414
|
|
|
->willReturn($expectedResult); |
415
|
|
|
|
416
|
|
|
$result = $this->api->closeICArray( |
417
|
|
|
Extension::light(), |
418
|
|
|
self::TOKEN, |
419
|
|
|
self::OMS_ID, |
420
|
|
|
self::ORDER_ID, |
421
|
|
|
self::GTIN, |
422
|
|
|
self::LAST_BLOCK_ID |
423
|
|
|
); |
424
|
|
|
|
425
|
|
|
$this->assertEquals($expectedResult, $result); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
} |
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..