4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Normalizer;
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;
20 class AbstractObjectNormalizerTest extends TestCase
22 public function testDenormalize()
24 $normalizer = new AbstractObjectNormalizerDummy();
25 $normalizedData = $normalizer->denormalize(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), __NAMESPACE__.'\Dummy');
27 $this->assertSame('foo', $normalizedData->foo);
28 $this->assertNull($normalizedData->bar);
29 $this->assertSame('baz', $normalizedData->baz);
32 public function testInstantiateObjectDenormalizer()
34 $data = array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz');
35 $class = __NAMESPACE__.'\Dummy';
38 $normalizer = new AbstractObjectNormalizerDummy();
40 $this->assertInstanceOf(__NAMESPACE__.'\Dummy', $normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array()));
44 * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
45 * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
47 public function testDenormalizeWithExtraAttributes()
49 $normalizer = new AbstractObjectNormalizerDummy();
50 $normalizer->denormalize(
51 array('fooFoo' => 'foo', 'fooBar' => 'bar'),
52 __NAMESPACE__.'\Dummy',
54 array('allow_extra_attributes' => false)
59 * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
60 * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
62 public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory()
64 $normalizer = new AbstractObjectNormalizerWithMetadata();
65 $normalizer->denormalize(
66 array('fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'),
69 array('allow_extra_attributes' => false)
74 class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
76 protected function extractAttributes($object, $format = null, array $context = array())
80 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
84 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
86 $object->$attribute = $value;
89 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
91 return in_array($attribute, array('foo', 'baz'));
94 public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
96 return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
107 class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
109 public function __construct()
111 parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
114 protected function extractAttributes($object, $format = null, array $context = array())
118 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
122 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
124 $object->$attribute = $value;