XmlEncoderTest.php 8.3 KB

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