DoctrineDriverTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $this->assertEquals($plainMetadata, $doctrineMetadata);
  80. }
  81. protected function getEntityManager()
  82. {
  83. $config = new Configuration();
  84. $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
  85. $config->setProxyNamespace('JMS\Tests\Proxies');
  86. $config->setMetadataDriverImpl(
  87. new DoctrineDriver(new AnnotationReader(), __DIR__.'/../../Fixtures/Doctrine')
  88. );
  89. $conn = array(
  90. 'driver' => 'pdo_sqlite',
  91. 'memory' => true,
  92. );
  93. return EntityManager::create($conn, $config);
  94. }
  95. public function getAnnotationDriver()
  96. {
  97. return new AnnotationDriver(new AnnotationReader());
  98. }
  99. protected function getDoctrineDriver()
  100. {
  101. $registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
  102. $registry->expects($this->atLeastOnce())
  103. ->method('getManagerForClass')
  104. ->will($this->returnValue($this->getEntityManager()));
  105. return new DoctrineTypeDriver(
  106. $this->getAnnotationDriver(),
  107. $registry
  108. );
  109. }
  110. }