* * 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:----YzxBAGCfULGzGfAu+vKu5ynFoEqLkYKK/lq8Ya8cS16Q2UDhCEtBu4+lo0ySPiDQfLsfK7fJCgba4BqBbHxcDU0dITz+v36ZAJiNRNf+j4mbWGdVi7A1s3dxTpCweVM9YhePfh6q6K/0D91IZNyrPQUW1DcPx3hF8spIieyROf02ZDlGFY0ju8coRuwrMs4SBxcLjSELuqWPVmjE/NYZpPrzw5FQZIB6RwOuXSbVyMR/5+HNHtG214MVToH/Fvp/xIJcHrFb1U4cmSetTjTOa3UkdHfO2j+ZMlzq9CyfcIcUUBrE0QxBkPN+g4WaKxbUQGl2yqgGWgyNDfndJdAQ85z+0m0x/cXuEoM3+ryEa554u7UIk+xf5UdAx8rt9KtvagOdcZJ8V2cm1/ZPJOKo4v7LKox+BXziQgiAuyM+X47VWYILsKyp1ptqIiYSf24z4BuJ196LUXn2utcqEKlD4JfzC12nGBQpujd4ZVmEwyAjbhSjpqEciipGh+b1PlG/+dY/q2V7KQuDPCH5odDZ8vVbm2tNK9S3+hBUJp9oSZE9oz1xkOicThkyX2MdBxIUoIFN7X8J2b0ID0q+2QfqRXbslyeLb2vGITrFBT0h6BO3IrVIF5DVwEzeLt92afVcb/abJch0Lpf2sjo+Tf6GjC3sWupR0rCrXdW6b0GOfdI=----ATTACHMENT:----MzkzNjk4MDQzNTA2MTI0MSAxNzkxNDMyNDM3Mzc2MDMzIDYzMTk5ODIzODA1NzQ2ODY=