BaseDriverTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. public function testXmlNamespaceInheritanceMetadata()
  188. {
  189. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleClassObject'));
  190. $this->assertNotNull($m);
  191. $this->assertCount(3, $m->xmlNamespaces);
  192. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  193. $this->assertEquals('http://old.foo.example.org', $m->xmlNamespaces['old_foo']);
  194. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  195. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['foo']);
  196. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  197. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  198. $this->assertCount(3, $m->propertyMetadata);
  199. $p = new PropertyMetadata($m->name, 'foo');
  200. $p->type = array('name' => 'string', 'params' => array());
  201. $p->xmlNamespace = "http://old.foo.example.org";
  202. $p->xmlAttribute = true;
  203. $this->assertEquals($p, $m->propertyMetadata['foo']);
  204. $p = new PropertyMetadata($m->name, 'bar');
  205. $p->type = array('name' => 'string', 'params' => array());
  206. $p->xmlNamespace = "http://foo.example.org";
  207. $this->assertEquals($p, $m->propertyMetadata['bar']);
  208. $p = new PropertyMetadata($m->name, 'moo');
  209. $p->type = array('name' => 'string', 'params' => array());
  210. $p->xmlNamespace = "http://new.foo.example.org";
  211. $this->assertEquals($p, $m->propertyMetadata['moo']);
  212. $subm = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleSubClassObject'));
  213. $this->assertNotNull($subm);
  214. $this->assertCount(2, $subm->xmlNamespaces);
  215. $this->assertArrayHasKey('old_foo', $subm->xmlNamespaces);
  216. $this->assertEquals('http://foo.example.org', $subm->xmlNamespaces['old_foo']);
  217. $this->assertArrayHasKey('foo', $subm->xmlNamespaces);
  218. $this->assertEquals('http://better.foo.example.org', $subm->xmlNamespaces['foo']);
  219. $this->assertCount(3, $subm->propertyMetadata);
  220. $p = new PropertyMetadata($subm->name, 'moo');
  221. $p->type = array('name' => 'string', 'params' => array());
  222. $p->xmlNamespace = "http://better.foo.example.org";
  223. $this->assertEquals($p, $subm->propertyMetadata['moo']);
  224. $p = new PropertyMetadata($subm->name, 'baz');
  225. $p->type = array('name' => 'string', 'params' => array());
  226. $p->xmlNamespace = "http://foo.example.org";
  227. $this->assertEquals($p, $subm->propertyMetadata['baz']);
  228. $p = new PropertyMetadata($subm->name, 'qux');
  229. $p->type = array('name' => 'string', 'params' => array());
  230. $p->xmlNamespace = "http://new.foo.example.org";
  231. $this->assertEquals($p, $subm->propertyMetadata['qux']);
  232. $m->merge($subm);
  233. $this->assertNotNull($m);
  234. $this->assertCount(3, $m->xmlNamespaces);
  235. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  236. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['old_foo']);
  237. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  238. $this->assertEquals('http://better.foo.example.org', $m->xmlNamespaces['foo']);
  239. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  240. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  241. $this->assertCount(5, $m->propertyMetadata);
  242. $p = new PropertyMetadata($m->name, 'foo');
  243. $p->type = array('name' => 'string', 'params' => array());
  244. $p->xmlNamespace = "http://old.foo.example.org";
  245. $p->xmlAttribute = true;
  246. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  247. $this->assertEquals($p, $m->propertyMetadata['foo']);
  248. $p = new PropertyMetadata($m->name, 'bar');
  249. $p->type = array('name' => 'string', 'params' => array());
  250. $p->xmlNamespace = "http://foo.example.org";
  251. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  252. $this->assertEquals($p, $m->propertyMetadata['bar']);
  253. $p = new PropertyMetadata($m->name, 'moo');
  254. $p->type = array('name' => 'string', 'params' => array());
  255. $p->xmlNamespace = "http://better.foo.example.org";
  256. $this->assertEquals($p, $m->propertyMetadata['moo']);
  257. $p = new PropertyMetadata($m->name, 'baz');
  258. $p->type = array('name' => 'string', 'params' => array());
  259. $p->xmlNamespace = "http://foo.example.org";
  260. $this->assertEquals($p, $m->propertyMetadata['baz']);
  261. $p = new PropertyMetadata($m->name, 'qux');
  262. $p->type = array('name' => 'string', 'params' => array());
  263. $p->xmlNamespace = "http://new.foo.example.org";
  264. $this->assertEquals($p, $m->propertyMetadata['qux']);
  265. }
  266. /**
  267. * @return DriverInterface
  268. */
  269. abstract protected function getDriver();
  270. }