AbstractMappingDriverTest.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. namespace Doctrine\Tests\ORM\Mapping;
  3. use Doctrine\ORM\Mapping\ClassMetadata,
  4. Doctrine\ORM\Mapping\ClassMetadataInfo,
  5. Doctrine\ORM\Mapping\Driver\XmlDriver,
  6. Doctrine\ORM\Mapping\Driver\YamlDriver;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
  9. {
  10. abstract protected function _loadDriver();
  11. public function createClassMetadata($entityClassName)
  12. {
  13. $mappingDriver = $this->_loadDriver();
  14. $class = new ClassMetadata($entityClassName);
  15. $mappingDriver->loadMetadataForClass($entityClassName, $class);
  16. return $class;
  17. }
  18. public function testLoadMapping()
  19. {
  20. $entityClassName = 'Doctrine\Tests\ORM\Mapping\User';
  21. return $this->createClassMetadata($entityClassName);
  22. }
  23. /**
  24. * @depends testLoadMapping
  25. * @param ClassMetadata $class
  26. */
  27. public function testEntityTableNameAndInheritance($class)
  28. {
  29. $this->assertEquals('cms_users', $class->getTableName());
  30. $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->inheritanceType);
  31. return $class;
  32. }
  33. /**
  34. * @depends testEntityTableNameAndInheritance
  35. * @param ClassMetadata $class
  36. */
  37. public function testEntityIndexes($class)
  38. {
  39. $this->assertArrayHasKey('indexes', $class->table, 'ClassMetadata should have indexes key in table property.');
  40. $this->assertEquals(array(
  41. 'name_idx' => array('columns' => array('name')),
  42. 0 => array('columns' => array('user_email'))
  43. ), $class->table['indexes']);
  44. return $class;
  45. }
  46. /**
  47. * @depends testEntityTableNameAndInheritance
  48. * @param ClassMetadata $class
  49. */
  50. public function testEntityUniqueConstraints($class)
  51. {
  52. $this->assertArrayHasKey('uniqueConstraints', $class->table,
  53. 'ClassMetadata should have uniqueConstraints key in table property when Unique Constraints are set.');
  54. $this->assertEquals(array(
  55. "search_idx" => array("columns" => array("name", "user_email"))
  56. ), $class->table['uniqueConstraints']);
  57. return $class;
  58. }
  59. /**
  60. * @depends testEntityTableNameAndInheritance
  61. * @param ClassMetadata $class
  62. */
  63. public function testEntitySequence($class)
  64. {
  65. $this->assertInternalType('array', $class->sequenceGeneratorDefinition, 'No Sequence Definition set on this driver.');
  66. $this->assertEquals(
  67. array(
  68. 'sequenceName' => 'tablename_seq',
  69. 'allocationSize' => 100,
  70. 'initialValue' => 1,
  71. ),
  72. $class->sequenceGeneratorDefinition
  73. );
  74. }
  75. /**
  76. * @depends testEntityTableNameAndInheritance
  77. * @param ClassMetadata $class
  78. */
  79. public function testFieldMappings($class)
  80. {
  81. $this->assertEquals(3, count($class->fieldMappings));
  82. $this->assertTrue(isset($class->fieldMappings['id']));
  83. $this->assertTrue(isset($class->fieldMappings['name']));
  84. $this->assertTrue(isset($class->fieldMappings['email']));
  85. return $class;
  86. }
  87. /**
  88. * @depends testEntityTableNameAndInheritance
  89. * @param ClassMetadata $class
  90. */
  91. public function testFieldMappingsColumnNames($class)
  92. {
  93. $this->assertEquals("id", $class->fieldMappings['id']['columnName']);
  94. $this->assertEquals("name", $class->fieldMappings['name']['columnName']);
  95. $this->assertEquals("user_email", $class->fieldMappings['email']['columnName']);
  96. return $class;
  97. }
  98. /**
  99. * @depends testEntityTableNameAndInheritance
  100. * @param ClassMetadata $class
  101. */
  102. public function testStringFieldMappings($class)
  103. {
  104. $this->assertEquals('string', $class->fieldMappings['name']['type']);
  105. $this->assertEquals(50, $class->fieldMappings['name']['length']);
  106. $this->assertTrue($class->fieldMappings['name']['nullable']);
  107. $this->assertTrue($class->fieldMappings['name']['unique']);
  108. return $class;
  109. }
  110. /**
  111. * @depends testFieldMappings
  112. * @param ClassMetadata $class
  113. */
  114. public function testIdentifier($class)
  115. {
  116. $this->assertEquals(array('id'), $class->identifier);
  117. $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->generatorType, "ID-Generator is not ClassMetadata::GENERATOR_TYPE_AUTO");
  118. return $class;
  119. }
  120. /**
  121. * @depends testIdentifier
  122. * @param ClassMetadata $class
  123. */
  124. public function testAssocations($class)
  125. {
  126. $this->assertEquals(3, count($class->associationMappings));
  127. return $class;
  128. }
  129. /**
  130. * @depends testAssocations
  131. * @param ClassMetadata $class
  132. */
  133. public function testOwningOneToOneAssocation($class)
  134. {
  135. $this->assertTrue(isset($class->associationMappings['address']));
  136. $this->assertTrue($class->associationMappings['address']['isOwningSide']);
  137. $this->assertEquals('user', $class->associationMappings['address']['inversedBy']);
  138. // Check cascading
  139. $this->assertTrue($class->associationMappings['address']['isCascadeRemove']);
  140. $this->assertFalse($class->associationMappings['address']['isCascadePersist']);
  141. $this->assertFalse($class->associationMappings['address']['isCascadeRefresh']);
  142. $this->assertFalse($class->associationMappings['address']['isCascadeDetach']);
  143. $this->assertFalse($class->associationMappings['address']['isCascadeMerge']);
  144. return $class;
  145. }
  146. /**
  147. * @depends testOwningOneToOneAssocation
  148. * @param ClassMetadata $class
  149. */
  150. public function testInverseOneToManyAssociation($class)
  151. {
  152. $this->assertTrue(isset($class->associationMappings['phonenumbers']));
  153. $this->assertFalse($class->associationMappings['phonenumbers']['isOwningSide']);
  154. $this->assertTrue($class->associationMappings['phonenumbers']['isCascadePersist']);
  155. $this->assertTrue($class->associationMappings['phonenumbers']['isCascadeRemove']);
  156. $this->assertFalse($class->associationMappings['phonenumbers']['isCascadeRefresh']);
  157. $this->assertFalse($class->associationMappings['phonenumbers']['isCascadeDetach']);
  158. $this->assertFalse($class->associationMappings['phonenumbers']['isCascadeMerge']);
  159. $this->assertTrue($class->associationMappings['phonenumbers']['orphanRemoval']);
  160. // Test Order By
  161. $this->assertEquals(array('number' => 'ASC'), $class->associationMappings['phonenumbers']['orderBy']);
  162. return $class;
  163. }
  164. /**
  165. * @depends testInverseOneToManyAssociation
  166. * @param ClassMetadata $class
  167. */
  168. public function testManyToManyAssociationWithCascadeAll($class)
  169. {
  170. $this->assertTrue(isset($class->associationMappings['groups']));
  171. $this->assertTrue($class->associationMappings['groups']['isOwningSide']);
  172. // Make sure that cascade-all works as expected
  173. $this->assertTrue($class->associationMappings['groups']['isCascadeRemove']);
  174. $this->assertTrue($class->associationMappings['groups']['isCascadePersist']);
  175. $this->assertTrue($class->associationMappings['groups']['isCascadeRefresh']);
  176. $this->assertTrue($class->associationMappings['groups']['isCascadeDetach']);
  177. $this->assertTrue($class->associationMappings['groups']['isCascadeMerge']);
  178. $this->assertFalse(isset($class->associationMappings['groups']['orderBy']));
  179. return $class;
  180. }
  181. /**
  182. * @depends testManyToManyAssociationWithCascadeAll
  183. * @param ClassMetadata $class
  184. */
  185. public function testLifecycleCallbacks($class)
  186. {
  187. $this->assertEquals(count($class->lifecycleCallbacks), 2);
  188. $this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
  189. $this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
  190. return $class;
  191. }
  192. /**
  193. * @depends testManyToManyAssociationWithCascadeAll
  194. * @param ClassMetadata $class
  195. */
  196. public function testLifecycleCallbacksSupportMultipleMethodNames($class)
  197. {
  198. $this->assertEquals(count($class->lifecycleCallbacks['prePersist']), 2);
  199. $this->assertEquals($class->lifecycleCallbacks['prePersist'][1], 'doOtherStuffOnPrePersistToo');
  200. return $class;
  201. }
  202. /**
  203. * @depends testLifecycleCallbacksSupportMultipleMethodNames
  204. * @param ClassMetadata $class
  205. */
  206. public function testJoinColumnUniqueAndNullable($class)
  207. {
  208. // Non-Nullability of Join Column
  209. $this->assertFalse($class->associationMappings['groups']['joinTable']['joinColumns'][0]['nullable']);
  210. $this->assertFalse($class->associationMappings['groups']['joinTable']['joinColumns'][0]['unique']);
  211. return $class;
  212. }
  213. /**
  214. * @depends testJoinColumnUniqueAndNullable
  215. * @param ClassMetadata $class
  216. */
  217. public function testColumnDefinition($class)
  218. {
  219. $this->assertEquals("CHAR(32) NOT NULL", $class->fieldMappings['email']['columnDefinition']);
  220. $this->assertEquals("INT NULL", $class->associationMappings['groups']['joinTable']['inverseJoinColumns'][0]['columnDefinition']);
  221. return $class;
  222. }
  223. /**
  224. * @depends testColumnDefinition
  225. * @param ClassMetadata $class
  226. */
  227. public function testJoinColumnOnDeleteAndOnUpdate($class)
  228. {
  229. $this->assertEquals('CASCADE', $class->associationMappings['address']['joinColumns'][0]['onDelete']);
  230. $this->assertEquals('CASCADE', $class->associationMappings['address']['joinColumns'][0]['onUpdate']);
  231. return $class;
  232. }
  233. /**
  234. * @group DDC-514
  235. */
  236. public function testDiscriminatorColumnDefaults()
  237. {
  238. if (strpos(get_class($this), 'PHPMappingDriver') !== false) {
  239. $this->markTestSkipped('PHP Mapping Drivers have no defaults.');
  240. }
  241. $class = $this->createClassMetadata('Doctrine\Tests\ORM\Mapping\Animal');
  242. $this->assertEquals(
  243. array('name' => 'dtype', 'type' => 'string', 'length' => 255, 'fieldName' => 'dtype'),
  244. $class->discriminatorColumn
  245. );
  246. }
  247. }
  248. /**
  249. * @Entity
  250. * @HasLifecycleCallbacks
  251. * @Table(
  252. * name="cms_users",
  253. * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})},
  254. * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})}
  255. * )
  256. */
  257. class User
  258. {
  259. /**
  260. * @Id
  261. * @Column(type="integer")
  262. * @generatedValue(strategy="AUTO")
  263. * @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
  264. **/
  265. public $id;
  266. /**
  267. * @Column(length=50, nullable=true, unique=true)
  268. */
  269. public $name;
  270. /**
  271. * @Column(name="user_email", columnDefinition="CHAR(32) NOT NULL")
  272. */
  273. public $email;
  274. /**
  275. * @OneToOne(targetEntity="Address", cascade={"remove"}, inversedBy="user")
  276. * @JoinColumn(onDelete="CASCADE", onUpdate="CASCADE")
  277. */
  278. public $address;
  279. /**
  280. * @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  281. * @OrderBy({"number"="ASC"})
  282. */
  283. public $phonenumbers;
  284. /**
  285. * @ManyToMany(targetEntity="Group", cascade={"all"})
  286. * @JoinTable(name="cms_user_groups",
  287. * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id", nullable=false, unique=false)},
  288. * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id", columnDefinition="INT NULL")}
  289. * )
  290. */
  291. public $groups;
  292. /**
  293. * @PrePersist
  294. */
  295. public function doStuffOnPrePersist()
  296. {
  297. }
  298. /**
  299. * @PrePersist
  300. */
  301. public function doOtherStuffOnPrePersistToo() {
  302. }
  303. /**
  304. * @PostPersist
  305. */
  306. public function doStuffOnPostPersist()
  307. {
  308. }
  309. public static function loadMetadata(ClassMetadataInfo $metadata)
  310. {
  311. $metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
  312. $metadata->setPrimaryTable(array(
  313. 'name' => 'cms_users',
  314. ));
  315. $metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT);
  316. $metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist');
  317. $metadata->addLifecycleCallback('doOtherStuffOnPrePersistToo', 'prePersist');
  318. $metadata->addLifecycleCallback('doStuffOnPostPersist', 'postPersist');
  319. $metadata->mapField(array(
  320. 'id' => true,
  321. 'fieldName' => 'id',
  322. 'type' => 'integer',
  323. 'columnName' => 'id',
  324. ));
  325. $metadata->mapField(array(
  326. 'fieldName' => 'name',
  327. 'type' => 'string',
  328. 'length' => 50,
  329. 'unique' => true,
  330. 'nullable' => true,
  331. 'columnName' => 'name',
  332. ));
  333. $metadata->mapField(array(
  334. 'fieldName' => 'email',
  335. 'type' => 'string',
  336. 'columnName' => 'user_email',
  337. 'columnDefinition' => 'CHAR(32) NOT NULL',
  338. ));
  339. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  340. $metadata->mapOneToOne(array(
  341. 'fieldName' => 'address',
  342. 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Address',
  343. 'cascade' =>
  344. array(
  345. 0 => 'remove',
  346. ),
  347. 'mappedBy' => NULL,
  348. 'inversedBy' => 'user',
  349. 'joinColumns' =>
  350. array(
  351. 0 =>
  352. array(
  353. 'name' => 'address_id',
  354. 'referencedColumnName' => 'id',
  355. 'onDelete' => 'CASCADE',
  356. 'onUpdate' => 'CASCADE'
  357. ),
  358. ),
  359. 'orphanRemoval' => false,
  360. ));
  361. $metadata->mapOneToMany(array(
  362. 'fieldName' => 'phonenumbers',
  363. 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Phonenumber',
  364. 'cascade' =>
  365. array(
  366. 1 => 'persist',
  367. ),
  368. 'mappedBy' => 'user',
  369. 'orphanRemoval' => true,
  370. 'orderBy' =>
  371. array(
  372. 'number' => 'ASC',
  373. ),
  374. ));
  375. $metadata->mapManyToMany(array(
  376. 'fieldName' => 'groups',
  377. 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Group',
  378. 'cascade' =>
  379. array(
  380. 0 => 'remove',
  381. 1 => 'persist',
  382. 2 => 'refresh',
  383. 3 => 'merge',
  384. 4 => 'detach',
  385. ),
  386. 'mappedBy' => NULL,
  387. 'joinTable' =>
  388. array(
  389. 'name' => 'cms_users_groups',
  390. 'joinColumns' =>
  391. array(
  392. 0 =>
  393. array(
  394. 'name' => 'user_id',
  395. 'referencedColumnName' => 'id',
  396. 'unique' => false,
  397. 'nullable' => false,
  398. ),
  399. ),
  400. 'inverseJoinColumns' =>
  401. array(
  402. 0 =>
  403. array(
  404. 'name' => 'group_id',
  405. 'referencedColumnName' => 'id',
  406. 'columnDefinition' => 'INT NULL',
  407. ),
  408. ),
  409. ),
  410. 'orderBy' => NULL,
  411. ));
  412. $metadata->table['uniqueConstraints'] = array(
  413. 'search_idx' => array('columns' => array('name', 'user_email')),
  414. );
  415. $metadata->table['indexes'] = array(
  416. 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email'))
  417. );
  418. $metadata->setSequenceGeneratorDefinition(array(
  419. 'sequenceName' => 'tablename_seq',
  420. 'allocationSize' => 100,
  421. 'initialValue' => 1,
  422. ));
  423. }
  424. }
  425. /**
  426. * @Entity
  427. * @InheritanceType("SINGLE_TABLE")
  428. * @DiscriminatorMap({"cat" = "Cat", "dog" = "Dog"})
  429. */
  430. abstract class Animal
  431. {
  432. /**
  433. * @Id @Column(type="string") @GeneratedValue
  434. */
  435. public $id;
  436. public static function loadMetadata(ClassMetadataInfo $metadata)
  437. {
  438. }
  439. }
  440. /** @Entity */
  441. class Cat extends Animal
  442. {
  443. public static function loadMetadata(ClassMetadataInfo $metadata)
  444. {
  445. }
  446. }
  447. /** @Entity */
  448. class Dog extends Animal
  449. {
  450. public static function loadMetadata(ClassMetadataInfo $metadata)
  451. {
  452. }
  453. }