XmlSerializationTest.php 6.5 KB

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