2016-05-25 21:21:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2017-11-02 18:07:23 +00:00
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
2017-02-20 13:34:33 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-08-01 08:31:22 +01:00
|
|
|
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
2018-06-11 14:57:31 +01:00
|
|
|
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
|
|
|
use Symfony\Component\PropertyInfo\Type;
|
2018-05-21 11:07:14 +01:00
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
2017-11-02 18:07:23 +00:00
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
2016-05-25 21:21:27 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
2018-05-21 11:07:14 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
2018-07-26 10:06:28 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
2018-05-21 11:07:14 +01:00
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface;
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2016-05-25 21:21:27 +01:00
|
|
|
|
2017-02-20 13:34:33 +00:00
|
|
|
class AbstractObjectNormalizerTest extends TestCase
|
2016-05-25 21:21:27 +01:00
|
|
|
{
|
2019-08-01 08:31:22 +01:00
|
|
|
use ForwardCompatTestTrait;
|
|
|
|
|
2016-05-25 21:21:27 +01:00
|
|
|
public function testDenormalize()
|
|
|
|
{
|
|
|
|
$normalizer = new AbstractObjectNormalizerDummy();
|
2019-01-16 09:39:14 +00:00
|
|
|
$normalizedData = $normalizer->denormalize(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], __NAMESPACE__.'\Dummy');
|
2016-05-25 21:21:27 +01:00
|
|
|
|
|
|
|
$this->assertSame('foo', $normalizedData->foo);
|
|
|
|
$this->assertNull($normalizedData->bar);
|
|
|
|
$this->assertSame('baz', $normalizedData->baz);
|
|
|
|
}
|
2016-07-05 11:41:11 +01:00
|
|
|
|
|
|
|
public function testInstantiateObjectDenormalizer()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
$data = ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'];
|
2016-07-05 11:41:11 +01:00
|
|
|
$class = __NAMESPACE__.'\Dummy';
|
2019-01-16 09:39:14 +00:00
|
|
|
$context = [];
|
2016-07-05 14:32:28 +01:00
|
|
|
|
2016-07-05 11:41:11 +01:00
|
|
|
$normalizer = new AbstractObjectNormalizerDummy();
|
2017-04-11 10:53:09 +01:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->assertInstanceOf(__NAMESPACE__.'\Dummy', $normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), []));
|
2016-07-05 11:41:11 +01:00
|
|
|
}
|
2016-09-17 15:54:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
|
|
|
|
* @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
|
|
|
|
*/
|
|
|
|
public function testDenormalizeWithExtraAttributes()
|
|
|
|
{
|
2018-06-21 18:40:47 +01:00
|
|
|
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$normalizer = new AbstractObjectNormalizerDummy($factory);
|
2016-09-17 15:54:28 +01:00
|
|
|
$normalizer->denormalize(
|
2019-01-16 09:39:14 +00:00
|
|
|
['fooFoo' => 'foo', 'fooBar' => 'bar'],
|
2016-09-17 15:54:28 +01:00
|
|
|
__NAMESPACE__.'\Dummy',
|
|
|
|
'any',
|
2019-01-16 09:39:14 +00:00
|
|
|
['allow_extra_attributes' => false]
|
2016-09-17 15:54:28 +01:00
|
|
|
);
|
|
|
|
}
|
2017-11-02 18:07:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
|
|
|
|
* @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
|
|
|
|
*/
|
|
|
|
public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory()
|
|
|
|
{
|
|
|
|
$normalizer = new AbstractObjectNormalizerWithMetadata();
|
|
|
|
$normalizer->denormalize(
|
2019-01-16 09:39:14 +00:00
|
|
|
['fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'],
|
2017-11-02 18:07:23 +00:00
|
|
|
Dummy::class,
|
|
|
|
'any',
|
2019-01-16 09:39:14 +00:00
|
|
|
['allow_extra_attributes' => false]
|
2017-11-02 18:07:23 +00:00
|
|
|
);
|
|
|
|
}
|
2018-05-21 11:07:14 +01:00
|
|
|
|
|
|
|
public function testDenormalizeCollectionDecodedFromXmlWithOneChild()
|
|
|
|
{
|
|
|
|
$denormalizer = $this->getDenormalizerForDummyCollection();
|
|
|
|
|
|
|
|
$dummyCollection = $denormalizer->denormalize(
|
2019-01-16 09:39:14 +00:00
|
|
|
[
|
|
|
|
'children' => [
|
2018-05-21 11:07:14 +01:00
|
|
|
'bar' => 'first',
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
],
|
2018-05-21 11:07:14 +01:00
|
|
|
DummyCollection::class,
|
|
|
|
'xml'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(DummyCollection::class, $dummyCollection);
|
2019-08-01 08:31:22 +01:00
|
|
|
$this->assertIsArray($dummyCollection->children);
|
2018-05-21 11:07:14 +01:00
|
|
|
$this->assertCount(1, $dummyCollection->children);
|
|
|
|
$this->assertInstanceOf(DummyChild::class, $dummyCollection->children[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDenormalizeCollectionDecodedFromXmlWithTwoChildren()
|
|
|
|
{
|
|
|
|
$denormalizer = $this->getDenormalizerForDummyCollection();
|
|
|
|
|
|
|
|
$dummyCollection = $denormalizer->denormalize(
|
2019-01-16 09:39:14 +00:00
|
|
|
[
|
|
|
|
'children' => [
|
|
|
|
['bar' => 'first'],
|
|
|
|
['bar' => 'second'],
|
|
|
|
],
|
|
|
|
],
|
2018-05-21 11:07:14 +01:00
|
|
|
DummyCollection::class,
|
|
|
|
'xml'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(DummyCollection::class, $dummyCollection);
|
2019-08-01 08:31:22 +01:00
|
|
|
$this->assertIsArray($dummyCollection->children);
|
2018-05-21 11:07:14 +01:00
|
|
|
$this->assertCount(2, $dummyCollection->children);
|
|
|
|
$this->assertInstanceOf(DummyChild::class, $dummyCollection->children[0]);
|
|
|
|
$this->assertInstanceOf(DummyChild::class, $dummyCollection->children[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDenormalizerForDummyCollection()
|
|
|
|
{
|
2018-06-11 14:57:31 +01:00
|
|
|
$extractor = $this->getMockBuilder(PhpDocExtractor::class)->getMock();
|
2018-05-21 11:07:14 +01:00
|
|
|
$extractor->method('getTypes')
|
|
|
|
->will($this->onConsecutiveCalls(
|
2019-01-16 09:39:14 +00:00
|
|
|
[
|
2018-06-11 14:57:31 +01:00
|
|
|
new Type(
|
2018-05-21 11:07:14 +01:00
|
|
|
'array',
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
true,
|
2018-06-11 14:57:31 +01:00
|
|
|
new Type('int'),
|
|
|
|
new Type('object', false, DummyChild::class)
|
2018-05-21 11:07:14 +01:00
|
|
|
),
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
2018-05-21 11:07:14 +01:00
|
|
|
null
|
|
|
|
));
|
|
|
|
|
|
|
|
$denormalizer = new AbstractObjectNormalizerCollectionDummy(null, null, $extractor);
|
|
|
|
$arrayDenormalizer = new ArrayDenormalizerDummy();
|
2019-01-16 09:39:14 +00:00
|
|
|
$serializer = new SerializerCollectionDummy([$arrayDenormalizer, $denormalizer]);
|
2018-05-21 11:07:14 +01:00
|
|
|
$arrayDenormalizer->setSerializer($serializer);
|
|
|
|
$denormalizer->setSerializer($serializer);
|
|
|
|
|
|
|
|
return $denormalizer;
|
|
|
|
}
|
2018-06-21 18:40:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that additional attributes throw an exception if no metadata factory is specified.
|
|
|
|
*
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
2018-06-21 21:04:03 +01:00
|
|
|
* @expectedExceptionMessage A class metadata factory must be provided in the constructor when setting "allow_extra_attributes" to false.
|
2018-06-21 18:40:47 +01:00
|
|
|
*/
|
|
|
|
public function testExtraAttributesException()
|
|
|
|
{
|
|
|
|
$normalizer = new ObjectNormalizer();
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$normalizer->denormalize([], \stdClass::class, 'xml', [
|
2018-06-21 18:40:47 +01:00
|
|
|
'allow_extra_attributes' => false,
|
2019-01-16 09:39:14 +00:00
|
|
|
]);
|
2018-06-21 18:40:47 +01:00
|
|
|
}
|
2016-05-25 21:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function extractAttributes($object, $format = null, array $context = [])
|
2016-05-25 21:21:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
|
2016-05-25 21:21:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
|
2016-05-25 21:21:27 +01:00
|
|
|
{
|
|
|
|
$object->$attribute = $value;
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
|
2016-05-25 21:21:27 +01:00
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return \in_array($attribute, ['foo', 'baz']);
|
2016-05-25 21:21:27 +01:00
|
|
|
}
|
2016-07-05 11:41:11 +01:00
|
|
|
|
2016-11-04 13:53:25 +00:00
|
|
|
public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
|
2016-07-05 11:41:11 +01:00
|
|
|
{
|
2016-11-04 13:53:25 +00:00
|
|
|
return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
2016-07-05 11:41:11 +01:00
|
|
|
}
|
2016-05-25 21:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class Dummy
|
|
|
|
{
|
|
|
|
public $foo;
|
|
|
|
public $bar;
|
|
|
|
public $baz;
|
|
|
|
}
|
2017-11-02 18:07:23 +00:00
|
|
|
|
|
|
|
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function extractAttributes($object, $format = null, array $context = [])
|
2017-11-02 18:07:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
|
2017-11-02 18:07:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
|
2017-11-02 18:07:23 +00:00
|
|
|
{
|
|
|
|
$object->$attribute = $value;
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 11:07:14 +01:00
|
|
|
|
|
|
|
class DummyCollection
|
|
|
|
{
|
|
|
|
/** @var DummyChild[] */
|
|
|
|
public $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DummyChild
|
|
|
|
{
|
|
|
|
public $bar;
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:57:31 +01:00
|
|
|
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
private $normalizers;
|
|
|
|
|
|
|
|
/**
|
2018-06-11 14:57:31 +01:00
|
|
|
* @param DenormalizerInterface[] $normalizers
|
2018-05-21 11:07:14 +01:00
|
|
|
*/
|
|
|
|
public function __construct($normalizers)
|
|
|
|
{
|
|
|
|
$this->normalizers = $normalizers;
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
public function serialize($data, $format, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
public function deserialize($data, $type, $format, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
public function denormalize($data, $type, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
foreach ($this->normalizers as $normalizer) {
|
2018-06-11 14:57:31 +01:00
|
|
|
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format, $context)) {
|
2018-05-21 11:07:14 +01:00
|
|
|
return $normalizer->denormalize($data, $type, $format, $context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supportsDenormalization($data, $type, $format = null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:57:31 +01:00
|
|
|
class AbstractObjectNormalizerCollectionDummy extends AbstractObjectNormalizer
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function extractAttributes($object, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
$object->$attribute = $value;
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
|
|
|
|
{
|
|
|
|
return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
public function serialize($data, $format, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
public function deserialize($data, $type, $format, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArrayDenormalizerDummy implements DenormalizerInterface, SerializerAwareInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var SerializerInterface|DenormalizerInterface
|
|
|
|
*/
|
|
|
|
private $serializer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws NotNormalizableValueException
|
|
|
|
*/
|
2019-01-16 09:39:14 +00:00
|
|
|
public function denormalize($data, $class, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
$serializer = $this->serializer;
|
|
|
|
$class = substr($class, 0, -2);
|
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$data[$key] = $serializer->denormalize($value, $class, $format, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2019-01-16 09:39:14 +00:00
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = [])
|
2018-05-21 11:07:14 +01:00
|
|
|
{
|
|
|
|
return '[]' === substr($type, -2)
|
|
|
|
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setSerializer(SerializerInterface $serializer)
|
|
|
|
{
|
|
|
|
$this->serializer = $serializer;
|
|
|
|
}
|
|
|
|
}
|