2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\Serializer;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SerializerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
*/
|
2011-06-10 00:11:51 +01:00
|
|
|
public function testNormalizeNoMatch()
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
|
|
|
$this->serializer->normalize(new \stdClass, 'xml');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
*/
|
2011-06-10 00:11:51 +01:00
|
|
|
public function testDenormalizeNoMatch()
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
|
|
|
$this->serializer->denormalize('foo', 'stdClass');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSerializeScalar()
|
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
2011-01-18 19:17:24 +00:00
|
|
|
$result = $this->serializer->serialize('foo', 'json');
|
|
|
|
$this->assertEquals('"foo"', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSerializeArrayOfScalars()
|
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
2011-01-18 19:17:24 +00:00
|
|
|
$data = array('foo', array(5, 3));
|
|
|
|
$result = $this->serializer->serialize($data, 'json');
|
|
|
|
$this->assertEquals(json_encode($data), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncode()
|
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
2011-01-18 19:17:24 +00:00
|
|
|
$data = array('foo', array(5, 3));
|
|
|
|
$result = $this->serializer->encode($data, 'json');
|
|
|
|
$this->assertEquals(json_encode($data), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDecode()
|
|
|
|
{
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
2011-01-18 19:17:24 +00:00
|
|
|
$data = array('foo', array(5, 3));
|
|
|
|
$result = $this->serializer->decode(json_encode($data), 'json');
|
|
|
|
$this->assertEquals($data, $result);
|
|
|
|
}
|
|
|
|
}
|