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