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\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;
27 class SerializerTest extends TestCase
29 public function testInterface()
31 $serializer = new Serializer();
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);
41 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
43 public function testNormalizeNoMatch()
45 $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
46 $serializer->normalize(new \stdClass(), 'xml');
49 public function testNormalizeTraversable()
51 $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
52 $result = $serializer->serialize(new TraversableDummy(), 'json');
53 $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
56 public function testNormalizeGivesPriorityToInterfaceOverTraversable()
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);
64 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
66 public function testNormalizeOnDenormalizer()
68 $serializer = new Serializer(array(new TestDenormalizer()), array());
69 $this->assertTrue($serializer->normalize(new \stdClass(), 'json'));
73 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
75 public function testDenormalizeNoMatch()
77 $serializer = new Serializer(array($this->getMockBuilder('Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock()));
78 $serializer->denormalize('foo', 'stdClass');
82 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
84 public function testDenormalizeOnNormalizer()
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'));
91 public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
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'));
100 public function testNormalizeWithSupportOnData()
102 $normalizer1 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
103 $normalizer1->method('supportsNormalization')
104 ->willReturnCallback(function ($data, $format) {
105 return isset($data->test);
107 $normalizer1->method('normalize')->willReturn('test1');
109 $normalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface')->getMock();
110 $normalizer2->method('supportsNormalization')
112 $normalizer2->method('normalize')->willReturn('test2');
114 $serializer = new Serializer(array($normalizer1, $normalizer2));
116 $data = new \stdClass();
118 $this->assertEquals('test1', $serializer->normalize($data));
120 $this->assertEquals('test2', $serializer->normalize(new \stdClass()));
123 public function testDenormalizeWithSupportOnData()
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']);
130 $denormalizer1->method('denormalize')->willReturn('test1');
132 $denormalizer2 = $this->getMockBuilder('Symfony\Component\Serializer\Normalizer\DenormalizerInterface')->getMock();
133 $denormalizer2->method('supportsDenormalization')
135 $denormalizer2->method('denormalize')->willReturn('test2');
137 $serializer = new Serializer(array($denormalizer1, $denormalizer2));
139 $this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
141 $this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
144 public function testSerialize()
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);
152 public function testSerializeScalar()
154 $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
155 $result = $serializer->serialize('foo', 'json');
156 $this->assertEquals('"foo"', $result);
159 public function testSerializeArrayOfScalars()
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);
168 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
170 public function testSerializeNoEncoder()
172 $serializer = new Serializer(array(), array());
173 $data = array('title' => 'foo', 'numbers' => array(5, 3));
174 $serializer->serialize($data, 'json');
178 * @expectedException \Symfony\Component\Serializer\Exception\LogicException
180 public function testSerializeNoNormalizer()
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');
187 public function testDeserialize()
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());
195 public function testDeserializeUseCache()
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());
206 * @expectedException \Symfony\Component\Serializer\Exception\LogicException
208 public function testDeserializeNoNormalizer()
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');
216 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
218 public function testDeserializeWrongNormalizer()
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');
226 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
228 public function testDeserializeNoEncoder()
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');
235 public function testDeserializeSupported()
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'));
242 public function testDeserializeNotSupported()
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'));
249 public function testDeserializeNotSupportedMissing()
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'));
256 public function testEncode()
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);
264 public function testDecode()
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);
272 public function testSupportsArrayDeserialization()
274 $serializer = new Serializer(
276 new GetSetMethodNormalizer(),
277 new PropertyNormalizer(),
278 new ObjectNormalizer(),
279 new CustomNormalizer(),
280 new ArrayDenormalizer(),
283 'json' => new JsonEncoder(),
288 $serializer->supportsDenormalization(array(), __NAMESPACE__.'\Model[]', 'json')
292 public function testDeserializeArray()
294 $jsonData = '[{"title":"foo","numbers":[5,3]},{"title":"bar","numbers":[2,8]}]';
296 $expectedData = array(
297 Model::fromArray(array('title' => 'foo', 'numbers' => array(5, 3))),
298 Model::fromArray(array('title' => 'bar', 'numbers' => array(2, 8))),
301 $serializer = new Serializer(
303 new GetSetMethodNormalizer(),
304 new ArrayDenormalizer(),
307 'json' => new JsonEncoder(),
313 $serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
323 public static function fromArray($array)
326 if (isset($array['title'])) {
327 $model->setTitle($array['title']);
329 if (isset($array['numbers'])) {
330 $model->setNumbers($array['numbers']);
336 public function getTitle()
341 public function setTitle($title)
343 $this->title = $title;
346 public function getNumbers()
348 return $this->numbers;
351 public function setNumbers($numbers)
353 $this->numbers = $numbers;
356 public function toArray()
358 return array('title' => $this->title, 'numbers' => $this->numbers);