XmlEncoderTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $serializer = new Serializer;
  23. $this->encoder = new XmlEncoder;
  24. $serializer->setEncoder('xml', $this->encoder);
  25. $serializer->addNormalizer(new CustomNormalizer);
  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 testEncodeScalarWithAttribute()
  97. {
  98. $array = array(
  99. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  100. );
  101. $expected = '<?xml version="1.0"?>'."\n".
  102. '<response><person gender="M"><![CDATA[Peter]]></person></response>'."\n";
  103. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  104. }
  105. public function testDecodeScalar()
  106. {
  107. $source = '<?xml version="1.0"?>'."\n".
  108. '<response>foo</response>'."\n";
  109. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  110. }
  111. public function testEncode()
  112. {
  113. $source = $this->getXmlSource();
  114. $obj = $this->getObject();
  115. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  116. }
  117. public function testDecode()
  118. {
  119. $source = $this->getXmlSource();
  120. $obj = $this->getObject();
  121. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  122. }
  123. public function testDecodeScalarWithAttribute()
  124. {
  125. $source = '<?xml version="1.0"?>'."\n".
  126. '<response><person gender="M">Peter</person></response>'."\n";
  127. $expected = array(
  128. 'person' => array('@gender' => 'M', '#' => 'Peter'),
  129. );
  130. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  131. }
  132. public function testDecodeArray()
  133. {
  134. $source = '<?xml version="1.0"?>'."\n".
  135. '<response>'.
  136. '<people>'.
  137. '<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
  138. '<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
  139. '</people>'.
  140. '</response>'."\n";
  141. $expected = array(
  142. 'people' => array('person' => array(
  143. array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
  144. array('firstname' => 'Damien', 'lastname' => 'Clay')
  145. ))
  146. );
  147. $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
  148. }
  149. protected function getXmlSource()
  150. {
  151. return '<?xml version="1.0"?>'."\n".
  152. '<response>'.
  153. '<foo><![CDATA[foo]]></foo>'.
  154. '<bar><![CDATA[a]]></bar><bar><![CDATA[b]]></bar>'.
  155. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item>'.
  156. '<Barry><FooBar id="1"><Baz><![CDATA[Ed]]></Baz></FooBar></Barry></baz>'.
  157. '<qux>1</qux>'.
  158. '</response>'."\n";
  159. }
  160. protected function getObject()
  161. {
  162. $obj = new Dummy;
  163. $obj->foo = 'foo';
  164. $obj->bar = array('a', 'b');
  165. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("Baz"=>"Ed", "@id"=>1)));
  166. $obj->qux = "1";
  167. return $obj;
  168. }
  169. }