XmlSerializationTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public function testExternalEntitiesAreDisabledByDefault()
  58. {
  59. $currentDir = getcwd();
  60. chdir(__DIR__);
  61. $entity = $this->deserialize('<?xml version="1.0"?>
  62. <!DOCTYPE author [
  63. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  64. ]>
  65. <result>
  66. &foo;
  67. </result>', 'JMS\SerializerBundle\Tests\Serializer\ExternalEntityTest');
  68. chdir($currentDir);
  69. $this->assertEquals('', trim($entity->foo));
  70. }
  71. public function testVirtualAttributes() {
  72. $serializer = $this->getSerializer();
  73. $serializer->setGroups(array('attributes'));
  74. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  75. }
  76. public function testVirtualValues() {
  77. $serializer = $this->getSerializer();
  78. $serializer->setGroups(array('values'));
  79. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  80. }
  81. public function testVirtualXmlList() {
  82. $serializer = $this->getSerializer();
  83. $serializer->setGroups(array('list'));
  84. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  85. }
  86. public function testVirtualXmlMap() {
  87. $serializer = $this->getSerializer();
  88. $serializer->setGroups(array('map'));
  89. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  90. }
  91. public function testArrayKeyValues()
  92. {
  93. $serializer = $this->getSerializer();
  94. $this->assertEquals($this->getContent('array_key_values'), $serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  95. }
  96. protected function getContent($key)
  97. {
  98. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  99. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  100. }
  101. return file_get_contents($file);
  102. }
  103. protected function getFormat()
  104. {
  105. return 'xml';
  106. }
  107. }
  108. class ExternalEntityTest
  109. {
  110. /** @Type("string") @XmlValue */
  111. public $foo;
  112. }