XmlSerializationTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\ObjectWithVirtualXmlProperties;
  23. class XmlSerializationTest extends BaseSerializationTest
  24. {
  25. /**
  26. * @expectedException \RuntimeException
  27. */
  28. public function testInvalidUsageOfXmlValue()
  29. {
  30. $obj = new InvalidUsageOfXmlValue();
  31. $this->serialize($obj);
  32. }
  33. public function testExternalEntitiesAreDisabledByDefault()
  34. {
  35. $currentDir = getcwd();
  36. chdir(__DIR__);
  37. $entity = $this->deserialize('<?xml version="1.0"?>
  38. <!DOCTYPE author [
  39. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  40. ]>
  41. <result>
  42. &foo;
  43. </result>', 'JMS\SerializerBundle\Tests\Serializer\ExternalEntityTest');
  44. chdir($currentDir);
  45. $this->assertEquals('', trim($entity->foo));
  46. }
  47. public function testVirtualAttributes() {
  48. $serializer = $this->getSerializer();
  49. $serializer->setGroups(array('attributes'));
  50. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  51. }
  52. public function testVirtualValues() {
  53. $serializer = $this->getSerializer();
  54. $serializer->setGroups(array('values'));
  55. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  56. }
  57. public function testVirtualXmlList() {
  58. $serializer = $this->getSerializer();
  59. $serializer->setGroups(array('list'));
  60. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  61. }
  62. public function testVirtualXmlMap() {
  63. $serializer = $this->getSerializer();
  64. $serializer->setGroups(array('map'));
  65. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  66. }
  67. protected function getContent($key)
  68. {
  69. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  70. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  71. }
  72. return file_get_contents($file);
  73. }
  74. protected function getFormat()
  75. {
  76. return 'xml';
  77. }
  78. }
  79. class ExternalEntityTest
  80. {
  81. /** @Type("string") @XmlValue */
  82. public $foo;
  83. }