DoctrinePHPCRDriverTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace JMS\Serializer\Tests\Metadata\Driver;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Doctrine\ODM\PHPCR\Configuration;
  5. use Doctrine\ODM\PHPCR\DocumentManager;
  6. use Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver as DoctrinePHPCRDriver;
  7. use JMS\Serializer\Metadata\Driver\AnnotationDriver;
  8. use JMS\Serializer\Metadata\Driver\DoctrinePHPCRTypeDriver;
  9. class DoctrinePHPCRDriverTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public function getMetadata()
  12. {
  13. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\BlogPost');
  14. $metadata = $this->getDoctrinePHPCRDriver()->loadMetadataForClass($refClass);
  15. return $metadata;
  16. }
  17. public function testTypelessPropertyIsGivenTypeFromDoctrineMetadata()
  18. {
  19. $metadata = $this->getMetadata();
  20. $this->assertEquals(
  21. array('name' => 'DateTime', 'params' => array()),
  22. $metadata->propertyMetadata['createdAt']->type
  23. );
  24. }
  25. public function testSingleValuedAssociationIsProperlyHinted()
  26. {
  27. $metadata = $this->getMetadata();
  28. $this->assertEquals(
  29. array('name' => 'JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Author', 'params' => array()),
  30. $metadata->propertyMetadata['author']->type
  31. );
  32. }
  33. public function testMultiValuedAssociationIsProperlyHinted()
  34. {
  35. $metadata = $this->getMetadata();
  36. $this->assertEquals(
  37. array('name' => 'ArrayCollection', 'params' => array(
  38. array('name' => 'JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Comment', 'params' => array()))
  39. ),
  40. $metadata->propertyMetadata['comments']->type
  41. );
  42. }
  43. public function testTypeGuessByDoctrineIsOverwrittenByDelegateDriver()
  44. {
  45. $metadata = $this->getMetadata();
  46. // This would be guessed as boolean but we've overridden it to integer
  47. $this->assertEquals(
  48. array('name' => 'integer', 'params' => array()),
  49. $metadata->propertyMetadata['published']->type
  50. );
  51. }
  52. public function testNonDoctrineDocumentClassIsNotModified()
  53. {
  54. // Note: Using regular BlogPost fixture here instead of Doctrine fixture
  55. // because it has no Doctrine metadata.
  56. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');
  57. $plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass);
  58. $doctrineMetadata = $this->getDoctrinePHPCRDriver()->loadMetadataForClass($refClass);
  59. // Do not compare timestamps
  60. if (abs($doctrineMetadata->createdAt - $plainMetadata->createdAt) < 2) {
  61. $plainMetadata->createdAt = $doctrineMetadata->createdAt;
  62. }
  63. $this->assertEquals($plainMetadata, $doctrineMetadata);
  64. }
  65. protected function getDocumentManager()
  66. {
  67. $config = new Configuration();
  68. $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
  69. $config->setProxyNamespace('JMS\Tests\Proxies');
  70. $config->setMetadataDriverImpl(
  71. new DoctrinePHPCRDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/DoctrinePHPCR')
  72. );
  73. $session = $this->getMockBuilder('PHPCR\SessionInterface')->getMock();
  74. return DocumentManager::create($session, $config);
  75. }
  76. public function getAnnotationDriver()
  77. {
  78. return new AnnotationDriver(new AnnotationReader());
  79. }
  80. protected function getDoctrinePHPCRDriver()
  81. {
  82. $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
  83. $registry->expects($this->atLeastOnce())
  84. ->method('getManagerForClass')
  85. ->will($this->returnValue($this->getDocumentManager()));
  86. return new DoctrinePHPCRTypeDriver(
  87. $this->getAnnotationDriver(),
  88. $registry
  89. );
  90. }
  91. }