XmlSerializationTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. protected function getContent($key)
  58. {
  59. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  60. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  61. }
  62. return file_get_contents($file);
  63. }
  64. protected function getFormat()
  65. {
  66. return 'xml';
  67. }
  68. }
  69. class ExternalEntityTest
  70. {
  71. /** @Type("string") @XmlValue */
  72. public $foo;
  73. }