XmlSerializationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Tests\Serializer;
  18. use JMS\Serializer\Tests\Fixtures\InvalidUsageOfXmlValue;
  19. use JMS\Serializer\Serializer;
  20. use JMS\Serializer\Exception\InvalidArgumentException;
  21. use JMS\Serializer\Tests\Fixtures\PersonCollection;
  22. use JMS\Serializer\Tests\Fixtures\PersonLocation;
  23. use JMS\Serializer\Tests\Fixtures\Person;
  24. use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  25. use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  26. use JMS\Serializer\Tests\Fixtures\Input;
  27. class XmlSerializationTest extends BaseSerializationTest
  28. {
  29. /**
  30. * @expectedException \RuntimeException
  31. */
  32. public function testInvalidUsageOfXmlValue()
  33. {
  34. $obj = new InvalidUsageOfXmlValue();
  35. $this->serialize($obj);
  36. }
  37. public function testPropertyIsObjectWithAttributeAndValue()
  38. {
  39. $personCollection = new PersonLocation;
  40. $person = new Person;
  41. $person->name = 'Matthias Noback';
  42. $person->age = 28;
  43. $personCollection->person = $person;
  44. $personCollection->location = 'The Netherlands';
  45. $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
  46. }
  47. public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
  48. {
  49. $personCollection = new PersonCollection;
  50. $person = new Person;
  51. $person->name = 'Matthias Noback';
  52. $person->age = 28;
  53. $personCollection->persons->add($person);
  54. $personCollection->location = 'The Netherlands';
  55. $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
  56. }
  57. /**
  58. * @expectedException \InvalidArgumentException
  59. * @expectedExceptionMessage The document type "<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource=XmlSerializationTest.php">]>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  60. */
  61. public function testExternalEntitiesAreDisabledByDefault()
  62. {
  63. $this->deserialize('<?xml version="1.0"?>
  64. <!DOCTYPE author [
  65. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  66. ]>
  67. <result>
  68. &foo;
  69. </result>', 'stdClass');
  70. }
  71. /**
  72. * @expectedException \InvalidArgumentException
  73. * @expectedExceptionMessage The document type "<!DOCTYPE foo>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  74. */
  75. public function testDocumentTypesAreNotAllowed()
  76. {
  77. $this->deserialize('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'stdClass');
  78. }
  79. public function testWhitelistedDocumentTypesAreAllowed()
  80. {
  81. $this->deserializationVisitors->get('xml')->get()->setDoctypeWhitelist(array(
  82. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  83. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  84. $this->serializer->deserialize('<?xml version="1.0"?>
  85. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  86. <foo></foo>', 'stdClass', 'xml');
  87. $this->serializer->deserialize('<?xml version="1.0"?>
  88. <!DOCTYPE author [
  89. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  90. ]>
  91. <foo></foo>', 'stdClass', 'xml');
  92. }
  93. public function testVirtualAttributes()
  94. {
  95. $this->serializer->setGroups(array('attributes'));
  96. $this->assertEquals($this->getContent('virtual_attributes'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  97. }
  98. public function testVirtualValues()
  99. {
  100. $this->serializer->setGroups(array('values'));
  101. $this->assertEquals($this->getContent('virtual_values'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  102. }
  103. public function testVirtualXmlList()
  104. {
  105. $this->serializer->setGroups(array('list'));
  106. $this->assertEquals($this->getContent('virtual_properties_list'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  107. }
  108. public function testVirtualXmlMap()
  109. {
  110. $this->serializer->setGroups(array('map'));
  111. $this->assertEquals($this->getContent('virtual_properties_map'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  112. }
  113. public function testArrayKeyValues()
  114. {
  115. $this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  116. }
  117. /**
  118. * @expectedException RuntimeException
  119. * @expectedExceptionMessage Unsupported value type for XML attribute map. Expected array but got object
  120. */
  121. public function testXmlAttributeMapWithoutArray()
  122. {
  123. $attributes = new \ArrayObject(array(
  124. 'type' => 'text',
  125. ));
  126. $this->serializer->serialize(new Input($attributes), $this->getFormat());
  127. }
  128. public function testDeserializingNull()
  129. {
  130. $this->markTestSkipped('Not supported in XML.');
  131. }
  132. /**
  133. * @param string $key
  134. */
  135. protected function getContent($key)
  136. {
  137. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  138. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  139. }
  140. return file_get_contents($file);
  141. }
  142. protected function getFormat()
  143. {
  144. return 'xml';
  145. }
  146. }