From a92aa5e849cb4e097e142243e7a83351277343d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gildas=20Qu=C3=A9m=C3=A9ner?= Date: Fri, 21 Feb 2014 10:42:31 +0100 Subject: [PATCH] Throw exception when unable to normalize embedded object Added a check to ensure that injected serializer of the GetSetMethodNormalizer is a normalizer before normalizing object value. --- .../Normalizer/GetSetMethodNormalizer.php | 3 + .../Normalizer/GetSetMethodNormalizerTest.php | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 1b1a5f568874..ceff71c4dfd7 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -99,6 +99,9 @@ public function normalize($object, $format = null, array $context = array()) $attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue); } if (null !== $attributeValue && !is_scalar($attributeValue)) { + if (!$this->serializer instanceof NormalizerInterface) { + throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName)); + } $attributeValue = $this->serializer->normalize($attributeValue, $format); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index f3bf9694d206..791934d7c161 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -12,23 +12,42 @@ namespace Symfony\Component\Serializer\Tests\Normalizer; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; +use Symfony\Component\Serializer\SerializerInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase { protected function setUp() { + $this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer'); $this->normalizer = new GetSetMethodNormalizer(); - $this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer')); + $this->normalizer->setSerializer($this->serializer); } public function testNormalize() { $obj = new GetSetDummy(); + $object = new \stdClass(); $obj->setFoo('foo'); $obj->setBar('bar'); $obj->setCamelCase('camelcase'); + $obj->setObject($object); + + $this->serializer + ->expects($this->once()) + ->method('normalize') + ->with($object, 'any') + ->will($this->returnValue('string_object')) + ; + $this->assertEquals( - array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'), + array( + 'foo' => 'foo', + 'bar' => 'bar', + 'fooBar' => 'foobar', + 'camelCase' => 'camelcase', + 'object' => 'string_object', + ), $this->normalizer->normalize($obj, 'any') ); } @@ -116,7 +135,7 @@ public function testUncallableCallbacks() public function testIgnoredAttributes() { - $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase')); + $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object')); $obj = new GetSetDummy(); $obj->setFoo('foo'); @@ -188,6 +207,22 @@ public function provideCallbacks() ), ); } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer + */ + public function testUnableToNormalizeObjectAttribute() + { + $serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface'); + $this->normalizer->setSerializer($serializer); + + $obj = new GetSetDummy(); + $object = new \stdClass(); + $obj->setObject($object); + + $this->normalizer->normalize($obj, 'any'); + } } class GetSetDummy @@ -195,6 +230,7 @@ class GetSetDummy protected $foo; private $bar; protected $camelCase; + protected $object; public function getFoo() { @@ -235,6 +271,16 @@ public function otherMethod() { throw new \RuntimeException("Dummy::otherMethod() should not be called"); } + + public function setObject($object) + { + $this->object = $object; + } + + public function getObject() + { + return $this->object; + } } class GetConstructorDummy @@ -263,3 +309,7 @@ public function otherMethod() throw new \RuntimeException("Dummy::otherMethod() should not be called"); } } + +abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface +{ +}