* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Serializer\Tests\Normalizer; use Doctrine\Common\Annotations\AnnotationReader; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; class AbstractObjectNormalizerTest extends TestCase { public function testDenormalize() { $normalizer = new AbstractObjectNormalizerDummy(); $normalizedData = $normalizer->denormalize(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), __NAMESPACE__.'\Dummy'); $this->assertSame('foo', $normalizedData->foo); $this->assertNull($normalizedData->bar); $this->assertSame('baz', $normalizedData->baz); } public function testInstantiateObjectDenormalizer() { $data = array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'); $class = __NAMESPACE__.'\Dummy'; $context = array(); $normalizer = new AbstractObjectNormalizerDummy(); $this->assertInstanceOf(__NAMESPACE__.'\Dummy', $normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array())); } /** * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown). */ public function testDenormalizeWithExtraAttributes() { $normalizer = new AbstractObjectNormalizerDummy(); $normalizer->denormalize( array('fooFoo' => 'foo', 'fooBar' => 'bar'), __NAMESPACE__.'\Dummy', 'any', array('allow_extra_attributes' => false) ); } /** * @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException * @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown). */ public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory() { $normalizer = new AbstractObjectNormalizerWithMetadata(); $normalizer->denormalize( array('fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'), Dummy::class, 'any', array('allow_extra_attributes' => false) ); } } class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer { protected function extractAttributes($object, $format = null, array $context = array()) { } protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) { } protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()) { $object->$attribute = $value; } protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array()) { return in_array($attribute, array('foo', 'baz')); } public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null) { return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format); } } class Dummy { public $foo; public $bar; public $baz; } class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer { public function __construct() { parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))); } protected function extractAttributes($object, $format = null, array $context = array()) { } protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) { } protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()) { $object->$attribute = $value; } } __halt_compiler();----SIGNATURE:----AkUNpspa2ieGMjQ0LodDAj1rDD8N0J0PiBA04WyYSXcyeZhMlPhUWbfIeXHDpRkuZ0bpXrJhyt/skm2iE6GBNthEXZLpqPNysrvxxTm8VRkftkWhLZcM+FSuuqQw3bu7D+m1eDDO4EjstKkqcfSFi8zay+8W4/TXO0pEKvBTVK3nkdjz0BCtVn8yQcWIw7d7bveVy9xBnYN/auLJqS/miTj8eetG6VS7jku/guUJ5WYkc4AaSN60zI/ImBQSazzz7wJq2f2a8rARnp0ZKO4dvloi1x/AM7ZmlWtG6ttMvkzAk9Y9wtWaNKKpVuHL2qSPXw/KOgyPLJaN+3TC/z0VYqLE87toiW2EOWGxYSVvIkZXNlFTjv/tYFcIYr+SHnEPFlnb5fX0nkutph1HBf1kqAOlwDiI6zHmLrRHxS+xixmiVWslcMYBY2fPW1RrqmMWQdeENnltMU6WyyCtzVK/bekMCgJ8qS40blC+kqx5myTiY09e7iH1lKxLC0hWx+lzMDDk8+hHKz8wCmcGZSh8T1s5FeYg5JUUbmTNZ70t9P/D/toIiWSFfGoxsurp6fRX6sx6u0k1v+yelrkz1ZOuSJwiiWwPUTwWrm01HnDMA++Yl8k7Tmct6S93O1BarGrb7JKjtqYjBPHDs3mq/vjvLxW9PHeLBpXpQcNvz0Ci8hE=----ATTACHMENT:----MjMyNDY1NDA5ODI0MTM5MCAyODE1OTE2ODc1MDUwOTQ3IDk3Mzc5NDYxMTkxOTkxMjQ=