XmlSerializationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * Copyright 2013 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\Context;
  19. use JMS\Serializer\SerializationContext;
  20. use JMS\Serializer\Tests\Fixtures\InvalidUsageOfXmlValue;
  21. use JMS\Serializer\Exception\InvalidArgumentException;
  22. use JMS\Serializer\Tests\Fixtures\PersonCollection;
  23. use JMS\Serializer\Tests\Fixtures\PersonLocation;
  24. use JMS\Serializer\Tests\Fixtures\Person;
  25. use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  26. use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  27. use JMS\Serializer\Tests\Fixtures\Input;
  28. class XmlSerializationTest extends BaseSerializationTest
  29. {
  30. /**
  31. * @expectedException JMS\Serializer\Exception\RuntimeException
  32. */
  33. public function testInvalidUsageOfXmlValue()
  34. {
  35. $obj = new InvalidUsageOfXmlValue();
  36. $this->serialize($obj);
  37. }
  38. public function testPropertyIsObjectWithAttributeAndValue()
  39. {
  40. $personCollection = new PersonLocation;
  41. $person = new Person;
  42. $person->name = 'Matthias Noback';
  43. $person->age = 28;
  44. $personCollection->person = $person;
  45. $personCollection->location = 'The Netherlands';
  46. $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
  47. }
  48. public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
  49. {
  50. $personCollection = new PersonCollection;
  51. $person = new Person;
  52. $person->name = 'Matthias Noback';
  53. $person->age = 28;
  54. $personCollection->persons->add($person);
  55. $personCollection->location = 'The Netherlands';
  56. $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
  57. }
  58. /**
  59. * @expectedException JMS\Serializer\Exception\InvalidArgumentException
  60. * @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.
  61. */
  62. public function testExternalEntitiesAreDisabledByDefault()
  63. {
  64. $this->deserialize('<?xml version="1.0"?>
  65. <!DOCTYPE author [
  66. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  67. ]>
  68. <result>
  69. &foo;
  70. </result>', 'stdClass');
  71. }
  72. /**
  73. * @expectedException JMS\Serializer\Exception\InvalidArgumentException
  74. * @expectedExceptionMessage The document type "<!DOCTYPE foo>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  75. */
  76. public function testDocumentTypesAreNotAllowed()
  77. {
  78. $this->deserialize('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'stdClass');
  79. }
  80. public function testWhitelistedDocumentTypesAreAllowed()
  81. {
  82. $this->deserializationVisitors->get('xml')->get()->setDoctypeWhitelist(array(
  83. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  84. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  85. $this->serializer->deserialize('<?xml version="1.0"?>
  86. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  87. <foo></foo>', 'stdClass', 'xml');
  88. $this->serializer->deserialize('<?xml version="1.0"?>
  89. <!DOCTYPE author [
  90. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  91. ]>
  92. <foo></foo>', 'stdClass', 'xml');
  93. }
  94. public function testVirtualAttributes()
  95. {
  96. $this->assertEquals(
  97. $this->getContent('virtual_attributes'),
  98. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('attributes')))
  99. );
  100. }
  101. public function testVirtualValues()
  102. {
  103. $this->assertEquals(
  104. $this->getContent('virtual_values'),
  105. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('values')))
  106. );
  107. }
  108. public function testVirtualXmlList()
  109. {
  110. $this->assertEquals(
  111. $this->getContent('virtual_properties_list'),
  112. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('list')))
  113. );
  114. }
  115. public function testVirtualXmlMap()
  116. {
  117. $this->assertEquals(
  118. $this->getContent('virtual_properties_map'),
  119. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('map')))
  120. );
  121. }
  122. public function testArrayKeyValues()
  123. {
  124. $this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  125. }
  126. /**
  127. * @expectedException JMS\Serializer\Exception\RuntimeException
  128. * @expectedExceptionMessage Unsupported value type for XML attribute map. Expected array but got object
  129. */
  130. public function testXmlAttributeMapWithoutArray()
  131. {
  132. $attributes = new \ArrayObject(array(
  133. 'type' => 'text',
  134. ));
  135. $this->serializer->serialize(new Input($attributes), $this->getFormat());
  136. }
  137. public function testDeserializingNull()
  138. {
  139. $this->markTestSkipped('Not supported in XML.');
  140. }
  141. /**
  142. * @param string $key
  143. */
  144. protected function getContent($key)
  145. {
  146. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  147. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  148. }
  149. return file_get_contents($file);
  150. }
  151. protected function getFormat()
  152. {
  153. return 'xml';
  154. }
  155. }