Completed
Pull Request — master (#1)
by Agaletskiy
02:02
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\Request;
10
use GuzzleHttp\Psr7\Response;
11
use GuzzleHttp\RequestOptions;
12
use Lamoda\OmsClient\Exception\OmsGeneralErrorException;
13
use Lamoda\OmsClient\Exception\OmsRequestErrorException;
14
use Lamoda\OmsClient\Serializer\SerializerInterface;
15
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse;
16
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse;
17
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse;
18
use Lamoda\OmsClient\V2\OmsApi;
19
use Lamoda\OmsClient\V2\Extension;
20
use PHPUnit\Framework\MockObject\MockObject;
21
use PHPUnit\Framework\TestCase;
22
use Psr\Http\Message\RequestInterface;
23
use function GuzzleHttp\Psr7\stream_for;
24
25
/**
26
 * @covers \Lamoda\OmsClient\V2\OmsApi
27
 */
28
final class OmsApiTest extends TestCase
29
{
30
    private const TOKEN = 'abcdefg12345678';
31
    private const API_RESPONSE = '{stub_result}';
32
    private const OMS_ID = '123456';
33
    private const ORDER_ID = 'af7a55ae-83de-470b-a1bd-6bbf657106ef';
34
    private const GTIN = '046000012345';
35
    private const MARKING_CODE = "010467003301005321gJk6o54AQBJfX\u{001d}91ffd0\u{001d}92LGYcm3FRQrRdNOO+8t0pz78QTyxxBmYKhLXaAS03jKV7oy+DWGy1SeU+BZ8o7B8+hs9LvPdNA7B6NPGjrCm34A==";
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, self::GTIN);
158
159
        $this->assertEquals($expectedResult, $result);
160
    }
161
162
    public function testGetICsFromOrder(): void
163
    {
164
        $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...
165
            ->method('request')
166
            ->with(
167
                'GET',
168
                'api/v2/light/codes',
169
                [
170
                    RequestOptions::BODY => null,
171
                    RequestOptions::HEADERS => [
172
                        'Content-Type' => 'application/json',
173
                        'clientToken' => self::TOKEN,
174
                    ],
175
                    RequestOptions::QUERY => [
176
                        'omsId' => self::OMS_ID,
177
                        'orderId' => self::ORDER_ID,
178
                        'gtin' => self::GTIN,
179
                        'quantity' => 2,
180
                        'lastBlockId' => '1',
181
                    ],
182
                    RequestOptions::HTTP_ERRORS => true,
183
                ]
184
            )
185
            ->willReturn(
186
                (new Response())
187
                    ->withBody(stream_for(self::API_RESPONSE))
188
            );
189
190
        $expectedResult = new GetICsFromOrderResponse(self::OMS_ID, [self::MARKING_CODE], '2');
191
        $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...
192
            ->method('deserialize')
193
            ->with(
194
                GetICsFromOrderResponse::class,
195
                self::API_RESPONSE
196
            )
197
            ->willReturn($expectedResult);
198
199
        $result = $this->api->getICsFromOrder(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN, 2, '1');
200
201
        $this->assertEquals($expectedResult, $result);
202
    }
203
204
    public function testCloseICArray(): void
205
    {
206
        $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...
207
            ->method('request')
208
            ->with(
209
                'GET',
210
                'api/v2/light/buffer/close',
211
                [
212
                    RequestOptions::BODY => null,
213
                    RequestOptions::HEADERS => [
214
                        'Content-Type' => 'application/json',
215
                        'clientToken' => self::TOKEN,
216
                    ],
217
                    RequestOptions::QUERY => [
218
                        'omsId' => self::OMS_ID,
219
                        'orderId' => self::ORDER_ID,
220
                        'gtin' => self::GTIN,
221
                    ],
222
                    RequestOptions::HTTP_ERRORS => true,
223
                ]
224
            )
225
            ->willReturn(
226
                (new Response())
227
                    ->withBody(stream_for(self::API_RESPONSE))
228
            );
229
230
        $expectedResult = new CloseICArrayResponse(self::OMS_ID);
231
        $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...
232
            ->method('deserialize')
233
            ->with(
234
                CloseICArrayResponse::class,
235
                self::API_RESPONSE
236
            )
237
            ->willReturn($expectedResult);
238
239
        $result = $this->api->closeICArray(Extension::light(), self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
240
241
        $this->assertEquals($expectedResult, $result);
242
    }
243
244
}