2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-02 04:52:14 +01:00
|
|
|
namespace Symfony\Component\Serializer\Tests\Normalizer;
|
|
|
|
|
2014-10-01 16:10:16 +01:00
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
2015-04-20 08:28:27 +01:00
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
2012-04-02 04:52:14 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
2014-10-02 14:26:39 +01:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
2014-02-21 09:42:31 +00:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2014-10-02 14:26:39 +01:00
|
|
|
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
2014-10-01 16:10:16 +01:00
|
|
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
2015-12-22 18:41:48 +00:00
|
|
|
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
2014-10-02 14:26:39 +01:00
|
|
|
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
2014-10-01 16:10:16 +01:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class GetSetMethodNormalizerTest extends TestCase
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2014-07-20 15:43:00 +01:00
|
|
|
/**
|
|
|
|
* @var GetSetMethodNormalizer
|
|
|
|
*/
|
|
|
|
private $normalizer;
|
2014-10-01 16:10:16 +01:00
|
|
|
/**
|
|
|
|
* @var SerializerInterface
|
|
|
|
*/
|
|
|
|
private $serializer;
|
2014-07-20 15:43:00 +01:00
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
protected function setUp()
|
2018-10-03 16:09:15 +01:00
|
|
|
{
|
|
|
|
$this->createNormalizer();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createNormalizer(array $defaultContext = array())
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$this->serializer = $this->getMockBuilder(__NAMESPACE__.'\SerializerNormalizer')->getMock();
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
|
2014-02-21 09:42:31 +00:00
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:32:01 +00:00
|
|
|
public function testInterface()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
public function testNormalize()
|
|
|
|
{
|
2013-11-14 14:30:56 +00:00
|
|
|
$obj = new GetSetDummy();
|
2014-02-21 09:42:31 +00:00
|
|
|
$object = new \stdClass();
|
2011-01-18 19:17:24 +00:00
|
|
|
$obj->setFoo('foo');
|
|
|
|
$obj->setBar('bar');
|
2014-02-22 23:56:53 +00:00
|
|
|
$obj->setBaz(true);
|
2013-02-02 16:53:42 +00:00
|
|
|
$obj->setCamelCase('camelcase');
|
2014-02-21 09:42:31 +00:00
|
|
|
$obj->setObject($object);
|
|
|
|
|
|
|
|
$this->serializer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('normalize')
|
|
|
|
->with($object, 'any')
|
|
|
|
->will($this->returnValue('string_object'))
|
|
|
|
;
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
$this->assertEquals(
|
2014-02-21 09:42:31 +00:00
|
|
|
array(
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => 'bar',
|
2014-03-03 15:53:45 +00:00
|
|
|
'baz' => true,
|
2014-02-21 09:42:31 +00:00
|
|
|
'fooBar' => 'foobar',
|
|
|
|
'camelCase' => 'camelcase',
|
|
|
|
'object' => 'string_object',
|
|
|
|
),
|
2011-05-06 18:37:13 +01:00
|
|
|
$this->normalizer->normalize($obj, 'any')
|
|
|
|
);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDenormalize()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
2014-02-22 23:56:53 +00:00
|
|
|
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
2011-05-06 18:37:13 +01:00
|
|
|
__NAMESPACE__.'\GetSetDummy',
|
|
|
|
'any'
|
|
|
|
);
|
2011-01-18 19:17:24 +00:00
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertEquals('bar', $obj->getBar());
|
2014-02-22 23:56:53 +00:00
|
|
|
$this->assertTrue($obj->isBaz());
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 15:43:00 +01:00
|
|
|
public function testDenormalizeWithObject()
|
|
|
|
{
|
|
|
|
$data = new \stdClass();
|
|
|
|
$data->foo = 'foo';
|
|
|
|
$data->bar = 'bar';
|
|
|
|
$data->fooBar = 'foobar';
|
|
|
|
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any');
|
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertEquals('bar', $obj->getBar());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDenormalizeNull()
|
|
|
|
{
|
|
|
|
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
public function testConstructorDenormalize()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
2014-02-22 23:56:53 +00:00
|
|
|
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
2011-01-18 19:17:24 +00:00
|
|
|
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertEquals('bar', $obj->getBar());
|
2014-02-22 23:56:53 +00:00
|
|
|
$this->assertTrue($obj->isBaz());
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2012-03-12 18:46:28 +00:00
|
|
|
|
2015-05-21 07:43:39 +01:00
|
|
|
public function testConstructorDenormalizeWithNullArgument()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array('foo' => 'foo', 'bar' => null, 'baz' => true),
|
|
|
|
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertNull($obj->getBar());
|
|
|
|
$this->assertTrue($obj->isBaz());
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:45:04 +01:00
|
|
|
public function testConstructorDenormalizeWithMissingOptionalArgument()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array('foo' => 'test', 'baz' => array(1, 2, 3)),
|
|
|
|
__NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any');
|
|
|
|
$this->assertEquals('test', $obj->getFoo());
|
|
|
|
$this->assertEquals(array(), $obj->getBar());
|
|
|
|
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:36:47 +01:00
|
|
|
public function testConstructorDenormalizeWithOptionalDefaultArgument()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array('bar' => 'test'),
|
|
|
|
__NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any');
|
|
|
|
$this->assertEquals(array(), $obj->getFoo());
|
|
|
|
$this->assertEquals('test', $obj->getBar());
|
|
|
|
}
|
|
|
|
|
2015-08-01 20:55:55 +01:00
|
|
|
public function testConstructorDenormalizeWithVariadicArgument()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array('foo' => array(1, 2, 3)),
|
|
|
|
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
|
|
|
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorDenormalizeWithMissingVariadicArgument()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array(),
|
|
|
|
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
|
|
|
$this->assertEquals(array(), $obj->getFoo());
|
|
|
|
}
|
|
|
|
|
2014-07-20 15:43:00 +01:00
|
|
|
public function testConstructorWithObjectDenormalize()
|
|
|
|
{
|
|
|
|
$data = new \stdClass();
|
|
|
|
$data->foo = 'foo';
|
|
|
|
$data->bar = 'bar';
|
2014-08-05 10:00:40 +01:00
|
|
|
$data->baz = true;
|
2014-07-20 15:43:00 +01:00
|
|
|
$data->fooBar = 'foobar';
|
|
|
|
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any');
|
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertEquals('bar', $obj->getBar());
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:44 +01:00
|
|
|
public function testConstructorWArgWithPrivateMutator()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
|
|
|
|
$this->assertEquals('bar', $obj->getFoo());
|
|
|
|
}
|
|
|
|
|
2014-10-01 16:10:16 +01:00
|
|
|
public function testGroupsNormalize()
|
|
|
|
{
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
|
|
|
|
|
|
|
$obj = new GroupDummy();
|
|
|
|
$obj->setFoo('foo');
|
|
|
|
$obj->setBar('bar');
|
|
|
|
$obj->setFooBar('fooBar');
|
|
|
|
$obj->setSymfony('symfony');
|
|
|
|
$obj->setKevin('kevin');
|
|
|
|
$obj->setCoopTilleuls('coopTilleuls');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'bar' => 'bar',
|
2016-01-18 16:42:55 +00:00
|
|
|
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('c'))));
|
2014-10-01 16:10:16 +01:00
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'symfony' => 'symfony',
|
|
|
|
'foo' => 'foo',
|
|
|
|
'fooBar' => 'fooBar',
|
|
|
|
'bar' => 'bar',
|
|
|
|
'kevin' => 'kevin',
|
|
|
|
'coopTilleuls' => 'coopTilleuls',
|
2016-01-18 16:42:55 +00:00
|
|
|
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('a', 'c'))));
|
2014-10-01 16:10:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGroupsDenormalize()
|
|
|
|
{
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
|
|
|
|
|
|
|
$obj = new GroupDummy();
|
|
|
|
$obj->setFoo('foo');
|
|
|
|
|
|
|
|
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
|
|
|
|
|
|
|
$normalized = $this->normalizer->denormalize(
|
|
|
|
$toNormalize,
|
|
|
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
|
|
|
null,
|
2016-01-18 16:42:55 +00:00
|
|
|
array(GetSetMethodNormalizer::GROUPS => array('a'))
|
2014-10-01 16:10:16 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($obj, $normalized);
|
|
|
|
|
|
|
|
$obj->setBar('bar');
|
|
|
|
|
|
|
|
$normalized = $this->normalizer->denormalize(
|
|
|
|
$toNormalize,
|
|
|
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
|
|
|
null,
|
2016-01-18 16:42:55 +00:00
|
|
|
array(GetSetMethodNormalizer::GROUPS => array('a', 'b'))
|
2014-10-01 16:10:16 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($obj, $normalized);
|
|
|
|
}
|
|
|
|
|
2015-04-20 08:28:27 +01:00
|
|
|
public function testGroupsNormalizeWithNameConverter()
|
|
|
|
{
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
|
|
|
|
|
|
|
$obj = new GroupDummy();
|
|
|
|
$obj->setFooBar('@dunglas');
|
|
|
|
$obj->setSymfony('@coopTilleuls');
|
|
|
|
$obj->setCoopTilleuls('les-tilleuls.coop');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'bar' => null,
|
|
|
|
'foo_bar' => '@dunglas',
|
|
|
|
'symfony' => '@coopTilleuls',
|
|
|
|
),
|
2016-01-18 16:42:55 +00:00
|
|
|
$this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
|
2015-04-20 08:28:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGroupsDenormalizeWithNameConverter()
|
|
|
|
{
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
|
|
|
|
|
|
|
$obj = new GroupDummy();
|
|
|
|
$obj->setFooBar('@dunglas');
|
|
|
|
$obj->setSymfony('@coopTilleuls');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj,
|
|
|
|
$this->normalizer->denormalize(array(
|
|
|
|
'bar' => null,
|
|
|
|
'foo_bar' => '@dunglas',
|
|
|
|
'symfony' => '@coopTilleuls',
|
|
|
|
'coop_tilleuls' => 'les-tilleuls.coop',
|
2016-01-18 16:42:55 +00:00
|
|
|
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
|
2015-04-20 08:28:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:46:28 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideCallbacks
|
|
|
|
*/
|
|
|
|
public function testCallbacks($callbacks, $value, $result, $message)
|
|
|
|
{
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->doTestCallbacks($callbacks, $value, $result, $message);
|
|
|
|
}
|
2012-03-12 18:46:28 +00:00
|
|
|
|
2018-10-03 16:09:15 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideCallbacks
|
|
|
|
*/
|
|
|
|
public function testLegacyCallbacks($callbacks, $value, $result, $message)
|
|
|
|
{
|
|
|
|
$this->doTestCallbacks($callbacks, $value, $result, $message, true);
|
|
|
|
}
|
2012-03-12 18:46:28 +00:00
|
|
|
|
2018-10-03 16:09:15 +01:00
|
|
|
private function doTestCallbacks($callbacks, $value, $result, $message, bool $legacy = false)
|
|
|
|
{
|
|
|
|
$legacy ? $this->normalizer->setCallbacks($callbacks) : $this->createNormalizer(array(GetSetMethodNormalizer::CALLBACKS => $callbacks));
|
|
|
|
|
|
|
|
$obj = new GetConstructorDummy('', $value, true);
|
2012-03-12 18:46:28 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$result,
|
|
|
|
$this->normalizer->normalize($obj, 'any'),
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testUncallableCallbacks()
|
|
|
|
{
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->doTestUncallableCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testLegacyUncallableCallbacks()
|
|
|
|
{
|
|
|
|
$this->doTestUncallableCallbacks(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doTestUncallableCallbacks(bool $legacy = false)
|
|
|
|
{
|
|
|
|
$callbacks = array('bar' => null);
|
|
|
|
$legacy ? $this->normalizer->setCallbacks($callbacks) : $this->createNormalizer(array(GetSetMethodNormalizer::CALLBACKS => $callbacks));
|
2012-03-12 18:46:28 +00:00
|
|
|
|
2014-02-22 23:56:53 +00:00
|
|
|
$obj = new GetConstructorDummy('baz', 'quux', true);
|
2012-03-12 18:46:28 +00:00
|
|
|
|
|
|
|
$this->normalizer->normalize($obj, 'any');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnoredAttributes()
|
|
|
|
{
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->doTestIgnoredAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLegacyIgnoredAttributes()
|
|
|
|
{
|
|
|
|
$this->doTestIgnoredAttributes(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doTestIgnoredAttributes(bool $legacy = false)
|
|
|
|
{
|
|
|
|
$ignoredAttributes = array('foo', 'bar', 'baz', 'camelCase', 'object');
|
|
|
|
$legacy ? $this->normalizer->setIgnoredAttributes($ignoredAttributes) : $this->createNormalizer(array(GetSetMethodNormalizer::IGNORED_ATTRIBUTES => $ignoredAttributes));
|
2012-03-12 18:46:28 +00:00
|
|
|
|
2013-11-14 14:30:56 +00:00
|
|
|
$obj = new GetSetDummy();
|
2012-03-12 18:46:28 +00:00
|
|
|
$obj->setFoo('foo');
|
|
|
|
$obj->setBar('bar');
|
2014-02-22 23:56:53 +00:00
|
|
|
$obj->setBaz(true);
|
2012-03-12 18:46:28 +00:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('fooBar' => 'foobar'),
|
|
|
|
$this->normalizer->normalize($obj, 'any')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideCallbacks()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'bar' => function ($bar) {
|
|
|
|
return 'baz';
|
|
|
|
},
|
|
|
|
),
|
|
|
|
'baz',
|
2014-02-22 23:56:53 +00:00
|
|
|
array('foo' => '', 'bar' => 'baz', 'baz' => true),
|
2012-03-12 18:46:28 +00:00
|
|
|
'Change a string',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'bar' => function ($bar) {
|
|
|
|
},
|
|
|
|
),
|
|
|
|
'baz',
|
2014-02-22 23:56:53 +00:00
|
|
|
array('foo' => '', 'bar' => null, 'baz' => true),
|
2014-09-21 19:53:12 +01:00
|
|
|
'Null an item',
|
2012-03-12 18:46:28 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'bar' => function ($bar) {
|
|
|
|
return $bar->format('d-m-Y H:i:s');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
new \DateTime('2011-09-10 06:30:00'),
|
2014-02-22 23:56:53 +00:00
|
|
|
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
|
2012-03-12 18:46:28 +00:00
|
|
|
'Format a date',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'bar' => function ($bars) {
|
|
|
|
$foos = '';
|
|
|
|
foreach ($bars as $bar) {
|
|
|
|
$foos .= $bar->getFoo();
|
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
|
2012-03-12 18:46:28 +00:00
|
|
|
return $foos;
|
|
|
|
},
|
|
|
|
),
|
2014-02-22 23:56:53 +00:00
|
|
|
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
|
|
|
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
|
2012-03-12 18:46:28 +00:00
|
|
|
'Collect a property',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'bar' => function ($bars) {
|
2018-07-05 12:24:53 +01:00
|
|
|
return \count($bars);
|
2012-03-12 18:46:28 +00:00
|
|
|
},
|
|
|
|
),
|
2014-02-22 23:56:53 +00:00
|
|
|
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
|
|
|
array('foo' => '', 'bar' => 2, 'baz' => true),
|
2012-03-12 18:46:28 +00:00
|
|
|
'Count a property',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2014-02-21 09:42:31 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-03 22:24:43 +00:00
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
2016-02-02 23:02:19 +00:00
|
|
|
* @expectedExceptionMessage Cannot normalize attribute "object" because the injected serializer is not a normalizer
|
2014-02-21 09:42:31 +00:00
|
|
|
*/
|
|
|
|
public function testUnableToNormalizeObjectAttribute()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$serializer = $this->getMockBuilder('Symfony\Component\Serializer\SerializerInterface')->getMock();
|
2014-02-21 09:42:31 +00:00
|
|
|
$this->normalizer->setSerializer($serializer);
|
|
|
|
|
2014-10-30 20:17:55 +00:00
|
|
|
$obj = new GetSetDummy();
|
2014-02-21 09:42:31 +00:00
|
|
|
$object = new \stdClass();
|
|
|
|
$obj->setObject($object);
|
|
|
|
|
|
|
|
$this->normalizer->normalize($obj, 'any');
|
|
|
|
}
|
2014-10-02 14:26:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testUnableToNormalizeCircularReference()
|
|
|
|
{
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->doTestUnableToNormalizeCircularReference();
|
|
|
|
}
|
2014-10-02 14:26:39 +01:00
|
|
|
|
2018-10-03 16:09:15 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testLegacyUnableToNormalizeCircularReference()
|
|
|
|
{
|
|
|
|
$this->doTestUnableToNormalizeCircularReference(true);
|
|
|
|
}
|
2014-10-02 14:26:39 +01:00
|
|
|
|
2018-10-03 16:09:15 +01:00
|
|
|
private function doTestUnableToNormalizeCircularReference(bool $legacy = false)
|
|
|
|
{
|
|
|
|
$legacy ? $this->normalizer->setCircularReferenceLimit(2) : $this->createNormalizer(array(GetSetMethodNormalizer::CIRCULAR_REFERENCE_LIMIT => 2));
|
|
|
|
$this->serializer = new Serializer(array($this->normalizer));
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
|
|
|
|
|
|
|
$obj = new CircularReferenceDummy();
|
2014-10-02 14:26:39 +01:00
|
|
|
$this->normalizer->normalize($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSiblingReference()
|
|
|
|
{
|
|
|
|
$serializer = new Serializer(array($this->normalizer));
|
|
|
|
$this->normalizer->setSerializer($serializer);
|
|
|
|
|
|
|
|
$siblingHolder = new SiblingHolder();
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
|
|
|
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
|
|
|
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCircularReferenceHandler()
|
|
|
|
{
|
2018-10-03 16:09:15 +01:00
|
|
|
$this->doTestCircularReferenceHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLegacyCircularReferenceHandler()
|
|
|
|
{
|
|
|
|
$this->doTestCircularReferenceHandler(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doTestCircularReferenceHandler(bool $legacy = false)
|
|
|
|
{
|
|
|
|
$handler = function ($obj) {
|
2018-07-05 12:24:53 +01:00
|
|
|
return \get_class($obj);
|
2018-10-03 16:09:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$legacy ? $this->normalizer->setCircularReferenceHandler($handler) : $this->createNormalizer(array(GetSetMethodNormalizer::CIRCULAR_REFERENCE_HANDLER => $handler));
|
|
|
|
$this->serializer = new Serializer(array($this->normalizer));
|
|
|
|
$this->normalizer->setSerializer($this->serializer);
|
2014-10-02 14:26:39 +01:00
|
|
|
|
|
|
|
$obj = new CircularReferenceDummy();
|
|
|
|
|
|
|
|
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
|
|
|
|
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
|
|
|
}
|
2015-01-04 20:11:42 +00:00
|
|
|
|
|
|
|
public function testObjectToPopulate()
|
|
|
|
{
|
|
|
|
$dummy = new GetSetDummy();
|
|
|
|
$dummy->setFoo('foo');
|
|
|
|
|
|
|
|
$obj = $this->normalizer->denormalize(
|
|
|
|
array('bar' => 'bar'),
|
|
|
|
__NAMESPACE__.'\GetSetDummy',
|
|
|
|
null,
|
2016-01-18 16:42:55 +00:00
|
|
|
array(GetSetMethodNormalizer::OBJECT_TO_POPULATE => $dummy)
|
2015-01-04 20:11:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($dummy, $obj);
|
|
|
|
$this->assertEquals('foo', $obj->getFoo());
|
|
|
|
$this->assertEquals('bar', $obj->getBar());
|
|
|
|
}
|
2015-03-01 11:31:43 +00:00
|
|
|
|
|
|
|
public function testDenormalizeNonExistingAttribute()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2015-07-22 20:42:44 +01:00
|
|
|
new GetSetDummy(),
|
|
|
|
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
|
2015-03-01 11:31:43 +00:00
|
|
|
);
|
|
|
|
}
|
2015-05-04 07:35:51 +01:00
|
|
|
|
2015-11-04 07:08:11 +00:00
|
|
|
public function testDenormalizeShouldNotSetStaticAttribute()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(array('staticObject' => true), __NAMESPACE__.'\GetSetDummy');
|
|
|
|
|
|
|
|
$this->assertEquals(new GetSetDummy(), $obj);
|
|
|
|
$this->assertNull(GetSetDummy::getStaticObject());
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:35:51 +01:00
|
|
|
public function testNoTraversableSupport()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
|
|
|
}
|
2015-07-22 20:42:44 +01:00
|
|
|
|
2015-11-04 07:08:11 +00:00
|
|
|
public function testNoStaticGetSetSupport()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:44 +01:00
|
|
|
public function testPrivateSetter()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
|
|
|
|
$this->assertEquals('bar', $obj->getFoo());
|
|
|
|
}
|
2015-12-22 18:41:48 +00:00
|
|
|
|
2017-06-30 15:33:54 +01:00
|
|
|
public function testHasGetterDenormalize()
|
|
|
|
{
|
|
|
|
$obj = $this->normalizer->denormalize(array('foo' => true), ObjectWithHasGetterDummy::class);
|
|
|
|
$this->assertTrue($obj->hasFoo());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasGetterNormalize()
|
|
|
|
{
|
|
|
|
$obj = new ObjectWithHasGetterDummy();
|
|
|
|
$obj->setFoo(true);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('foo' => true),
|
|
|
|
$this->normalizer->normalize($obj, 'any')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-22 18:41:48 +00:00
|
|
|
public function testMaxDepth()
|
|
|
|
{
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
|
|
|
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
|
|
|
$serializer = new Serializer(array($this->normalizer));
|
|
|
|
$this->normalizer->setSerializer($serializer);
|
|
|
|
|
|
|
|
$level1 = new MaxDepthDummy();
|
|
|
|
$level1->bar = 'level1';
|
|
|
|
|
|
|
|
$level2 = new MaxDepthDummy();
|
|
|
|
$level2->bar = 'level2';
|
|
|
|
$level1->child = $level2;
|
|
|
|
|
|
|
|
$level3 = new MaxDepthDummy();
|
|
|
|
$level3->bar = 'level3';
|
|
|
|
$level2->child = $level3;
|
|
|
|
|
|
|
|
$level4 = new MaxDepthDummy();
|
|
|
|
$level4->bar = 'level4';
|
|
|
|
$level3->child = $level4;
|
|
|
|
|
|
|
|
$result = $serializer->normalize($level1, null, array(GetSetMethodNormalizer::ENABLE_MAX_DEPTH => true));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'bar' => 'level1',
|
|
|
|
'child' => array(
|
|
|
|
'bar' => 'level2',
|
|
|
|
'child' => array(
|
|
|
|
'bar' => 'level3',
|
|
|
|
'child' => array(
|
|
|
|
'child' => null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class GetSetDummy
|
|
|
|
{
|
|
|
|
protected $foo;
|
|
|
|
private $bar;
|
2014-02-22 23:56:53 +00:00
|
|
|
private $baz;
|
2013-02-02 16:53:42 +00:00
|
|
|
protected $camelCase;
|
2014-02-21 09:42:31 +00:00
|
|
|
protected $object;
|
2015-11-04 07:08:11 +00:00
|
|
|
private static $staticObject;
|
2011-01-18 19:17:24 +00:00
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFoo($foo)
|
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar()
|
|
|
|
{
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBar($bar)
|
|
|
|
{
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:56:53 +00:00
|
|
|
public function isBaz()
|
|
|
|
{
|
|
|
|
return $this->baz;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBaz($baz)
|
|
|
|
{
|
|
|
|
$this->baz = $baz;
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
public function getFooBar()
|
|
|
|
{
|
2013-04-02 10:39:57 +01:00
|
|
|
return $this->foo.$this->bar;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 16:53:42 +00:00
|
|
|
public function getCamelCase()
|
|
|
|
{
|
|
|
|
return $this->camelCase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCamelCase($camelCase)
|
|
|
|
{
|
|
|
|
$this->camelCase = $camelCase;
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
public function otherMethod()
|
|
|
|
{
|
2015-03-21 10:51:07 +00:00
|
|
|
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2014-02-21 09:42:31 +00:00
|
|
|
|
|
|
|
public function setObject($object)
|
|
|
|
{
|
|
|
|
$this->object = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getObject()
|
|
|
|
{
|
|
|
|
return $this->object;
|
|
|
|
}
|
2015-11-04 07:08:11 +00:00
|
|
|
|
|
|
|
public static function getStaticObject()
|
|
|
|
{
|
|
|
|
return self::$staticObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setStaticObject($object)
|
|
|
|
{
|
|
|
|
self::$staticObject = $object;
|
|
|
|
}
|
2016-03-07 13:45:47 +00:00
|
|
|
|
2016-03-05 11:41:21 +00:00
|
|
|
protected function getPrivate()
|
|
|
|
{
|
2016-03-07 14:04:32 +00:00
|
|
|
throw new \RuntimeException('Dummy::getPrivate() should not be called');
|
2016-03-05 11:41:21 +00:00
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class GetConstructorDummy
|
|
|
|
{
|
|
|
|
protected $foo;
|
|
|
|
private $bar;
|
2014-02-22 23:56:53 +00:00
|
|
|
private $baz;
|
2011-01-18 19:17:24 +00:00
|
|
|
|
2014-02-22 23:56:53 +00:00
|
|
|
public function __construct($foo, $bar, $baz)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
$this->bar = $bar;
|
2014-02-22 23:56:53 +00:00
|
|
|
$this->baz = $baz;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar()
|
|
|
|
{
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:56:53 +00:00
|
|
|
public function isBaz()
|
|
|
|
{
|
|
|
|
return $this->baz;
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
public function otherMethod()
|
|
|
|
{
|
2015-03-21 10:51:07 +00:00
|
|
|
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|
2014-02-21 09:42:31 +00:00
|
|
|
|
|
|
|
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
|
|
|
|
{
|
|
|
|
}
|
2014-07-01 07:40:32 +01:00
|
|
|
|
2014-06-24 22:45:04 +01:00
|
|
|
class GetConstructorOptionalArgsDummy
|
|
|
|
{
|
|
|
|
protected $foo;
|
|
|
|
private $bar;
|
|
|
|
private $baz;
|
|
|
|
|
|
|
|
public function __construct($foo, $bar = array(), $baz = array())
|
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
$this->bar = $bar;
|
|
|
|
$this->baz = $baz;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar()
|
|
|
|
{
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBaz()
|
|
|
|
{
|
|
|
|
return $this->baz;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function otherMethod()
|
|
|
|
{
|
2015-03-21 10:51:07 +00:00
|
|
|
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
2014-06-24 22:45:04 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-25 23:45:46 +00:00
|
|
|
|
2015-05-25 00:36:47 +01:00
|
|
|
class GetConstructorArgsWithDefaultValueDummy
|
|
|
|
{
|
|
|
|
protected $foo;
|
|
|
|
protected $bar;
|
|
|
|
|
|
|
|
public function __construct($foo = array(), $bar)
|
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar()
|
|
|
|
{
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function otherMethod()
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-25 23:45:46 +00:00
|
|
|
class GetCamelizedDummy
|
|
|
|
{
|
|
|
|
private $kevinDunglas;
|
|
|
|
private $fooBar;
|
|
|
|
private $bar_foo;
|
|
|
|
|
|
|
|
public function __construct($kevinDunglas = null)
|
|
|
|
{
|
|
|
|
$this->kevinDunglas = $kevinDunglas;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getKevinDunglas()
|
|
|
|
{
|
|
|
|
return $this->kevinDunglas;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFooBar($fooBar)
|
|
|
|
{
|
|
|
|
$this->fooBar = $fooBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFooBar()
|
|
|
|
{
|
|
|
|
return $this->fooBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBar_foo($bar_foo)
|
|
|
|
{
|
|
|
|
$this->bar_foo = $bar_foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar_foo()
|
|
|
|
{
|
|
|
|
return $this->bar_foo;
|
|
|
|
}
|
|
|
|
}
|
2015-07-22 20:42:44 +01:00
|
|
|
|
|
|
|
class ObjectConstructorArgsWithPrivateMutatorDummy
|
|
|
|
{
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct($foo)
|
|
|
|
{
|
|
|
|
$this->setFoo($foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setFoo($foo)
|
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ObjectWithPrivateSetterDummy
|
|
|
|
{
|
|
|
|
private $foo = 'bar';
|
|
|
|
|
|
|
|
public function getFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setFoo($foo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 07:08:11 +00:00
|
|
|
|
|
|
|
class ObjectWithJustStaticSetterDummy
|
|
|
|
{
|
|
|
|
private static $foo = 'bar';
|
|
|
|
|
|
|
|
public static function getFoo()
|
|
|
|
{
|
|
|
|
return self::$foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setFoo($foo)
|
|
|
|
{
|
|
|
|
self::$foo = $foo;
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 15:33:54 +01:00
|
|
|
|
|
|
|
class ObjectWithHasGetterDummy
|
|
|
|
{
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function setFoo($foo)
|
|
|
|
{
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasFoo()
|
|
|
|
{
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}
|