Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / serializer / Tests / SerializerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\JsonEncoder;
16 use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
17 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
18 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
19 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20 use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
21 use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23 use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
24 use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
25 use Symfony\Component\Serializer\Serializer;
26 use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
27 use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
28 use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
29 use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
30
31 class SerializerTest extends TestCase
32 {
33     public function testInterface()
34     {
35         $serializer = new Serializer();
36
37         $this->assertInstanceOf('Symfony\Component\Serializer\SerializerInterface', $serializer);
38         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $serializer);
39         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $serializer);
40         $this->assertInstanceOf('Symfony\Component\Serializer\Encoder\EncoderInterface', $serializer);
41         $this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
42     }
43
44     /**
45      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
46      */
47     public function testNormalizeNoMatch()
48     {
49         $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
50         $serializer->normalize(new \stdClass(), 'xml');
51     }
52
53     public function testNormalizeTraversable()
54     {
55         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
56         $result = $serializer->serialize(new TraversableDummy(), 'json');
57         $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
58     }
59
60     public function testNormalizeGivesPriorityToInterfaceOverTraversable()
61     {
62         $serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
63         $result = $serializer->serialize(new NormalizableTraversableDummy(), 'json');
64         $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
65     }
66
67     /**
68      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
69      */
70     public function testNormalizeOnDenormalizer()
71     {
72         $serializer = new Serializer(array(new TestDenormalizer()), array());
73         $this->assertTrue($serializer->normalize(new \stdClass(), 'json'));
74     }
75
76     /**
77      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
78      */
79     public function testDenormalizeNoMatch()
80     {
81         $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
82         $serializer->denormalize('foo', 'stdClass');
83     }
84
85     /**
86      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
87      */
88     public function testDenormalizeOnNormalizer()
89     {
90         $serializer = new Serializer(array(new TestNormalizer()), array());
91         $data = array('title' => 'foo', 'numbers' => array(5, 3));
92         $this->assertTrue($serializer->denormalize(json_encode($data), 'stdClass', 'json'));
93     }
94
95     public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
96     {
97         $serializer = new Serializer(array(new TestNormalizer()), array());
98         $this->assertNull($serializer->normalize(array('a', 'b')));
99         $this->assertNull($serializer->normalize(new \ArrayObject(array('c', 'd'))));
100         $this->assertNull($serializer->normalize(array()));
101         $this->assertNull($serializer->normalize('test'));
102     }
103
104     public function testNormalizeWithSupportOnData()
105     {
106         $normalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
107         $normalizer1->method('supportsNormalization')
108             ->willReturnCallback(function ($data, $format) {
109                 return isset($data->test);
110             });
111         $normalizer1->method('normalize')->willReturn('test1');
112
113         $normalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
114         $normalizer2->method('supportsNormalization')
115             ->willReturn(true);
116         $normalizer2->method('normalize')->willReturn('test2');
117
118         $serializer = new Serializer(array($normalizer1, $normalizer2));
119
120         $data = new \stdClass();
121         $data->test = true;
122         $this->assertEquals('test1', $serializer->normalize($data));
123
124         $this->assertEquals('test2', $serializer->normalize(new \stdClass()));
125     }
126
127     public function testDenormalizeWithSupportOnData()
128     {
129         $denormalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
130         $denormalizer1->method('supportsDenormalization')
131             ->willReturnCallback(function ($data, $type, $format) {
132                 return isset($data['test1']);
133             });
134         $denormalizer1->method('denormalize')->willReturn('test1');
135
136         $denormalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
137         $denormalizer2->method('supportsDenormalization')
138             ->willReturn(true);
139         $denormalizer2->method('denormalize')->willReturn('test2');
140
141         $serializer = new Serializer(array($denormalizer1, $denormalizer2));
142
143         $this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
144
145         $this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
146     }
147
148     public function testSerialize()
149     {
150         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
151         $data = array('title' => 'foo', 'numbers' => array(5, 3));
152         $result = $serializer->serialize(Model::fromArray($data), 'json');
153         $this->assertEquals(json_encode($data), $result);
154     }
155
156     public function testSerializeScalar()
157     {
158         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
159         $result = $serializer->serialize('foo', 'json');
160         $this->assertEquals('"foo"', $result);
161     }
162
163     public function testSerializeArrayOfScalars()
164     {
165         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
166         $data = array('foo', array(5, 3));
167         $result = $serializer->serialize($data, 'json');
168         $this->assertEquals(json_encode($data), $result);
169     }
170
171     /**
172      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
173      */
174     public function testSerializeNoEncoder()
175     {
176         $serializer = new Serializer(array(), array());
177         $data = array('title' => 'foo', 'numbers' => array(5, 3));
178         $serializer->serialize($data, 'json');
179     }
180
181     /**
182      * @expectedException \Symfony\Component\Serializer\Exception\LogicException
183      */
184     public function testSerializeNoNormalizer()
185     {
186         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
187         $data = array('title' => 'foo', 'numbers' => array(5, 3));
188         $serializer->serialize(Model::fromArray($data), 'json');
189     }
190
191     public function testDeserialize()
192     {
193         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
194         $data = array('title' => 'foo', 'numbers' => array(5, 3));
195         $result = $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
196         $this->assertEquals($data, $result->toArray());
197     }
198
199     public function testDeserializeUseCache()
200     {
201         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
202         $data = array('title' => 'foo', 'numbers' => array(5, 3));
203         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
204         $data = array('title' => 'bar', 'numbers' => array(2, 8));
205         $result = $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
206         $this->assertEquals($data, $result->toArray());
207     }
208
209     /**
210      * @expectedException \Symfony\Component\Serializer\Exception\LogicException
211      */
212     public function testDeserializeNoNormalizer()
213     {
214         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
215         $data = array('title' => 'foo', 'numbers' => array(5, 3));
216         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
217     }
218
219     /**
220      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
221      */
222     public function testDeserializeWrongNormalizer()
223     {
224         $serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
225         $data = array('title' => 'foo', 'numbers' => array(5, 3));
226         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
227     }
228
229     /**
230      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
231      */
232     public function testDeserializeNoEncoder()
233     {
234         $serializer = new Serializer(array(), array());
235         $data = array('title' => 'foo', 'numbers' => array(5, 3));
236         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
237     }
238
239     public function testDeserializeSupported()
240     {
241         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
242         $data = array('title' => 'foo', 'numbers' => array(5, 3));
243         $this->assertTrue($serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
244     }
245
246     public function testDeserializeNotSupported()
247     {
248         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
249         $data = array('title' => 'foo', 'numbers' => array(5, 3));
250         $this->assertFalse($serializer->supportsDenormalization(json_encode($data), 'stdClass', 'json'));
251     }
252
253     public function testDeserializeNotSupportedMissing()
254     {
255         $serializer = new Serializer(array(), array());
256         $data = array('title' => 'foo', 'numbers' => array(5, 3));
257         $this->assertFalse($serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
258     }
259
260     public function testEncode()
261     {
262         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
263         $data = array('foo', array(5, 3));
264         $result = $serializer->encode($data, 'json');
265         $this->assertEquals(json_encode($data), $result);
266     }
267
268     public function testDecode()
269     {
270         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
271         $data = array('foo', array(5, 3));
272         $result = $serializer->decode(json_encode($data), 'json');
273         $this->assertEquals($data, $result);
274     }
275
276     public function testSupportsArrayDeserialization()
277     {
278         $serializer = new Serializer(
279             array(
280                 new GetSetMethodNormalizer(),
281                 new PropertyNormalizer(),
282                 new ObjectNormalizer(),
283                 new CustomNormalizer(),
284                 new ArrayDenormalizer(),
285             ),
286             array(
287                 'json' => new JsonEncoder(),
288             )
289         );
290
291         $this->assertTrue(
292             $serializer->supportsDenormalization(array(), __NAMESPACE__.'\Model[]', 'json')
293         );
294     }
295
296     public function testDeserializeArray()
297     {
298         $jsonData = '[{"title":"foo","numbers":[5,3]},{"title":"bar","numbers":[2,8]}]';
299
300         $expectedData = array(
301             Model::fromArray(array('title' => 'foo', 'numbers' => array(5, 3))),
302             Model::fromArray(array('title' => 'bar', 'numbers' => array(2, 8))),
303         );
304
305         $serializer = new Serializer(
306             array(
307                 new GetSetMethodNormalizer(),
308                 new ArrayDenormalizer(),
309             ),
310             array(
311                 'json' => new JsonEncoder(),
312             )
313         );
314
315         $this->assertEquals(
316             $expectedData,
317             $serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
318         );
319     }
320
321     public function testNormalizerAware()
322     {
323         $normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock();
324         $normalizerAware->expects($this->once())
325             ->method('setNormalizer')
326             ->with($this->isInstanceOf(NormalizerInterface::class));
327
328         new Serializer(array($normalizerAware));
329     }
330
331     public function testDenormalizerAware()
332     {
333         $denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock();
334         $denormalizerAware->expects($this->once())
335             ->method('setDenormalizer')
336             ->with($this->isInstanceOf(DenormalizerInterface::class));
337
338         new Serializer(array($denormalizerAware));
339     }
340
341     public function testDeserializeObjectConstructorWithObjectTypeHint()
342     {
343         $jsonData = '{"bar":{"value":"baz"}}';
344
345         $serializer = new Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()));
346
347         $this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
348     }
349 }
350
351 class Model
352 {
353     private $title;
354     private $numbers;
355
356     public static function fromArray($array)
357     {
358         $model = new self();
359         if (isset($array['title'])) {
360             $model->setTitle($array['title']);
361         }
362         if (isset($array['numbers'])) {
363             $model->setNumbers($array['numbers']);
364         }
365
366         return $model;
367     }
368
369     public function getTitle()
370     {
371         return $this->title;
372     }
373
374     public function setTitle($title)
375     {
376         $this->title = $title;
377     }
378
379     public function getNumbers()
380     {
381         return $this->numbers;
382     }
383
384     public function setNumbers($numbers)
385     {
386         $this->numbers = $numbers;
387     }
388
389     public function toArray()
390     {
391         return array('title' => $this->title, 'numbers' => $this->numbers);
392     }
393 }
394
395 class Foo
396 {
397     private $bar;
398
399     public function __construct(Bar $bar)
400     {
401         $this->bar = $bar;
402     }
403 }
404
405 class Bar
406 {
407     private $value;
408
409     public function __construct($value)
410     {
411         $this->value = $value;
412     }
413 }