Passed
Push — master ( 31fd3d...9d78ee )
by Agaletskiy
52s queued 14s
created

OmsApiTest::testCreateOrderForEmissionIC()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.829
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Guzzl...ClientInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<GuzzleHttp\ClientInterface> of property $client.

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..

Loading history...
57
        $this->serializer = $this->createMock(SerializerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Lamod...alizerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Lamoda\OmsClient\...er\SerializerInterface> of property $serializer.

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..

Loading history...
58
59
        $this->api = new OmsApi(
60
            $this->client,
0 ignored issues
show
Documentation introduced by
$this->client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GuzzleHttp\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
            $this->serializer
0 ignored issues
show
Documentation introduced by
$this->serializer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Lamoda\OmsClient\...er\SerializerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        );
63
    }
64
65
    public function testExceptionWithHttpCode(): void
66
    {
67
        $this->client
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            ->method('request')
69
            ->willThrowException(new BadResponseException('Bad response', $this->createMock(RequestInterface::class)));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Psr\H...equestInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Http\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            ->method('serialize')
140
            ->with(
141
                $createOrderForEmissionICRequestLight
142
            )
143
            ->willReturn($serializedRequest);
144
145
        $this->client->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Lamoda\OmsClient\...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}