Passed
Push — master ( b1cf97...31fd3d )
by Agaletskiy
42s queued 11s
created

OmsApiTest::testCorrectUsageOfExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\GetICBufferStatusResponse;
16
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse;
17
use Lamoda\OmsClient\V2\Extension;
18
use Lamoda\OmsClient\V2\OmsApi;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use Psr\Http\Message\RequestInterface;
22
use function GuzzleHttp\Psr7\stream_for;
23
24
/**
25
 * @covers \Lamoda\OmsClient\V2\OmsApi
26
 */
27
final class OmsApiTest extends TestCase
28
{
29
    private const TOKEN = 'abcdefg12345678';
30
    private const API_RESPONSE = '{stub_result}';
31
    private const OMS_ID = '123456';
32
    private const ORDER_ID = 'af7a55ae-83de-470b-a1bd-6bbf657106ef';
33
    private const GTIN = '046000012345';
34
    private const MARKING_CODE = "010467003301005321gJk6o54AQBJfX\u{001d}91ffd0\u{001d}92LGYcm3FRQrRdNOO+8t0pz78QTyxxBmYKhLXaAS03jKV7oy+DWGy1SeU+BZ8o7B8+hs9LvPdNA7B6NPGjrCm34A==";
35
    private const LAST_BLOCK_ID = '123456';
36
37
    /**
38
     * @var ClientInterface | MockObject
39
     */
40
    private $client;
41
    /**
42
     * @var SerializerInterface | MockObject
43
     */
44
    private $serializer;
45
    /**
46
     * @var OmsApi
47
     */
48
    private $api;
49
50
    protected function setUp()
51
    {
52
        parent::setUp();
53
54
        $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...
55
        $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...
56
57
        $this->api = new OmsApi(
58
            $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...
59
            $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...
60
        );
61
    }
62
63
    public function testExceptionWithHttpCode(): void
64
    {
65
        $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...
66
            ->method('request')
67
            ->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...
68
69
        $this->expectException(OmsRequestErrorException::class);
70
        $this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
71
    }
72
73
    public function testGeneralException(): void
74
    {
75
        $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...
76
            ->method('request')
77
            ->willThrowException(new \RuntimeException());
78
79
        $this->expectException(OmsGeneralErrorException::class);
80
        $this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
81
    }
82
83
    /**
84
     * @dataProvider dataCorrectUsageOfExtensions
85
     */
86
    public function testCorrectUsageOfExtensions(Extension $extension, string $extensionName): void
87
    {
88
        $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...
89
            ->method('request')
90
            ->with(
91
                $this->anything(),
92
                sprintf('api/v2/%s/buffer/status', $extensionName),
93
                $this->anything()
94
            )
95
            ->willReturn(
96
                (new Response())
97
                    ->withBody(stream_for(self::API_RESPONSE))
98
            );
99
100
        $expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], '');
101
        $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...
102
            ->method('deserialize')
103
            ->willReturn($expectedResult);
104
105
        $this->api->getICBufferStatus($extension, self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
106
    }
107
108
    public function dataCorrectUsageOfExtensions(): array
109
    {
110
        return [
111
            [
112
                Extension::light(),
113
                'light',
114
            ],
115
            [
116
                Extension::pharma(),
117
                'pharma',
118
            ],
119
        ];
120
    }
121
122
    public function testGetICBufferStatus(): void
123
    {
124
        $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...
125
            ->method('request')
126
            ->with(
127
                'GET',
128
                'api/v2/light/buffer/status',
129
                [
130
                    RequestOptions::BODY => null,
131
                    RequestOptions::HEADERS => [
132
                        'Content-Type' => 'application/json',
133
                        'clientToken' => self::TOKEN,
134
                    ],
135
                    RequestOptions::QUERY => [
136
                        'omsId' => self::OMS_ID,
137
                        'orderId' => self::ORDER_ID,
138
                        'gtin' => self::GTIN,
139
                    ],
140
                    RequestOptions::HTTP_ERRORS => true,
141
                ]
142
            )
143
            ->willReturn(
144
                (new Response())
145
                    ->withBody(stream_for(self::API_RESPONSE))
146
            );
147
148
        $expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], '');
149
        $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...
150
            ->method('deserialize')
151
            ->with(
152
                GetICBufferStatusResponse::class,
153
                self::API_RESPONSE
154
            )
155
            ->willReturn($expectedResult);
156
157
        $result = $this->api->getICBufferStatus(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID,
158
            self::GTIN);
159
160
        $this->assertEquals($expectedResult, $result);
161
    }
162
163
    public function testGetICsFromOrder(): void
164
    {
165
        $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...
166
            ->method('request')
167
            ->with(
168
                'GET',
169
                'api/v2/light/codes',
170
                [
171
                    RequestOptions::BODY => null,
172
                    RequestOptions::HEADERS => [
173
                        'Content-Type' => 'application/json',
174
                        'clientToken' => self::TOKEN,
175
                    ],
176
                    RequestOptions::QUERY => [
177
                        'omsId' => self::OMS_ID,
178
                        'orderId' => self::ORDER_ID,
179
                        'gtin' => self::GTIN,
180
                        'quantity' => 2,
181
                        'lastBlockId' => '1',
182
                    ],
183
                    RequestOptions::HTTP_ERRORS => true,
184
                ]
185
            )
186
            ->willReturn(
187
                (new Response())
188
                    ->withBody(stream_for(self::API_RESPONSE))
189
            );
190
191
        $expectedResult = new GetICsFromOrderResponse(self::OMS_ID, [self::MARKING_CODE], '2');
192
        $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...
193
            ->method('deserialize')
194
            ->with(
195
                GetICsFromOrderResponse::class,
196
                self::API_RESPONSE
197
            )
198
            ->willReturn($expectedResult);
199
200
        $result = $this->api->getICsFromOrder(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN,
201
            2, '1');
202
203
        $this->assertEquals($expectedResult, $result);
204
    }
205
206
    public function testCloseICArray(): void
207
    {
208
        $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...
209
            ->method('request')
210
            ->with(
211
                'POST',
212
                'api/v2/light/buffer/close',
213
                [
214
                    RequestOptions::BODY => null,
215
                    RequestOptions::HEADERS => [
216
                        'Content-Type' => 'application/json',
217
                        'clientToken' => self::TOKEN,
218
                    ],
219
                    RequestOptions::QUERY => [
220
                        'omsId' => self::OMS_ID,
221
                        'orderId' => self::ORDER_ID,
222
                        'gtin' => self::GTIN,
223
                        'lastBlockId' => self::LAST_BLOCK_ID,
224
                    ],
225
                    RequestOptions::HTTP_ERRORS => true,
226
                ]
227
            )
228
            ->willReturn(
229
                (new Response())
230
                    ->withBody(stream_for(self::API_RESPONSE))
231
            );
232
233
        $expectedResult = new CloseICArrayResponse(self::OMS_ID);
234
        $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...
235
            ->method('deserialize')
236
            ->with(
237
                CloseICArrayResponse::class,
238
                self::API_RESPONSE
239
            )
240
            ->willReturn($expectedResult);
241
242
        $result = $this->api->closeICArray(
243
            Extension::light(),
244
            self::TOKEN,
245
            self::OMS_ID,
246
            self::ORDER_ID,
247
            self::GTIN,
248
            self::LAST_BLOCK_ID
249
        );
250
251
        $this->assertEquals($expectedResult, $result);
252
    }
253
254
}