Update to drupal-org-drupal 8.0.0-rc2. For more information, see https://www.drupal.org/node/2598668
This commit is contained in:
parent
f32e58e4b1
commit
8e18df8c36
3062 changed files with 15044 additions and 172506 deletions
|
@ -1,52 +0,0 @@
|
|||
<?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\Annotation;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupsTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testEmptyGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testNotAnArrayGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => 'coopTilleuls'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => array('a', 1, new \stdClass())));
|
||||
}
|
||||
|
||||
public function testGroupsParameters()
|
||||
{
|
||||
$validData = array('a', 'b');
|
||||
|
||||
$groups = new Groups(array('value' => $validData));
|
||||
$this->assertEquals($validData, $groups->getGroups());
|
||||
}
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
<?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\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
|
||||
class JsonEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder = new JsonEncoder();
|
||||
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
|
||||
}
|
||||
|
||||
public function testEncodeScalar()
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->foo = 'foo';
|
||||
|
||||
$expected = '{"foo":"foo"}';
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
|
||||
}
|
||||
|
||||
public function testComplexObject()
|
||||
{
|
||||
$obj = $this->getObject();
|
||||
|
||||
$expected = $this->getJsonSource();
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
|
||||
}
|
||||
|
||||
public function testOptions()
|
||||
{
|
||||
$context = array('json_encode_options' => JSON_NUMERIC_CHECK);
|
||||
|
||||
$arr = array();
|
||||
$arr['foo'] = '3';
|
||||
|
||||
$expected = '{"foo":3}';
|
||||
|
||||
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json', $context));
|
||||
|
||||
$arr = array();
|
||||
$arr['foo'] = '3';
|
||||
|
||||
$expected = '{"foo":"3"}';
|
||||
|
||||
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
|
||||
}
|
||||
|
||||
protected function getJsonSource()
|
||||
{
|
||||
return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';
|
||||
}
|
||||
|
||||
protected function getObject()
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->foo = 'foo';
|
||||
$obj->bar = array('a', 'b');
|
||||
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
|
||||
$obj->qux = '1';
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
|
@ -1,510 +0,0 @@
|
|||
<?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\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
|
||||
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $encoder;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder = new XmlEncoder();
|
||||
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
|
||||
$this->encoder->setSerializer($serializer);
|
||||
}
|
||||
|
||||
public function testEncodeScalar()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = 'foo';
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>foo</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testSetRootNodeName()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = 'foo';
|
||||
|
||||
$this->encoder->setRootNodeName('test');
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<test>foo</test>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
* @expectedExceptionMessage Document types are not allowed.
|
||||
*/
|
||||
public function testDocTypeIsNotAllowed()
|
||||
{
|
||||
$this->encoder->decode('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'foo');
|
||||
}
|
||||
|
||||
public function testAttributes()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => array(
|
||||
'@id' => 1,
|
||||
'@name' => 'Bar',
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo-bar id="1" name="Bar"/>'.
|
||||
'<Foo Type="test"><Bar>Test</Bar></Foo>'.
|
||||
'<föo_bär>a</föo_bär>'.
|
||||
'<Bar>1</Bar>'.
|
||||
'<Bar>2</Bar>'.
|
||||
'<Bar>3</Bar>'.
|
||||
'<a>b</a>'.
|
||||
'</response>'."\n";
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testElementNameValid()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => 'a',
|
||||
'foo_bar' => 'a',
|
||||
'föo_bär' => 'a',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo-bar>a</foo-bar>'.
|
||||
'<foo_bar>a</foo_bar>'.
|
||||
'<föo_bär>a</föo_bär>'.
|
||||
'</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeSimpleXML()
|
||||
{
|
||||
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
||||
$array = array('person' => $xml);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person><firstname>Peter</firstname></person></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeXmlAttributes()
|
||||
{
|
||||
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
||||
$array = array('person' => $xml);
|
||||
|
||||
$expected = '<?xml version="1.1" encoding="utf-8" standalone="yes"?>'."\n".
|
||||
'<response><person><firstname>Peter</firstname></person></response>'."\n";
|
||||
|
||||
$context = array(
|
||||
'xml_version' => '1.1',
|
||||
'xml_encoding' => 'utf-8',
|
||||
'xml_standalone' => true,
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
|
||||
}
|
||||
|
||||
public function testContext()
|
||||
{
|
||||
$array = array('person' => array('name' => 'George Abitbol'));
|
||||
$expected = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<response>
|
||||
<person>
|
||||
<name>George Abitbol</name>
|
||||
</person>
|
||||
</response>
|
||||
|
||||
XML;
|
||||
|
||||
$context = array(
|
||||
'xml_format_output' => true,
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
|
||||
}
|
||||
|
||||
public function testEncodeScalarRootAttributes()
|
||||
{
|
||||
$array = array(
|
||||
'#' => 'Paul',
|
||||
'@gender' => 'm',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response gender="m">Paul</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeRootAttributes()
|
||||
{
|
||||
$array = array(
|
||||
'firstname' => 'Paul',
|
||||
'@gender' => 'm',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response gender="m"><firstname>Paul</firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeCdataWrapping()
|
||||
{
|
||||
$array = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeScalarWithAttribute()
|
||||
{
|
||||
$array = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person gender="M">Peter</person></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalar()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response>foo</response>'."\n";
|
||||
|
||||
$this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$source = $this->getXmlSource();
|
||||
$obj = $this->getObject();
|
||||
|
||||
$this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeWithNamespace()
|
||||
{
|
||||
$source = $this->getNamespacedXmlSource();
|
||||
$array = $this->getNamespacedArray();
|
||||
|
||||
$this->assertEquals($source, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeSerializerXmlRootNodeNameOption()
|
||||
{
|
||||
$options = array('xml_root_node_name' => 'test');
|
||||
$this->encoder = new XmlEncoder();
|
||||
$serializer = new Serializer(array(), array('xml' => new XmlEncoder()));
|
||||
$this->encoder->setSerializer($serializer);
|
||||
|
||||
$array = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<test><person gender="M">Peter</person></test>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$source = $this->getXmlSource();
|
||||
$obj = $this->getObject();
|
||||
|
||||
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeCdataWrapping()
|
||||
{
|
||||
$expected = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$xml = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeCdataWrappingAndWhitespace()
|
||||
{
|
||||
$expected = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$xml = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname>'."\n".
|
||||
'<![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeWithNamespace()
|
||||
{
|
||||
$source = $this->getNamespacedXmlSource();
|
||||
$array = $this->getNamespacedArray();
|
||||
|
||||
$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalarWithAttribute()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person gender="M">Peter</person></response>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalarRootAttributes()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<person gender="M">Peter</person>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'#' => 'Peter',
|
||||
'@gender' => 'M',
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeRootAttributes()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'firstname' => 'Peter',
|
||||
'lastname' => 'Mac Calloway',
|
||||
'@gender' => 'M',
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeArray()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<people>'.
|
||||
'<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
|
||||
'<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
|
||||
'</people>'.
|
||||
'</response>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'people' => array('person' => array(
|
||||
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
|
||||
array('firstname' => 'Damien', 'lastname' => 'Clay'),
|
||||
)),
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeIgnoreWhiteSpace()
|
||||
{
|
||||
$source = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<people>
|
||||
<person>
|
||||
<firstname>Benjamin</firstname>
|
||||
<lastname>Alexandre</lastname>
|
||||
</person>
|
||||
<person>
|
||||
<firstname>Damien</firstname>
|
||||
<lastname>Clay</lastname>
|
||||
</person>
|
||||
</people>
|
||||
XML;
|
||||
$expected = array('person' => array(
|
||||
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
|
||||
array('firstname' => 'Damien', 'lastname' => 'Clay'),
|
||||
));
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeWithoutItemHash()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => array(
|
||||
'@key' => 'value',
|
||||
'item' => array('@key' => 'key', 'key-val' => 'val'),
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$expected = array(
|
||||
'foo-bar' => array(
|
||||
'@key' => 'value',
|
||||
'key' => array('@key' => 'key', 'key-val' => 'val'),
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$xml = $this->encoder->encode($obj, 'xml');
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDecodeInvalidXml()
|
||||
{
|
||||
$this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
|
||||
}
|
||||
|
||||
public function testPreventsComplexExternalEntities()
|
||||
{
|
||||
$oldCwd = getcwd();
|
||||
chdir(__DIR__);
|
||||
|
||||
try {
|
||||
$this->encoder->decode('<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=XmlEncoderTest.php">]><scan>&test;</scan>', 'xml');
|
||||
chdir($oldCwd);
|
||||
|
||||
$this->fail('No exception was thrown.');
|
||||
} catch (\Exception $e) {
|
||||
chdir($oldCwd);
|
||||
|
||||
if (!$e instanceof UnexpectedValueException) {
|
||||
$this->fail('Expected UnexpectedValueException');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testDecodeEmptyXml()
|
||||
{
|
||||
$this->setExpectedException('Symfony\Component\Serializer\Exception\UnexpectedValueException', 'Invalid XML data, it can not be empty.');
|
||||
$this->encoder->decode(' ', 'xml');
|
||||
}
|
||||
|
||||
protected function getXmlSource()
|
||||
{
|
||||
return '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo>foo</foo>'.
|
||||
'<bar>a</bar><bar>b</bar>'.
|
||||
'<baz><key>val</key><key2>val</key2><item key="A B">bar</item>'.
|
||||
'<item><title>title1</title></item><item><title>title2</title></item>'.
|
||||
'<Barry><FooBar id="1"><Baz>Ed</Baz></FooBar></Barry></baz>'.
|
||||
'<qux>1</qux>'.
|
||||
'</response>'."\n";
|
||||
}
|
||||
|
||||
protected function getNamespacedXmlSource()
|
||||
{
|
||||
return '<?xml version="1.0"?>'."\n".
|
||||
'<response xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">'.
|
||||
'<qux>1</qux>'.
|
||||
'<app:foo>foo</app:foo>'.
|
||||
'<yt:bar>a</yt:bar><yt:bar>b</yt:bar>'.
|
||||
'<media:baz><media:key>val</media:key><media:key2>val</media:key2><item key="A B">bar</item>'.
|
||||
'<item><title>title1</title></item><item><title>title2</title></item>'.
|
||||
'<Barry size="large"><FooBar gd:id="1"><Baz>Ed</Baz></FooBar></Barry></media:baz>'.
|
||||
'</response>'."\n";
|
||||
}
|
||||
|
||||
protected function getNamespacedArray()
|
||||
{
|
||||
return array(
|
||||
'@xmlns' => 'http://www.w3.org/2005/Atom',
|
||||
'@xmlns:app' => 'http://www.w3.org/2007/app',
|
||||
'@xmlns:media' => 'http://search.yahoo.com/mrss/',
|
||||
'@xmlns:gd' => 'http://schemas.google.com/g/2005',
|
||||
'@xmlns:yt' => 'http://gdata.youtube.com/schemas/2007',
|
||||
'qux' => '1',
|
||||
'app:foo' => 'foo',
|
||||
'yt:bar' => array('a', 'b'),
|
||||
'media:baz' => array(
|
||||
'media:key' => 'val',
|
||||
'media:key2' => 'val',
|
||||
'A B' => 'bar',
|
||||
'item' => array(
|
||||
array(
|
||||
'title' => 'title1',
|
||||
),
|
||||
array(
|
||||
'title' => 'title2',
|
||||
),
|
||||
),
|
||||
'Barry' => array(
|
||||
'@size' => 'large',
|
||||
'FooBar' => array(
|
||||
'Baz' => 'Ed',
|
||||
'@gd:id' => 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getObject()
|
||||
{
|
||||
$obj = new Dummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->bar = array('a', 'b');
|
||||
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
|
||||
$obj->qux = '1';
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CircularReferenceDummy
|
||||
{
|
||||
public function getMe()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class DenormalizableDummy implements DenormalizableInterface
|
||||
{
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class Dummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public $foo;
|
||||
public $bar;
|
||||
public $baz;
|
||||
public $qux;
|
||||
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => $this->foo,
|
||||
'bar' => $this->bar,
|
||||
'baz' => $this->baz,
|
||||
'qux' => $this->qux,
|
||||
);
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
$this->foo = $data['foo'];
|
||||
$this->bar = $data['bar'];
|
||||
$this->baz = $data['baz'];
|
||||
$this->qux = $data['qux'];
|
||||
}
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupDummy extends GroupDummyParent implements GroupDummyInterface
|
||||
{
|
||||
/**
|
||||
* @Groups({"a"})
|
||||
*/
|
||||
private $foo;
|
||||
/**
|
||||
* @Groups({"b", "c", "name_converter"})
|
||||
*/
|
||||
protected $bar;
|
||||
private $fooBar;
|
||||
private $symfony;
|
||||
|
||||
/**
|
||||
* @Groups({"b"})
|
||||
*/
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"c"})
|
||||
*/
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFooBar($fooBar)
|
||||
{
|
||||
$this->fooBar = $fooBar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"a", "b", "name_converter"})
|
||||
*/
|
||||
public function isFooBar()
|
||||
{
|
||||
return $this->fooBar;
|
||||
}
|
||||
|
||||
public function setSymfony($symfony)
|
||||
{
|
||||
$this->symfony = $symfony;
|
||||
}
|
||||
|
||||
public function getSymfony()
|
||||
{
|
||||
return $this->symfony;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface GroupDummyInterface
|
||||
{
|
||||
/**
|
||||
* @Groups({"a", "name_converter"})
|
||||
*/
|
||||
public function getSymfony();
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupDummyParent
|
||||
{
|
||||
/**
|
||||
* @Groups({"a"})
|
||||
*/
|
||||
private $kevin;
|
||||
private $coopTilleuls;
|
||||
|
||||
public function setKevin($kevin)
|
||||
{
|
||||
$this->kevin = $kevin;
|
||||
}
|
||||
|
||||
public function getKevin()
|
||||
{
|
||||
return $this->kevin;
|
||||
}
|
||||
|
||||
public function setCoopTilleuls($coopTilleuls)
|
||||
{
|
||||
$this->coopTilleuls = $coopTilleuls;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"a", "b"})
|
||||
*/
|
||||
public function getCoopTilleuls()
|
||||
{
|
||||
return $this->coopTilleuls;
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class NormalizableTraversableDummy extends TraversableDummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => 'normalizedFoo',
|
||||
'bar' => 'normalizedBar',
|
||||
);
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => 'denormalizedFoo',
|
||||
'bar' => 'denormalizedBar',
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertyCircularReferenceDummy
|
||||
{
|
||||
public $me;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->me = $this;
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertySiblingHolder
|
||||
{
|
||||
public $sibling0;
|
||||
public $sibling1;
|
||||
public $sibling2;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$sibling = new PropertySibling();
|
||||
|
||||
$this->sibling0 = $sibling;
|
||||
$this->sibling1 = $sibling;
|
||||
$this->sibling2 = $sibling;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertySibling
|
||||
{
|
||||
public $coopTilleuls = 'Les-Tilleuls.coop';
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class ScalarDummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public $foo;
|
||||
public $xmlFoo;
|
||||
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return $format === 'xml' ? $this->xmlFoo : $this->foo;
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
if ($format === 'xml') {
|
||||
$this->xmlFoo = $data;
|
||||
} else {
|
||||
$this->foo = $data;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class SiblingHolder
|
||||
{
|
||||
private $sibling0;
|
||||
private $sibling1;
|
||||
private $sibling2;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$sibling = new Sibling();
|
||||
|
||||
$this->sibling0 = $sibling;
|
||||
$this->sibling1 = $sibling;
|
||||
$this->sibling2 = $sibling;
|
||||
}
|
||||
|
||||
public function getSibling0()
|
||||
{
|
||||
return $this->sibling0;
|
||||
}
|
||||
|
||||
public function getSibling1()
|
||||
{
|
||||
return $this->sibling1;
|
||||
}
|
||||
|
||||
public function getSibling2()
|
||||
{
|
||||
return $this->sibling2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class Sibling
|
||||
{
|
||||
public function getCoopTilleuls()
|
||||
{
|
||||
return 'Les-Tilleuls.coop';
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
class TraversableDummy implements \IteratorAggregate
|
||||
{
|
||||
public $foo = 'foo';
|
||||
public $bar = 'bar';
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator(get_object_vars($this));
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
class VariadicConstructorArgsDummy
|
||||
{
|
||||
private $foo;
|
||||
|
||||
public function __construct(...$foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
foo
|
|
@ -1,18 +0,0 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd">
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\GroupDummy">
|
||||
<attribute name="foo">
|
||||
<group>group1</group>
|
||||
<group>group2</group>
|
||||
</attribute>
|
||||
|
||||
<attribute name="bar">
|
||||
<group>group2</group>
|
||||
</attribute>
|
||||
</class>
|
||||
|
||||
</serializer>
|
|
@ -1,6 +0,0 @@
|
|||
Symfony\Component\Serializer\Tests\Fixtures\GroupDummy:
|
||||
attributes:
|
||||
foo:
|
||||
groups: ['group1', 'group2']
|
||||
bar:
|
||||
groups: ['group2']
|
|
@ -1,67 +0,0 @@
|
|||
<?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\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AttributeMetadataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('name');
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface', $attributeMetadata);
|
||||
}
|
||||
|
||||
public function testGetName()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('name');
|
||||
$this->assertEquals('name', $attributeMetadata->getName());
|
||||
}
|
||||
|
||||
public function testGroups()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('group');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('b');
|
||||
|
||||
$this->assertEquals(array('a', 'b'), $attributeMetadata->getGroups());
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
$attributeMetadata1 = new AttributeMetadata('a1');
|
||||
$attributeMetadata1->addGroup('a');
|
||||
$attributeMetadata1->addGroup('b');
|
||||
|
||||
$attributeMetadata2 = new AttributeMetadata('a2');
|
||||
$attributeMetadata2->addGroup('a');
|
||||
$attributeMetadata2->addGroup('c');
|
||||
|
||||
$attributeMetadata1->merge($attributeMetadata2);
|
||||
|
||||
$this->assertEquals(array('a', 'b', 'c'), $attributeMetadata1->getGroups());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('attribute');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('b');
|
||||
|
||||
$serialized = serialize($attributeMetadata);
|
||||
$this->assertEquals($attributeMetadata, unserialize($serialized));
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
<?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\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('name');
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\ClassMetadataInterface', $classMetadata);
|
||||
}
|
||||
|
||||
public function testAttributeMetadata()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('c');
|
||||
|
||||
$a1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a1->method('getName')->willReturn('a1');
|
||||
|
||||
$a2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a2->method('getName')->willReturn('a2');
|
||||
|
||||
$classMetadata->addAttributeMetadata($a1);
|
||||
$classMetadata->addAttributeMetadata($a2);
|
||||
|
||||
$this->assertEquals(array('a1' => $a1, 'a2' => $a2), $classMetadata->getAttributesMetadata());
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
$classMetadata1 = new ClassMetadata('c1');
|
||||
$classMetadata2 = new ClassMetadata('c2');
|
||||
|
||||
$ac1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$ac1->method('getName')->willReturn('a1');
|
||||
$ac1->method('getGroups')->willReturn(array('a', 'b'));
|
||||
|
||||
$ac2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$ac2->method('getName')->willReturn('a1');
|
||||
$ac2->method('getGroups')->willReturn(array('b', 'c'));
|
||||
|
||||
$classMetadata1->addAttributeMetadata($ac1);
|
||||
$classMetadata2->addAttributeMetadata($ac2);
|
||||
|
||||
$classMetadata1->merge($classMetadata2);
|
||||
|
||||
$ac1->method('getGroups')->willReturn('a', 'b', 'c');
|
||||
|
||||
$this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('a');
|
||||
|
||||
$a1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a1->method('getName')->willReturn('b1');
|
||||
|
||||
$a2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a2->method('getName')->willReturn('b2');
|
||||
|
||||
$classMetadata->addAttributeMetadata($a1);
|
||||
$classMetadata->addAttributeMetadata($a2);
|
||||
|
||||
$serialized = serialize($classMetadata);
|
||||
$this->assertEquals($classMetadata, unserialize($serialized));
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
<?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\Mapping\Factory;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadataFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$classMetadata = new ClassMetadataFactory(new LoaderChain(array()));
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', $classMetadata);
|
||||
}
|
||||
|
||||
public function testGetMetadataFor()
|
||||
{
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$classMetadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $classMetadata);
|
||||
}
|
||||
|
||||
public function testHasMetadataFor()
|
||||
{
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent'));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface'));
|
||||
$this->assertFalse($factory->hasMetadataFor('Dunglas\Entity'));
|
||||
}
|
||||
|
||||
public function testCacheExists()
|
||||
{
|
||||
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
|
||||
$cache
|
||||
->expects($this->once())
|
||||
->method('fetch')
|
||||
->will($this->returnValue('foo'))
|
||||
;
|
||||
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
|
||||
$this->assertEquals('foo', $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
|
||||
}
|
||||
|
||||
public function testCacheNotExists()
|
||||
{
|
||||
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
|
||||
$cache
|
||||
->method('fetch')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$cache
|
||||
->method('save')
|
||||
;
|
||||
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
|
||||
$metadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $metadata);
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var AnnotationLoader
|
||||
*/
|
||||
private $loader;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new AnnotationLoader(new AnnotationReader());
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertTrue($this->loader->loadClassMetadata($classMetadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
$this->loader->loadClassMetadata($classMetadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(), $classMetadata);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataAndMerge()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
$parentClassMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent');
|
||||
|
||||
$this->loader->loadClassMetadata($parentClassMetadata);
|
||||
$classMetadata->merge($parentClassMetadata);
|
||||
|
||||
$this->loader->loadClassMetadata($classMetadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true), $classMetadata);
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var XmlFileLoader
|
||||
*/
|
||||
private $loader;
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
private $metadata;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new XmlFileLoader(__DIR__.'/../../Fixtures/serialization.xml');
|
||||
$this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$this->assertTrue($this->loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$this->loader->loadClassMetadata($this->metadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createXmlCLassMetadata(), $this->metadata);
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var YamlFileLoader
|
||||
*/
|
||||
private $loader;
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
private $metadata;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new YamlFileLoader(__DIR__.'/../../Fixtures/serialization.yml');
|
||||
$this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$this->assertTrue($this->loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsFalseWhenEmpty()
|
||||
{
|
||||
$loader = new YamlFileLoader(__DIR__.'/../../Fixtures/empty-mapping.yml');
|
||||
$this->assertFalse($loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\MappingException
|
||||
*/
|
||||
public function testLoadClassMetadataReturnsThrowsInvalidMapping()
|
||||
{
|
||||
$loader = new YamlFileLoader(__DIR__.'/../../Fixtures/invalid-mapping.yml');
|
||||
$loader->loadClassMetadata($this->metadata);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$this->loader->loadClassMetadata($this->metadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createXmlCLassMetadata(), $this->metadata);
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
<?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\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class TestClassMetadataFactory
|
||||
{
|
||||
public static function createClassMetadata($withParent = false, $withInterface = false)
|
||||
{
|
||||
$expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$foo = new AttributeMetadata('foo');
|
||||
$foo->addGroup('a');
|
||||
$expected->addAttributeMetadata($foo);
|
||||
|
||||
$bar = new AttributeMetadata('bar');
|
||||
$bar->addGroup('b');
|
||||
$bar->addGroup('c');
|
||||
$bar->addGroup('name_converter');
|
||||
$expected->addAttributeMetadata($bar);
|
||||
|
||||
$fooBar = new AttributeMetadata('fooBar');
|
||||
$fooBar->addGroup('a');
|
||||
$fooBar->addGroup('b');
|
||||
$fooBar->addGroup('name_converter');
|
||||
$expected->addAttributeMetadata($fooBar);
|
||||
|
||||
$symfony = new AttributeMetadata('symfony');
|
||||
$expected->addAttributeMetadata($symfony);
|
||||
|
||||
if ($withParent) {
|
||||
$kevin = new AttributeMetadata('kevin');
|
||||
$kevin->addGroup('a');
|
||||
$expected->addAttributeMetadata($kevin);
|
||||
|
||||
$coopTilleuls = new AttributeMetadata('coopTilleuls');
|
||||
$coopTilleuls->addGroup('a');
|
||||
$coopTilleuls->addGroup('b');
|
||||
$expected->addAttributeMetadata($coopTilleuls);
|
||||
}
|
||||
|
||||
if ($withInterface) {
|
||||
$symfony->addGroup('a');
|
||||
$symfony->addGroup('name_converter');
|
||||
}
|
||||
|
||||
// load reflection class so that the comparison passes
|
||||
$expected->getReflectionClass();
|
||||
|
||||
return $expected;
|
||||
}
|
||||
|
||||
public static function createXmlCLassMetadata()
|
||||
{
|
||||
$expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$foo = new AttributeMetadata('foo');
|
||||
$foo->addGroup('group1');
|
||||
$foo->addGroup('group2');
|
||||
$expected->addAttributeMetadata($foo);
|
||||
|
||||
$bar = new AttributeMetadata('bar');
|
||||
$bar->addGroup('group2');
|
||||
$expected->addAttributeMetadata($bar);
|
||||
|
||||
return $expected;
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
<?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\NameConverter;
|
||||
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CamelCaseToSnakeCaseNameConverterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$attributeMetadata = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\NameConverter\NameConverterInterface', $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeProvider
|
||||
*/
|
||||
public function testNormalize($underscored, $lowerCamelCased)
|
||||
{
|
||||
$nameConverter = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertEquals($nameConverter->normalize($lowerCamelCased), $underscored);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeProvider
|
||||
*/
|
||||
public function testDenormalize($underscored, $lowerCamelCased)
|
||||
{
|
||||
$nameConverter = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertEquals($nameConverter->denormalize($underscored), $lowerCamelCased);
|
||||
}
|
||||
|
||||
public function attributeProvider()
|
||||
{
|
||||
return array(
|
||||
array('coop_tilleuls', 'coopTilleuls'),
|
||||
array('_kevin_dunglas', '_kevinDunglas'),
|
||||
array('this_is_a_test', 'thisIsATest'),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
<?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\Tests\Fixtures\ScalarDummy;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var CustomNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->normalizer = new CustomNormalizer();
|
||||
$this->normalizer->setSerializer(new Serializer());
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->xmlFoo = 'xml';
|
||||
$this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
|
||||
$this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testDeserialize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'xml');
|
||||
$this->assertEquals('foo', $obj->xmlFoo);
|
||||
$this->assertNull($obj->foo);
|
||||
|
||||
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'json');
|
||||
$this->assertEquals('foo', $obj->foo);
|
||||
$this->assertNull($obj->xmlFoo);
|
||||
}
|
||||
|
||||
public function testSupportsNormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy()));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
public function testSupportsDenormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy'));
|
||||
$this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
|
||||
$this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy'));
|
||||
}
|
||||
}
|
|
@ -1,799 +0,0 @@
|
|||
<?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 Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
|
||||
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var GetSetMethodNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer');
|
||||
$this->normalizer = new GetSetMethodNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setBaz(true);
|
||||
$obj->setCamelCase('camelcase');
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->serializer
|
||||
->expects($this->once())
|
||||
->method('normalize')
|
||||
->with($object, 'any')
|
||||
->will($this->returnValue('string_object'))
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'foo' => 'foo',
|
||||
'bar' => 'bar',
|
||||
'baz' => true,
|
||||
'fooBar' => 'foobar',
|
||||
'camelCase' => 'camelcase',
|
||||
'object' => 'string_object',
|
||||
),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\GetSetDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testDenormalizeWithObject()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\GetSetDummy'
|
||||
);
|
||||
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\GetSetDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testDenormalizeNull()
|
||||
{
|
||||
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesNormalize()
|
||||
{
|
||||
$obj = new GetCamelizedDummy('dunglas.fr');
|
||||
$obj->setFooBar('les-tilleuls.coop');
|
||||
$obj->setBar_foo('lostinthesupermarket.fr');
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesDenormalize()
|
||||
{
|
||||
$obj = new GetCamelizedDummy('dunglas.fr');
|
||||
$obj->setFooBar('les-tilleuls.coop');
|
||||
$obj->setBar_foo('lostinthesupermarket.fr');
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\GetCamelizedDummy'), $obj);
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\GetCamelizedDummy'), $obj);
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => null, 'baz' => true),
|
||||
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertNull($obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithMissingOptionalArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'test', 'baz' => array(1, 2, 3)),
|
||||
__NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any');
|
||||
$this->assertEquals('test', $obj->getFoo());
|
||||
$this->assertEquals(array(), $obj->getBar());
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithOptionalDefaultArgument()
|
||||
{
|
||||
if (PHP_VERSION_ID <= 50316) {
|
||||
$this->markTestSkipped('See https://bugs.php.net/62715');
|
||||
}
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'test'),
|
||||
__NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
$this->assertEquals('test', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 5.6
|
||||
*/
|
||||
public function testConstructorDenormalizeWithVariadicArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => array(1, 2, 3)),
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 5.6
|
||||
*/
|
||||
public function testConstructorDenormalizeWithMissingVariadicArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array(),
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
}
|
||||
|
||||
public function testConstructorWithObjectDenormalize()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->baz = true;
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorWArgWithPrivateMutator()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
|
||||
$this->assertEquals('bar', $obj->getFoo());
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
'kevin' => 'kevin',
|
||||
'coopTilleuls' => 'coopTilleuls',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new GetConstructorDummy('', $value, true);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new GetConstructorDummy('baz', 'quux', true);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setBaz(true);
|
||||
|
||||
$this->assertEquals(
|
||||
array('fooBar' => 'foobar'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz', 'baz' => true),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null, 'baz' => true),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 2, 'baz' => true),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new SiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testObjectToPopulate()
|
||||
{
|
||||
$dummy = new GetSetDummy();
|
||||
$dummy->setFoo('foo');
|
||||
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'bar'),
|
||||
__NAMESPACE__.'\GetSetDummy',
|
||||
null,
|
||||
array('object_to_populate' => $dummy)
|
||||
);
|
||||
|
||||
$this->assertEquals($dummy, $obj);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new GetSetDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
|
||||
public function testPrivateSetter()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
|
||||
$this->assertEquals('bar', $obj->getFoo());
|
||||
}
|
||||
}
|
||||
|
||||
class GetSetDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function setBaz($baz)
|
||||
{
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->foo.$this->bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
|
||||
public function setObject($object)
|
||||
{
|
||||
$this->object = $object;
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
}
|
||||
|
||||
class GetConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar, $baz)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
|
||||
{
|
||||
}
|
||||
|
||||
class GetConstructorOptionalArgsDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar = array(), $baz = array())
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function getBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class GetConstructorArgsWithDefaultValueDummy
|
||||
{
|
||||
protected $foo;
|
||||
protected $bar;
|
||||
|
||||
public function __construct($foo = array(), $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class GetCamelizedDummy
|
||||
{
|
||||
private $kevinDunglas;
|
||||
private $fooBar;
|
||||
private $bar_foo;
|
||||
|
||||
public function __construct($kevinDunglas = null)
|
||||
{
|
||||
$this->kevinDunglas = $kevinDunglas;
|
||||
}
|
||||
|
||||
public function getKevinDunglas()
|
||||
{
|
||||
return $this->kevinDunglas;
|
||||
}
|
||||
|
||||
public function setFooBar($fooBar)
|
||||
{
|
||||
$this->fooBar = $fooBar;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->fooBar;
|
||||
}
|
||||
|
||||
public function setBar_foo($bar_foo)
|
||||
{
|
||||
$this->bar_foo = $bar_foo;
|
||||
}
|
||||
|
||||
public function getBar_foo()
|
||||
{
|
||||
return $this->bar_foo;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorArgsWithPrivateMutatorDummy
|
||||
{
|
||||
private $foo;
|
||||
|
||||
public function __construct($foo)
|
||||
{
|
||||
$this->setFoo($foo);
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
private function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectWithPrivateSetterDummy
|
||||
{
|
||||
private $foo = 'bar';
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
private function setFoo($foo)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,605 +0,0 @@
|
|||
<?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 Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectNormalizerTest
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock(__NAMESPACE__.'\ObjectSerializerNormalizer');
|
||||
$this->normalizer = new ObjectNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new ObjectDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setFoo('foo');
|
||||
$obj->bar = 'bar';
|
||||
$obj->setBaz(true);
|
||||
$obj->setCamelCase('camelcase');
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->serializer
|
||||
->expects($this->once())
|
||||
->method('normalize')
|
||||
->with($object, 'any')
|
||||
->will($this->returnValue('string_object'))
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'foo' => 'foo',
|
||||
'bar' => 'bar',
|
||||
'baz' => true,
|
||||
'fooBar' => 'foobar',
|
||||
'camelCase' => 'camelcase',
|
||||
'object' => 'string_object',
|
||||
),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\ObjectDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testDenormalizeWithObject()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\ObjectDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\ObjectDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testDenormalizeNull()
|
||||
{
|
||||
$this->assertEquals(new ObjectDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\ObjectDummy'));
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => null, 'baz' => true),
|
||||
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertNull($obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithMissingOptionalArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'test', 'baz' => array(1, 2, 3)),
|
||||
__NAMESPACE__.'\ObjectConstructorOptionalArgsDummy', 'any');
|
||||
$this->assertEquals('test', $obj->getFoo());
|
||||
$this->assertEquals(array(), $obj->bar);
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithOptionalDefaultArgument()
|
||||
{
|
||||
if (PHP_VERSION_ID <= 50316) {
|
||||
$this->markTestSkipped('See https://bugs.php.net/62715');
|
||||
}
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'test'),
|
||||
__NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
$this->assertEquals('test', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorWithObjectDenormalize()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->baz = true;
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
'kevin' => 'kevin',
|
||||
'coopTilleuls' => 'coopTilleuls',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new ObjectConstructorDummy('', $value, true);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new ObjectConstructorDummy('baz', 'quux', true);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
|
||||
|
||||
$obj = new ObjectDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->bar = 'bar';
|
||||
$obj->setBaz(true);
|
||||
|
||||
$this->assertEquals(
|
||||
array('fooBar' => 'foobar'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz', 'baz' => true),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null, 'baz' => true),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 2, 'baz' => true),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new ObjectDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new SiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new ObjectDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\ObjectDummy')
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function setBaz($baz)
|
||||
{
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->foo.$this->bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
|
||||
public function setObject($object)
|
||||
{
|
||||
$this->object = $object;
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar, $baz)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ObjectSerializerNormalizer implements SerializerInterface, NormalizerInterface
|
||||
{
|
||||
}
|
||||
|
||||
class ObjectConstructorOptionalArgsDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar = array(), $baz = array())
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorArgsWithDefaultValueDummy
|
||||
{
|
||||
protected $foo;
|
||||
protected $bar;
|
||||
|
||||
public function __construct($foo = array(), $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
|
@ -1,493 +0,0 @@
|
|||
<?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 Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
||||
|
||||
class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var PropertyNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer = new PropertyNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new PropertyDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->setBar('bar');
|
||||
$obj->setCamelCase('camelcase');
|
||||
$this->assertEquals(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'camelCase' => 'camelcase'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->foo);
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'value'),
|
||||
__NAMESPACE__.'\PropertyDummy'
|
||||
);
|
||||
$this->assertEquals('value', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesNormalize()
|
||||
{
|
||||
$obj = new PropertyCamelizedDummy('dunglas.fr');
|
||||
$obj->fooBar = 'les-tilleuls.coop';
|
||||
$obj->bar_foo = 'lostinthesupermarket.fr';
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesDenormalize()
|
||||
{
|
||||
$obj = new PropertyCamelizedDummy('dunglas.fr');
|
||||
$obj->fooBar = 'les-tilleuls.coop';
|
||||
$obj->bar_foo = 'lostinthesupermarket.fr';
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\PropertyCamelizedDummy'), $obj);
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\PropertyCamelizedDummy'), $obj);
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new PropertyNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\PropertyDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyConstructorDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => null, 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyConstructorDummy', '
|
||||
any'
|
||||
);
|
||||
$this->assertNull($obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new PropertyConstructorDummy('', $value);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new PropertyConstructorDummy('baz', 'quux');
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
|
||||
|
||||
$obj = new PropertyDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->setBar('bar');
|
||||
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
// The PropertyNormalizer is not able to hydrate properties from parent classes
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz'),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new PropertyConstructorDummy('baz', ''), new PropertyConstructorDummy('quux', '')),
|
||||
array('foo' => '', 'bar' => 'bazquux'),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new PropertyConstructorDummy('baz', ''), new PropertyConstructorDummy('quux', '')),
|
||||
array('foo' => '', 'bar' => 2),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new PropertyCircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new PropertySiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new PropertyCircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new PropertyDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new PropertyDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setBar($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyDummy
|
||||
{
|
||||
public $foo;
|
||||
private $bar;
|
||||
protected $camelCase;
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
|
||||
public function __construct($foo, $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyCamelizedDummy
|
||||
{
|
||||
private $kevinDunglas;
|
||||
public $fooBar;
|
||||
public $bar_foo;
|
||||
|
||||
public function __construct($kevinDunglas = null)
|
||||
{
|
||||
$this->kevinDunglas = $kevinDunglas;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
267
vendor/symfony/serializer/Tests/SerializerTest.php
vendored
267
vendor/symfony/serializer/Tests/SerializerTest.php
vendored
|
@ -1,267 +0,0 @@
|
|||
<?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;
|
||||
|
||||
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\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
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$serializer = new Serializer();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\SerializerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\EncoderInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testNormalizeNoMatch()
|
||||
{
|
||||
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
||||
$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 \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
|
||||
*/
|
||||
public function testDenormalizeNoMatch()
|
||||
{
|
||||
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
||||
$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 testCustomNormalizerCanNormalizeCollectionsAndScalar()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new TestNormalizer()), array());
|
||||
$this->assertNull($this->serializer->normalize(array('a', 'b')));
|
||||
$this->assertNull($this->serializer->normalize(new \ArrayObject(array('c', 'd'))));
|
||||
$this->assertNull($this->serializer->normalize(array()));
|
||||
$this->assertNull($this->serializer->normalize('test'));
|
||||
}
|
||||
|
||||
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()));
|
||||
$result = $this->serializer->serialize('foo', 'json');
|
||||
$this->assertEquals('"foo"', $result);
|
||||
}
|
||||
|
||||
public function testSerializeArrayOfScalars()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->serialize($data, 'json');
|
||||
$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\Component\Serializer\Tests\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\Component\Serializer\Tests\Model', 'json');
|
||||
$data = array('title' => 'bar', 'numbers' => array(2, 8));
|
||||
$result = $this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\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\Component\Serializer\Tests\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\Component\Serializer\Tests\Model', 'json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDeserializeNoEncoder()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
}
|
||||
|
||||
public function testDeserializeSupported()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertTrue($this->serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
|
||||
}
|
||||
|
||||
public function testDeserializeNotSupported()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertFalse($this->serializer->supportsDenormalization(json_encode($data), 'stdClass', 'json'));
|
||||
}
|
||||
|
||||
public function testDeserializeNotSupportedMissing()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertFalse($this->serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->encode($data, 'json');
|
||||
$this->assertEquals(json_encode($data), $result);
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->decode(json_encode($data), 'json');
|
||||
$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);
|
||||
}
|
||||
}
|
Reference in a new issue