DoctrineDriverTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Driver\AnnotationDriver;
  19. use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
  20. use Doctrine\Common\Annotations\AnnotationReader;
  21. use Doctrine\ORM\Configuration;
  22. use Doctrine\ORM\EntityManager;
  23. use Doctrine\ORM\Mapping\Driver\AnnotationDriver as DoctrineDriver;
  24. class DoctrineDriverTest extends \PHPUnit_Framework_TestCase
  25. {
  26. public function getMetadata()
  27. {
  28. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Doctrine\BlogPost');
  29. $metadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
  30. return $metadata;
  31. }
  32. public function testTypelessPropertyIsGivenTypeFromDoctrineMetadata()
  33. {
  34. $metadata = $this->getMetadata();
  35. $this->assertEquals(
  36. array('name'=> 'DateTime', 'params' => array()),
  37. $metadata->propertyMetadata['createdAt']->type
  38. );
  39. }
  40. public function testSingleValuedAssociationIsProperlyHinted()
  41. {
  42. $metadata = $this->getMetadata();
  43. $this->assertEquals(
  44. array('name'=> 'JMS\Serializer\Tests\Fixtures\Doctrine\Author', 'params' => array()),
  45. $metadata->propertyMetadata['author']->type
  46. );
  47. }
  48. public function testMultiValuedAssociationIsProperlyHinted()
  49. {
  50. $metadata = $this->getMetadata();
  51. $this->assertEquals(
  52. array('name'=> 'ArrayCollection', 'params' => array(
  53. array('name' => 'JMS\Serializer\Tests\Fixtures\Doctrine\Comment', 'params' => array()))
  54. ),
  55. $metadata->propertyMetadata['comments']->type
  56. );
  57. }
  58. public function testTypeGuessByDoctrineIsOverwrittenByDelegateDriver()
  59. {
  60. $metadata = $this->getMetadata();
  61. // This would be guessed as boolean but we've overriden it to integer
  62. $this->assertEquals(
  63. array('name'=> 'integer', 'params' => array()),
  64. $metadata->propertyMetadata['published']->type
  65. );
  66. }
  67. public function testUnknownDoctrineTypeDoesNotResultInAGuess()
  68. {
  69. $metadata = $this->getMetadata();
  70. $this->assertNull($metadata->propertyMetadata['slug']->type);
  71. }
  72. public function testNonDoctrineEntityClassIsNotModified()
  73. {
  74. // Note: Using regular BlogPost fixture here instead of Doctrine fixture
  75. // because it has no Doctrine metadata.
  76. $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');
  77. $plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass);
  78. $doctrineMetadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
  79. // Do not compare timestamps
  80. if (abs($doctrineMetadata->createdAt - $plainMetadata->createdAt) < 2) {
  81. $plainMetadata->createdAt = $doctrineMetadata->createdAt;
  82. }
  83. $this->assertEquals($plainMetadata, $doctrineMetadata);
  84. }
  85. protected function getEntityManager()
  86. {
  87. $config = new Configuration();
  88. $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
  89. $config->setProxyNamespace('JMS\Tests\Proxies');
  90. $config->setMetadataDriverImpl(
  91. new DoctrineDriver(new AnnotationReader(), __DIR__.'/../../Fixtures/Doctrine')
  92. );
  93. $conn = array(
  94. 'driver' => 'pdo_sqlite',
  95. 'memory' => true,
  96. );
  97. return EntityManager::create($conn, $config);
  98. }
  99. public function getAnnotationDriver()
  100. {
  101. return new AnnotationDriver(new AnnotationReader());
  102. }
  103. protected function getDoctrineDriver()
  104. {
  105. $registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
  106. $registry->expects($this->atLeastOnce())
  107. ->method('getManagerForClass')
  108. ->will($this->returnValue($this->getEntityManager()));
  109. return new DoctrineTypeDriver(
  110. $this->getAnnotationDriver(),
  111. $registry
  112. );
  113. }
  114. }