1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\SerializationTypeResolver; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer\SwaggerSerializer; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Bar; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Foo; |
15
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Meh; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class SwaggerSerializerTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
24
|
|
|
*/ |
25
|
|
|
private $typeResolverMock; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SwaggerSerializer |
29
|
|
|
*/ |
30
|
|
|
private $serializer; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
/** @var SerializationTypeResolver $typeResolver */ |
35
|
|
|
$typeResolver = $this->typeResolverMock = $this->getMockBuilder(SerializationTypeResolver::class) |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
|
39
|
|
|
$this->typeResolverMock->expects($this->any())->method('reverseLookup')->willReturn('Foo'); |
40
|
|
|
$this->typeResolverMock |
41
|
|
|
->expects($this->any()) |
42
|
|
|
->method('resolveUsingSchema') |
43
|
|
|
->willReturnCallback(function (\stdClass $schema) { |
44
|
|
|
return "KleijnWeb\\SwaggerBundle\\Tests\\Serialize\\Serializer\\Stubs\\$schema->class"; |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
$this->serializer = new SwaggerSerializer($typeResolver, self::getTestSchema()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
* |
53
|
|
|
* @param Foo $data |
54
|
|
|
* @param string $expected |
55
|
|
|
* |
56
|
|
|
* @dataProvider serializationProvider |
57
|
|
|
*/ |
58
|
|
|
public function canSerialize(Foo $data, string $expected) |
59
|
|
|
{ |
60
|
|
|
$actual = $this->serializer->serialize($data); |
61
|
|
|
$this->assertSame($expected, $actual); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
* |
67
|
|
|
* @dataProvider deserializationProvider |
68
|
|
|
* |
69
|
|
|
* @param string $data |
70
|
|
|
* @param Foo $expected |
71
|
|
|
*/ |
72
|
|
|
public function canDeSerialize(string $data, Foo $expected) |
73
|
|
|
{ |
74
|
|
|
$actual = $this->serializer->deserialize($data, Foo::class); |
75
|
|
|
$this->assertEquals($expected, $actual); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function serializationProvider(): array |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
[ |
85
|
|
|
new Foo('5', new Bar(6, new Meh(1), new Meh(2), new Meh(3))), |
86
|
|
|
json_encode([ |
87
|
|
|
'a' => '5', |
88
|
|
|
'bar' => [ |
89
|
|
|
'b' => 6, |
90
|
|
|
'meh' => ['c' => 1], |
91
|
|
|
'mehs' => [ |
92
|
|
|
['c' => 2], |
93
|
|
|
['c' => 3], |
94
|
|
|
] |
95
|
|
|
] |
96
|
|
|
]) |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
// Since this will not pass validation, were going to allow it |
100
|
|
|
new Foo('5', new Bar(6, new Meh([1]), new Meh(2), new Meh(3))), |
101
|
|
|
json_encode([ |
102
|
|
|
'a' => '5', |
103
|
|
|
'bar' => [ |
104
|
|
|
'b' => 6, |
105
|
|
|
'meh' => ['c' => [1]], |
106
|
|
|
'mehs' => [ |
107
|
|
|
['c' => 2], |
108
|
|
|
['c' => 3], |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
]) |
112
|
|
|
] |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function deserializationProvider(): array |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
[ |
123
|
|
|
json_encode([ |
124
|
|
|
'a' => '5', |
125
|
|
|
'bar' => [ |
126
|
|
|
'b' => 6, |
127
|
|
|
'meh' => ['c' => 1], |
128
|
|
|
'mehs' => [ |
129
|
|
|
['c' => 2], |
130
|
|
|
['c' => 3], |
131
|
|
|
] |
132
|
|
|
] |
133
|
|
|
]), |
134
|
|
|
new Foo('5', new Bar(6, new Meh(1), new Meh(2), new Meh(3))), |
135
|
|
|
], |
136
|
|
|
[ |
137
|
|
|
json_encode([ |
138
|
|
|
'a' => '5', |
139
|
|
|
'bar' => [ |
140
|
|
|
'b' => 6, |
141
|
|
|
'meh' => ['c' => [1]], |
142
|
|
|
'mehs' => [ |
143
|
|
|
['c' => 2], |
144
|
|
|
['c' => 3], |
145
|
|
|
] |
146
|
|
|
] |
147
|
|
|
]), |
148
|
|
|
new Foo('5', new Bar(6, new Meh([1]), new Meh(2), new Meh(3))), |
149
|
|
|
] |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return \stdClass |
155
|
|
|
*/ |
156
|
|
|
private static function getTestSchema(): \stdClass |
157
|
|
|
{ |
158
|
|
|
return (object)[ |
159
|
|
|
'Foo' => (object)[ |
160
|
|
|
'class' => 'Foo', |
161
|
|
|
'type' => 'object', |
162
|
|
|
'properties' => (object)[ |
163
|
|
|
'a' => (object)['type' => 'int'], |
164
|
|
|
'bar' => (object)[ |
165
|
|
|
'class' => 'Bar', |
166
|
|
|
'type' => 'object', |
167
|
|
|
'properties' => (object)[ |
168
|
|
|
'b' => (object)['type' => 'int'], |
169
|
|
|
'meh' => (object)[ |
170
|
|
|
'class' => 'Meh', |
171
|
|
|
'type' => 'object', |
172
|
|
|
'properties' => (object)['a' => (object)['type' => 'int']] |
173
|
|
|
], |
174
|
|
|
'mehs' => (object)[ |
175
|
|
|
'type' => 'array', |
176
|
|
|
'items' => (object)[ |
177
|
|
|
'class' => 'Meh', |
178
|
|
|
'type' => 'object', |
179
|
|
|
'properties' => (object)['a' => (object)['type' => 'int']] |
180
|
|
|
] |
181
|
|
|
] |
182
|
|
|
] |
183
|
|
|
] |
184
|
|
|
] |
185
|
|
|
] |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.