Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / serializer / Tests / Normalizer / AbstractNormalizerTest.php
1 <?php
2
3 namespace Symfony\Component\Serializer\Tests\Normalizer;
4
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
7 use Symfony\Component\Serializer\Mapping\ClassMetadata;
8 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
9 use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
10 use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11 use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
12 use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
13 use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
14 use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
15 use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
16
17 /**
18  * Provides a dummy Normalizer which extends the AbstractNormalizer.
19  *
20  * @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
21  */
22 class AbstractNormalizerTest extends TestCase
23 {
24     /**
25      * @var AbstractNormalizerDummy
26      */
27     private $normalizer;
28
29     /**
30      * @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
31      */
32     private $classMetadata;
33
34     protected function setUp()
35     {
36         $loader = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Loader\LoaderChain')->setConstructorArgs(array(array()))->getMock();
37         $this->classMetadata = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory')->setConstructorArgs(array($loader))->getMock();
38         $this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
39     }
40
41     public function testGetAllowedAttributesAsString()
42     {
43         $classMetadata = new ClassMetadata('c');
44
45         $a1 = new AttributeMetadata('a1');
46         $classMetadata->addAttributeMetadata($a1);
47
48         $a2 = new AttributeMetadata('a2');
49         $a2->addGroup('test');
50         $classMetadata->addAttributeMetadata($a2);
51
52         $a3 = new AttributeMetadata('a3');
53         $a3->addGroup('other');
54         $classMetadata->addAttributeMetadata($a3);
55
56         $a4 = new AttributeMetadata('a4');
57         $a4->addGroup('test');
58         $a4->addGroup('other');
59         $classMetadata->addAttributeMetadata($a4);
60
61         $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
62
63         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), true);
64         $this->assertEquals(array('a2', 'a4'), $result);
65
66         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), true);
67         $this->assertEquals(array('a3', 'a4'), $result);
68     }
69
70     public function testGetAllowedAttributesAsObjects()
71     {
72         $classMetadata = new ClassMetadata('c');
73
74         $a1 = new AttributeMetadata('a1');
75         $classMetadata->addAttributeMetadata($a1);
76
77         $a2 = new AttributeMetadata('a2');
78         $a2->addGroup('test');
79         $classMetadata->addAttributeMetadata($a2);
80
81         $a3 = new AttributeMetadata('a3');
82         $a3->addGroup('other');
83         $classMetadata->addAttributeMetadata($a3);
84
85         $a4 = new AttributeMetadata('a4');
86         $a4->addGroup('test');
87         $a4->addGroup('other');
88         $classMetadata->addAttributeMetadata($a4);
89
90         $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
91
92         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
93         $this->assertEquals(array($a2, $a4), $result);
94
95         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
96         $this->assertEquals(array($a3, $a4), $result);
97     }
98
99     public function testObjectToPopulateWithProxy()
100     {
101         $proxyDummy = new ProxyDummy();
102
103         $context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy);
104
105         $normalizer = new ObjectNormalizer();
106         $normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
107
108         $this->assertSame('bar', $proxyDummy->getFoo());
109     }
110
111     public function testObjectWithStaticConstructor()
112     {
113         $normalizer = new StaticConstructorNormalizer();
114         $dummy = $normalizer->denormalize(array('foo' => 'baz'), StaticConstructorDummy::class);
115
116         $this->assertInstanceOf(StaticConstructorDummy::class, $dummy);
117         $this->assertEquals('baz', $dummy->quz);
118         $this->assertNull($dummy->foo);
119     }
120
121     /**
122      * @requires PHP 7.1
123      */
124     public function testObjectWithNullableConstructorArgument()
125     {
126         $normalizer = new ObjectNormalizer();
127         $dummy = $normalizer->denormalize(array('foo' => null), NullableConstructorArgumentDummy::class);
128
129         $this->assertNull($dummy->getFoo());
130     }
131 }