XmlEncoderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 testElementNameValid()
  45. {
  46. $obj = new ScalarDummy;
  47. $obj->xmlFoo = array(
  48. 'foo-bar' => '',
  49. 'foo_bar' => '',
  50. 'föo_bär' => '',
  51. );
  52. $expected = '<?xml version="1.0"?>'."\n".
  53. '<response>'.
  54. '<foo-bar><![CDATA[]]></foo-bar>'.
  55. '<foo_bar><![CDATA[]]></foo_bar>'.
  56. '<föo_bär><![CDATA[]]></föo_bär>'.
  57. '</response>'."\n";
  58. $this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
  59. }
  60. public function testEncodeSimpleXML()
  61. {
  62. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  63. $array = array('person' => $xml);
  64. $expected = '<?xml version="1.0"?>'."\n".
  65. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  66. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  67. }
  68. public function testDecodeScalar()
  69. {
  70. $source = '<?xml version="1.0"?>'."\n".
  71. '<response>foo</response>'."\n";
  72. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  73. }
  74. public function testEncode()
  75. {
  76. $source = $this->getXmlSource();
  77. $obj = $this->getObject();
  78. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  79. }
  80. public function testDecode()
  81. {
  82. $source = $this->getXmlSource();
  83. $obj = $this->getObject();
  84. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  85. }
  86. protected function getXmlSource()
  87. {
  88. return '<?xml version="1.0"?>'."\n".
  89. '<response>'.
  90. '<foo><![CDATA[foo]]></foo>'.
  91. '<bar><item key="0"><![CDATA[a]]></item><item key="1"><![CDATA[b]]></item></bar>'.
  92. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item></baz>'.
  93. '<qux>1</qux>'.
  94. '</response>'."\n";
  95. }
  96. protected function getObject()
  97. {
  98. $obj = new Dummy;
  99. $obj->foo = 'foo';
  100. $obj->bar = array('a', 'b');
  101. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar');
  102. $obj->qux = "1";
  103. return $obj;
  104. }
  105. }