DoctrineDriverTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace JMS\Serializer\Tests\Metadata\Driver;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Doctrine\ORM\Configuration;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\Mapping\Driver\AnnotationDriver as DoctrineDriver;
  7. use JMS\Serializer\Metadata\Driver\AnnotationDriver;
  8. use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
  9. class DoctrineDriverTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public function getMetadata()
  12. {
  13. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Doctrine\BlogPost');
  14. $metadata = $this->getDoctrineDriver()->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\Doctrine\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\Doctrine\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 overriden it to integer
  47. $this->assertEquals(
  48. array('name' => 'integer', 'params' => array()),
  49. $metadata->propertyMetadata['published']->type
  50. );
  51. }
  52. public function testUnknownDoctrineTypeDoesNotResultInAGuess()
  53. {
  54. $metadata = $this->getMetadata();
  55. $this->assertNull($metadata->propertyMetadata['slug']->type);
  56. }
  57. public function testNonDoctrineEntityClassIsNotModified()
  58. {
  59. // Note: Using regular BlogPost fixture here instead of Doctrine fixture
  60. // because it has no Doctrine metadata.
  61. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');
  62. $plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass);
  63. $doctrineMetadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
  64. // Do not compare timestamps
  65. if (abs($doctrineMetadata->createdAt - $plainMetadata->createdAt) < 2) {
  66. $plainMetadata->createdAt = $doctrineMetadata->createdAt;
  67. }
  68. $this->assertEquals($plainMetadata, $doctrineMetadata);
  69. }
  70. public function testExcludePropertyNoPublicAccessorException()
  71. {
  72. $first = $this->getAnnotationDriver()
  73. ->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
  74. $this->assertArrayHasKey('id', $first->propertyMetadata);
  75. $this->assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
  76. }
  77. public function testVirtualPropertiesAreNotModified()
  78. {
  79. $doctrineMetadata = $this->getMetadata();
  80. $this->assertNull($doctrineMetadata->propertyMetadata['ref']->type);
  81. }
  82. public function testGuidPropertyIsGivenStringType()
  83. {
  84. $metadata = $this->getMetadata();
  85. $this->assertEquals(
  86. array('name' => 'string', 'params' => array()),
  87. $metadata->propertyMetadata['id']->type
  88. );
  89. }
  90. protected function getEntityManager()
  91. {
  92. $config = new Configuration();
  93. $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
  94. $config->setProxyNamespace('JMS\Tests\Proxies');
  95. $config->setMetadataDriverImpl(
  96. new DoctrineDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/Doctrine')
  97. );
  98. $conn = array(
  99. 'driver' => 'pdo_sqlite',
  100. 'memory' => true,
  101. );
  102. return EntityManager::create($conn, $config);
  103. }
  104. public function getAnnotationDriver()
  105. {
  106. return new AnnotationDriver(new AnnotationReader());
  107. }
  108. protected function getDoctrineDriver()
  109. {
  110. $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
  111. $registry->expects($this->atLeastOnce())
  112. ->method('getManagerForClass')
  113. ->will($this->returnValue($this->getEntityManager()));
  114. return new DoctrineTypeDriver(
  115. $this->getAnnotationDriver(),
  116. $registry
  117. );
  118. }
  119. }