Skip to content

Commit

Permalink
Throw exception when unable to normalize embedded object
Browse files Browse the repository at this point in the history
Added a check to ensure that injected serializer of the
GetSetMethodNormalizer is a normalizer before normalizing object value.
  • Loading branch information
gquemener authored and fabpot committed Feb 27, 2014
1 parent 4986f6d commit a92aa5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Expand Up @@ -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);
}

Expand Down
Expand Up @@ -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')
);
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -188,13 +207,30 @@ 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
{
protected $foo;
private $bar;
protected $camelCase;
protected $object;

public function getFoo()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -263,3 +309,7 @@ public function otherMethod()
throw new \RuntimeException("Dummy::otherMethod() should not be called");
}
}

abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
{
}

0 comments on commit a92aa5e

Please sign in to comment.