Updated SerializerTest with "normalizeTraversable" & "testNormalizeGivesPriorityToInterfaceOverTraversable"

This commit is contained in:
Eric Clemmons 2011-11-01 20:19:55 -07:00
parent d789f9424e
commit e851efc8d6
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace Symfony\Tests\Component\Serializer\Fixtures;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\SerializerInterface;
class NormalizableTraversableDummy extends TraversableDummy implements NormalizableInterface
{
public function normalize(SerializerInterface $serializer, $format = null)
{
return array(
'foo' => 'normalizedFoo',
'bar' => 'normalizedBar',
);
}
public function denormalize(SerializerInterface $serializer, $data, $format = null)
{
return array(
'foo' => 'denormalizedFoo',
'bar' => 'denormalizedBar',
);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Symfony\Tests\Component\Serializer\Fixtures;
class TraversableDummy implements \IteratorAggregate
{
public $foo = 'foo';
public $bar = 'bar';
public function getIterator()
{
return new \ArrayIterator(get_object_vars($this));
}
}

View File

@ -4,6 +4,9 @@ namespace Symfony\Tests\Component\Serializer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
/*
* This file is part of the Symfony framework.
@ -25,6 +28,20 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->serializer->normalize(new \stdClass, 'xml');
}
public function testNormalizeTraversable()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
$result = $this->serializer->serialize(new TraversableDummy, 'json');
$this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
}
public function testNormalizeGivesPriorityToInterfaceOverTraversable()
{
$this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
$result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
}
/**
* @expectedException \UnexpectedValueException
*/