BaseDriverTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. namespace JMS\Serializer\Tests\Metadata\Driver;
  3. use JMS\Serializer\GraphNavigator;
  4. use JMS\Serializer\Metadata\ClassMetadata;
  5. use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
  6. use JMS\Serializer\Metadata\PropertyMetadata;
  7. use JMS\Serializer\Metadata\VirtualPropertyMetadata;
  8. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorChild;
  9. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorParent;
  10. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlNamespaceAttributeDiscriminatorChild;
  11. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlNamespaceAttributeDiscriminatorParent;
  12. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlNamespaceDiscriminatorChild;
  13. use JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlNamespaceDiscriminatorParent;
  14. use JMS\Serializer\Tests\Fixtures\ParentSkipWithEmptyChild;
  15. use Metadata\Driver\DriverInterface;
  16. abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testLoadBlogPostMetadata()
  19. {
  20. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  21. $this->assertNotNull($m);
  22. $this->assertEquals('blog-post', $m->xmlRootName);
  23. $this->assertCount(4, $m->xmlNamespaces);
  24. $this->assertArrayHasKey('', $m->xmlNamespaces);
  25. $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
  26. $this->assertArrayHasKey('gd', $m->xmlNamespaces);
  27. $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
  28. $this->assertArrayHasKey('atom', $m->xmlNamespaces);
  29. $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
  30. $this->assertArrayHasKey('dc', $m->xmlNamespaces);
  31. $this->assertEquals('http://purl.org/dc/elements/1.1/', $m->xmlNamespaces['dc']);
  32. $p = new PropertyMetadata($m->name, 'id');
  33. $p->type = array('name' => 'string', 'params' => array());
  34. $p->groups = array("comments", "post");
  35. $p->xmlElementCData = false;
  36. $this->assertEquals($p, $m->propertyMetadata['id']);
  37. $p = new PropertyMetadata($m->name, 'title');
  38. $p->type = array('name' => 'string', 'params' => array());
  39. $p->groups = array("comments", "post");
  40. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  41. $this->assertEquals($p, $m->propertyMetadata['title']);
  42. $p = new PropertyMetadata($m->name, 'createdAt');
  43. $p->type = array('name' => 'DateTime', 'params' => array());
  44. $p->xmlAttribute = true;
  45. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  46. $p = new PropertyMetadata($m->name, 'published');
  47. $p->type = array('name' => 'boolean', 'params' => array());
  48. $p->serializedName = 'is_published';
  49. $p->xmlAttribute = true;
  50. $p->groups = array("post");
  51. $this->assertEquals($p, $m->propertyMetadata['published']);
  52. $p = new PropertyMetadata($m->name, 'etag');
  53. $p->type = array('name' => 'string', 'params' => array());
  54. $p->xmlAttribute = true;
  55. $p->groups = array("post");
  56. $p->xmlNamespace = "http://schemas.google.com/g/2005";
  57. $this->assertEquals($p, $m->propertyMetadata['etag']);
  58. $p = new PropertyMetadata($m->name, 'comments');
  59. $p->type = array('name' => 'ArrayCollection', 'params' => array(array('name' => 'JMS\Serializer\Tests\Fixtures\Comment', 'params' => array())));
  60. $p->xmlCollection = true;
  61. $p->xmlCollectionInline = true;
  62. $p->xmlEntryName = 'comment';
  63. $p->groups = array("comments");
  64. $this->assertEquals($p, $m->propertyMetadata['comments']);
  65. $p = new PropertyMetadata($m->name, 'author');
  66. $p->type = array('name' => 'JMS\Serializer\Tests\Fixtures\Author', 'params' => array());
  67. $p->groups = array("post");
  68. $p->xmlNamespace = 'http://www.w3.org/2005/Atom';
  69. $this->assertEquals($p, $m->propertyMetadata['author']);
  70. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Price'));
  71. $this->assertNotNull($m);
  72. $p = new PropertyMetadata($m->name, 'price');
  73. $p->type = array('name' => 'float', 'params' => array());
  74. $p->xmlValue = true;
  75. $this->assertEquals($p, $m->propertyMetadata['price']);
  76. }
  77. public function testXMLListAbsentNode()
  78. {
  79. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithAbsentXmlListNode'));
  80. $this->assertArrayHasKey('absent', $m->propertyMetadata);
  81. $this->assertArrayHasKey('present', $m->propertyMetadata);
  82. $this->assertArrayHasKey('skipDefault', $m->propertyMetadata);
  83. $this->assertTrue($m->propertyMetadata['absent']->xmlCollectionSkipWhenEmpty);
  84. $this->assertTrue($m->propertyMetadata['skipDefault']->xmlCollectionSkipWhenEmpty);
  85. $this->assertFalse($m->propertyMetadata['present']->xmlCollectionSkipWhenEmpty);
  86. }
  87. public function testVirtualProperty()
  88. {
  89. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithVirtualProperties'));
  90. $this->assertArrayHasKey('existField', $m->propertyMetadata);
  91. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  92. $this->assertArrayHasKey('virtualSerializedValue', $m->propertyMetadata);
  93. $this->assertArrayHasKey('typedVirtualProperty', $m->propertyMetadata);
  94. $this->assertEquals($m->propertyMetadata['virtualSerializedValue']->serializedName, 'test', 'Serialized name is missing');
  95. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  96. $p->getter = 'getVirtualValue';
  97. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  98. }
  99. public function testXmlKeyValuePairs()
  100. {
  101. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs'));
  102. $this->assertArrayHasKey('array', $m->propertyMetadata);
  103. $this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
  104. }
  105. public function testExpressionVirtualPropertyWithExcludeAll()
  106. {
  107. $a = new \JMS\Serializer\Tests\Fixtures\ObjectWithExpressionVirtualPropertiesAndExcludeAll();
  108. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));;
  109. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  110. $p = new ExpressionPropertyMetadata($m->name, 'virtualValue', 'object.getVirtualValue()');
  111. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  112. }
  113. public function testVirtualPropertyWithExcludeAll()
  114. {
  115. $a = new \JMS\Serializer\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
  116. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
  117. $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
  118. $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
  119. $p->getter = 'getVirtualValue';
  120. $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
  121. }
  122. public function testReadOnlyDefinedBeforeGetterAndSetter()
  123. {
  124. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\AuthorReadOnly'));
  125. $this->assertNotNull($m);
  126. }
  127. public function testExpressionVirtualProperty()
  128. {
  129. /** @var $m ClassMetadata */
  130. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess'));
  131. $keys = array_keys($m->propertyMetadata);
  132. $this->assertEquals(['firstName', 'lastName', 'id'], $keys);
  133. }
  134. public function testLoadDiscriminator()
  135. {
  136. /** @var $m ClassMetadata */
  137. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle'));
  138. $this->assertNotNull($m);
  139. $this->assertEquals('type', $m->discriminatorFieldName);
  140. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  141. $this->assertEquals(
  142. array(
  143. 'car' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Car',
  144. 'moped' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Moped',
  145. ),
  146. $m->discriminatorMap
  147. );
  148. }
  149. public function testLoadDiscriminatorWhenParentIsInDiscriminatorMap()
  150. {
  151. /** @var ClassMetadata $m */
  152. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Post'));
  153. self::assertNotNull($m);
  154. self::assertEquals('type', $m->discriminatorFieldName);
  155. self::assertEquals($m->name, $m->discriminatorBaseClass);
  156. self::assertEquals(
  157. [
  158. 'post' => 'JMS\Serializer\Tests\Fixtures\Discriminator\Post',
  159. 'image_post' => 'JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost',
  160. ],
  161. $m->discriminatorMap
  162. );
  163. }
  164. public function testLoadXmlDiscriminator()
  165. {
  166. /** @var $m ClassMetadata */
  167. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(ObjectWithXmlAttributeDiscriminatorParent::class));
  168. $this->assertNotNull($m);
  169. $this->assertEquals('type', $m->discriminatorFieldName);
  170. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  171. $this->assertEquals(
  172. array(
  173. 'child' => ObjectWithXmlAttributeDiscriminatorChild::class,
  174. ),
  175. $m->discriminatorMap
  176. );
  177. $this->assertTrue($m->xmlDiscriminatorAttribute);
  178. $this->assertFalse($m->xmlDiscriminatorCData);
  179. }
  180. public function testLoadXmlDiscriminatorWithNamespaces()
  181. {
  182. /** @var $m ClassMetadata */
  183. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(ObjectWithXmlNamespaceDiscriminatorParent::class));
  184. $this->assertNotNull($m);
  185. $this->assertEquals('type', $m->discriminatorFieldName);
  186. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  187. $this->assertEquals(
  188. array(
  189. 'child' => ObjectWithXmlNamespaceDiscriminatorChild::class,
  190. ),
  191. $m->discriminatorMap
  192. );
  193. $this->assertEquals('http://example.com/', $m->xmlDiscriminatorNamespace);
  194. $this->assertFalse($m->xmlDiscriminatorAttribute);
  195. }
  196. public function testLoadXmlDiscriminatorWithAttributeNamespaces()
  197. {
  198. /** @var $m ClassMetadata */
  199. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(ObjectWithXmlNamespaceAttributeDiscriminatorParent::class));
  200. $this->assertNotNull($m);
  201. $this->assertEquals('type', $m->discriminatorFieldName);
  202. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  203. $this->assertEquals(
  204. array(
  205. 'child' => ObjectWithXmlNamespaceAttributeDiscriminatorChild::class,
  206. ),
  207. $m->discriminatorMap
  208. );
  209. $this->assertEquals('http://example.com/', $m->xmlDiscriminatorNamespace);
  210. $this->assertTrue($m->xmlDiscriminatorAttribute);
  211. }
  212. public function testLoadDiscriminatorWithGroup()
  213. {
  214. /** @var $m ClassMetadata */
  215. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\DiscriminatorGroup\Vehicle'));
  216. $this->assertNotNull($m);
  217. $this->assertEquals('type', $m->discriminatorFieldName);
  218. $this->assertEquals(array('foo'), $m->discriminatorGroups);
  219. $this->assertEquals($m->name, $m->discriminatorBaseClass);
  220. $this->assertEquals(
  221. array(
  222. 'car' => 'JMS\Serializer\Tests\Fixtures\DiscriminatorGroup\Car'
  223. ),
  224. $m->discriminatorMap
  225. );
  226. }
  227. public function testSkipWhenEmptyOption()
  228. {
  229. /** @var $m ClassMetadata */
  230. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(ParentSkipWithEmptyChild::class));
  231. $this->assertNotNull($m);
  232. $this->assertInstanceOf(PropertyMetadata::class, $m->propertyMetadata['c']);
  233. $this->assertInstanceOf(PropertyMetadata::class, $m->propertyMetadata['d']);
  234. $this->assertInstanceOf(PropertyMetadata::class, $m->propertyMetadata['child']);
  235. $this->assertFalse($m->propertyMetadata['c']->skipWhenEmpty);
  236. $this->assertFalse($m->propertyMetadata['d']->skipWhenEmpty);
  237. $this->assertTrue($m->propertyMetadata['child']->skipWhenEmpty);
  238. }
  239. public function testLoadDiscriminatorSubClass()
  240. {
  241. /** @var $m ClassMetadata */
  242. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\Car'));
  243. $this->assertNotNull($m);
  244. $this->assertNull($m->discriminatorValue);
  245. $this->assertNull($m->discriminatorBaseClass);
  246. $this->assertNull($m->discriminatorFieldName);
  247. $this->assertEquals(array(), $m->discriminatorMap);
  248. }
  249. public function testLoadDiscriminatorSubClassWhenParentIsInDiscriminatorMap()
  250. {
  251. /** @var ClassMetadata $m */
  252. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost'));
  253. self::assertNotNull($m);
  254. self::assertNull($m->discriminatorValue);
  255. self::assertNull($m->discriminatorBaseClass);
  256. self::assertNull($m->discriminatorFieldName);
  257. self::assertEquals([], $m->discriminatorMap);
  258. }
  259. public function testLoadXmlObjectWithNamespacesMetadata()
  260. {
  261. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces'));
  262. $this->assertNotNull($m);
  263. $this->assertEquals('test-object', $m->xmlRootName);
  264. $this->assertEquals('http://example.com/namespace', $m->xmlRootNamespace);
  265. $this->assertCount(3, $m->xmlNamespaces);
  266. $this->assertArrayHasKey('', $m->xmlNamespaces);
  267. $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
  268. $this->assertArrayHasKey('gd', $m->xmlNamespaces);
  269. $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
  270. $this->assertArrayHasKey('atom', $m->xmlNamespaces);
  271. $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
  272. $p = new PropertyMetadata($m->name, 'title');
  273. $p->type = array('name' => 'string', 'params' => array());
  274. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  275. $this->assertEquals($p, $m->propertyMetadata['title']);
  276. $p = new PropertyMetadata($m->name, 'createdAt');
  277. $p->type = array('name' => 'DateTime', 'params' => array());
  278. $p->xmlAttribute = true;
  279. $this->assertEquals($p, $m->propertyMetadata['createdAt']);
  280. $p = new PropertyMetadata($m->name, 'etag');
  281. $p->type = array('name' => 'string', 'params' => array());
  282. $p->xmlAttribute = true;
  283. $p->xmlNamespace = "http://schemas.google.com/g/2005";
  284. $this->assertEquals($p, $m->propertyMetadata['etag']);
  285. $p = new PropertyMetadata($m->name, 'author');
  286. $p->type = array('name' => 'string', 'params' => array());
  287. $p->xmlAttribute = false;
  288. $p->xmlNamespace = "http://www.w3.org/2005/Atom";
  289. $this->assertEquals($p, $m->propertyMetadata['author']);
  290. $p = new PropertyMetadata($m->name, 'language');
  291. $p->type = array('name' => 'string', 'params' => array());
  292. $p->xmlAttribute = true;
  293. $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
  294. $this->assertEquals($p, $m->propertyMetadata['language']);
  295. }
  296. public function testMaxDepth()
  297. {
  298. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Node'));
  299. $this->assertEquals(2, $m->propertyMetadata['children']->maxDepth);
  300. }
  301. public function testPersonCData()
  302. {
  303. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
  304. $this->assertNotNull($m);
  305. $this->assertFalse($m->propertyMetadata['name']->xmlElementCData);
  306. }
  307. public function testXmlNamespaceInheritanceMetadata()
  308. {
  309. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleClassObject'));
  310. $this->assertNotNull($m);
  311. $this->assertCount(3, $m->xmlNamespaces);
  312. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  313. $this->assertEquals('http://old.foo.example.org', $m->xmlNamespaces['old_foo']);
  314. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  315. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['foo']);
  316. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  317. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  318. $this->assertCount(3, $m->propertyMetadata);
  319. $p = new PropertyMetadata($m->name, 'foo');
  320. $p->type = array('name' => 'string', 'params' => array());
  321. $p->xmlNamespace = "http://old.foo.example.org";
  322. $p->xmlAttribute = true;
  323. $this->assertEquals($p, $m->propertyMetadata['foo']);
  324. $p = new PropertyMetadata($m->name, 'bar');
  325. $p->type = array('name' => 'string', 'params' => array());
  326. $p->xmlNamespace = "http://foo.example.org";
  327. $this->assertEquals($p, $m->propertyMetadata['bar']);
  328. $p = new PropertyMetadata($m->name, 'moo');
  329. $p->type = array('name' => 'string', 'params' => array());
  330. $p->xmlNamespace = "http://new.foo.example.org";
  331. $this->assertEquals($p, $m->propertyMetadata['moo']);
  332. $subm = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\SimpleSubClassObject'));
  333. $this->assertNotNull($subm);
  334. $this->assertCount(2, $subm->xmlNamespaces);
  335. $this->assertArrayHasKey('old_foo', $subm->xmlNamespaces);
  336. $this->assertEquals('http://foo.example.org', $subm->xmlNamespaces['old_foo']);
  337. $this->assertArrayHasKey('foo', $subm->xmlNamespaces);
  338. $this->assertEquals('http://better.foo.example.org', $subm->xmlNamespaces['foo']);
  339. $this->assertCount(3, $subm->propertyMetadata);
  340. $p = new PropertyMetadata($subm->name, 'moo');
  341. $p->type = array('name' => 'string', 'params' => array());
  342. $p->xmlNamespace = "http://better.foo.example.org";
  343. $this->assertEquals($p, $subm->propertyMetadata['moo']);
  344. $p = new PropertyMetadata($subm->name, 'baz');
  345. $p->type = array('name' => 'string', 'params' => array());
  346. $p->xmlNamespace = "http://foo.example.org";
  347. $this->assertEquals($p, $subm->propertyMetadata['baz']);
  348. $p = new PropertyMetadata($subm->name, 'qux');
  349. $p->type = array('name' => 'string', 'params' => array());
  350. $p->xmlNamespace = "http://new.foo.example.org";
  351. $this->assertEquals($p, $subm->propertyMetadata['qux']);
  352. $m->merge($subm);
  353. $this->assertNotNull($m);
  354. $this->assertCount(3, $m->xmlNamespaces);
  355. $this->assertArrayHasKey('old_foo', $m->xmlNamespaces);
  356. $this->assertEquals('http://foo.example.org', $m->xmlNamespaces['old_foo']);
  357. $this->assertArrayHasKey('foo', $m->xmlNamespaces);
  358. $this->assertEquals('http://better.foo.example.org', $m->xmlNamespaces['foo']);
  359. $this->assertArrayHasKey('new_foo', $m->xmlNamespaces);
  360. $this->assertEquals('http://new.foo.example.org', $m->xmlNamespaces['new_foo']);
  361. $this->assertCount(5, $m->propertyMetadata);
  362. $p = new PropertyMetadata($m->name, 'foo');
  363. $p->type = array('name' => 'string', 'params' => array());
  364. $p->xmlNamespace = "http://old.foo.example.org";
  365. $p->xmlAttribute = true;
  366. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  367. $this->assetMetadataEquals($p, $m->propertyMetadata['foo']);
  368. $p = new PropertyMetadata($m->name, 'bar');
  369. $p->type = array('name' => 'string', 'params' => array());
  370. $p->xmlNamespace = "http://foo.example.org";
  371. $p->class = 'JMS\Serializer\Tests\Fixtures\SimpleClassObject';
  372. $this->assetMetadataEquals($p, $m->propertyMetadata['bar']);
  373. $p = new PropertyMetadata($m->name, 'moo');
  374. $p->type = array('name' => 'string', 'params' => array());
  375. $p->xmlNamespace = "http://better.foo.example.org";
  376. $this->assetMetadataEquals($p, $m->propertyMetadata['moo']);
  377. $p = new PropertyMetadata($m->name, 'baz');
  378. $p->type = array('name' => 'string', 'params' => array());
  379. $p->xmlNamespace = "http://foo.example.org";
  380. $this->assetMetadataEquals($p, $m->propertyMetadata['baz']);
  381. $p = new PropertyMetadata($m->name, 'qux');
  382. $p->type = array('name' => 'string', 'params' => array());
  383. $p->xmlNamespace = "http://new.foo.example.org";
  384. $this->assetMetadataEquals($p, $m->propertyMetadata['qux']);
  385. }
  386. private function assetMetadataEquals(PropertyMetadata $expected, PropertyMetadata $actual)
  387. {
  388. $expectedVars = get_object_vars($expected);
  389. $actualVars = get_object_vars($actual);
  390. $expectedReflection = (array)$expectedVars['reflection'];
  391. $actualReflection = (array)$actualVars['reflection'];
  392. // HHVM bug with class property
  393. unset($expectedVars['reflection'], $actualVars['reflection']);
  394. $this->assertEquals($expectedVars, $actualVars);
  395. // HHVM bug with class property
  396. if (isset($expectedReflection['info']) || isset($actualReflection['info'])) {
  397. $expectedReflection['class'] = $expectedReflection['info']['class'];
  398. $actualReflection['class'] = $actualReflection['info']['class'];
  399. }
  400. $this->assertEquals($expectedReflection, $actualReflection);
  401. }
  402. public function testHandlerCallbacks()
  403. {
  404. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks'));
  405. $this->assertEquals('toJson', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['json']);
  406. $this->assertEquals('toXml', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['xml']);
  407. }
  408. public function testExclusionIf()
  409. {
  410. $class = 'JMS\Serializer\Tests\Fixtures\PersonSecret';
  411. $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($class));
  412. $p = new PropertyMetadata($class, 'name');
  413. $p->type = array('name' => 'string', 'params' => array());
  414. $this->assertEquals($p, $m->propertyMetadata['name']);
  415. $p = new PropertyMetadata($class, 'gender');
  416. $p->type = array('name' => 'string', 'params' => array());
  417. $p->excludeIf = "show_data('gender')";
  418. $this->assertEquals($p, $m->propertyMetadata['gender']);
  419. $p = new PropertyMetadata($class, 'age');
  420. $p->type = array('name' => 'string', 'params' => array());
  421. $p->excludeIf = "!(show_data('age'))";
  422. $this->assertEquals($p, $m->propertyMetadata['age']);
  423. }
  424. public function testExcludePropertyNoPublicAccessorException()
  425. {
  426. $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
  427. if ($this instanceof PhpDriverTest) {
  428. return;
  429. }
  430. $this->assertArrayHasKey('id', $first->propertyMetadata);
  431. $this->assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
  432. }
  433. /**
  434. * @return DriverInterface
  435. */
  436. abstract protected function getDriver();
  437. }