Passed
Push — master ( fd71a6...5b3f98 )
by
unknown
51s queued 12s
created

AdapterTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\Tests\Impl\Serializer;
6
7
use Lamoda\IsmpClient\Exception\IsmpSerializerErrorException;
8
use Lamoda\IsmpClient\Impl\Serializer\SymfonySerializerAdapterFactory;
9
use Lamoda\IsmpClient\Serializer\SerializerInterface;
10
use Lamoda\IsmpClient\V3\Dto\AuthCertRequest;
11
use Lamoda\IsmpClient\V3\Dto\AuthCertResponse;
12
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2ItemResponse;
13
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response;
14
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse;
15
use PHPUnit\Framework\TestCase;
16
17
final class SymfonySerializerAdapterTest extends TestCase
18
{
19
    /**
20
     * @var SerializerInterface
21
     */
22
    private $serializer;
23
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->serializer = SymfonySerializerAdapterFactory::create();
29
    }
30
31
    /**
32
     * @dataProvider dataDeserialize
33
     */
34
    public function testDeserialize(string $class, string $data, object $expected): void
35
    {
36
        $result = $this->serializer->deserialize($class, $data);
37
38
        $this->assertEquals($expected, $result);
39
    }
40
41
    public function dataDeserialize(): array
42
    {
43
        return [
44
            [
45
                AuthCertResponse::class,
46
                json_encode(
47
                    [
48
                        'token' => 'test_token',
49
                    ]
50
                ),
51
                new AuthCertResponse(
52
                    'test_token'
53
                ),
54
            ],
55
            [
56
                FacadeDocListV2Response::class,
57
                json_encode(
58
                    [
59
                        'total' => 25,
60
                        'results' => [
61
                            [
62
                                'number' => 'b917dfb0-523d-41e0-9e64-e8bf0052c5bd',
63
                                'docDate' => '2019-01-18T06:45:35.630Z',
64
                                'receivedAt' => '2019-01-19T06:45:35.630Z',
65
                                'type' => 'LP_INTRODUCE_GOODS',
66
                                'status' => 'CHECKED_OK',
67
                                'senderName' => 'test',
68
                            ],
69
                        ],
70
                    ]
71
                ),
72
                new FacadeDocListV2Response(
73
                    25,
74
                    [
75
                        new FacadeDocListV2ItemResponse(
76
                            'b917dfb0-523d-41e0-9e64-e8bf0052c5bd',
77
                            '2019-01-18T06:45:35.630Z',
78
                            '2019-01-19T06:45:35.630Z',
79
                            'LP_INTRODUCE_GOODS',
80
                            'CHECKED_OK',
81
                            'test'
82
                        ),
83
                    ]
84
                ),
85
            ],
86
            'agent item' => [
87
                FacadeMarkedProductsResponse::class,
88
                json_encode(
89
                    [
90
                        'cis' => '010290000021360921&XjcbJ.KYB+pT',
91
                        'gtin' => '02900000213609',
92
                        'sgtin' => '&XjcbJ.KYB+pT',
93
                        'producerName' => 'ООО "ОБУВЬОПТ"',
94
                        'producerInn' => '7731362094',
95
                        'ownerName' => 'ООО "ОБУВЬОПТ"',
96
                        'ownerInn' => '7731362094',
97
                        'agentName' => 'ООО "Купишуз"',
98
                        'agentInn' => '7705935687',
99
                        'emissionDate' => '2020-01-21T13:04:54.416Z',
100
                        'introducedDate' => '2020-01-31T18:11:15.139Z',
101
                        'emissionType' => 'REMAINS',
102
                        'lastDocId' => '6e71f305-1ee4-4f1c-92ab-4a69f5bb7bf8',
103
                        'prevCises' => [],
104
                        'nextCises' => [],
105
                        'status' => 'INTRODUCED',
106
                        'countChildren' => 0,
107
                        'packType' => 'UNIT'
108
                    ]
109
                ),
110
                (new FacadeMarkedProductsResponse(
111
                    '010290000021360921&XjcbJ.KYB+pT',
112
                    '02900000213609',
113
                    '&XjcbJ.KYB+pT',
114
                    'ООО "ОБУВЬОПТ"',
115
                    '7731362094',
116
                    'ООО "ОБУВЬОПТ"',
117
                    '7731362094',
118
                    'INTRODUCED',
119
                    '2020-01-21T13:04:54.416Z',
120
                    'REMAINS',
121
                    [],
122
                    [],
123
                    'UNIT',
124
                    0
125
                ))
126
                    ->setAgentName('ООО "Купишуз"')
127
                    ->setAgentInn('7705935687')
128
                    ->setLastDocId('6e71f305-1ee4-4f1c-92ab-4a69f5bb7bf8')
129
                    ->setIntroducedDate('2020-01-31T18:11:15.139Z')
130
            ],
131
            [
132
                FacadeMarkedProductsResponse::class,
133
                json_encode(
134
                    [
135
                        'cis' => "010463007034375021UptR1qHZW6\"B'",
136
                        'gtin' => '04630070343750',
137
                        'sgtin' => "UptR1qHZW6\"B'",
138
                        'productName' => 'Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380',
139
                        'producerName' => 'ООО "Купишуз"',
140
                        'producerInn' => '7705935687',
141
                        'ownerName' => 'ООО "Купишуз"',
142
                        'ownerInn' => '7705935687',
143
                        'emissionDate' => '2020-02-17T07:48:13.797Z',
144
                        'emissionType' => 'FOREIGN',
145
                        'name' => 'Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380',
146
                        'brand' => 'Chiara Ferragni Collection',
147
                        'model' => 'CF2612',
148
                        'prevCises' => [],
149
                        'nextCises' => [],
150
                        'status' => 'APPLIED',
151
                        'countChildren' => 0,
152
                        'packType' => 'UNIT',
153
                        'country' => 'ИТАЛИЯ',
154
                        'productTypeDesc' => 'КЕДЫ',
155
                        'color' => '005',
156
                        'materialDown' => '100 - резина',
157
                        'materialUpper' => '100 - натуральная кожа',
158
                        'goodSignedFlag' => 'true',
159
                        'materialLining' => '100 - натуральная кожа',
160
                        'goodTurnFlag' => 'true',
161
                        'goodMarkFlag' => 'true'
162
                    ]
163
                ),
164
                (new FacadeMarkedProductsResponse(
165
                    "010463007034375021UptR1qHZW6\"B'",
166
                    '04630070343750',
167
                    "UptR1qHZW6\"B'",
168
                    'ООО "Купишуз"',
169
                    '7705935687',
170
                    'ООО "Купишуз"',
171
                    '7705935687',
172
                    'APPLIED',
173
                    '2020-02-17T07:48:13.797Z',
174
                    'FOREIGN',
175
                    [],
176
                    [],
177
                    'UNIT',
178
                    0
179
                ))
180
                    ->setProductName('Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380')
181
                    ->setName('Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380')
182
                    ->setBrand('Chiara Ferragni Collection')
183
                    ->setModel('CF2612')
184
                    ->setCountry('ИТАЛИЯ')
185
                    ->setProductTypeDesc('КЕДЫ')
186
                    ->setColor('005')
187
                    ->setMaterialDown('100 - резина')
188
                    ->setMaterialUpper('100 - натуральная кожа')
189
                    ->setMaterialLining('100 - натуральная кожа')
190
                    ->setGoodSignedFlag('true')
191
                    ->setGoodTurnFlag('true')
192
                    ->setGoodMarkFlag('true')
193
            ],
194
        ];
195
    }
196
197
    /**
198
     * @dataProvider dataSerialize
199
     */
200
    public function testSerialize(object $data, string $expected): void
201
    {
202
        $result = $this->serializer->serialize($data);
203
204
        $this->assertJsonStringEqualsJsonString($expected, $result);
205
    }
206
207
    public function dataSerialize(): array
208
    {
209
        return [
210
            [
211
                new AuthCertRequest(
212
                    'uuid_value',
213
                    'data_value'
214
                ),
215
                <<<JSON
216
{
217
  "uuid": "uuid_value",
218
  "data": "data_value"
219
}
220
JSON
221
                ,
222
            ],
223
        ];
224
    }
225
226
    public function testDeserializeError(): void
227
    {
228
        $this->expectException(IsmpSerializerErrorException::class);
229
230
        $this->serializer->deserialize('NOT_A_CLASS', null);
231
    }
232
233
    public function testSerializeError(): void
234
    {
235
        $this->expectException(IsmpSerializerErrorException::class);
236
237
        $this->serializer->serialize(new class {
0 ignored issues
show
Documentation introduced by
new class { public f...w \Exception(); } } is of type object<Lamoda\IsmpClient...lizerAdapterTest.php$0>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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...
238
            public function getProperty(): string
239
            {
240
                throw new \Exception();
241
            }
242
        });
243
    }
244
}
245