Fixed fatal error in normalize/denormalizeObject.

This commit is contained in:
Lin Clark 2013-05-07 18:53:47 -04:00 committed by Fabien Potencier
parent c59c10ced4
commit 60edc58325
4 changed files with 101 additions and 2 deletions

View File

@ -239,7 +239,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsNormalization($object, $format)) {
if ($normalizer instanceof NormalizerInterface
&& $normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
return $normalizer->normalize($object, $format, $context);
@ -273,7 +274,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsDenormalization($data, $class, $format)) {
if ($normalizer instanceof DenormalizerInterface
&& $normalizer->supportsDenormalization($data, $class, $format)) {
$this->denormalizerCache[$class][$format] = $normalizer;
return $normalizer->denormalize($data, $class, $format, $context);

View File

@ -0,0 +1,38 @@
<?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;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Provides a test Normalizer which only implements the DenormalizerInterface.
*
* @author Lin Clark <lin@lin-clark.com>
*/
class TestDenormalizer implements DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return TRUE;
}
}

View File

@ -0,0 +1,38 @@
<?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;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Provides a test Normalizer which only implements the NormalizerInterface.
*
* @author Lin Clark <lin@lin-clark.com>
*/
class TestNormalizer implements NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return TRUE;
}
}

View File

@ -17,6 +17,8 @@ use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
class SerializerTest extends \PHPUnit_Framework_TestCase
{
@ -43,6 +45,15 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testNormalizeOnDenormalizer()
{
$this->serializer = new Serializer(array(new TestDenormalizer()), array());
$this->assertTrue($this->serializer->normalize(new \stdClass, 'json'));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
@ -52,6 +63,16 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->serializer->denormalize('foo', 'stdClass');
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDenormalizeOnNormalizer()
{
$this->serializer = new Serializer(array(new TestNormalizer()), array());
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
}
public function testSerialize()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));