XmlEncoderTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 testDecodeScalar()
  97. {
  98. $source = '<?xml version="1.0"?>'."\n".
  99. '<response>foo</response>'."\n";
  100. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  101. }
  102. public function testEncode()
  103. {
  104. $source = $this->getXmlSource();
  105. $obj = $this->getObject();
  106. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  107. }
  108. public function testDecode()
  109. {
  110. $source = $this->getXmlSource();
  111. $obj = $this->getObject();
  112. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  113. }
  114. protected function getXmlSource()
  115. {
  116. return '<?xml version="1.0"?>'."\n".
  117. '<response>'.
  118. '<foo><![CDATA[foo]]></foo>'.
  119. '<bar><![CDATA[a]]></bar><bar><![CDATA[b]]></bar>'.
  120. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item>'.
  121. '<Barry><FooBar id="1"><Baz><![CDATA[Ed]]></Baz></FooBar></Barry></baz>'.
  122. '<qux>1</qux>'.
  123. '</response>'."\n";
  124. }
  125. protected function getObject()
  126. {
  127. $obj = new Dummy;
  128. $obj->foo = 'foo';
  129. $obj->bar = array('a', 'b');
  130. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("@id"=>1,"Baz"=>"Ed")));
  131. $obj->qux = "1";
  132. return $obj;
  133. }
  134. }