2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\Serializer\Encoder;
|
|
|
|
|
|
|
|
require_once __DIR__.'/../Fixtures/Dummy.php';
|
|
|
|
require_once __DIR__.'/../Fixtures/ScalarDummy.php';
|
|
|
|
|
|
|
|
use Symfony\Tests\Component\Serializer\Fixtures\Dummy;
|
|
|
|
use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
|
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->encoder = new XmlEncoder;
|
2011-12-11 19:40:52 +00:00
|
|
|
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
|
2011-06-10 00:11:51 +01:00
|
|
|
$this->encoder->setSerializer($serializer);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncodeScalar()
|
|
|
|
{
|
|
|
|
$obj = new ScalarDummy;
|
|
|
|
$obj->xmlFoo = "foo";
|
|
|
|
|
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
2012-01-22 09:20:46 +00:00
|
|
|
'<response>foo</response>'."\n";
|
2011-01-18 19:17:24 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
|
|
|
}
|
|
|
|
|
2011-02-13 14:58:52 +00:00
|
|
|
public function testSetRootNodeName()
|
|
|
|
{
|
|
|
|
$obj = new ScalarDummy;
|
|
|
|
$obj->xmlFoo = "foo";
|
|
|
|
|
|
|
|
$this->encoder->setRootNodeName('test');
|
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
2012-01-22 09:20:46 +00:00
|
|
|
'<test>foo</test>'."\n";
|
2011-02-13 14:58:52 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
|
|
|
}
|
|
|
|
|
2011-02-14 09:42:58 +00:00
|
|
|
public function testAttributes()
|
|
|
|
{
|
|
|
|
$obj = new ScalarDummy;
|
|
|
|
$obj->xmlFoo = array(
|
|
|
|
'foo-bar' => array(
|
|
|
|
'@id' => 1,
|
|
|
|
'@name' => 'Bar'
|
|
|
|
),
|
|
|
|
'Foo' => array(
|
|
|
|
'Bar' => "Test",
|
|
|
|
'@Type' => 'test'
|
|
|
|
),
|
2011-03-23 19:27:58 +00:00
|
|
|
'föo_bär' => 'a',
|
2011-03-12 14:23:30 +00:00
|
|
|
"Bar" => array(1,2,3),
|
|
|
|
'a' => 'b',
|
2011-02-14 09:42:58 +00:00
|
|
|
);
|
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
|
|
|
'<response>'.
|
|
|
|
'<foo-bar id="1" name="Bar"/>'.
|
2012-01-22 09:20:46 +00:00
|
|
|
'<Foo Type="test"><Bar>Test</Bar></Foo>'.
|
|
|
|
'<föo_bär>a</föo_bär>'.
|
2011-02-14 09:42:58 +00:00
|
|
|
'<Bar>1</Bar>'.
|
|
|
|
'<Bar>2</Bar>'.
|
|
|
|
'<Bar>3</Bar>'.
|
2012-01-22 09:20:46 +00:00
|
|
|
'<a>b</a>'.
|
2011-02-14 09:42:58 +00:00
|
|
|
'</response>'."\n";
|
|
|
|
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
|
|
|
}
|
|
|
|
|
2011-02-13 15:14:43 +00:00
|
|
|
public function testElementNameValid()
|
|
|
|
{
|
|
|
|
$obj = new ScalarDummy;
|
|
|
|
$obj->xmlFoo = array(
|
2011-03-23 19:27:58 +00:00
|
|
|
'foo-bar' => 'a',
|
|
|
|
'foo_bar' => 'a',
|
|
|
|
'föo_bär' => 'a',
|
2011-02-13 15:14:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
|
|
|
'<response>'.
|
2012-01-22 09:20:46 +00:00
|
|
|
'<foo-bar>a</foo-bar>'.
|
|
|
|
'<foo_bar>a</foo_bar>'.
|
|
|
|
'<föo_bär>a</föo_bär>'.
|
2011-02-13 15:14:43 +00:00
|
|
|
'</response>'."\n";
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
|
|
|
}
|
|
|
|
|
2011-02-12 12:02:04 +00:00
|
|
|
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'));
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
public function testEncodeScalarRootAttributes()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'#' => 'Paul',
|
2011-06-08 18:56:59 +01:00
|
|
|
'@gender' => 'm'
|
2011-04-27 21:16:24 +01:00
|
|
|
);
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
2012-01-22 09:20:46 +00:00
|
|
|
'<response gender="m">Paul</response>'."\n";
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
public function testEncodeRootAttributes()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'firstname' => 'Paul',
|
2011-06-08 18:56:59 +01:00
|
|
|
'@gender' => 'm'
|
2011-04-27 21:16:24 +01:00
|
|
|
);
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
2012-01-22 09:20:46 +00:00
|
|
|
'<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";
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
public function testEncodeScalarWithAttribute()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
|
|
|
);
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$expected = '<?xml version="1.0"?>'."\n".
|
2012-01-22 09:20:46 +00:00
|
|
|
'<response><person gender="M">Peter</person></response>'."\n";
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
|
|
|
}
|
2011-02-12 12:02:04 +00:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
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 testDecode()
|
|
|
|
{
|
|
|
|
$source = $this->getXmlSource();
|
|
|
|
$obj = $this->getObject();
|
|
|
|
|
|
|
|
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
public function testDecodeScalarWithAttribute()
|
|
|
|
{
|
|
|
|
$source = '<?xml version="1.0"?>'."\n".
|
|
|
|
'<response><person gender="M">Peter</person></response>'."\n";
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$expected = array(
|
|
|
|
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
|
|
|
);
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
public function testDecodeScalarRootAttributes()
|
|
|
|
{
|
|
|
|
$source = '<?xml version="1.0"?>'."\n".
|
|
|
|
'<person gender="M">Peter</person>'."\n";
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$expected = array(
|
|
|
|
'#' => 'Peter',
|
|
|
|
'@gender' => 'M'
|
|
|
|
);
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
public function testDecodeRootAttributes()
|
|
|
|
{
|
|
|
|
$source = '<?xml version="1.0"?>'."\n".
|
|
|
|
'<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$expected = array(
|
|
|
|
'firstname' => 'Peter',
|
|
|
|
'lastname' => 'Mac Calloway',
|
|
|
|
'@gender' => 'M'
|
|
|
|
);
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-04-27 21:16:24 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
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";
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$expected = array(
|
|
|
|
'people' => array('person' => array(
|
|
|
|
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
|
|
|
|
array('firstname' => 'Damien', 'lastname' => 'Clay')
|
|
|
|
))
|
|
|
|
);
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-11 23:58:21 +01:00
|
|
|
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
|
|
|
|
protected function getXmlSource()
|
|
|
|
{
|
|
|
|
return '<?xml version="1.0"?>'."\n".
|
|
|
|
'<response>'.
|
2012-01-22 09:20:46 +00:00
|
|
|
'<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>'.
|
2011-01-18 19:17:24 +00:00
|
|
|
'<qux>1</qux>'.
|
|
|
|
'</response>'."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getObject()
|
|
|
|
{
|
|
|
|
$obj = new Dummy;
|
|
|
|
$obj->foo = 'foo';
|
|
|
|
$obj->bar = array('a', 'b');
|
2011-11-24 17:09:38 +00:00
|
|
|
$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)));
|
2011-01-18 19:17:24 +00:00
|
|
|
$obj->qux = "1";
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
}
|