XmlSerializationTest.php 3.1 KB

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