diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index ab6fd58b5f..c19951a9bd 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -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); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/TestDenormalizer.php b/src/Symfony/Component/Serializer/Tests/Normalizer/TestDenormalizer.php new file mode 100644 index 0000000000..ce1c7d2f7f --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/TestDenormalizer.php @@ -0,0 +1,38 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/TestNormalizer.php b/src/Symfony/Component/Serializer/Tests/Normalizer/TestNormalizer.php new file mode 100644 index 0000000000..149c402110 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/TestNormalizer.php @@ -0,0 +1,38 @@ + + * + * 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 + */ +class TestNormalizer implements NormalizerInterface +{ + /** + * {@inheritdoc} + */ + public function normalize($object, $format = null, array $context = array()) + { + + } + + /** + * {@inheritdoc} + */ + public function supportsNormalization($data, $format = null) + { + return TRUE; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 6470393b13..99ec90cfe3 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -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()));