XmlEncoderTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 testEncodeSimpleXML()
  36. {
  37. $xml = simplexml_load_string('<firstname>Peter</firstname>');
  38. $array = array('person' => $xml);
  39. $expected = '<?xml version="1.0"?>'."\n".
  40. '<response><person><firstname>Peter</firstname></person></response>'."\n";
  41. $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
  42. }
  43. public function testDecodeScalar()
  44. {
  45. $source = '<?xml version="1.0"?>'."\n".
  46. '<response>foo</response>'."\n";
  47. $this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
  48. }
  49. public function testEncode()
  50. {
  51. $source = $this->getXmlSource();
  52. $obj = $this->getObject();
  53. $this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
  54. }
  55. public function testDecode()
  56. {
  57. $source = $this->getXmlSource();
  58. $obj = $this->getObject();
  59. $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
  60. }
  61. protected function getXmlSource()
  62. {
  63. return '<?xml version="1.0"?>'."\n".
  64. '<response>'.
  65. '<foo><![CDATA[foo]]></foo>'.
  66. '<bar><item key="0"><![CDATA[a]]></item><item key="1"><![CDATA[b]]></item></bar>'.
  67. '<baz><key><![CDATA[val]]></key><key2><![CDATA[val]]></key2><item key="A B"><![CDATA[bar]]></item></baz>'.
  68. '<qux>1</qux>'.
  69. '</response>'."\n";
  70. }
  71. protected function getObject()
  72. {
  73. $obj = new Dummy;
  74. $obj->foo = 'foo';
  75. $obj->bar = array('a', 'b');
  76. $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar');
  77. $obj->qux = "1";
  78. return $obj;
  79. }
  80. }