+ $this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, array('not_serializable' => function () {
+ })));
+ }
+
+ public function testMaxDepth()
+ {
+ $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
+ $this->normalizer = new ObjectNormalizer($classMetadataFactory);
+ $serializer = new Serializer(array($this->normalizer));
+ $this->normalizer->setSerializer($serializer);
+
+ $level1 = new MaxDepthDummy();
+ $level1->foo = 'level1';
+
+ $level2 = new MaxDepthDummy();
+ $level2->foo = 'level2';
+ $level1->child = $level2;
+
+ $level3 = new MaxDepthDummy();
+ $level3->foo = 'level3';
+ $level2->child = $level3;
+
+ $result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
+
+ $expected = array(
+ 'bar' => null,
+ 'foo' => 'level1',
+ 'child' => array(
+ 'bar' => null,
+ 'foo' => 'level2',
+ 'child' => array(
+ 'bar' => null,
+ 'child' => null,
+ ),
+ ),
+ );
+
+ $this->assertEquals($expected, $result);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ */
+ public function testThrowUnexpectedValueException()
+ {
+ $this->normalizer->denormalize(array('foo' => 'bar'), ObjectTypeHinted::class);
+ }
+
+ public function testDenomalizeRecursive()
+ {
+ $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
+ $normalizer = new ObjectNormalizer(null, null, null, $extractor);
+ $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
+
+ $obj = $serializer->denormalize(array(
+ 'inner' => array('foo' => 'foo', 'bar' => 'bar'),
+ 'date' => '1988/01/21',
+ 'inners' => array(array('foo' => 1), array('foo' => 2)),
+ ), ObjectOuter::class);
+
+ $this->assertSame('foo', $obj->getInner()->foo);
+ $this->assertSame('bar', $obj->getInner()->bar);
+ $this->assertSame('1988-01-21', $obj->getDate()->format('Y-m-d'));
+ $this->assertSame(1, $obj->getInners()[0]->foo);
+ $this->assertSame(2, $obj->getInners()[1]->foo);
+ }
+
+ public function testAcceptJsonNumber()
+ {
+ $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
+ $normalizer = new ObjectNormalizer(null, null, null, $extractor);
+ $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
+
+ $this->assertSame(10.0, $serializer->denormalize(array('number' => 10), JsonNumber::class, 'json')->number);
+ $this->assertSame(10.0, $serializer->denormalize(array('number' => 10), JsonNumber::class, 'jsonld')->number);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ * @expectedExceptionMessage The type of the "date" attribute for class "Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter" must be one of "DateTimeInterface" ("string" given).
+ */
+ public function testRejectInvalidType()
+ {
+ $normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
+ $serializer = new Serializer(array($normalizer));
+
+ $serializer->denormalize(array('date' => 'foo'), ObjectOuter::class);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ * @expectedExceptionMessage The type of the key "a" must be "int" ("string" given).
+ */
+ public function testRejectInvalidKey()
+ {
+ $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
+ $normalizer = new ObjectNormalizer(null, null, null, $extractor);
+ $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
+
+ $serializer->denormalize(array('inners' => array('a' => array('foo' => 1))), ObjectOuter::class);
+ }
+
+ public function testExtractAttributesRespectsFormat()
+ {
+ $normalizer = new FormatAndContextAwareNormalizer();
+
+ $data = new ObjectDummy();
+ $data->setFoo('bar');
+ $data->bar = 'foo';
+
+ $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, 'foo_and_bar_included'));
+ }
+
+ public function testExtractAttributesRespectsContext()
+ {
+ $normalizer = new FormatAndContextAwareNormalizer();
+
+ $data = new ObjectDummy();
+ $data->setFoo('bar');
+ $data->bar = 'foo';
+
+ $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));