PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php

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