XmlSerializationTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Tests\Fixtures\InvalidUsageOfXmlValue;
  19. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  20. use JMS\SerializerBundle\Annotation\Type;
  21. use JMS\SerializerBundle\Annotation\XmlValue;
  22. use JMS\SerializerBundle\Tests\Fixtures\PersonCollection;
  23. use JMS\SerializerBundle\Tests\Fixtures\PersonLocation;
  24. use JMS\SerializerBundle\Tests\Fixtures\Person;
  25. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  26. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  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 Document types are not allowed
  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 Document types are not allowed
  74. */
  75. public function testDocumentTypesAreNotAllowed()
  76. {
  77. $this->deserialize('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'stdClass');
  78. }
  79. public function testVirtualAttributes() {
  80. $serializer = $this->getSerializer();
  81. $serializer->setGroups(array('attributes'));
  82. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  83. }
  84. public function testVirtualValues() {
  85. $serializer = $this->getSerializer();
  86. $serializer->setGroups(array('values'));
  87. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  88. }
  89. public function testVirtualXmlList() {
  90. $serializer = $this->getSerializer();
  91. $serializer->setGroups(array('list'));
  92. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  93. }
  94. public function testVirtualXmlMap() {
  95. $serializer = $this->getSerializer();
  96. $serializer->setGroups(array('map'));
  97. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  98. }
  99. public function testArrayKeyValues()
  100. {
  101. $serializer = $this->getSerializer();
  102. $this->assertEquals($this->getContent('array_key_values'), $serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  103. }
  104. protected function getContent($key)
  105. {
  106. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  107. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  108. }
  109. return file_get_contents($file);
  110. }
  111. protected function getFormat()
  112. {
  113. return 'xml';
  114. }
  115. }