XmlDriverTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Metadata\Driver;
  18. use Metadata\Driver\FileLocator;
  19. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  20. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  21. use JMS\SerializerBundle\Metadata\Driver\XmlDriver;
  22. class XmlDriverTest extends BaseDriverTest
  23. {
  24. /**
  25. * @expectedException JMS\SerializerBundle\Exception\XmlErrorException
  26. * @expectedExceptionMessage [FATAL] Start tag expected, '<' not found
  27. */
  28. public function testInvalidXml()
  29. {
  30. $driver = $this->getDriver();
  31. $ref = new \ReflectionMethod($driver, 'loadMetadataFromFile');
  32. $ref->setAccessible(true);
  33. $ref->invoke($driver, new \ReflectionClass('stdClass'), __DIR__.'/xml/invalid.xml');
  34. }
  35. public function testBlogPostExcludeAllStrategy()
  36. {
  37. $m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  38. $this->assertArrayHasKey('title', $m->propertyMetadata);
  39. $excluded = array('createdAt', 'published', 'comments', 'author');
  40. foreach ($excluded as $key) {
  41. $this->assertArrayNotHasKey($key, $m->propertyMetadata);
  42. }
  43. }
  44. public function testBlogPostExcludeNoneStrategy()
  45. {
  46. $m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  47. $this->assertArrayNotHasKey('title', $m->propertyMetadata);
  48. $excluded = array('createdAt', 'published', 'comments', 'author');
  49. foreach ($excluded as $key) {
  50. $this->assertArrayHasKey($key, $m->propertyMetadata);
  51. }
  52. }
  53. public function testBlogPostCaseInsensitive()
  54. {
  55. $m = $this->getDriver('case')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  56. $p = new PropertyMetadata($m->name, 'title');
  57. $p->type = 'string';
  58. $this->assertEquals($p, $m->propertyMetadata['title']);
  59. }
  60. public function testAccessorAttributes()
  61. {
  62. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\GetSetObject'));
  63. $p = new PropertyMetadata($m->name, 'name');
  64. $p->type = 'string';
  65. $p->getter = 'getTrimmedName';
  66. $p->setter = 'setCapitalizedName';
  67. $this->assertEquals($p, $m->propertyMetadata['name']);
  68. }
  69. public function testVirtualProperty()
  70. {
  71. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualProperties'));
  72. $this->assertArrayHasKey('existField', $m->propertyMetadata);
  73. $this->assertArrayHasKey('foo', $m->propertyMetadata);
  74. $this->assertArrayHasKey('prop_name', $m->propertyMetadata);
  75. $p = new VirtualPropertyMetadata($m->name, 'foo');
  76. $p->getter = 'getVirtualValue';
  77. $this->assertEquals($p, $m->propertyMetadata['foo']);
  78. }
  79. protected function getDriver()
  80. {
  81. $append = '';
  82. if (func_num_args() == 1) {
  83. $append = '/'.func_get_arg(0);
  84. }
  85. return new XmlDriver(new FileLocator(array(
  86. 'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/xml'.$append,
  87. )));
  88. }
  89. }