BaseDriverTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 JMS\SerializerBundle\Metadata\PropertyMetadata;
  19. use JMS\SerializerBundle\Metadata\ClassMetadata;
  20. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  21. abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testLoadBlogPostMetadata()
  24. {
  25. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  26. $this->assertNotNull($m);
  27. $this->assertEquals('blog-post', $m->xmlRootName);
  28. $p = new PropertyMetadata($m->name, 'title');
  29. $p->type = 'string';
  30. $p->groups = array("comments","post");
  31. $this->assertEquals($p, $m->propertyMetadata['title']);
  32. $p = new PropertyMetadata($m->name, 'createdAt');
  33. $p->type = 'DateTime';
  34. $p->xmlAttribute = true;
  35. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  36. $p = new PropertyMetadata($m->name, 'published');
  37. $p->type = 'boolean';
  38. $p->serializedName = 'is_published';
  39. $p->xmlAttribute = true;
  40. $p->groups = array("post");
  41. $this->assertEquals($p, $m->propertyMetadata['published']);
  42. $p = new PropertyMetadata($m->name, 'comments');
  43. $p->type = 'ArrayCollection<JMS\SerializerBundle\Tests\Fixtures\Comment>';
  44. $p->xmlCollection = true;
  45. $p->xmlCollectionInline = true;
  46. $p->xmlEntryName = 'comment';
  47. $p->groups = array("comments");
  48. $this->assertEquals($p, $m->propertyMetadata['comments']);
  49. $p = new PropertyMetadata($m->name, 'author');
  50. $p->type = 'JMS\SerializerBundle\Tests\Fixtures\Author';
  51. $p->groups = array("post");
  52. $this->assertEquals($p, $m->propertyMetadata['author']);
  53. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\Price'));
  54. $this->assertNotNull($m);
  55. $p = new PropertyMetadata($m->name, 'price');
  56. $p->type = 'double';
  57. $p->xmlValue = true;
  58. $this->assertEquals($p, $m->propertyMetadata['price']);
  59. }
  60. public function testVirtualProperty()
  61. {
  62. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualProperties'));
  63. $this->assertArrayHasKey('existField', $m->propertyMetadata);
  64. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  65. $this->assertArrayHasKey('virtualSerializedValue', $m->propertyMetadata);
  66. $this->assertEquals($m->propertyMetadata['virtualSerializedValue']->serializedName, 'test', 'Serialized name is missing' );
  67. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  68. $p->getter = 'getVirtualValue';
  69. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  70. }
  71. public function testXmlKeyValuePairs()
  72. {
  73. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\ObjectWithXmlKeyValuePairs'));
  74. $this->assertArrayHasKey('array', $m->propertyMetadata);
  75. $this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
  76. }
  77. public function testVirtualPropertyWithExcludeAll()
  78. {
  79. $a = new \JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
  80. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
  81. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  82. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  83. $p->getter = 'getVirtualValue';
  84. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  85. }
  86. abstract protected function getDriver();
  87. }