This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Thunder\Serializard\Tests; |
||
3 | |||
4 | use Thunder\Serializard\Exception\FormatNotFoundException; |
||
5 | use Thunder\Serializard\Exception\NormalizerNotFoundException; |
||
6 | use Thunder\Serializard\Exception\SerializationFailureException; |
||
7 | use Thunder\Serializard\Format\ArrayFormat; |
||
8 | use Thunder\Serializard\Format\JsonFormat; |
||
9 | use Thunder\Serializard\Format\XmlFormat; |
||
10 | use Thunder\Serializard\Format\YamlFormat; |
||
11 | use Thunder\Serializard\FormatContainer\FormatContainer; |
||
12 | use Thunder\Serializard\Hydrator\ReflectionHydrator; |
||
13 | use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer; |
||
14 | use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators; |
||
15 | use Thunder\Serializard\HydratorContainer\HydratorContainerInterface; |
||
16 | use Thunder\Serializard\Normalizer\ReflectionNormalizer; |
||
17 | use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer; |
||
18 | use Thunder\Serializard\NormalizerContext\NormalizerContextInterface; |
||
19 | use Thunder\Serializard\Serializard; |
||
20 | use Thunder\Serializard\Tests\Fake\Context\FakeNormalizerContext; |
||
21 | use Thunder\Serializard\Tests\Fake\FakeArticle; |
||
22 | use Thunder\Serializard\Tests\Fake\FakeTag; |
||
23 | use Thunder\Serializard\Tests\Fake\FakeUser; |
||
24 | use Thunder\Serializard\Tests\Fake\FakeUserParent; |
||
25 | use Thunder\Serializard\Tests\Fake\FakeUserParentParent; |
||
26 | use Thunder\Serializard\Tests\Fake\Interfaces\TypeA; |
||
27 | use Thunder\Serializard\Tests\Fake\Interfaces\TypeB; |
||
28 | use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface; |
||
29 | use Thunder\Serializard\Utility\RootElementProviderUtility; |
||
30 | |||
31 | /** |
||
32 | * @author Tomasz Kowalczyk <[email protected]> |
||
33 | */ |
||
34 | final class SerializardTest extends AbstractTestCase |
||
35 | { |
||
36 | /** @dataProvider provideExamples */ |
||
37 | public function testSerializard($prefix, $factory) |
||
38 | { |
||
39 | $object = $factory(); |
||
40 | |||
41 | $serializard = $this->getSerializard(); |
||
42 | |||
43 | $file = __DIR__.'/examples/'.$prefix; |
||
44 | |||
45 | $json = $serializard->serialize($object, 'json'); |
||
46 | $yaml = $serializard->serialize($object, 'yaml'); |
||
47 | $xml = $serializard->serialize($object, 'xml'); |
||
48 | $array = $serializard->serialize($object, 'array'); |
||
49 | |||
50 | $this->assertStringEqualsFile($file.'.json', $json."\n"); |
||
51 | $this->assertStringEqualsFile($file.'.yaml', $yaml); |
||
52 | $this->assertStringEqualsFile($file.'.xml', $xml); |
||
53 | $this->assertSame(require $file.'.php', $array); |
||
54 | |||
55 | $this->assertSame($json, $serializard->serialize($serializard->unserialize($json, FakeUser::class, 'json'), 'json')); |
||
56 | $this->assertSame($yaml, $serializard->serialize($serializard->unserialize($yaml, FakeUser::class, 'yaml'), 'yaml')); |
||
57 | $this->assertSame($xml, $serializard->serialize($serializard->unserialize($xml, FakeUser::class, 'xml'), 'xml')); |
||
58 | $this->assertSame($array, $serializard->serialize($serializard->unserialize($array, FakeUser::class, 'array'), 'array')); |
||
59 | } |
||
60 | |||
61 | public function provideExamples() |
||
62 | { |
||
63 | return [ |
||
64 | ['simple', function() { |
||
65 | $user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
||
66 | $user->addTag(new FakeTag(10, 'sth')); |
||
67 | $user->addTag(new FakeTag(11, 'xyz')); |
||
68 | $user->addTag(new FakeTag(12, 'rnd')); |
||
69 | |||
70 | return $user; |
||
71 | }], |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | public function testInterfaces() |
||
76 | { |
||
77 | $normalizers = new FallbackNormalizerContainer(); |
||
78 | $normalizers->add(TypeInterface::class, function(TypeInterface $type) { |
||
79 | return [ |
||
80 | 'type' => $type->getType(), |
||
81 | 'value' => $type->getValue(), |
||
82 | ]; |
||
83 | }); |
||
84 | |||
85 | $hydrators = new FallbackHydratorContainer(); |
||
86 | |||
87 | $formats = new FormatContainer(); |
||
88 | $formats->add('array', new ArrayFormat()); |
||
89 | |||
90 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
91 | |||
92 | $this->assertSame(['type' => 'typeA', 'value' => 'valueA'], $serializard->serialize(new TypeA(), 'array')); |
||
93 | $this->assertSame(['type' => 'typeB', 'value' => 'valueB'], $serializard->serialize(new TypeB(), 'array')); |
||
94 | } |
||
95 | |||
96 | /** @dataProvider provideCycles */ |
||
97 | public function testCycleException($var, $format) |
||
98 | { |
||
99 | $normalizers = new FallbackNormalizerContainer(); |
||
100 | $normalizers->add(FakeUser::class, new ReflectionNormalizer()); |
||
101 | $normalizers->add(FakeTag::class, new ReflectionNormalizer()); |
||
102 | |||
103 | $hydrators = new FallbackHydratorContainer(); |
||
104 | |||
105 | $formats = new FormatContainer(); |
||
106 | $formats->add('xml', new XmlFormat(new RootElementProviderUtility([ |
||
107 | FakeUser::class => 'user', |
||
108 | FakeTag::class => 'tag', |
||
109 | ]))); |
||
110 | $formats->add('yaml', new YamlFormat()); |
||
111 | $formats->add('json', new JsonFormat()); |
||
112 | $formats->add('array', new ArrayFormat()); |
||
113 | |||
114 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
115 | |||
116 | $this->expectExceptionClass(SerializationFailureException::class); |
||
117 | $serializard->serialize($var, $format); |
||
118 | } |
||
119 | |||
120 | public function provideCycles() |
||
121 | { |
||
122 | $user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
||
123 | $user->addTag(new FakeTag(10, 'sth')); |
||
124 | $user->addTag(new FakeTag(11, 'xyz')); |
||
125 | $user->addTag(new FakeTag(12, 'rnd')); |
||
126 | |||
127 | return [ |
||
128 | [$user, 'xml'], |
||
129 | [$user, 'json'], |
||
130 | [$user, 'yaml'], |
||
131 | [$user, 'array'], |
||
132 | ]; |
||
133 | } |
||
134 | |||
135 | private function getSerializard() |
||
136 | { |
||
137 | $normalizers = new FallbackNormalizerContainer(); |
||
138 | $normalizers->add(FakeUser::class, new ReflectionNormalizer()); |
||
139 | $normalizers->add(FakeTag::class, function(FakeTag $tag) { |
||
140 | return [ |
||
141 | 'id' => $tag->getId(), |
||
142 | 'name' => $tag->getName(), |
||
143 | ]; |
||
144 | }); |
||
145 | |||
146 | $hydrators = new FallbackHydratorContainer(); |
||
147 | View Code Duplication | $hydrators->add(FakeUser::class, function(array $data, Hydrators $handlers) { |
|
0 ignored issues
–
show
|
|||
148 | $tagHandler = $handlers->getHandler(FakeTag::class); |
||
149 | |||
150 | $user = new FakeUser($data['id'], $data['name'], $tagHandler($data['tag'], $handlers)); |
||
151 | foreach($data['tags'] as $tag) { |
||
152 | $user->addTag($tagHandler($tag, $handlers)); |
||
153 | } |
||
154 | |||
155 | return $user; |
||
156 | }); |
||
157 | $hydrators->add(FakeTag::class, function(array $data, Hydrators $handlers) { |
||
0 ignored issues
–
show
|
|||
158 | return new FakeTag($data['id'], $data['name']); |
||
159 | }); |
||
160 | |||
161 | $formats = new FormatContainer(); |
||
162 | $formats->add('xml', new XmlFormat(new RootElementProviderUtility([ |
||
163 | FakeUser::class => 'user', |
||
164 | FakeTag::class => 'tag', |
||
165 | ]))); |
||
166 | $formats->add('yaml', new YamlFormat()); |
||
167 | $formats->add('json', new JsonFormat()); |
||
168 | $formats->add('array', new ArrayFormat()); |
||
169 | |||
170 | return new Serializard($formats, $normalizers, $hydrators); |
||
171 | } |
||
172 | |||
173 | public function testParent() |
||
174 | { |
||
175 | $user = new FakeUser(1, '[email protected]', new FakeTag(1, 'tag')); |
||
176 | |||
177 | $formats = new FormatContainer(); |
||
178 | $formats->add('array', new ArrayFormat()); |
||
179 | $normalizers = new FallbackNormalizerContainer(); |
||
180 | $hydrators = new FallbackHydratorContainer(); |
||
181 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
182 | |||
183 | $normalizers->add(FakeUserParentParent::class, function(FakeUserParentParent $user) { return 'ancestor'; }); |
||
0 ignored issues
–
show
|
|||
184 | $this->assertSame('ancestor', $serializard->serialize($user, 'array')); |
||
185 | |||
186 | $normalizers->add(FakeUserParent::class, function(FakeUserParent $user) { return 'parent'; }); |
||
0 ignored issues
–
show
|
|||
187 | $this->assertSame('parent', $serializard->serialize($user, 'array')); |
||
188 | |||
189 | $normalizers->add(FakeUser::class, function(FakeUser $user) { return 'user'; }); |
||
0 ignored issues
–
show
|
|||
190 | $this->assertSame('user', $serializard->serialize($user, 'array')); |
||
191 | } |
||
192 | |||
193 | public function testContext() |
||
194 | { |
||
195 | $formats = new FormatContainer(); |
||
196 | $formats->add('format', new ArrayFormat()); |
||
197 | |||
198 | $hydrators = new FallbackHydratorContainer(); |
||
199 | $normalizers = new FallbackNormalizerContainer(); |
||
200 | $normalizers->add(FakeArticle::class, function(FakeArticle $article) { |
||
201 | return $article->getTag(); |
||
202 | }); |
||
203 | $normalizers->add(FakeUser::class, function(FakeUser $user) { |
||
204 | return $user->getTag(); |
||
205 | }); |
||
206 | $normalizers->add(FakeTag::class, function(FakeTag $tag, NormalizerContextInterface $context) { |
||
207 | return get_class($context->getParent()); |
||
208 | }); |
||
209 | |||
210 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
211 | |||
212 | $tag = new FakeTag(1, 'name'); |
||
213 | $user = new FakeUser(1, 'name', $tag); |
||
214 | $article = new FakeArticle(1, 'title', $user, $tag); |
||
215 | |||
216 | $this->assertSame(FakeUser::class, $serializard->serialize($user, 'format', new FakeNormalizerContext())); |
||
217 | $context = new FakeNormalizerContext(); |
||
218 | $this->assertSame(FakeArticle::class, $serializard->serialize($article, 'format', $context)); |
||
219 | $this->assertSame(2, $context->getLevel()); |
||
220 | } |
||
221 | |||
222 | public function testProcessWithCallbacks() |
||
223 | { |
||
224 | $formats = new FormatContainer(); |
||
225 | $formats->add('json', new JsonFormat()); |
||
226 | |||
227 | $hydrators = new FallbackHydratorContainer(); |
||
228 | $hydrators->add(FakeArticle::class, function(array $data, FallbackHydratorContainer $hydrators) { |
||
229 | $user = $hydrators->hydrate(FakeUser::class, $data['user']); |
||
230 | $tag = $hydrators->hydrate(FakeTag::class, $data['tag']); |
||
231 | |||
232 | return new FakeArticle($data['id'], $data['title'], $user, $tag); |
||
233 | }); |
||
234 | View Code Duplication | $hydrators->add(FakeUser::class, function(array $data, FallbackHydratorContainer $hydrators) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
235 | $tag = $hydrators->hydrate(FakeTag::class, $data['tag']); |
||
236 | |||
237 | $user = new FakeUser($data['id'], $data['name'], $tag); |
||
238 | foreach($data['tags'] as $tagData) { |
||
239 | $user->addTag(call_user_func($hydrators->getHandler(FakeTag::class), $tagData, $hydrators)); |
||
240 | } |
||
241 | |||
242 | return $user; |
||
243 | }); |
||
244 | $hydrators->add(FakeTag::class, function(array $data, HydratorContainerInterface $hydrators) { |
||
0 ignored issues
–
show
|
|||
245 | return new FakeTag($data['id'], $data['name']); |
||
246 | }); |
||
247 | |||
248 | $normalizers = new FallbackNormalizerContainer(); |
||
249 | $normalizers->add(FakeArticle::class, function(FakeArticle $article, NormalizerContextInterface $context) { |
||
0 ignored issues
–
show
|
|||
250 | return [ |
||
251 | 'id' => $article->getId(), |
||
252 | 'title' => $article->getTitle(), |
||
253 | 'user' => $article->getUser(), |
||
254 | 'tag' => $article->getTag(), |
||
255 | ]; |
||
256 | }); |
||
257 | $normalizers->add(FakeUser::class, function(FakeUser $user, NormalizerContextInterface $context) { |
||
0 ignored issues
–
show
|
|||
258 | return [ |
||
259 | 'id' => $user->getId(), |
||
260 | 'name' => $user->getName(), |
||
261 | 'tag' => $user->getTag(), |
||
262 | 'tags' => $user->getTags(), |
||
263 | ]; |
||
264 | }); |
||
265 | $normalizers->add(FakeTag::class, function(FakeTag $tag, NormalizerContextInterface $context) { |
||
0 ignored issues
–
show
|
|||
266 | return [ |
||
267 | 'id' => $tag->getId(), |
||
268 | 'name' => $tag->getName(), |
||
269 | ]; |
||
270 | }); |
||
271 | |||
272 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
273 | |||
274 | $tag = new FakeTag(1, 'tag'); |
||
275 | $user = new FakeUser(1, 'user', $tag); |
||
276 | $user->addTag(new FakeTag(1, 'tag')); |
||
277 | $article = new FakeArticle(1, 'title', $user, $tag); |
||
278 | |||
279 | $json = $serializard->serialize($article, 'json', new FakeNormalizerContext()); |
||
280 | |||
281 | $this->assertEquals($article, $serializard->unserialize($json, FakeArticle::class, 'json')); |
||
282 | } |
||
283 | |||
284 | public function testProcessWithReflection() |
||
285 | { |
||
286 | $formats = new FormatContainer(); |
||
287 | $formats->add('json', new JsonFormat()); |
||
288 | |||
289 | $hydrators = new FallbackHydratorContainer(); |
||
290 | $hydrators->add(FakeArticle::class, new ReflectionHydrator(FakeArticle::class, [ |
||
291 | 'user' => FakeUser::class, |
||
292 | 'tag' => FakeTag::class, |
||
293 | ])); |
||
294 | $hydrators->add(FakeUser::class, new ReflectionHydrator(FakeUser::class, [ |
||
295 | 'tag' => FakeTag::class, |
||
296 | 'tags' => FakeTag::class.'[]', |
||
297 | ])); |
||
298 | $hydrators->add(FakeTag::class, new ReflectionHydrator(FakeTag::class, [])); |
||
299 | |||
300 | $normalizers = new FallbackNormalizerContainer(); |
||
301 | $normalizers->add(FakeArticle::class, new ReflectionNormalizer()); |
||
302 | $normalizers->add(FakeUser::class, new ReflectionNormalizer()); |
||
303 | $normalizers->add(FakeTag::class, new ReflectionNormalizer(['user'])); |
||
304 | |||
305 | $serializard = new Serializard($formats, $normalizers, $hydrators); |
||
306 | |||
307 | $tag = new FakeTag(1, 'tag'); |
||
308 | $user = new FakeUser(1, 'user', $tag); |
||
309 | $user->addTag(new FakeTag(1, 'tag')); |
||
310 | $tags = $user->getTags(); |
||
311 | $tags[0]->clearUser(); |
||
312 | $article = new FakeArticle(1, 'title', $user, $tag); |
||
313 | |||
314 | $json = $serializard->serialize($article, 'json', new FakeNormalizerContext()); |
||
315 | |||
316 | $this->assertEquals($article, $serializard->unserialize($json, FakeArticle::class, 'json')); |
||
317 | } |
||
318 | |||
319 | public function testInvalidSerializationFormat() |
||
320 | { |
||
321 | $this->expectExceptionClass(FormatNotFoundException::class); |
||
322 | $this->getSerializard()->serialize(new \stdClass(), 'invalid'); |
||
323 | } |
||
324 | |||
325 | public function testInvalidUnserializationFormat() |
||
326 | { |
||
327 | $this->expectExceptionClass(FormatNotFoundException::class); |
||
328 | $this->getSerializard()->unserialize(new \stdClass(), \stdClass::class, 'invalid'); |
||
329 | } |
||
330 | |||
331 | public function testMissingClassSerializationHandler() |
||
332 | { |
||
333 | $this->expectExceptionClass(NormalizerNotFoundException::class); |
||
334 | $this->getSerializard()->serialize(new \stdClass(), 'json'); |
||
335 | } |
||
336 | } |
||
337 |
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.