XmlEncoderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.potencier@symfony-project.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' => '',
  57. "Bar" => array(1,2,3)
  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[]]></föo_bär>'.
  64. '<Bar>1</Bar>'.
  65. '<Bar>2</Bar>'.
  66. '<Bar>3</Bar>'.
  67. '</response>'."\n";
  68. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  69. }
  70. public function testElementNameValid()
  71. {
  72. $obj = new ScalarDummy;
  73. $obj->xmlFoo = array(
  74. 'foo-bar' => '',
  75. 'foo_bar' => '',
  76. 'föo_bär' => '',
  77. );
  78. $expected = '<?xml version="1.0"?>'."\n".
  79. '<response>'.
  80. '<foo-bar><![CDATA[]]></foo-bar>'.
  81. '<foo_bar><![CDATA[]]></foo_bar>'.
  82. '<föo_bär><![CDATA[]]></föo_bär>'.
  83. '</response>'."\n";
  84. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  85. }
  86. public function testEncodeSimpleXML()
  87. {
  88. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  89. $array = array('person' => $xml);
  90. $expected = '<?xml version="1.0"?>'."\n".
  91. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  92. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  93. }
  94. public function testDecodeScalar()
  95. {
  96. $source = '<?xml version="1.0"?>'."\n".
  97. '<response>foo</response>'."\n";
  98. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  99. }
  100. public function testEncode()
  101. {
  102. $source = $this->getXmlSource();
  103. $obj = $this->getObject();
  104. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  105. }
  106. public function testDecode()
  107. {
  108. $source = $this->getXmlSource();
  109. $obj = $this->getObject();
  110. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  111. }
  112. protected function getXmlSource()
  113. {
  114. return '<?xml version="1.0"?>'."\n".
  115. '<response>'.
  116. '<foo><![CDATA[foo]]></foo>'.
  117. '<bar><![CDATA[a]]></bar><bar><![CDATA[b]]></bar>'.
  118. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item>'.
  119. '<Barry><FooBar id="1"><Baz><![CDATA[Ed]]></Baz></FooBar></Barry></baz>'.
  120. '<qux>1</qux>'.
  121. '</response>'."\n";
  122. }
  123. protected function getObject()
  124. {
  125. $obj = new Dummy;
  126. $obj->foo = 'foo';
  127. $obj->bar = array('a', 'b');
  128. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', "Barry" => array('FooBar' => array("@id"=>1,"Baz"=>"Ed")));
  129. $obj->qux = "1";
  130. return $obj;
  131. }
  132. }