Completed
Push — master ( f3d222...548fd9 )
by Agaletskiy
01:48
created

OmsApiTest::testExceptionWithHttpCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 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
36
    /**
37
     * @var ClientInterface | MockObject
38
     */
39
    private $client;
40
    /**
41
     * @var SerializerInterface | MockObject
42
     */
43
    private $serializer;
44
    /**
45
     * @var OmsApi
46
     */
47
    private $api;
48
49
    protected function setUp()
50
    {
51
        parent::setUp();
52
53
        $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...
54
        $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...
55
56
        $this->api = new OmsApi(
57
            $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...
58
            $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...
59
        );
60
    }
61
62
    public function testExceptionWithHttpCode(): void
63
    {
64
        $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...
65
            ->method('request')
66
            ->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...
67
68
        $this->expectException(OmsRequestErrorException::class);
69
        $this->api->getICBufferStatus(self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
70
    }
71
72
    public function testGeneralException(): void
73
    {
74
        $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...
75
            ->method('request')
76
            ->willThrowException(new \RuntimeException());
77
78
        $this->expectException(OmsGeneralErrorException::class);
79
        $this->api->getICBufferStatus(self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
80
    }
81
82
    public function testGetICBufferStatus(): void
83
    {
84
        $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...
85
            ->method('request')
86
            ->with(
87
                'GET',
88
                'api/v2/buffer/status',
89
                [
90
                    RequestOptions::BODY => null,
91
                    RequestOptions::HEADERS => [
92
                        'Content-Type' => 'application/json',
93
                        'clientToken' => self::TOKEN,
94
                    ],
95
                    RequestOptions::QUERY => [
96
                        'omsId' => self::OMS_ID,
97
                        'orderId' => self::ORDER_ID,
98
                        'gtin' => self::GTIN,
99
                    ],
100
                    RequestOptions::HTTP_ERRORS => true,
101
                ]
102
            )
103
            ->willReturn(
104
                (new Response())
105
                ->withBody(stream_for(self::API_RESPONSE))
106
            );
107
108
        $expectedResult = new GetICBufferStatusResponse('', '', '', 0, 0, 0, [], '');
109
        $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...
110
            ->method('deserialize')
111
            ->with(
112
                GetICBufferStatusResponse::class,
113
                self::API_RESPONSE
114
            )
115
            ->willReturn($expectedResult);
116
117
        $result = $this->api->getICBufferStatus(self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
118
119
        $this->assertEquals($expectedResult, $result);
120
    }
121
122
    public function testGetICsFromOrder(): 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/codes',
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
                        'quantity' => 2,
140
                        'lastBlockId' => '1',
141
                    ],
142
                    RequestOptions::HTTP_ERRORS => true,
143
                ]
144
            )
145
            ->willReturn(
146
                (new Response())
147
                    ->withBody(stream_for(self::API_RESPONSE))
148
            );
149
150
        $expectedResult = new GetICsFromOrderResponse(self::OMS_ID, [self::MARKING_CODE], '2');
151
        $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...
152
            ->method('deserialize')
153
            ->with(
154
                GetICsFromOrderResponse::class,
155
                self::API_RESPONSE
156
            )
157
            ->willReturn($expectedResult);
158
159
        $result = $this->api->getICsFromOrder(self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN, 2, '1');
160
161
        $this->assertEquals($expectedResult, $result);
162
    }
163
164
    public function testCloseICArray(): void
165
    {
166
        $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...
167
            ->method('request')
168
            ->with(
169
                'GET',
170
                'api/v2/buffer/close',
171
                [
172
                    RequestOptions::BODY => null,
173
                    RequestOptions::HEADERS => [
174
                        'Content-Type' => 'application/json',
175
                        'clientToken' => self::TOKEN,
176
                    ],
177
                    RequestOptions::QUERY => [
178
                        'omsId' => self::OMS_ID,
179
                        'orderId' => self::ORDER_ID,
180
                        'gtin' => self::GTIN,
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 CloseICArrayResponse(self::OMS_ID);
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
                CloseICArrayResponse::class,
195
                self::API_RESPONSE
196
            )
197
            ->willReturn($expectedResult);
198
199
        $result = $this->api->closeICArray(self::TOKEN, self::OMS_ID, self::ORDER_ID, self::GTIN);
200
201
        $this->assertEquals($expectedResult, $result);
202
    }
203
204
}