ClassMetadataBuilderTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\ORM\Mapping;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
  22. /**
  23. * @group DDC-659
  24. */
  25. class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
  26. {
  27. /**
  28. * @var ClassMetadata
  29. */
  30. private $cm;
  31. /**
  32. * @var ClassMetadataBuilder
  33. */
  34. private $builder;
  35. public function setUp()
  36. {
  37. $this->cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
  38. $this->cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  39. $this->builder = new ClassMetadataBuilder($this->cm);
  40. }
  41. public function testSetMappedSuperClass()
  42. {
  43. $this->assertIsFluent($this->builder->setMappedSuperClass());
  44. $this->assertTrue($this->cm->isMappedSuperclass);
  45. }
  46. public function testSetCustomRepositoryClass()
  47. {
  48. $this->assertIsFluent($this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'));
  49. $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName);
  50. }
  51. public function testSetReadOnly()
  52. {
  53. $this->assertIsFluent($this->builder->setReadOnly());
  54. $this->assertTrue($this->cm->isReadOnly);
  55. }
  56. public function testSetTable()
  57. {
  58. $this->assertIsFluent($this->builder->setTable('users'));
  59. $this->assertEquals('users', $this->cm->table['name']);
  60. }
  61. public function testAddIndex()
  62. {
  63. $this->assertIsFluent($this->builder->addIndex(array('username', 'name'), 'users_idx'));
  64. $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['indexes']);
  65. }
  66. public function testAddUniqueConstraint()
  67. {
  68. $this->assertIsFluent($this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'));
  69. $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['uniqueConstraints']);
  70. }
  71. public function testSetPrimaryTableRelated()
  72. {
  73. $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx');
  74. $this->builder->addIndex(array('username', 'name'), 'users_idx');
  75. $this->builder->setTable('users');
  76. $this->assertEquals(
  77. array(
  78. 'name' => 'users',
  79. 'indexes' => array('users_idx' => array('columns' => array('username', 'name'))),
  80. 'uniqueConstraints' => array('users_idx' => array('columns' => array('username', 'name'))),
  81. ),
  82. $this->cm->table
  83. );
  84. }
  85. public function testSetInheritanceJoined()
  86. {
  87. $this->assertIsFluent($this->builder->setJoinedTableInheritance());
  88. $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_JOINED, $this->cm->inheritanceType);
  89. }
  90. public function testSetInheritanceSingleTable()
  91. {
  92. $this->assertIsFluent($this->builder->setSingleTableInheritance());
  93. $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE, $this->cm->inheritanceType);
  94. }
  95. public function testSetDiscriminatorColumn()
  96. {
  97. $this->assertIsFluent($this->builder->setDiscriminatorColumn('discr', 'string', '124'));
  98. $this->assertEquals(array('fieldName' => 'discr', 'name' => 'discr', 'type' => 'string', 'length' => '124'), $this->cm->discriminatorColumn);
  99. }
  100. public function testAddDiscriminatorMapClass()
  101. {
  102. $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test', 'Doctrine\Tests\Models\CMS\CmsUser'));
  103. $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test2', 'Doctrine\Tests\Models\CMS\CmsGroup'));
  104. $this->assertEquals(array('test' => 'Doctrine\Tests\Models\CMS\CmsUser', 'test2' => 'Doctrine\Tests\Models\CMS\CmsGroup'), $this->cm->discriminatorMap);
  105. $this->assertEquals('test', $this->cm->discriminatorValue);
  106. }
  107. public function testChangeTrackingPolicyExplicit()
  108. {
  109. $this->assertIsFluent($this->builder->setChangeTrackingPolicyDeferredExplicit());
  110. $this->assertEquals(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT, $this->cm->changeTrackingPolicy);
  111. }
  112. public function testChangeTrackingPolicyNotify()
  113. {
  114. $this->assertIsFluent($this->builder->setChangeTrackingPolicyNotify());
  115. $this->assertEquals(ClassMetadata::CHANGETRACKING_NOTIFY, $this->cm->changeTrackingPolicy);
  116. }
  117. public function testAddField()
  118. {
  119. $this->assertIsFluent($this->builder->addField('name', 'string'));
  120. $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
  121. }
  122. public function testCreateField()
  123. {
  124. $fieldBuilder = ($this->builder->createField('name', 'string'));
  125. $this->assertInstanceOf('Doctrine\ORM\Mapping\Builder\FieldBuilder', $fieldBuilder);
  126. $this->assertFalse(isset($this->cm->fieldMappings['name']));
  127. $this->assertIsFluent($fieldBuilder->build());
  128. $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
  129. }
  130. public function testCreateVersionedField()
  131. {
  132. $this->builder->createField('name', 'integer')->columnName('username')->length(124)->nullable()->columnDefinition('foobar')->unique()->isVersionField()->build();
  133. $this->assertEquals(array(
  134. 'columnDefinition' => 'foobar',
  135. 'columnName' => 'username',
  136. 'default' => 1,
  137. 'fieldName' => 'name',
  138. 'length' => 124,
  139. 'type' => 'integer',
  140. 'nullable' => true,
  141. 'unique' => true,
  142. ), $this->cm->fieldMappings['name']);
  143. }
  144. public function testCreatePrimaryField()
  145. {
  146. $this->builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
  147. $this->assertEquals(array('id'), $this->cm->identifier);
  148. $this->assertEquals(array('columnName' => 'id', 'fieldName' => 'id', 'id' => true, 'type' => 'integer'), $this->cm->fieldMappings['id']);
  149. }
  150. public function testAddLifecycleEvent()
  151. {
  152. $this->builder->addLifecycleEvent('getStatus', 'postLoad');
  153. $this->assertEquals(array('postLoad' => array('getStatus')), $this->cm->lifecycleCallbacks);
  154. }
  155. public function testCreateManyToOne()
  156. {
  157. $this->assertIsFluent(
  158. $this->builder->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
  159. ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
  160. ->cascadeAll()
  161. ->fetchExtraLazy()
  162. ->build()
  163. );
  164. $this->assertEquals(array('groups' => array (
  165. 'fieldName' => 'groups',
  166. 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
  167. 'cascade' => array (
  168. 0 => 'remove',
  169. 1 => 'persist',
  170. 2 => 'refresh',
  171. 3 => 'merge',
  172. 4 => 'detach',
  173. ),
  174. 'fetch' => 4,
  175. 'joinColumns' => array (
  176. 0 =>
  177. array (
  178. 'name' => 'group_id',
  179. 'referencedColumnName' => 'id',
  180. 'nullable' => true,
  181. 'unique' => false,
  182. 'onDelete' => 'CASCADE',
  183. 'columnDefinition' => NULL,
  184. ),
  185. ),
  186. 'type' => 2,
  187. 'mappedBy' => NULL,
  188. 'inversedBy' => NULL,
  189. 'isOwningSide' => true,
  190. 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
  191. 'isCascadeRemove' => true,
  192. 'isCascadePersist' => true,
  193. 'isCascadeRefresh' => true,
  194. 'isCascadeMerge' => true,
  195. 'isCascadeDetach' => true,
  196. 'sourceToTargetKeyColumns' =>
  197. array (
  198. 'group_id' => 'id',
  199. ),
  200. 'joinColumnFieldNames' =>
  201. array (
  202. 'group_id' => 'group_id',
  203. ),
  204. 'targetToSourceKeyColumns' =>
  205. array (
  206. 'id' => 'group_id',
  207. ),
  208. 'orphanRemoval' => false,
  209. ),
  210. ), $this->cm->associationMappings);
  211. }
  212. public function testCreateOneToOne()
  213. {
  214. $this->assertIsFluent(
  215. $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
  216. ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
  217. ->cascadeAll()
  218. ->fetchExtraLazy()
  219. ->build()
  220. );
  221. $this->assertEquals(array('groups' => array (
  222. 'fieldName' => 'groups',
  223. 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
  224. 'cascade' => array (
  225. 0 => 'remove',
  226. 1 => 'persist',
  227. 2 => 'refresh',
  228. 3 => 'merge',
  229. 4 => 'detach',
  230. ),
  231. 'fetch' => 4,
  232. 'joinColumns' => array (
  233. 0 =>
  234. array (
  235. 'name' => 'group_id',
  236. 'referencedColumnName' => 'id',
  237. 'nullable' => true,
  238. 'unique' => true,
  239. 'onDelete' => 'CASCADE',
  240. 'columnDefinition' => NULL,
  241. ),
  242. ),
  243. 'type' => 1,
  244. 'mappedBy' => NULL,
  245. 'inversedBy' => NULL,
  246. 'isOwningSide' => true,
  247. 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
  248. 'isCascadeRemove' => true,
  249. 'isCascadePersist' => true,
  250. 'isCascadeRefresh' => true,
  251. 'isCascadeMerge' => true,
  252. 'isCascadeDetach' => true,
  253. 'sourceToTargetKeyColumns' =>
  254. array (
  255. 'group_id' => 'id',
  256. ),
  257. 'joinColumnFieldNames' =>
  258. array (
  259. 'group_id' => 'group_id',
  260. ),
  261. 'targetToSourceKeyColumns' =>
  262. array (
  263. 'id' => 'group_id',
  264. ),
  265. 'orphanRemoval' => false,
  266. ),
  267. ), $this->cm->associationMappings);
  268. }
  269. public function testCreateManyToMany()
  270. {
  271. $this->assertIsFluent(
  272. $this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
  273. ->setJoinTable('groups_users')
  274. ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
  275. ->addInverseJoinColumn('user_id', 'id')
  276. ->cascadeAll()
  277. ->fetchExtraLazy()
  278. ->build()
  279. );
  280. $this->assertEquals(array(
  281. 'groups' =>
  282. array(
  283. 'fieldName' => 'groups',
  284. 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
  285. 'cascade' =>
  286. array(
  287. 0 => 'remove',
  288. 1 => 'persist',
  289. 2 => 'refresh',
  290. 3 => 'merge',
  291. 4 => 'detach',
  292. ),
  293. 'fetch' => 4,
  294. 'joinTable' =>
  295. array(
  296. 'joinColumns' =>
  297. array(
  298. 0 =>
  299. array(
  300. 'name' => 'group_id',
  301. 'referencedColumnName' => 'id',
  302. 'nullable' => true,
  303. 'unique' => false,
  304. 'onDelete' => 'CASCADE',
  305. 'columnDefinition' => NULL,
  306. ),
  307. ),
  308. 'inverseJoinColumns' =>
  309. array(
  310. 0 =>
  311. array(
  312. 'name' => 'user_id',
  313. 'referencedColumnName' => 'id',
  314. 'nullable' => true,
  315. 'unique' => false,
  316. 'onDelete' => NULL,
  317. 'columnDefinition' => NULL,
  318. ),
  319. ),
  320. 'name' => 'groups_users',
  321. ),
  322. 'type' => 8,
  323. 'mappedBy' => NULL,
  324. 'inversedBy' => NULL,
  325. 'isOwningSide' => true,
  326. 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
  327. 'isCascadeRemove' => true,
  328. 'isCascadePersist' => true,
  329. 'isCascadeRefresh' => true,
  330. 'isCascadeMerge' => true,
  331. 'isCascadeDetach' => true,
  332. 'isOnDeleteCascade' => true,
  333. 'relationToSourceKeyColumns' =>
  334. array(
  335. 'group_id' => 'id',
  336. ),
  337. 'joinTableColumns' =>
  338. array(
  339. 0 => 'group_id',
  340. 1 => 'user_id',
  341. ),
  342. 'relationToTargetKeyColumns' =>
  343. array(
  344. 'user_id' => 'id',
  345. ),
  346. 'orphanRemoval' => false,
  347. ),
  348. ), $this->cm->associationMappings);
  349. }
  350. public function testCreateOneToMany()
  351. {
  352. $this->assertIsFluent(
  353. $this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
  354. ->mappedBy('test')
  355. ->setOrderBy(array('test'))
  356. ->setIndexBy('test')
  357. ->build()
  358. );
  359. $this->assertEquals(array(
  360. 'groups' =>
  361. array(
  362. 'fieldName' => 'groups',
  363. 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
  364. 'mappedBy' => 'test',
  365. 'orderBy' =>
  366. array(
  367. 0 => 'test',
  368. ),
  369. 'indexBy' => 'test',
  370. 'type' => 4,
  371. 'inversedBy' => NULL,
  372. 'isOwningSide' => false,
  373. 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
  374. 'fetch' => 2,
  375. 'cascade' =>
  376. array(
  377. ),
  378. 'isCascadeRemove' => false,
  379. 'isCascadePersist' => false,
  380. 'isCascadeRefresh' => false,
  381. 'isCascadeMerge' => false,
  382. 'isCascadeDetach' => false,
  383. 'orphanRemoval' => false,
  384. ),
  385. ), $this->cm->associationMappings);
  386. }
  387. public function assertIsFluent($ret)
  388. {
  389. $this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder");
  390. }
  391. }