Passed
Push — master ( 69b252...c02560 )
by
unknown
56s queued 11s
created

SymfonySerializerAdapterTest::dataDeserialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 154

Duplication

Lines 154
Ratio 100 %

Importance

Changes 0
Metric Value
dl 154
loc 154
rs 8
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\IsmpClient\Tests\Impl\Serializer\V3;
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 View Code Duplication
final class SymfonySerializerAdapterTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
                        ))->setType('LP_INTRODUCE_GOODS')
80
                        ->setStatus('CHECKED_OK')
81
                        ->setSenderName('test')
82
                    ]
83
                ),
84
            ],
85
            'agent item' => [
86
                FacadeMarkedProductsResponse::class,
87
                json_encode(
88
                    [
89
                        'cis' => '010290000021360921&XjcbJ.KYB+pT',
90
                        'gtin' => '02900000213609',
91
                        'sgtin' => '&XjcbJ.KYB+pT',
92
                        'producerName' => 'ООО "ОБУВЬОПТ"',
93
                        'producerInn' => '7731362094',
94
                        'ownerName' => 'ООО "ОБУВЬОПТ"',
95
                        'ownerInn' => '7731362094',
96
                        'agentName' => 'ООО "Купишуз"',
97
                        'agentInn' => '7705935687',
98
                        'emissionDate' => '2020-01-21T13:04:54.416Z',
99
                        'introducedDate' => '2020-01-31T18:11:15.139Z',
100
                        'emissionType' => 'REMAINS',
101
                        'lastDocId' => '6e71f305-1ee4-4f1c-92ab-4a69f5bb7bf8',
102
                        'prevCises' => [],
103
                        'nextCises' => [],
104
                        'status' => 'INTRODUCED',
105
                        'countChildren' => 0,
106
                        'packType' => 'UNIT'
107
                    ]
108
                ),
109
                (new FacadeMarkedProductsResponse(
110
                    '010290000021360921&XjcbJ.KYB+pT',
111
                    '02900000213609',
112
                    '&XjcbJ.KYB+pT',
113
                    'ООО "ОБУВЬОПТ"',
114
                    '7731362094',
115
                    'INTRODUCED',
116
                    '2020-01-21T13:04:54.416Z',
117
                    'REMAINS',
118
                    [],
119
                    [],
120
                    'UNIT',
121
                    0
122
                ))
123
                    ->setOwnerInn('7731362094')
124
                    ->setOwnerName('ООО "ОБУВЬОПТ"')
125
                    ->setAgentName('ООО "Купишуз"')
126
                    ->setAgentInn('7705935687')
127
                    ->setLastDocId('6e71f305-1ee4-4f1c-92ab-4a69f5bb7bf8')
128
                    ->setIntroducedDate('2020-01-31T18:11:15.139Z')
129
            ],
130
            [
131
                FacadeMarkedProductsResponse::class,
132
                json_encode(
133
                    [
134
                        'cis' => "010463007034375021UptR1qHZW6\"B'",
135
                        'gtin' => '04630070343750',
136
                        'sgtin' => "UptR1qHZW6\"B'",
137
                        'productName' => 'Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380',
138
                        'producerName' => 'ООО "Купишуз"',
139
                        'producerInn' => '7705935687',
140
                        'ownerName' => 'ООО "Купишуз"',
141
                        'ownerInn' => '7705935687',
142
                        'emissionDate' => '2020-02-17T07:48:13.797Z',
143
                        'emissionType' => 'FOREIGN',
144
                        'name' => 'Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380',
145
                        'brand' => 'Chiara Ferragni Collection',
146
                        'model' => 'CF2612',
147
                        'prevCises' => [],
148
                        'nextCises' => [],
149
                        'status' => 'APPLIED',
150
                        'countChildren' => 0,
151
                        'packType' => 'UNIT',
152
                        'country' => 'ИТАЛИЯ',
153
                        'productTypeDesc' => 'КЕДЫ',
154
                        'color' => '005',
155
                        'materialDown' => '100 - резина',
156
                        'materialUpper' => '100 - натуральная кожа',
157
                        'goodSignedFlag' => 'true',
158
                        'materialLining' => '100 - натуральная кожа',
159
                        'goodTurnFlag' => 'true',
160
                        'goodMarkFlag' => 'true'
161
                    ]
162
                ),
163
                (new FacadeMarkedProductsResponse(
164
                    "010463007034375021UptR1qHZW6\"B'",
165
                    '04630070343750',
166
                    "UptR1qHZW6\"B'",
167
                    'ООО "Купишуз"',
168
                    '7705935687',
169
                    'APPLIED',
170
                    '2020-02-17T07:48:13.797Z',
171
                    'FOREIGN',
172
                    [],
173
                    [],
174
                    'UNIT',
175
                    0
176
                ))
177
                    ->setOwnerInn('7705935687')
178
                    ->setOwnerName('ООО "Купишуз"')
179
                    ->setProductName('Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380')
180
                    ->setName('Жен Полуботинки кроссовые типа кеды 005 модель CF2612 размер производителя 38 EUR, российский 37 код в учетной системе CH057AWHPGH0E380')
181
                    ->setBrand('Chiara Ferragni Collection')
182
                    ->setModel('CF2612')
183
                    ->setCountry('ИТАЛИЯ')
184
                    ->setProductTypeDesc('КЕДЫ')
185
                    ->setColor('005')
186
                    ->setMaterialDown('100 - резина')
187
                    ->setMaterialUpper('100 - натуральная кожа')
188
                    ->setMaterialLining('100 - натуральная кожа')
189
                    ->setGoodSignedFlag('true')
190
                    ->setGoodTurnFlag('true')
191
                    ->setGoodMarkFlag('true')
192
            ],
193
        ];
194
    }
195
196
    /**
197
     * @dataProvider dataSerialize
198
     */
199
    public function testSerialize(object $data, string $expected): void
200
    {
201
        $result = $this->serializer->serialize($data);
202
203
        $this->assertJsonStringEqualsJsonString($expected, $result);
204
    }
205
206
    public function dataSerialize(): array
207
    {
208
        return [
209
            [
210
                new AuthCertRequest(
211
                    'uuid_value',
212
                    'data_value'
213
                ),
214
                <<<JSON
215
{
216
  "uuid": "uuid_value",
217
  "data": "data_value"
218
}
219
JSON
220
                ,
221
            ],
222
        ];
223
    }
224
225
    public function testDeserializeError(): void
226
    {
227
        $this->expectException(IsmpSerializerErrorException::class);
228
229
        $this->serializer->deserialize('NOT_A_CLASS', null);
230
    }
231
232
    public function testSerializeError(): void
233
    {
234
        $this->expectException(IsmpSerializerErrorException::class);
235
236
        $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...
237
            public function getProperty(): string
238
            {
239
                throw new \Exception();
240
            }
241
        });
242
    }
243
}
244