Security update for Core, with self-updated composer
[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\ProxyDummy;
13 use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
14 use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
15
16 /**
17  * Provides a dummy Normalizer which extends the AbstractNormalizer.
18  *
19  * @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
20  */
21 class AbstractNormalizerTest extends TestCase
22 {
23     /**
24      * @var AbstractNormalizerDummy
25      */
26     private $normalizer;
27
28     /**
29      * @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
30      */
31     private $classMetadata;
32
33     protected function setUp()
34     {
35         $loader = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Loader\LoaderChain')->setConstructorArgs(array(array()))->getMock();
36         $this->classMetadata = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory')->setConstructorArgs(array($loader))->getMock();
37         $this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
38     }
39
40     public function testGetAllowedAttributesAsString()
41     {
42         $classMetadata = new ClassMetadata('c');
43
44         $a1 = new AttributeMetadata('a1');
45         $classMetadata->addAttributeMetadata($a1);
46
47         $a2 = new AttributeMetadata('a2');
48         $a2->addGroup('test');
49         $classMetadata->addAttributeMetadata($a2);
50
51         $a3 = new AttributeMetadata('a3');
52         $a3->addGroup('other');
53         $classMetadata->addAttributeMetadata($a3);
54
55         $a4 = new AttributeMetadata('a4');
56         $a4->addGroup('test');
57         $a4->addGroup('other');
58         $classMetadata->addAttributeMetadata($a4);
59
60         $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
61
62         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), true);
63         $this->assertEquals(array('a2', 'a4'), $result);
64
65         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), true);
66         $this->assertEquals(array('a3', 'a4'), $result);
67     }
68
69     public function testGetAllowedAttributesAsObjects()
70     {
71         $classMetadata = new ClassMetadata('c');
72
73         $a1 = new AttributeMetadata('a1');
74         $classMetadata->addAttributeMetadata($a1);
75
76         $a2 = new AttributeMetadata('a2');
77         $a2->addGroup('test');
78         $classMetadata->addAttributeMetadata($a2);
79
80         $a3 = new AttributeMetadata('a3');
81         $a3->addGroup('other');
82         $classMetadata->addAttributeMetadata($a3);
83
84         $a4 = new AttributeMetadata('a4');
85         $a4->addGroup('test');
86         $a4->addGroup('other');
87         $classMetadata->addAttributeMetadata($a4);
88
89         $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
90
91         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
92         $this->assertEquals(array($a2, $a4), $result);
93
94         $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
95         $this->assertEquals(array($a3, $a4), $result);
96     }
97
98     public function testObjectToPopulateWithProxy()
99     {
100         $proxyDummy = new ProxyDummy();
101
102         $context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy);
103
104         $normalizer = new ObjectNormalizer();
105         $normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
106
107         $this->assertSame('bar', $proxyDummy->getFoo());
108     }
109
110     public function testObjectWithStaticConstructor()
111     {
112         $normalizer = new StaticConstructorNormalizer();
113         $dummy = $normalizer->denormalize(array('foo' => 'baz'), StaticConstructorDummy::class);
114
115         $this->assertInstanceOf(StaticConstructorDummy::class, $dummy);
116         $this->assertEquals('baz', $dummy->quz);
117         $this->assertNull($dummy->foo);
118     }
119 }