XmlEncoderTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Encoder;
  11. use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
  12. use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
  13. use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
  14. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  15. use Symfony\Component\Serializer\Serializer;
  16. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  17. use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
  18. class XmlEncoderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. private $encoder;
  21. protected 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>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>foo</test>'."\n";
  42. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  43. }
  44. /**
  45. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  46. * @expectedExceptionMessage Document types are not allowed.
  47. */
  48. public function testDocTypeIsNotAllowed()
  49. {
  50. $this->encoder->decode('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'foo');
  51. }
  52. public function testAttributes()
  53. {
  54. $obj = new ScalarDummy();
  55. $obj->xmlFoo = array(
  56. 'foo-bar' => array(
  57. '@id' => 1,
  58. '@name' => 'Bar',
  59. ),
  60. 'Foo' => array(
  61. 'Bar' => 'Test',
  62. '@Type' => 'test',
  63. ),
  64. 'föo_bär' => 'a',
  65. 'Bar' => array(1, 2, 3),
  66. 'a' => 'b',
  67. );
  68. $expected = '<?xml version="1.0"?>'."\n".
  69. '<response>'.
  70. '<foo-bar id="1" name="Bar"/>'.
  71. '<Foo Type="test"><Bar>Test</Bar></Foo>'.
  72. '<föo_bär>a</föo_bär>'.
  73. '<Bar>1</Bar>'.
  74. '<Bar>2</Bar>'.
  75. '<Bar>3</Bar>'.
  76. '<a>b</a>'.
  77. '</response>'."\n";
  78. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  79. }
  80. public function testElementNameValid()
  81. {
  82. $obj = new ScalarDummy();
  83. $obj->xmlFoo = array(
  84. 'foo-bar' => 'a',
  85. 'foo_bar' => 'a',
  86. 'föo_bär' => 'a',
  87. );
  88. $expected = '<?xml version="1.0"?>'."\n".
  89. '<response>'.
  90. '<foo-bar>a</foo-bar>'.
  91. '<foo_bar>a</foo_bar>'.
  92. '<föo_bär>a</föo_bär>'.
  93. '</response>'."\n";
  94. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  95. }
  96. public function testEncodeSimpleXML()
  97. {
  98. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  99. $array = array('person' => $xml);
  100. $expected = '<?xml version="1.0"?>'."\n".
  101. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  102. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  103. }
  104. public function testEncodeXmlAttributes()
  105. {
  106. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  107. $array = array('person' => $xml);
  108. $expected = '<?xml version="1.1" encoding="utf-8" standalone="yes"?>'."\n".
  109. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  110. $context = array(
  111. 'xml_version' => '1.1',
  112. 'xml_encoding' => 'utf-8',
  113. 'xml_standalone' => true,
  114. );
  115. $this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
  116. }
  117. public function testContext()
  118. {
  119. $array = array('person' => array('name' => 'George Abitbol'));
  120. $expected = <<<'XML'
  121. <?xml version="1.0"?>
  122. <response>
  123. <person>
  124. <name>George Abitbol</name>
  125. </person>
  126. </response>
  127. XML;
  128. $context = array(
  129. 'xml_format_output' => true,
  130. );
  131. $this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
  132. }
  133. public function testEncodeScalarRootAttributes()
  134. {
  135. $array = array(
  136. '#' => 'Paul',
  137. '@gender' => 'm',
  138. );
  139. $expected = '<?xml version="1.0"?>'."\n".
  140. '<response gender="m">Paul</response>'."\n";
  141. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  142. }
  143. public function testEncodeRootAttributes()
  144. {
  145. $array = array(
  146. 'firstname' => 'Paul',
  147. '@gender' => 'm',
  148. );
  149. $expected = '<?xml version="1.0"?>'."\n".
  150. '<response gender="m"><firstname>Paul</firstname></response>'."\n";
  151. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  152. }
  153. public function testEncodeCdataWrapping()
  154. {
  155. $array = array(
  156. 'firstname' => 'Paul <or Me>',
  157. );
  158. $expected = '<?xml version="1.0"?>'."\n".
  159. '<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
  160. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  161. }
  162. public function testEncodeScalarWithAttribute()
  163. {
  164. $array = array(
  165. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  166. );
  167. $expected = '<?xml version="1.0"?>'."\n".
  168. '<response><person gender="M">Peter</person></response>'."\n";
  169. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  170. }
  171. public function testDecodeScalar()
  172. {
  173. $source = '<?xml version="1.0"?>'."\n".
  174. '<response>foo</response>'."\n";
  175. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  176. }
  177. public function testEncode()
  178. {
  179. $source = $this->getXmlSource();
  180. $obj = $this->getObject();
  181. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  182. }
  183. public function testEncodeWithNamespace()
  184. {
  185. $source = $this->getNamespacedXmlSource();
  186. $array = $this->getNamespacedArray();
  187. $this->assertEquals($source, $this->encoder->encode($array, 'xml'));
  188. }
  189. public function testEncodeSerializerXmlRootNodeNameOption()
  190. {
  191. $options = array('xml_root_node_name' => 'test');
  192. $this->encoder = new XmlEncoder();
  193. $serializer = new Serializer(array(), array('xml' => new XmlEncoder()));
  194. $this->encoder->setSerializer($serializer);
  195. $array = array(
  196. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  197. );
  198. $expected = '<?xml version="1.0"?>'."\n".
  199. '<test><person gender="M">Peter</person></test>'."\n";
  200. $this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
  201. }
  202. public function testEncodeTraversableWhenNormalizable()
  203. {
  204. $this->encoder = new XmlEncoder();
  205. $serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
  206. $this->encoder->setSerializer($serializer);
  207. $expected = <<<'XML'
  208. <?xml version="1.0"?>
  209. <response><foo>normalizedFoo</foo><bar>normalizedBar</bar></response>
  210. XML;
  211. $this->assertEquals($expected, $serializer->serialize(new NormalizableTraversableDummy(), 'xml'));
  212. }
  213. public function testDecode()
  214. {
  215. $source = $this->getXmlSource();
  216. $obj = $this->getObject();
  217. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  218. }
  219. public function testDecodeCdataWrapping()
  220. {
  221. $expected = array(
  222. 'firstname' => 'Paul <or Me>',
  223. );
  224. $xml = '<?xml version="1.0"?>'."\n".
  225. '<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
  226. $this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
  227. }
  228. public function testDecodeCdataWrappingAndWhitespace()
  229. {
  230. $expected = array(
  231. 'firstname' => 'Paul <or Me>',
  232. );
  233. $xml = '<?xml version="1.0"?>'."\n".
  234. '<response><firstname>'."\n".
  235. '<![CDATA[Paul <or Me>]]></firstname></response>'."\n";
  236. $this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
  237. }
  238. public function testDecodeWithNamespace()
  239. {
  240. $source = $this->getNamespacedXmlSource();
  241. $array = $this->getNamespacedArray();
  242. $this->assertEquals($array, $this->encoder->decode($source, 'xml'));
  243. }
  244. public function testDecodeScalarWithAttribute()
  245. {
  246. $source = '<?xml version="1.0"?>'."\n".
  247. '<response><person gender="M">Peter</person></response>'."\n";
  248. $expected = array(
  249. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  250. );
  251. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  252. }
  253. public function testDecodeScalarRootAttributes()
  254. {
  255. $source = '<?xml version="1.0"?>'."\n".
  256. '<person gender="M">Peter</person>'."\n";
  257. $expected = array(
  258. '#' => 'Peter',
  259. '@gender' => 'M',
  260. );
  261. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  262. }
  263. public function testDecodeRootAttributes()
  264. {
  265. $source = '<?xml version="1.0"?>'."\n".
  266. '<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
  267. $expected = array(
  268. 'firstname' => 'Peter',
  269. 'lastname' => 'Mac Calloway',
  270. '@gender' => 'M',
  271. );
  272. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  273. }
  274. public function testDecodeArray()
  275. {
  276. $source = '<?xml version="1.0"?>'."\n".
  277. '<response>'.
  278. '<people>'.
  279. '<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
  280. '<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
  281. '</people>'.
  282. '</response>'."\n";
  283. $expected = array(
  284. 'people' => array('person' => array(
  285. array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
  286. array('firstname' => 'Damien', 'lastname' => 'Clay'),
  287. )),
  288. );
  289. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  290. }
  291. public function testDecodeIgnoreWhiteSpace()
  292. {
  293. $source = <<<'XML'
  294. <?xml version="1.0"?>
  295. <people>
  296. <person>
  297. <firstname>Benjamin</firstname>
  298. <lastname>Alexandre</lastname>
  299. </person>
  300. <person>
  301. <firstname>Damien</firstname>
  302. <lastname>Clay</lastname>
  303. </person>
  304. </people>
  305. XML;
  306. $expected = array('person' => array(
  307. array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
  308. array('firstname' => 'Damien', 'lastname' => 'Clay'),
  309. ));
  310. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  311. }
  312. public function testDecodeWithoutItemHash()
  313. {
  314. $obj = new ScalarDummy();
  315. $obj->xmlFoo = array(
  316. 'foo-bar' => array(
  317. '@key' => 'value',
  318. 'item' => array('@key' => 'key', 'key-val' => 'val'),
  319. ),
  320. 'Foo' => array(
  321. 'Bar' => 'Test',
  322. '@Type' => 'test',
  323. ),
  324. 'föo_bär' => 'a',
  325. 'Bar' => array(1, 2, 3),
  326. 'a' => 'b',
  327. );
  328. $expected = array(
  329. 'foo-bar' => array(
  330. '@key' => 'value',
  331. 'key' => array('@key' => 'key', 'key-val' => 'val'),
  332. ),
  333. 'Foo' => array(
  334. 'Bar' => 'Test',
  335. '@Type' => 'test',
  336. ),
  337. 'föo_bär' => 'a',
  338. 'Bar' => array(1, 2, 3),
  339. 'a' => 'b',
  340. );
  341. $xml = $this->encoder->encode($obj, 'xml');
  342. $this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
  343. }
  344. /**
  345. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  346. */
  347. public function testDecodeInvalidXml()
  348. {
  349. $this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
  350. }
  351. public function testPreventsComplexExternalEntities()
  352. {
  353. $oldCwd = getcwd();
  354. chdir(__DIR__);
  355. try {
  356. $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');
  357. chdir($oldCwd);
  358. $this->fail('No exception was thrown.');
  359. } catch (\Exception $e) {
  360. chdir($oldCwd);
  361. if (!$e instanceof UnexpectedValueException) {
  362. $this->fail('Expected UnexpectedValueException');
  363. }
  364. }
  365. }
  366. public function testDecodeEmptyXml()
  367. {
  368. $this->setExpectedException('Symfony\Component\Serializer\Exception\UnexpectedValueException', 'Invalid XML data, it can not be empty.');
  369. $this->encoder->decode(' ', 'xml');
  370. }
  371. protected function getXmlSource()
  372. {
  373. return '<?xml version="1.0"?>'."\n".
  374. '<response>'.
  375. '<foo>foo</foo>'.
  376. '<bar>a</bar><bar>b</bar>'.
  377. '<baz><key>val</key><key2>val</key2><item key="A B">bar</item>'.
  378. '<item><title>title1</title></item><item><title>title2</title></item>'.
  379. '<Barry><FooBar id="1"><Baz>Ed</Baz></FooBar></Barry></baz>'.
  380. '<qux>1</qux>'.
  381. '</response>'."\n";
  382. }
  383. protected function getNamespacedXmlSource()
  384. {
  385. return '<?xml version="1.0"?>'."\n".
  386. '<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">'.
  387. '<qux>1</qux>'.
  388. '<app:foo>foo</app:foo>'.
  389. '<yt:bar>a</yt:bar><yt:bar>b</yt:bar>'.
  390. '<media:baz><media:key>val</media:key><media:key2>val</media:key2><item key="A B">bar</item>'.
  391. '<item><title>title1</title></item><item><title>title2</title></item>'.
  392. '<Barry size="large"><FooBar gd:id="1"><Baz>Ed</Baz></FooBar></Barry></media:baz>'.
  393. '</response>'."\n";
  394. }
  395. protected function getNamespacedArray()
  396. {
  397. return array(
  398. '@xmlns' => 'http://www.w3.org/2005/Atom',
  399. '@xmlns:app' => 'http://www.w3.org/2007/app',
  400. '@xmlns:media' => 'http://search.yahoo.com/mrss/',
  401. '@xmlns:gd' => 'http://schemas.google.com/g/2005',
  402. '@xmlns:yt' => 'http://gdata.youtube.com/schemas/2007',
  403. 'qux' => '1',
  404. 'app:foo' => 'foo',
  405. 'yt:bar' => array('a', 'b'),
  406. 'media:baz' => array(
  407. 'media:key' => 'val',
  408. 'media:key2' => 'val',
  409. 'A B' => 'bar',
  410. 'item' => array(
  411. array(
  412. 'title' => 'title1',
  413. ),
  414. array(
  415. 'title' => 'title2',
  416. ),
  417. ),
  418. 'Barry' => array(
  419. '@size' => 'large',
  420. 'FooBar' => array(
  421. 'Baz' => 'Ed',
  422. '@gd:id' => 1,
  423. ),
  424. ),
  425. ),
  426. );
  427. }
  428. protected function getObject()
  429. {
  430. $obj = new Dummy();
  431. $obj->foo = 'foo';
  432. $obj->bar = array('a', 'b');
  433. $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)));
  434. $obj->qux = '1';
  435. return $obj;
  436. }
  437. }