BaseDriverTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * Copyright 2013 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\Serializer\Tests\Metadata\Driver;
  18. use JMS\Serializer\Metadata\ClassMetadata;
  19. use JMS\Serializer\Metadata\PropertyMetadata;
  20. use JMS\Serializer\Metadata\VirtualPropertyMetadata;
  21. use Metadata\Driver\DriverInterface;
  22. abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testLoadBlogPostMetadata()
  25. {
  26. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  27. $this->assertNotNull($m);
  28. $this->assertEquals('blog-post', $m->xmlRootName);
  29. $p = new PropertyMetadata($m->name, 'title');
  30. $p->type = array('name' => 'string', 'params' => array());
  31. $p->groups = array("comments","post");
  32. $this->assertEquals($p, $m->propertyMetadata['title']);
  33. $p = new PropertyMetadata($m->name, 'createdAt');
  34. $p->type = array('name' => 'DateTime', 'params' => array());
  35. $p->xmlAttribute = true;
  36. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  37. $p = new PropertyMetadata($m->name, 'published');
  38. $p->type = array('name' => 'boolean', 'params' => array());
  39. $p->serializedName = 'is_published';
  40. $p->xmlAttribute = true;
  41. $p->groups = array("post");
  42. $this->assertEquals($p, $m->propertyMetadata['published']);
  43. $p = new PropertyMetadata($m->name, 'comments');
  44. $p->type = array('name' => 'ArrayCollection', 'params' => array(array('name' => 'JMS\Serializer\Tests\Fixtures\Comment', 'params' => array())));
  45. $p->xmlCollection = true;
  46. $p->xmlCollectionInline = true;
  47. $p->xmlEntryName = 'comment';
  48. $p->groups = array("comments");
  49. $this->assertEquals($p, $m->propertyMetadata['comments']);
  50. $p = new PropertyMetadata($m->name, 'author');
  51. $p->type = array('name' => 'JMS\Serializer\Tests\Fixtures\Author', 'params' => array());
  52. $p->groups = array("post");
  53. $this->assertEquals($p, $m->propertyMetadata['author']);
  54. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Price'));
  55. $this->assertNotNull($m);
  56. $p = new PropertyMetadata($m->name, 'price');
  57. $p->type = array('name' => 'double', 'params' => array());
  58. $p->xmlValue = true;
  59. $this->assertEquals($p, $m->propertyMetadata['price']);
  60. }
  61. public function testVirtualProperty()
  62. {
  63. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithVirtualProperties'));
  64. $this->assertArrayHasKey('existField', $m->propertyMetadata);
  65. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  66. $this->assertArrayHasKey('virtualSerializedValue', $m->propertyMetadata);
  67. $this->assertArrayHasKey('typedVirtualProperty', $m->propertyMetadata);
  68. $this->assertEquals($m->propertyMetadata['virtualSerializedValue']->serializedName, 'test', 'Serialized name is missing' );
  69. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  70. $p->getter = 'getVirtualValue';
  71. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  72. }
  73. public function testXmlKeyValuePairs()
  74. {
  75. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs'));
  76. $this->assertArrayHasKey('array', $m->propertyMetadata);
  77. $this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
  78. }
  79. public function testVirtualPropertyWithExcludeAll()
  80. {
  81. $a = new \JMS\Serializer\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
  82. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
  83. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  84. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  85. $p->getter = 'getVirtualValue';
  86. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  87. }
  88. public function testReadOnlyDefinedBeforeGetterAndSetter()
  89. {
  90. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\AuthorReadOnly'));
  91. $this->assertNotNull($m);
  92. }
  93. public function testLoadDiscriminator()
  94. {
  95. /** @var $m ClassMetadata */
  96. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle'));
  97. $this->assertNotNull($m);
  98. $this->assertEquals('type', $m->discriminatorFieldName);
  99. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  100. $this->assertEquals(
  101. array(
  102. 'car' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Car',
  103. 'moped' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Moped',
  104. ),
  105. $m->discriminatorMap
  106. );
  107. }
  108. public function testLoadDiscriminatorSubClass()
  109. {
  110. /** @var $m ClassMetadata */
  111. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Car'));
  112. $this->assertNotNull($m);
  113. $this->assertNull($m->discriminatorValue);
  114. $this->assertNull($m->discriminatorBaseClass);
  115. $this->assertNull($m->discriminatorFieldName);
  116. $this->assertEquals(array(), $m->discriminatorMap);
  117. }
  118. /**
  119. * @return DriverInterface
  120. */
  121. abstract protected function getDriver();
  122. }