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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
17 class AbstractObjectNormalizerTest extends TestCase
19 public function testDenormalize()
21 $normalizer = new AbstractObjectNormalizerDummy();
22 $normalizedData = $normalizer->denormalize(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), __NAMESPACE__.'\Dummy');
24 $this->assertSame('foo', $normalizedData->foo);
25 $this->assertNull($normalizedData->bar);
26 $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 class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
46 protected function extractAttributes($object, $format = null, array $context = array())
50 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
54 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
56 $object->$attribute = $value;
59 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
61 return in_array($attribute, array('foo', 'baz'));
64 public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
66 return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);