Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / serializer / Tests / Normalizer / ObjectNormalizerTest.php
index 005dfe92f90b4004a438038df667eef14860b3da..f2d389a0df07efaf64e16c4bc4180196bbb2f001 100644 (file)
@@ -628,6 +628,16 @@ class ObjectNormalizerTest extends TestCase
         $serializer->denormalize(array('inners' => array('a' => array('foo' => 1))), ObjectOuter::class);
     }
 
+    public function testDoNotRejectInvalidTypeOnDisableTypeEnforcementContextOption()
+    {
+        $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor()));
+        $normalizer = new ObjectNormalizer(null, null, null, $extractor);
+        $serializer = new Serializer(array($normalizer));
+        $context = array(ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true);
+
+        $this->assertSame('foo', $serializer->denormalize(array('number' => 'foo'), JsonNumber::class, null, $context)->number);
+    }
+
     public function testExtractAttributesRespectsFormat()
     {
         $normalizer = new FormatAndContextAwareNormalizer();
@@ -649,6 +659,111 @@ class ObjectNormalizerTest extends TestCase
 
         $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
     }
+
+    public function testAttributesContextNormalize()
+    {
+        $normalizer = new ObjectNormalizer();
+        $serializer = new Serializer(array($normalizer));
+
+        $objectInner = new ObjectInner();
+        $objectInner->foo = 'innerFoo';
+        $objectInner->bar = 'innerBar';
+
+        $objectDummy = new ObjectDummy();
+        $objectDummy->setFoo('foo');
+        $objectDummy->setBaz(true);
+        $objectDummy->setObject($objectInner);
+
+        $context = array('attributes' => array('foo', 'baz', 'object' => array('foo')));
+        $this->assertEquals(
+            array(
+                'foo' => 'foo',
+                'baz' => true,
+                'object' => array('foo' => 'innerFoo'),
+            ),
+            $serializer->normalize($objectDummy, null, $context)
+        );
+
+        $context = array('attributes' => array('foo', 'baz', 'object'));
+        $this->assertEquals(
+            array(
+                'foo' => 'foo',
+                'baz' => true,
+                'object' => array('foo' => 'innerFoo', 'bar' => 'innerBar'),
+            ),
+            $serializer->normalize($objectDummy, null, $context)
+        );
+    }
+
+    public function testAttributesContextDenormalize()
+    {
+        $normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
+        $serializer = new Serializer(array($normalizer));
+
+        $objectInner = new ObjectInner();
+        $objectInner->foo = 'innerFoo';
+
+        $objectOuter = new ObjectOuter();
+        $objectOuter->bar = 'bar';
+        $objectOuter->setInner($objectInner);
+
+        $context = array('attributes' => array('bar', 'inner' => array('foo')));
+        $this->assertEquals($objectOuter, $serializer->denormalize(
+            array(
+                'foo' => 'foo',
+                'bar' => 'bar',
+                'date' => '2017-02-03',
+                'inner' => array('foo' => 'innerFoo', 'bar' => 'innerBar'),
+            ), ObjectOuter::class, null, $context));
+    }
+
+    public function testAttributesContextDenormalizeConstructor()
+    {
+        $normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
+        $serializer = new Serializer(array($normalizer));
+
+        $objectInner = new ObjectInner();
+        $objectInner->bar = 'bar';
+
+        $obj = new DummyWithConstructorObjectAndDefaultValue('a', $objectInner);
+
+        $context = array('attributes' => array('inner' => array('bar')));
+        $this->assertEquals($obj, $serializer->denormalize(array(
+            'foo' => 'b',
+            'inner' => array('foo' => 'foo', 'bar' => 'bar'),
+        ), DummyWithConstructorObjectAndDefaultValue::class, null, $context));
+    }
+
+    public function testNormalizeSameObjectWithDifferentAttributes()
+    {
+        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
+        $this->normalizer = new ObjectNormalizer($classMetadataFactory);
+        $serializer = new Serializer(array($this->normalizer));
+        $this->normalizer->setSerializer($serializer);
+
+        $dummy = new ObjectOuter();
+        $dummy->foo = new ObjectInner();
+        $dummy->foo->foo = 'foo.foo';
+        $dummy->foo->bar = 'foo.bar';
+
+        $dummy->bar = new ObjectInner();
+        $dummy->bar->foo = 'bar.foo';
+        $dummy->bar->bar = 'bar.bar';
+
+        $this->assertEquals(array(
+            'foo' => array(
+                'bar' => 'foo.bar',
+            ),
+            'bar' => array(
+                'foo' => 'bar.foo',
+            ),
+        ), $this->normalizer->normalize($dummy, 'json', array(
+            'attributes' => array(
+                'foo' => array('bar'),
+                'bar' => array('foo'),
+            ),
+        )));
+    }
 }
 
 class ObjectDummy
@@ -819,6 +934,8 @@ class ObjectTypeHinted
 
 class ObjectOuter
 {
+    public $foo;
+    public $bar;
     private $inner;
     private $date;
 
@@ -917,6 +1034,28 @@ class JsonNumber
     public $number;
 }
 
+class DummyWithConstructorObjectAndDefaultValue
+{
+    private $foo;
+    private $inner;
+
+    public function __construct($foo = 'a', ObjectInner $inner)
+    {
+        $this->foo = $foo;
+        $this->inner = $inner;
+    }
+
+    public function getFoo()
+    {
+        return $this->foo;
+    }
+
+    public function getInner()
+    {
+        return $this->inner;
+    }
+}
+
 class ObjectWithUpperCaseAttributeNames
 {
     private $Foo = 'Foo';