4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests;
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;
31 class SerializerTest extends TestCase
33 public function testInterface()
35 $serializer = new Serializer();
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);
45 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
47 public function testNormalizeNoMatch()
49 $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
50 $serializer->normalize(new \stdClass(), 'xml');
53 public function testNormalizeTraversable()
55 $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
56 $result = $serializer->serialize(new TraversableDummy(), 'json');
57 $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
60 public function testNormalizeGivesPriorityToInterfaceOverTraversable()
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);
68 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
70 public function testNormalizeOnDenormalizer()
72 $serializer = new Serializer(array(new TestDenormalizer()), array());
73 $this->assertTrue($serializer->normalize(new \stdClass(), 'json'));
77 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
79 public function testDenormalizeNoMatch()
81 $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
82 $serializer->denormalize('foo', 'stdClass');
86 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
88 public function testDenormalizeOnNormalizer()
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'));
95 public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
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'));
104 public function testNormalizeWithSupportOnData()
106 $normalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
107 $normalizer1->method('supportsNormalization')
108 ->willReturnCallback(function ($data, $format) {
109 return isset($data->test);
111 $normalizer1->method('normalize')->willReturn('test1');
113 $normalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
114 $normalizer2->method('supportsNormalization')
116 $normalizer2->method('normalize')->willReturn('test2');
118 $serializer = new Serializer(array($normalizer1, $normalizer2));
120 $data = new \stdClass();
122 $this->assertEquals('test1', $serializer->normalize($data));
124 $this->assertEquals('test2', $serializer->normalize(new \stdClass()));
127 public function testDenormalizeWithSupportOnData()
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']);
134 $denormalizer1->method('denormalize')->willReturn('test1');
136 $denormalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
137 $denormalizer2->method('supportsDenormalization')
139 $denormalizer2->method('denormalize')->willReturn('test2');
141 $serializer = new Serializer(array($denormalizer1, $denormalizer2));
143 $this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
145 $this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
148 public function testSerialize()
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);
156 public function testSerializeScalar()
158 $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
159 $result = $serializer->serialize('foo', 'json');
160 $this->assertEquals('"foo"', $result);
163 public function testSerializeArrayOfScalars()
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);
172 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
174 public function testSerializeNoEncoder()
176 $serializer = new Serializer(array(), array());
177 $data = array('title' => 'foo', 'numbers' => array(5, 3));
178 $serializer->serialize($data, 'json');
182 * @expectedException \Symfony\Component\Serializer\Exception\LogicException
184 public function testSerializeNoNormalizer()
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');
191 public function testDeserialize()
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());
199 public function testDeserializeUseCache()
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());
210 * @expectedException \Symfony\Component\Serializer\Exception\LogicException
212 public function testDeserializeNoNormalizer()
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');
220 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
222 public function testDeserializeWrongNormalizer()
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');
230 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
232 public function testDeserializeNoEncoder()
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');
239 public function testDeserializeSupported()
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'));
246 public function testDeserializeNotSupported()
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'));
253 public function testDeserializeNotSupportedMissing()
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'));
260 public function testEncode()
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);
268 public function testDecode()
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);
276 public function testSupportsArrayDeserialization()
278 $serializer = new Serializer(
280 new GetSetMethodNormalizer(),
281 new PropertyNormalizer(),
282 new ObjectNormalizer(),
283 new CustomNormalizer(),
284 new ArrayDenormalizer(),
287 'json' => new JsonEncoder(),
292 $serializer->supportsDenormalization(array(), __NAMESPACE__.'\Model[]', 'json')
296 public function testDeserializeArray()
298 $jsonData = '[{"title":"foo","numbers":[5,3]},{"title":"bar","numbers":[2,8]}]';
300 $expectedData = array(
301 Model::fromArray(array('title' => 'foo', 'numbers' => array(5, 3))),
302 Model::fromArray(array('title' => 'bar', 'numbers' => array(2, 8))),
305 $serializer = new Serializer(
307 new GetSetMethodNormalizer(),
308 new ArrayDenormalizer(),
311 'json' => new JsonEncoder(),
317 $serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
321 public function testNormalizerAware()
323 $normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock();
324 $normalizerAware->expects($this->once())
325 ->method('setNormalizer')
326 ->with($this->isInstanceOf(NormalizerInterface::class));
328 new Serializer(array($normalizerAware));
331 public function testDenormalizerAware()
333 $denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock();
334 $denormalizerAware->expects($this->once())
335 ->method('setDenormalizer')
336 ->with($this->isInstanceOf(DenormalizerInterface::class));
338 new Serializer(array($denormalizerAware));
341 public function testDeserializeObjectConstructorWithObjectTypeHint()
343 $jsonData = '{"bar":{"value":"baz"}}';
345 $serializer = new Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()));
347 $this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
356 public static function fromArray($array)
359 if (isset($array['title'])) {
360 $model->setTitle($array['title']);
362 if (isset($array['numbers'])) {
363 $model->setNumbers($array['numbers']);
369 public function getTitle()
374 public function setTitle($title)
376 $this->title = $title;
379 public function getNumbers()
381 return $this->numbers;
384 public function setNumbers($numbers)
386 $this->numbers = $numbers;
389 public function toArray()
391 return array('title' => $this->title, 'numbers' => $this->numbers);
399 public function __construct(Bar $bar)
409 public function __construct($value)
411 $this->value = $value;