BaseDriverTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $this->assertCount(4, $m->xmlNamespaces);
  30. $this->assertArrayHasKey('', $m->xmlNamespaces);
  31. $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
  32. $this->assertArrayHasKey('gd', $m->xmlNamespaces);
  33. $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
  34. $this->assertArrayHasKey('atom', $m->xmlNamespaces);
  35. $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
  36. $this->assertArrayHasKey('dc', $m->xmlNamespaces);
  37. $this->assertEquals('http://purl.org/dc/elements/1.1/', $m->xmlNamespaces['dc']);
  38. $p = new PropertyMetadata($m->name, 'id');
  39. $p->type = array('name' => 'string', 'params' => array());
  40. $p->groups = array("comments","post");
  41. $p->xmlElementCData = false;
  42. $this->assertEquals($p, $m->propertyMetadata['id']);
  43. $p = new PropertyMetadata($m->name, 'title');
  44. $p->type = array('name' => 'string', 'params' => array());
  45. $p->groups = array("comments","post");
  46. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  47. $this->assertEquals($p, $m->propertyMetadata['title']);
  48. $p = new PropertyMetadata($m->name, 'createdAt');
  49. $p->type = array('name' => 'DateTime', 'params' => array());
  50. $p->xmlAttribute = true;
  51. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  52. $p = new PropertyMetadata($m->name, 'published');
  53. $p->type = array('name' => 'boolean', 'params' => array());
  54. $p->serializedName = 'is_published';
  55. $p->xmlAttribute = true;
  56. $p->groups = array("post");
  57. $this->assertEquals($p, $m->propertyMetadata['published']);
  58. $p = new PropertyMetadata($m->name, 'etag');
  59. $p->type = array('name' => 'string', 'params' => array());
  60. $p->xmlAttribute = true;
  61. $p->groups = array("post");
  62. $p->xmlNamespace = "http://schemas.google.com/g/2005";
  63. $this->assertEquals($p, $m->propertyMetadata['etag']);
  64. $p = new PropertyMetadata($m->name, 'comments');
  65. $p->type = array('name' => 'ArrayCollection', 'params' => array(array('name' => 'JMS\Serializer\Tests\Fixtures\Comment', 'params' => array())));
  66. $p->xmlCollection = true;
  67. $p->xmlCollectionInline = true;
  68. $p->xmlEntryName = 'comment';
  69. $p->groups = array("comments");
  70. $this->assertEquals($p, $m->propertyMetadata['comments']);
  71. $p = new PropertyMetadata($m->name, 'author');
  72. $p->type = array('name' => 'JMS\Serializer\Tests\Fixtures\Author', 'params' => array());
  73. $p->groups = array("post");
  74. $p->xmlNamespace = 'http://www.w3.org/2005/Atom';
  75. $this->assertEquals($p, $m->propertyMetadata['author']);
  76. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Price'));
  77. $this->assertNotNull($m);
  78. $p = new PropertyMetadata($m->name, 'price');
  79. $p->type = array('name' => 'double', 'params' => array());
  80. $p->xmlValue = true;
  81. $this->assertEquals($p, $m->propertyMetadata['price']);
  82. }
  83. public function testVirtualProperty()
  84. {
  85. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithVirtualProperties'));
  86. $this->assertArrayHasKey('existField', $m->propertyMetadata);
  87. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  88. $this->assertArrayHasKey('virtualSerializedValue', $m->propertyMetadata);
  89. $this->assertArrayHasKey('typedVirtualProperty', $m->propertyMetadata);
  90. $this->assertEquals($m->propertyMetadata['virtualSerializedValue']->serializedName, 'test', 'Serialized name is missing' );
  91. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  92. $p->getter = 'getVirtualValue';
  93. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  94. }
  95. public function testXmlKeyValuePairs()
  96. {
  97. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs'));
  98. $this->assertArrayHasKey('array', $m->propertyMetadata);
  99. $this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
  100. }
  101. public function testVirtualPropertyWithExcludeAll()
  102. {
  103. $a = new \JMS\Serializer\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
  104. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
  105. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  106. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  107. $p->getter = 'getVirtualValue';
  108. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  109. }
  110. public function testReadOnlyDefinedBeforeGetterAndSetter()
  111. {
  112. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\AuthorReadOnly'));
  113. $this->assertNotNull($m);
  114. }
  115. public function testLoadDiscriminator()
  116. {
  117. /** @var $m ClassMetadata */
  118. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle'));
  119. $this->assertNotNull($m);
  120. $this->assertEquals('type', $m->discriminatorFieldName);
  121. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  122. $this->assertEquals(
  123. array(
  124. 'car' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Car',
  125. 'moped' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Moped',
  126. ),
  127. $m->discriminatorMap
  128. );
  129. }
  130. public function testLoadDiscriminatorSubClass()
  131. {
  132. /** @var $m ClassMetadata */
  133. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Car'));
  134. $this->assertNotNull($m);
  135. $this->assertNull($m->discriminatorValue);
  136. $this->assertNull($m->discriminatorBaseClass);
  137. $this->assertNull($m->discriminatorFieldName);
  138. $this->assertEquals(array(), $m->discriminatorMap);
  139. }
  140. public function testLoadXmlObjectWithNamespacesMetadata()
  141. {
  142. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces'));
  143. $this->assertNotNull($m);
  144. $this->assertEquals('test-object', $m->xmlRootName);
  145. $this->assertCount(3, $m->xmlNamespaces);
  146. $this->assertArrayHasKey('', $m->xmlNamespaces);
  147. $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
  148. $this->assertArrayHasKey('gd', $m->xmlNamespaces);
  149. $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
  150. $this->assertArrayHasKey('atom', $m->xmlNamespaces);
  151. $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
  152. $p = new PropertyMetadata($m->name, 'title');
  153. $p->type = array('name' => 'string', 'params' => array());
  154. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  155. $this->assertEquals($p, $m->propertyMetadata['title']);
  156. $p = new PropertyMetadata($m->name, 'createdAt');
  157. $p->type = array('name' => 'DateTime', 'params' => array());
  158. $p->xmlAttribute = true;
  159. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  160. $p = new PropertyMetadata($m->name, 'etag');
  161. $p->type = array('name' => 'string', 'params' => array());
  162. $p->xmlAttribute = true;
  163. $p->xmlNamespace = "http://schemas.google.com/g/2005";
  164. $this->assertEquals($p, $m->propertyMetadata['etag']);
  165. $p = new PropertyMetadata($m->name, 'author');
  166. $p->type = array('name' => 'string', 'params' => array());
  167. $p->xmlAttribute = false;
  168. $p->xmlNamespace = "http://www.w3.org/2005/Atom";
  169. $this->assertEquals($p, $m->propertyMetadata['author']);
  170. $p = new PropertyMetadata($m->name, 'language');
  171. $p->type = array('name' => 'string', 'params' => array());
  172. $p->xmlAttribute = true;
  173. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  174. $this->assertEquals($p, $m->propertyMetadata['language']);
  175. }
  176. public function testMaxDepth()
  177. {
  178. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Node'));
  179. $this->assertEquals(2, $m->propertyMetadata['children']->maxDepth);
  180. }
  181. public function testPersonCData()
  182. {
  183. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
  184. $this->assertNotNull($m);
  185. $this->assertFalse($m->propertyMetadata['name']->xmlElementCData);
  186. }
  187. /**
  188. * @return DriverInterface
  189. */
  190. abstract protected function getDriver();
  191. }