XmlSerializationTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class XmlSerializationTest extends BaseSerializationTest
  27. {
  28. /**
  29. * @expectedException \RuntimeException
  30. */
  31. public function testInvalidUsageOfXmlValue()
  32. {
  33. $obj = new InvalidUsageOfXmlValue();
  34. $this->serialize($obj);
  35. }
  36. public function testPropertyIsObjectWithAttributeAndValue()
  37. {
  38. $personCollection = new PersonLocation;
  39. $person = new Person;
  40. $person->name = 'Matthias Noback';
  41. $person->age = 28;
  42. $personCollection->person = $person;
  43. $personCollection->location = 'The Netherlands';
  44. $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
  45. }
  46. public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
  47. {
  48. $personCollection = new PersonCollection;
  49. $person = new Person;
  50. $person->name = 'Matthias Noback';
  51. $person->age = 28;
  52. $personCollection->persons->add($person);
  53. $personCollection->location = 'The Netherlands';
  54. $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
  55. }
  56. public function testExternalEntitiesAreDisabledByDefault()
  57. {
  58. $currentDir = getcwd();
  59. chdir(__DIR__);
  60. $entity = $this->deserialize('<?xml version="1.0"?>
  61. <!DOCTYPE author [
  62. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  63. ]>
  64. <result>
  65. &foo;
  66. </result>', 'JMS\SerializerBundle\Tests\Serializer\ExternalEntityTest');
  67. chdir($currentDir);
  68. $this->assertEquals('', trim($entity->foo));
  69. }
  70. public function testVirtualAttributes() {
  71. $serializer = $this->getSerializer();
  72. $serializer->setGroups(array('attributes'));
  73. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  74. }
  75. public function testVirtualValues() {
  76. $serializer = $this->getSerializer();
  77. $serializer->setGroups(array('values'));
  78. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  79. }
  80. public function testVirtualXmlList() {
  81. $serializer = $this->getSerializer();
  82. $serializer->setGroups(array('list'));
  83. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  84. }
  85. public function testVirtualXmlMap() {
  86. $serializer = $this->getSerializer();
  87. $serializer->setGroups(array('map'));
  88. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  89. }
  90. protected function getContent($key)
  91. {
  92. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  93. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  94. }
  95. return file_get_contents($file);
  96. }
  97. protected function getFormat()
  98. {
  99. return 'xml';
  100. }
  101. }
  102. class ExternalEntityTest
  103. {
  104. /** @Type("string") @XmlValue */
  105. public $foo;
  106. }