Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed fatal error in normalize/denormalizeObject.
  • Loading branch information
linclark authored and fabpot committed May 8, 2013
1 parent c59c10c commit 60edc58
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -239,7 +239,8 @@ private function normalizeObject($object, $format = null, array $context = array
}

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);
Expand Down Expand Up @@ -273,7 +274,8 @@ private function denormalizeObject($data, $class, $format = null, array $context
}

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);
Expand Down
@@ -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;
}
}
@@ -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;
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -17,6 +17,8 @@
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
{
Expand All @@ -43,6 +45,15 @@ public function testNormalizeGivesPriorityToInterfaceOverTraversable()
$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
*/
Expand All @@ -52,6 +63,16 @@ public function testDenormalizeNoMatch()
$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()));
Expand Down

0 comments on commit 60edc58

Please sign in to comment.