Version 1
[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\Normalizer\ArrayDenormalizer;
16 use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17 use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
18 use Symfony\Component\Serializer\Serializer;
19 use Symfony\Component\Serializer\Encoder\JsonEncoder;
20 use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
21 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
22 use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
23 use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
24 use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
25 use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
26
27 class SerializerTest extends TestCase
28 {
29     public function testInterface()
30     {
31         $serializer = new Serializer();
32
33         $this->assertInstanceOf('Symfony\Component\Serializer\SerializerInterface', $serializer);
34         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $serializer);
35         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $serializer);
36         $this->assertInstanceOf('Symfony\Component\Serializer\Encoder\EncoderInterface', $serializer);
37         $this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
38     }
39
40     /**
41      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
42      */
43     public function testNormalizeNoMatch()
44     {
45         $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
46         $serializer->normalize(new \stdClass(), 'xml');
47     }
48
49     public function testNormalizeTraversable()
50     {
51         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
52         $result = $serializer->serialize(new TraversableDummy(), 'json');
53         $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
54     }
55
56     public function testNormalizeGivesPriorityToInterfaceOverTraversable()
57     {
58         $serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
59         $result = $serializer->serialize(new NormalizableTraversableDummy(), 'json');
60         $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
61     }
62
63     /**
64      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
65      */
66     public function testNormalizeOnDenormalizer()
67     {
68         $serializer = new Serializer(array(new TestDenormalizer()), array());
69         $this->assertTrue($serializer->normalize(new \stdClass(), 'json'));
70     }
71
72     /**
73      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
74      */
75     public function testDenormalizeNoMatch()
76     {
77         $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
78         $serializer->denormalize('foo', 'stdClass');
79     }
80
81     /**
82      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
83      */
84     public function testDenormalizeOnNormalizer()
85     {
86         $serializer = new Serializer(array(new TestNormalizer()), array());
87         $data = array('title' => 'foo', 'numbers' => array(5, 3));
88         $this->assertTrue($serializer->denormalize(json_encode($data), 'stdClass', 'json'));
89     }
90
91     public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
92     {
93         $serializer = new Serializer(array(new TestNormalizer()), array());
94         $this->assertNull($serializer->normalize(array('a', 'b')));
95         $this->assertNull($serializer->normalize(new \ArrayObject(array('c', 'd'))));
96         $this->assertNull($serializer->normalize(array()));
97         $this->assertNull($serializer->normalize('test'));
98     }
99
100     public function testNormalizeWithSupportOnData()
101     {
102         $normalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
103         $normalizer1->method('supportsNormalization')
104             ->willReturnCallback(function ($data, $format) {
105                 return isset($data->test);
106             });
107         $normalizer1->method('normalize')->willReturn('test1');
108
109         $normalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
110         $normalizer2->method('supportsNormalization')
111             ->willReturn(true);
112         $normalizer2->method('normalize')->willReturn('test2');
113
114         $serializer = new Serializer(array($normalizer1, $normalizer2));
115
116         $data = new \stdClass();
117         $data->test = true;
118         $this->assertEquals('test1', $serializer->normalize($data));
119
120         $this->assertEquals('test2', $serializer->normalize(new \stdClass()));
121     }
122
123     public function testDenormalizeWithSupportOnData()
124     {
125         $denormalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
126         $denormalizer1->method('supportsDenormalization')
127             ->willReturnCallback(function ($data, $type, $format) {
128                 return isset($data['test1']);
129             });
130         $denormalizer1->method('denormalize')->willReturn('test1');
131
132         $denormalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
133         $denormalizer2->method('supportsDenormalization')
134             ->willReturn(true);
135         $denormalizer2->method('denormalize')->willReturn('test2');
136
137         $serializer = new Serializer(array($denormalizer1, $denormalizer2));
138
139         $this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
140
141         $this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
142     }
143
144     public function testSerialize()
145     {
146         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
147         $data = array('title' => 'foo', 'numbers' => array(5, 3));
148         $result = $serializer->serialize(Model::fromArray($data), 'json');
149         $this->assertEquals(json_encode($data), $result);
150     }
151
152     public function testSerializeScalar()
153     {
154         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
155         $result = $serializer->serialize('foo', 'json');
156         $this->assertEquals('"foo"', $result);
157     }
158
159     public function testSerializeArrayOfScalars()
160     {
161         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
162         $data = array('foo', array(5, 3));
163         $result = $serializer->serialize($data, 'json');
164         $this->assertEquals(json_encode($data), $result);
165     }
166
167     /**
168      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
169      */
170     public function testSerializeNoEncoder()
171     {
172         $serializer = new Serializer(array(), array());
173         $data = array('title' => 'foo', 'numbers' => array(5, 3));
174         $serializer->serialize($data, 'json');
175     }
176
177     /**
178      * @expectedException \Symfony\Component\Serializer\Exception\LogicException
179      */
180     public function testSerializeNoNormalizer()
181     {
182         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
183         $data = array('title' => 'foo', 'numbers' => array(5, 3));
184         $serializer->serialize(Model::fromArray($data), 'json');
185     }
186
187     public function testDeserialize()
188     {
189         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
190         $data = array('title' => 'foo', 'numbers' => array(5, 3));
191         $result = $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
192         $this->assertEquals($data, $result->toArray());
193     }
194
195     public function testDeserializeUseCache()
196     {
197         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
198         $data = array('title' => 'foo', 'numbers' => array(5, 3));
199         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
200         $data = array('title' => 'bar', 'numbers' => array(2, 8));
201         $result = $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
202         $this->assertEquals($data, $result->toArray());
203     }
204
205     /**
206      * @expectedException \Symfony\Component\Serializer\Exception\LogicException
207      */
208     public function testDeserializeNoNormalizer()
209     {
210         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
211         $data = array('title' => 'foo', 'numbers' => array(5, 3));
212         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
213     }
214
215     /**
216      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
217      */
218     public function testDeserializeWrongNormalizer()
219     {
220         $serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
221         $data = array('title' => 'foo', 'numbers' => array(5, 3));
222         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
223     }
224
225     /**
226      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
227      */
228     public function testDeserializeNoEncoder()
229     {
230         $serializer = new Serializer(array(), array());
231         $data = array('title' => 'foo', 'numbers' => array(5, 3));
232         $serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
233     }
234
235     public function testDeserializeSupported()
236     {
237         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
238         $data = array('title' => 'foo', 'numbers' => array(5, 3));
239         $this->assertTrue($serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
240     }
241
242     public function testDeserializeNotSupported()
243     {
244         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
245         $data = array('title' => 'foo', 'numbers' => array(5, 3));
246         $this->assertFalse($serializer->supportsDenormalization(json_encode($data), 'stdClass', 'json'));
247     }
248
249     public function testDeserializeNotSupportedMissing()
250     {
251         $serializer = new Serializer(array(), array());
252         $data = array('title' => 'foo', 'numbers' => array(5, 3));
253         $this->assertFalse($serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
254     }
255
256     public function testEncode()
257     {
258         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
259         $data = array('foo', array(5, 3));
260         $result = $serializer->encode($data, 'json');
261         $this->assertEquals(json_encode($data), $result);
262     }
263
264     public function testDecode()
265     {
266         $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
267         $data = array('foo', array(5, 3));
268         $result = $serializer->decode(json_encode($data), 'json');
269         $this->assertEquals($data, $result);
270     }
271
272     public function testSupportsArrayDeserialization()
273     {
274         $serializer = new Serializer(
275             array(
276                 new GetSetMethodNormalizer(),
277                 new PropertyNormalizer(),
278                 new ObjectNormalizer(),
279                 new CustomNormalizer(),
280                 new ArrayDenormalizer(),
281             ),
282             array(
283                 'json' => new JsonEncoder(),
284             )
285         );
286
287         $this->assertTrue(
288             $serializer->supportsDenormalization(array(), __NAMESPACE__.'\Model[]', 'json')
289         );
290     }
291
292     public function testDeserializeArray()
293     {
294         $jsonData = '[{"title":"foo","numbers":[5,3]},{"title":"bar","numbers":[2,8]}]';
295
296         $expectedData = array(
297             Model::fromArray(array('title' => 'foo', 'numbers' => array(5, 3))),
298             Model::fromArray(array('title' => 'bar', 'numbers' => array(2, 8))),
299         );
300
301         $serializer = new Serializer(
302             array(
303                 new GetSetMethodNormalizer(),
304                 new ArrayDenormalizer(),
305             ),
306             array(
307                 'json' => new JsonEncoder(),
308             )
309         );
310
311         $this->assertEquals(
312             $expectedData,
313             $serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
314         );
315     }
316 }
317
318 class Model
319 {
320     private $title;
321     private $numbers;
322
323     public static function fromArray($array)
324     {
325         $model = new self();
326         if (isset($array['title'])) {
327             $model->setTitle($array['title']);
328         }
329         if (isset($array['numbers'])) {
330             $model->setNumbers($array['numbers']);
331         }
332
333         return $model;
334     }
335
336     public function getTitle()
337     {
338         return $this->title;
339     }
340
341     public function setTitle($title)
342     {
343         $this->title = $title;
344     }
345
346     public function getNumbers()
347     {
348         return $this->numbers;
349     }
350
351     public function setNumbers($numbers)
352     {
353         $this->numbers = $numbers;
354     }
355
356     public function toArray()
357     {
358         return array('title' => $this->title, 'numbers' => $this->numbers);
359     }
360 }