3ca418d55ed6b8171f19f3e762cd956330e6aaa9
[yaffs-website] / vendor / symfony / serializer / Tests / Normalizer / AbstractObjectNormalizerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Tests\Normalizer;
13
14 use Doctrine\Common\Annotations\AnnotationReader;
15 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
17 use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
18 use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
19
20 class AbstractObjectNormalizerTest extends TestCase
21 {
22     public function testDenormalize()
23     {
24         $normalizer = new AbstractObjectNormalizerDummy();
25         $normalizedData = $normalizer->denormalize(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), __NAMESPACE__.'\Dummy');
26
27         $this->assertSame('foo', $normalizedData->foo);
28         $this->assertNull($normalizedData->bar);
29         $this->assertSame('baz', $normalizedData->baz);
30     }
31
32     public function testInstantiateObjectDenormalizer()
33     {
34         $data = array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz');
35         $class = __NAMESPACE__.'\Dummy';
36         $context = array();
37
38         $normalizer = new AbstractObjectNormalizerDummy();
39
40         $this->assertInstanceOf(__NAMESPACE__.'\Dummy', $normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array()));
41     }
42
43     /**
44      * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
45      * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
46      */
47     public function testDenormalizeWithExtraAttributes()
48     {
49         $normalizer = new AbstractObjectNormalizerDummy();
50         $normalizer->denormalize(
51             array('fooFoo' => 'foo', 'fooBar' => 'bar'),
52             __NAMESPACE__.'\Dummy',
53             'any',
54             array('allow_extra_attributes' => false)
55         );
56     }
57
58     /**
59      * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
60      * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
61      */
62     public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory()
63     {
64         $normalizer = new AbstractObjectNormalizerWithMetadata();
65         $normalizer->denormalize(
66             array('fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'),
67             Dummy::class,
68             'any',
69             array('allow_extra_attributes' => false)
70         );
71     }
72 }
73
74 class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
75 {
76     protected function extractAttributes($object, $format = null, array $context = array())
77     {
78     }
79
80     protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
81     {
82     }
83
84     protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
85     {
86         $object->$attribute = $value;
87     }
88
89     protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
90     {
91         return in_array($attribute, array('foo', 'baz'));
92     }
93
94     public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
95     {
96         return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
97     }
98 }
99
100 class Dummy
101 {
102     public $foo;
103     public $bar;
104     public $baz;
105 }
106
107 class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
108 {
109     public function __construct()
110     {
111         parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
112     }
113
114     protected function extractAttributes($object, $format = null, array $context = array())
115     {
116     }
117
118     protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
119     {
120     }
121
122     protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
123     {
124         $object->$attribute = $value;
125     }
126 }