/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php

https://github.com/Faianca/symfony · PHP · 257 lines · 201 code · 48 blank · 8 comment · 0 complexity · b5c219b93425084e3f5422efc140f288 MD5 · raw file

  1. <?php
  2. namespace Symfony\Tests\Component\Serializer\Encoder;
  3. require_once __DIR__.'/../Fixtures/Dummy.php';
  4. require_once __DIR__.'/../Fixtures/ScalarDummy.php';
  5. use Symfony\Tests\Component\Serializer\Fixtures\Dummy;
  6. use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
  7. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  8. use Symfony\Component\Serializer\Serializer;
  9. use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
  10. /*
  11. * This file is part of the Symfony framework.
  12. *
  13. * (c) Fabien Potencier <fabien@symfony.com>
  14. *
  15. * This source file is subject to the MIT license that is bundled
  16. * with this source code in the file LICENSE.
  17. */
  18. class XmlEncoderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function setUp()
  21. {
  22. $this->encoder = new XmlEncoder;
  23. $serializer = new Serializer(array(new CustomNormalizer), array('xml' => new XmlEncoder()));
  24. $this->encoder->setSerializer($serializer);
  25. }
  26. public function testEncodeScalar()
  27. {
  28. $obj = new ScalarDummy;
  29. $obj->xmlFoo = "foo";
  30. $expected = '<?xml version="1.0"?>'."\n".
  31. '<response><![CDATA[foo]]></response>'."\n";
  32. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  33. }
  34. public function testSetRootNodeName()
  35. {
  36. $obj = new ScalarDummy;
  37. $obj->xmlFoo = "foo";
  38. $this->encoder->setRootNodeName('test');
  39. $expected = '<?xml version="1.0"?>'."\n".
  40. '<test><![CDATA[foo]]></test>'."\n";
  41. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  42. }
  43. public function testAttributes()
  44. {
  45. $obj = new ScalarDummy;
  46. $obj->xmlFoo = array(
  47. 'foo-bar' => array(
  48. '@id' => 1,
  49. '@name' => 'Bar'
  50. ),
  51. 'Foo' => array(
  52. 'Bar' => "Test",
  53. '@Type' => 'test'
  54. ),
  55. 'föo_bär' => 'a',
  56. "Bar" => array(1,2,3),
  57. 'a' => 'b',
  58. );
  59. $expected = '<?xml version="1.0"?>'."\n".
  60. '<response>'.
  61. '<foo-bar id="1" name="Bar"/>'.
  62. '<Foo Type="test"><Bar><![CDATA[Test]]></Bar></Foo>'.
  63. '<föo_bär><![CDATA[a]]></föo_bär>'.
  64. '<Bar>1</Bar>'.
  65. '<Bar>2</Bar>'.
  66. '<Bar>3</Bar>'.
  67. '<a><![CDATA[b]]></a>'.
  68. '</response>'."\n";
  69. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  70. }
  71. public function testElementNameValid()
  72. {
  73. $obj = new ScalarDummy;
  74. $obj->xmlFoo = array(
  75. 'foo-bar' => 'a',
  76. 'foo_bar' => 'a',
  77. 'föo_bär' => 'a',
  78. );
  79. $expected = '<?xml version="1.0"?>'."\n".
  80. '<response>'.
  81. '<foo-bar><![CDATA[a]]></foo-bar>'.
  82. '<foo_bar><![CDATA[a]]></foo_bar>'.
  83. '<föo_bär><![CDATA[a]]></föo_bär>'.
  84. '</response>'."\n";
  85. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  86. }
  87. public function testEncodeSimpleXML()
  88. {
  89. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  90. $array = array('person' => $xml);
  91. $expected = '<?xml version="1.0"?>'."\n".
  92. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  93. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  94. }
  95. public function testEncodeScalarRootAttributes()
  96. {
  97. $array = array(
  98. '#' => 'Paul',
  99. '@gender' => 'm'
  100. );
  101. $expected = '<?xml version="1.0"?>'."\n".
  102. '<response gender="m"><![CDATA[Paul]]></response>'."\n";
  103. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  104. }
  105. public function testEncodeRootAttributes()
  106. {
  107. $array = array(
  108. 'firstname' => 'Paul',
  109. '@gender' => 'm'
  110. );
  111. $expected = '<?xml version="1.0"?>'."\n".
  112. '<response gender="m"><firstname><![CDATA[Paul]]></firstname></response>'."\n";
  113. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  114. }
  115. public function testEncodeScalarWithAttribute()
  116. {
  117. $array = array(
  118. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  119. );
  120. $expected = '<?xml version="1.0"?>'."\n".
  121. '<response><person gender="M"><![CDATA[Peter]]></person></response>'."\n";
  122. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  123. }
  124. public function testDecodeScalar()
  125. {
  126. $source = '<?xml version="1.0"?>'."\n".
  127. '<response>foo</response>'."\n";
  128. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  129. }
  130. public function testEncode()
  131. {
  132. $source = $this->getXmlSource();
  133. $obj = $this->getObject();
  134. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  135. }
  136. public function testDecode()
  137. {
  138. $source = $this->getXmlSource();
  139. $obj = $this->getObject();
  140. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  141. }
  142. public function testDecodeScalarWithAttribute()
  143. {
  144. $source = '<?xml version="1.0"?>'."\n".
  145. '<response><person gender="M">Peter</person></response>'."\n";
  146. $expected = array(
  147. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  148. );
  149. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  150. }
  151. public function testDecodeScalarRootAttributes()
  152. {
  153. $source = '<?xml version="1.0"?>'."\n".
  154. '<person gender="M">Peter</person>'."\n";
  155. $expected = array(
  156. '#' => 'Peter',
  157. '@gender' => 'M'
  158. );
  159. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  160. }
  161. public function testDecodeRootAttributes()
  162. {
  163. $source = '<?xml version="1.0"?>'."\n".
  164. '<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
  165. $expected = array(
  166. 'firstname' => 'Peter',
  167. 'lastname' => 'Mac Calloway',
  168. '@gender' => 'M'
  169. );
  170. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  171. }
  172. public function testDecodeArray()
  173. {
  174. $source = '<?xml version="1.0"?>'."\n".
  175. '<response>'.
  176. '<people>'.
  177. '<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
  178. '<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
  179. '</people>'.
  180. '</response>'."\n";
  181. $expected = array(
  182. 'people' => array('person' => array(
  183. array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
  184. array('firstname' => 'Damien', 'lastname' => 'Clay')
  185. ))
  186. );
  187. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  188. }
  189. protected function getXmlSource()
  190. {
  191. return '<?xml version="1.0"?>'."\n".
  192. '<response>'.
  193. '<foo><![CDATA[foo]]></foo>'.
  194. '<bar><![CDATA[a]]></bar><bar><![CDATA[b]]></bar>'.
  195. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item>'.
  196. '<Barry><FooBar id="1"><Baz><![CDATA[Ed]]></Baz></FooBar></Barry></baz>'.
  197. '<qux>1</qux>'.
  198. '</response>'."\n";
  199. }
  200. protected function getObject()
  201. {
  202. $obj = new Dummy;
  203. $obj->foo = 'foo';
  204. $obj->bar = array('a', 'b');
  205. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("Baz"=>"Ed", "@id"=>1)));
  206. $obj->qux = "1";
  207. return $obj;
  208. }
  209. }