BaseDriverTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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->assertEquals('http://example.com/namespace', $m->xmlRootNamespace);
  146. $this->assertCount(3, $m->xmlNamespaces);
  147. $this->assertArrayHasKey('', $m->xmlNamespaces);
  148. $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
  149. $this->assertArrayHasKey('gd', $m->xmlNamespaces);
  150. $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
  151. $this->assertArrayHasKey('atom', $m->xmlNamespaces);
  152. $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
  153. $p = new PropertyMetadata($m->name, 'title');
  154. $p->type = array('name' => 'string', 'params' => array());
  155. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  156. $this->assertEquals($p, $m->propertyMetadata['title']);
  157. $p = new PropertyMetadata($m->name, 'createdAt');
  158. $p->type = array('name' => 'DateTime', 'params' => array());
  159. $p->xmlAttribute = true;
  160. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  161. $p = new PropertyMetadata($m->name, 'etag');
  162. $p->type = array('name' => 'string', 'params' => array());
  163. $p->xmlAttribute = true;
  164. $p->xmlNamespace = "http://schemas.google.com/g/2005";
  165. $this->assertEquals($p, $m->propertyMetadata['etag']);
  166. $p = new PropertyMetadata($m->name, 'author');
  167. $p->type = array('name' => 'string', 'params' => array());
  168. $p->xmlAttribute = false;
  169. $p->xmlNamespace = "http://www.w3.org/2005/Atom";
  170. $this->assertEquals($p, $m->propertyMetadata['author']);
  171. $p = new PropertyMetadata($m->name, 'language');
  172. $p->type = array('name' => 'string', 'params' => array());
  173. $p->xmlAttribute = true;
  174. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  175. $this->assertEquals($p, $m->propertyMetadata['language']);
  176. }
  177. public function testMaxDepth()
  178. {
  179. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Node'));
  180. $this->assertEquals(2, $m->propertyMetadata['children']->maxDepth);
  181. }
  182. public function testPersonCData()
  183. {
  184. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
  185. $this->assertNotNull($m);
  186. $this->assertFalse($m->propertyMetadata['name']->xmlElementCData);
  187. }
  188. public function testXmlNamespaceInheritanceMetadata()
  189. {
  190. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleClassObject'));
  191. $this->assertNotNull($m);
  192. $this->assertCount(3, $m->xmlNamespaces);
  193. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  194. $this->assertEquals('http://old.foo.example.org', $m->xmlNamespaces['old_foo']);
  195. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  196. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['foo']);
  197. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  198. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  199. $this->assertCount(3, $m->propertyMetadata);
  200. $p = new PropertyMetadata($m->name, 'foo');
  201. $p->type = array('name' => 'string', 'params' => array());
  202. $p->xmlNamespace = "http://old.foo.example.org";
  203. $p->xmlAttribute = true;
  204. $this->assertEquals($p, $m->propertyMetadata['foo']);
  205. $p = new PropertyMetadata($m->name, 'bar');
  206. $p->type = array('name' => 'string', 'params' => array());
  207. $p->xmlNamespace = "http://foo.example.org";
  208. $this->assertEquals($p, $m->propertyMetadata['bar']);
  209. $p = new PropertyMetadata($m->name, 'moo');
  210. $p->type = array('name' => 'string', 'params' => array());
  211. $p->xmlNamespace = "http://new.foo.example.org";
  212. $this->assertEquals($p, $m->propertyMetadata['moo']);
  213. $subm = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleSubClassObject'));
  214. $this->assertNotNull($subm);
  215. $this->assertCount(2, $subm->xmlNamespaces);
  216. $this->assertArrayHasKey('old_foo', $subm->xmlNamespaces);
  217. $this->assertEquals('http://foo.example.org', $subm->xmlNamespaces['old_foo']);
  218. $this->assertArrayHasKey('foo', $subm->xmlNamespaces);
  219. $this->assertEquals('http://better.foo.example.org', $subm->xmlNamespaces['foo']);
  220. $this->assertCount(3, $subm->propertyMetadata);
  221. $p = new PropertyMetadata($subm->name, 'moo');
  222. $p->type = array('name' => 'string', 'params' => array());
  223. $p->xmlNamespace = "http://better.foo.example.org";
  224. $this->assertEquals($p, $subm->propertyMetadata['moo']);
  225. $p = new PropertyMetadata($subm->name, 'baz');
  226. $p->type = array('name' => 'string', 'params' => array());
  227. $p->xmlNamespace = "http://foo.example.org";
  228. $this->assertEquals($p, $subm->propertyMetadata['baz']);
  229. $p = new PropertyMetadata($subm->name, 'qux');
  230. $p->type = array('name' => 'string', 'params' => array());
  231. $p->xmlNamespace = "http://new.foo.example.org";
  232. $this->assertEquals($p, $subm->propertyMetadata['qux']);
  233. $m->merge($subm);
  234. $this->assertNotNull($m);
  235. $this->assertCount(3, $m->xmlNamespaces);
  236. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  237. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['old_foo']);
  238. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  239. $this->assertEquals('http://better.foo.example.org', $m->xmlNamespaces['foo']);
  240. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  241. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  242. $this->assertCount(5, $m->propertyMetadata);
  243. $p = new PropertyMetadata($m->name, 'foo');
  244. $p->type = array('name' => 'string', 'params' => array());
  245. $p->xmlNamespace = "http://old.foo.example.org";
  246. $p->xmlAttribute = true;
  247. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  248. $this->assertEquals($p, $m->propertyMetadata['foo']);
  249. $p = new PropertyMetadata($m->name, 'bar');
  250. $p->type = array('name' => 'string', 'params' => array());
  251. $p->xmlNamespace = "http://foo.example.org";
  252. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  253. $this->assertEquals($p, $m->propertyMetadata['bar']);
  254. $p = new PropertyMetadata($m->name, 'moo');
  255. $p->type = array('name' => 'string', 'params' => array());
  256. $p->xmlNamespace = "http://better.foo.example.org";
  257. $this->assertEquals($p, $m->propertyMetadata['moo']);
  258. $p = new PropertyMetadata($m->name, 'baz');
  259. $p->type = array('name' => 'string', 'params' => array());
  260. $p->xmlNamespace = "http://foo.example.org";
  261. $this->assertEquals($p, $m->propertyMetadata['baz']);
  262. $p = new PropertyMetadata($m->name, 'qux');
  263. $p->type = array('name' => 'string', 'params' => array());
  264. $p->xmlNamespace = "http://new.foo.example.org";
  265. $this->assertEquals($p, $m->propertyMetadata['qux']);
  266. }
  267. /**
  268. * @return DriverInterface
  269. */
  270. abstract protected function getDriver();
  271. }