1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Impl\Serializer\V4; |
4
|
|
|
|
5
|
|
|
use Lamoda\IsmpClient\Impl\Serializer\V4\FacadeDocBodyResponseBodyDenormalizer; |
6
|
|
|
use Lamoda\IsmpClient\Tests\Stub\SerializerDenormalizer; |
7
|
|
|
use Lamoda\IsmpClient\V4\Dto\FacadeDocBodyResponse\Body; |
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\Serializer\Exception\BadMethodCallException; |
11
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
13
|
|
|
|
14
|
|
View Code Duplication |
final class FacadeDocBodyResponseBodyDenormalizerTest extends TestCase |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var FacadeDocBodyResponseBodyDenormalizer |
18
|
|
|
*/ |
19
|
|
|
private $denormalizer; |
20
|
|
|
/** |
21
|
|
|
* @var MockObject|DenormalizerInterface |
22
|
|
|
*/ |
23
|
|
|
private $inner; |
24
|
|
|
|
25
|
|
|
protected function setUp(): void |
26
|
|
|
{ |
27
|
|
|
$this->inner = $this->createMock(SerializerDenormalizer::class); |
28
|
|
|
$this->denormalizer = new FacadeDocBodyResponseBodyDenormalizer(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider dataSupportsDenormalization |
33
|
|
|
*/ |
34
|
|
|
public function testSupportsDenormalization(string $type, array $data, bool $expected): void |
35
|
|
|
{ |
36
|
|
|
$result = $this->denormalizer->supportsDenormalization($data, $type); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($expected, $result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function dataSupportsDenormalization(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
[ |
45
|
|
|
Body::class, |
46
|
|
|
[ |
47
|
|
|
'original_string' => 'foobar', |
48
|
|
|
], |
49
|
|
|
false, |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
Body::class, |
53
|
|
|
[], |
54
|
|
|
true, |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
\stdClass::class, |
58
|
|
|
[], |
59
|
|
|
false, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testDenormalizeWithDenormalizerIsNotSet(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectException(BadMethodCallException::class); |
67
|
|
|
$this->denormalizer->denormalize([], Body::class); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testDenormalizeWithWrongData(): void |
71
|
|
|
{ |
72
|
|
|
$this->denormalizer->setDenormalizer($this->inner); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->expectException(InvalidArgumentException::class); |
75
|
|
|
$this->denormalizer->denormalize('wrong', Body::class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testDenormalizeWithWrongType(): void |
79
|
|
|
{ |
80
|
|
|
$this->denormalizer->setDenormalizer($this->inner); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->expectException(InvalidArgumentException::class); |
83
|
|
|
$this->denormalizer->denormalize([], \stdClass::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSetDenormalizerWithNoSerializer(): void |
87
|
|
|
{ |
88
|
|
|
$this->expectException(InvalidArgumentException::class); |
89
|
|
|
$this->denormalizer->setDenormalizer($this->createMock(DenormalizerInterface::class)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testDenormalize(): void |
93
|
|
|
{ |
94
|
|
|
$this->denormalizer->setDenormalizer($this->inner); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$originalBodyData = [ |
97
|
|
|
'some_new_crpt_field' => true, |
98
|
|
|
'document_description' => 'description', |
99
|
|
|
'products' => [ |
100
|
|
|
[ |
101
|
|
|
'uit_code' => '010464005741102021PMRanDom32406404', |
102
|
|
|
'product_cost' => 42, |
103
|
|
|
'product_tax' => 1.1, |
104
|
|
|
'product_description' => 'product description 2' |
105
|
|
|
], |
106
|
|
|
[ |
107
|
|
|
'uit_code' => '010RanDom463007357256021c140640422', |
108
|
|
|
'product_cost' => 42, |
109
|
|
|
'product_tax' => 1.1, |
110
|
|
|
'product_description' => 'product description 2' |
111
|
|
|
], |
112
|
|
|
] |
113
|
|
|
]; |
114
|
|
|
$expectedResult = new Body(); |
115
|
|
|
$property = new \ReflectionProperty(Body::class, 'originalString'); |
116
|
|
|
$property->setAccessible(true); |
117
|
|
|
$property->setValue($expectedResult, json_encode($originalBodyData)); |
118
|
|
|
$property = new \ReflectionProperty(Body::class, 'documentDescription'); |
119
|
|
|
$property->setAccessible(true); |
120
|
|
|
$property->setValue($expectedResult, 'description'); |
121
|
|
|
|
122
|
|
|
$this->inner->expects($this->once()) |
|
|
|
|
123
|
|
|
->method('serialize') |
124
|
|
|
->with($originalBodyData, null, []) |
125
|
|
|
->willReturn($originalBodyData); |
126
|
|
|
$enrichedOriginalBodyData = array_merge($originalBodyData, ['original_string' => $originalBodyData]); |
127
|
|
|
$this->inner->expects($this->once()) |
128
|
|
|
->method('denormalize') |
129
|
|
|
->with($enrichedOriginalBodyData, Body::class, null, []) |
130
|
|
|
->willReturn($expectedResult); |
131
|
|
|
|
132
|
|
|
$result = $this->denormalizer->denormalize( |
133
|
|
|
$originalBodyData, |
134
|
|
|
Body::class |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertEquals($expectedResult, $result); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testHasCacheableSupportsMethod(): void |
141
|
|
|
{ |
142
|
|
|
$this->denormalizer->setDenormalizer($this->inner); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$result = $this->denormalizer->hasCacheableSupportsMethod(); |
145
|
|
|
$this->assertFalse($result); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.