XmlSerializationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Metadata\MetadataFactory;
  19. use Doctrine\Common\Annotations\AnnotationReader;
  20. use JMS\SerializerBundle\Tests\Fixtures\InvalidUsageOfXmlValue;
  21. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  22. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  23. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  24. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  25. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  26. use JMS\SerializerBundle\Serializer\Serializer;
  27. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  28. use JMS\SerializerBundle\Annotation\Type;
  29. use JMS\SerializerBundle\Annotation\XmlValue;
  30. use JMS\SerializerBundle\Tests\Fixtures\PersonCollection;
  31. use JMS\SerializerBundle\Tests\Fixtures\PersonLocation;
  32. use JMS\SerializerBundle\Tests\Fixtures\Person;
  33. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  34. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  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 Document types are not allowed
  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 Document types are not allowed
  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. $xmlVisitor = new XmlDeserializationVisitor(
  90. new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()),
  91. $this->getDeserializationHandlers(),
  92. new UnserializeObjectConstructor()
  93. );
  94. $xmlVisitor->setDocumentWhitelist(array(
  95. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  96. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  97. $serializer = new Serializer(new MetadataFactory(new AnnotationDriver(new AnnotationReader())), array(), array('xml' => $xmlVisitor));
  98. $serializer->deserialize('<?xml version="1.0"?>
  99. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  100. <foo></foo>', 'stdClass', 'xml');
  101. $serializer->deserialize('<?xml version="1.0"?>
  102. <!DOCTYPE author [
  103. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  104. ]>
  105. <foo></foo>', 'stdClass', 'xml');
  106. }
  107. public function testVirtualAttributes() {
  108. $serializer = $this->getSerializer();
  109. $serializer->setGroups(array('attributes'));
  110. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  111. }
  112. public function testVirtualValues() {
  113. $serializer = $this->getSerializer();
  114. $serializer->setGroups(array('values'));
  115. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  116. }
  117. public function testVirtualXmlList() {
  118. $serializer = $this->getSerializer();
  119. $serializer->setGroups(array('list'));
  120. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  121. }
  122. public function testVirtualXmlMap() {
  123. $serializer = $this->getSerializer();
  124. $serializer->setGroups(array('map'));
  125. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  126. }
  127. public function testArrayKeyValues()
  128. {
  129. $serializer = $this->getSerializer();
  130. $this->assertEquals($this->getContent('array_key_values'), $serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  131. }
  132. protected function getContent($key)
  133. {
  134. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  135. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  136. }
  137. return file_get_contents($file);
  138. }
  139. protected function getFormat()
  140. {
  141. return 'xml';
  142. }
  143. }