AnnotationDriverTest.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace Doctrine\Tests\ORM\Mapping;
  3. use Doctrine\ORM\Mapping\ClassMetadata;
  4. use Doctrine\ORM\Events;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class AnnotationDriverTest extends AbstractMappingDriverTest
  7. {
  8. /**
  9. * @group DDC-268
  10. */
  11. public function testLoadMetadataForNonEntityThrowsException()
  12. {
  13. $cm = new ClassMetadata('stdClass');
  14. $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache());
  15. $annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
  16. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  17. $annotationDriver->loadMetadataForClass('stdClass', $cm);
  18. }
  19. /**
  20. * @group DDC-268
  21. */
  22. public function testColumnWithMissingTypeDefaultsToString()
  23. {
  24. $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\ColumnWithoutType');
  25. $annotationDriver = $this->_loadDriver();
  26. $annotationDriver->loadMetadataForClass('Doctrine\Tests\ORM\Mapping\InvalidColumn', $cm);
  27. $this->assertEquals('string', $cm->fieldMappings['id']['type']);
  28. }
  29. /**
  30. * @group DDC-318
  31. */
  32. public function testGetAllClassNamesIsIdempotent()
  33. {
  34. $annotationDriver = $this->_loadDriverForCMSModels();
  35. $original = $annotationDriver->getAllClassNames();
  36. $annotationDriver = $this->_loadDriverForCMSModels();
  37. $afterTestReset = $annotationDriver->getAllClassNames();
  38. $this->assertEquals($original, $afterTestReset);
  39. }
  40. /**
  41. * @group DDC-318
  42. */
  43. public function testGetAllClassNamesIsIdempotentEvenWithDifferentDriverInstances()
  44. {
  45. $annotationDriver = $this->_loadDriverForCMSModels();
  46. $original = $annotationDriver->getAllClassNames();
  47. $annotationDriver = $this->_loadDriverForCMSModels();
  48. $afterTestReset = $annotationDriver->getAllClassNames();
  49. $this->assertEquals($original, $afterTestReset);
  50. }
  51. /**
  52. * @group DDC-318
  53. */
  54. public function testGetAllClassNamesReturnsAlreadyLoadedClassesIfAppropriate()
  55. {
  56. $rightClassName = 'Doctrine\Tests\Models\CMS\CmsUser';
  57. $this->_ensureIsLoaded($rightClassName);
  58. $annotationDriver = $this->_loadDriverForCMSModels();
  59. $classes = $annotationDriver->getAllClassNames();
  60. $this->assertContains($rightClassName, $classes);
  61. }
  62. /**
  63. * @group DDC-318
  64. */
  65. public function testGetClassNamesReturnsOnlyTheAppropriateClasses()
  66. {
  67. $extraneousClassName = 'Doctrine\Tests\Models\ECommerce\ECommerceCart';
  68. $this->_ensureIsLoaded($extraneousClassName);
  69. $annotationDriver = $this->_loadDriverForCMSModels();
  70. $classes = $annotationDriver->getAllClassNames();
  71. $this->assertNotContains($extraneousClassName, $classes);
  72. }
  73. protected function _loadDriverForCMSModels()
  74. {
  75. $annotationDriver = $this->_loadDriver();
  76. $annotationDriver->addPaths(array(__DIR__ . '/../../Models/CMS/'));
  77. return $annotationDriver;
  78. }
  79. protected function _loadDriver()
  80. {
  81. return $this->createAnnotationDriver();
  82. }
  83. protected function _ensureIsLoaded($entityClassName)
  84. {
  85. new $entityClassName;
  86. }
  87. /**
  88. * @group DDC-671
  89. *
  90. * Entities for this test are in AbstractMappingDriverTest
  91. */
  92. public function testJoinTablesWithMappedSuperclassForAnnotationDriver()
  93. {
  94. $annotationDriver = $this->_loadDriver();
  95. $annotationDriver->addPaths(array(__DIR__ . '/../../Models/DirectoryTree/'));
  96. $em = $this->_getTestEntityManager();
  97. $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  98. $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
  99. $factory->setEntityManager($em);
  100. $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File');
  101. $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\File', $classPage->associationMappings['parentDirectory']['sourceEntity']);
  102. $classDirectory = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\Directory');
  103. $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
  104. }
  105. /**
  106. * @group DDC-945
  107. */
  108. public function testInvalidMappedSuperClassWithManyToManyAssociation()
  109. {
  110. $annotationDriver = $this->_loadDriver();
  111. $em = $this->_getTestEntityManager();
  112. $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  113. $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
  114. $factory->setEntityManager($em);
  115. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
  116. "It is illegal to put an inverse side one-to-many or many-to-many association on ".
  117. "mapped superclass 'Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass#users'");
  118. $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\UsingInvalidMappedSuperClass');
  119. }
  120. /**
  121. * @group DDC-1050
  122. */
  123. public function testInvalidMappedSuperClassWithInheritanceInformation()
  124. {
  125. $annotationDriver = $this->_loadDriver();
  126. $em = $this->_getTestEntityManager();
  127. $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  128. $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
  129. $factory->setEntityManager($em);
  130. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
  131. "Its not supported to define inheritance information on a mapped ".
  132. "superclass 'Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence'.");
  133. $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence');
  134. }
  135. /**
  136. * @group DDC-1034
  137. */
  138. public function testInheritanceSkipsParentLifecycleCallbacks()
  139. {
  140. $annotationDriver = $this->_loadDriver();
  141. $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\AnnotationChild');
  142. $em = $this->_getTestEntityManager();
  143. $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  144. $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
  145. $factory->setEntityManager($em);
  146. $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationChild');
  147. $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
  148. $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationParent');
  149. $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
  150. }
  151. /**
  152. * @group DDC-1156
  153. */
  154. public function testMappedSuperclassInMiddleOfInheritanceHierachy()
  155. {
  156. $annotationDriver = $this->_loadDriver();
  157. $em = $this->_getTestEntityManager();
  158. $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  159. $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
  160. $factory->setEntityManager($em);
  161. $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\ChildEntity');
  162. }
  163. }
  164. /**
  165. * @Entity
  166. */
  167. class ColumnWithoutType
  168. {
  169. /** @Id @Column */
  170. public $id;
  171. }
  172. /**
  173. * @MappedSuperclass
  174. */
  175. class InvalidMappedSuperClass
  176. {
  177. /**
  178. * @ManyToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", mappedBy="invalid")
  179. */
  180. private $users;
  181. }
  182. /**
  183. * @Entity
  184. */
  185. class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass
  186. {
  187. /**
  188. * @Id @Column(type="integer") @GeneratedValue
  189. */
  190. private $id;
  191. }
  192. /**
  193. * @MappedSuperclass
  194. * @InheritanceType("JOINED")
  195. * @DiscriminatorMap({"test" = "ColumnWithoutType"})
  196. */
  197. class MappedSuperClassInheritence
  198. {
  199. }
  200. /**
  201. * @Entity
  202. * @InheritanceType("JOINED")
  203. * @DiscriminatorMap({"parent" = "AnnotationParent", "child" = "AnnotationChild"})
  204. * @HasLifecycleCallbacks
  205. */
  206. class AnnotationParent
  207. {
  208. /**
  209. * @Id @Column(type="integer") @GeneratedValue
  210. */
  211. private $id;
  212. /**
  213. * @PostLoad
  214. */
  215. public function postLoad()
  216. {
  217. }
  218. /**
  219. * @PreUpdate
  220. */
  221. public function preUpdate()
  222. {
  223. }
  224. }
  225. /**
  226. * @Entity
  227. * @HasLifecycleCallbacks
  228. */
  229. class AnnotationChild extends AnnotationParent
  230. {
  231. }
  232. /**
  233. * @Entity
  234. * @InheritanceType("SINGLE_TABLE")
  235. * @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
  236. */
  237. class SuperEntity
  238. {
  239. /** @Id @Column(type="string") */
  240. private $id;
  241. }
  242. /**
  243. * @MappedSuperclass
  244. */
  245. class MiddleMappedSuperclass extends SuperEntity
  246. {
  247. /** @Column(type="string") */
  248. private $name;
  249. }
  250. /**
  251. * @Entity
  252. */
  253. class ChildEntity extends MiddleMappedSuperclass
  254. {
  255. /**
  256. * @Column(type="string")
  257. */
  258. private $text;
  259. }