added several tests to the serializer (mainly for deserialization)

This commit is contained in:
lsmith77 2011-12-11 12:09:53 +01:00
parent ab0b4f3c2c
commit b1ca0cdfe9

View File

@ -4,6 +4,7 @@ namespace Symfony\Tests\Component\Serializer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
@ -20,7 +21,7 @@ use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
class SerializerTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \UnexpectedValueException
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testNormalizeNoMatch()
{
@ -43,7 +44,7 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException \UnexpectedValueException
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDenormalizeNoMatch()
{
@ -51,6 +52,14 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->serializer->denormalize('foo', 'stdClass');
}
public function testSerialize()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$result = $this->serializer->serialize(Model::fromArray($data), 'json');
$this->assertEquals(json_encode($data), $result);
}
public function testSerializeScalar()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@ -66,6 +75,74 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(json_encode($data), $result);
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testSerializeNoEncoder()
{
$this->serializer = new Serializer(array(), array());
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->serialize($data, 'json');
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
*/
public function testSerializeNoNormalizer()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->serialize(Model::fromArray($data), 'json');
}
public function testDeserialize()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
$this->assertEquals($data, $result->toArray());
}
public function testDeserializeUseCache()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
$data = array('title' => 'bar', 'numbers' => array(2, 8));
$result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
$this->assertEquals($data, $result->toArray());
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
*/
public function testDeserializeNoNormalizer()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDeserializeWrongNormalizer()
{
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDeerializeNoEncoder()
{
$this->serializer = new Serializer(array(), array());
$data = array('title' => 'foo', 'numbers' => array(5, 3));
$this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
}
public function testEncode()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@ -82,3 +159,48 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($data, $result);
}
}
class Model
{
private $title;
private $numbers;
public static function fromArray($array)
{
$model = new self();
if (isset($array['title'])) {
$model->setTitle($array['title']);
}
if (isset($array['numbers'])) {
$model->setNumbers($array['numbers']);
}
return $model;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getNumbers()
{
return $this->numbers;
}
public function setNumbers($numbers)
{
$this->numbers = $numbers;
}
public function toArray()
{
return array('title' => $this->title, 'numbers' => $this->numbers);
}
}