/tests/Propel/Tests/Generator/Model/Diff/TableDiffTest.php

http://github.com/propelorm/Propel2 · PHP · 684 lines · 440 code · 128 blank · 116 comment · 2 complexity · e4c36300728265b5ecddf9837245cdb8 MD5 · raw file

  1. <?php
  2. /**
  3. * MIT License. This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Propel\Tests\Generator\Model\Diff;
  8. use PHPUnit\Framework\TestCase;
  9. use Propel\Generator\Exception\DiffException;
  10. use Propel\Generator\Model\Column;
  11. use Propel\Generator\Model\Database;
  12. use Propel\Generator\Model\Diff\ColumnDiff;
  13. use Propel\Generator\Model\Diff\TableDiff;
  14. use Propel\Generator\Model\ForeignKey;
  15. use Propel\Generator\Model\Index;
  16. use Propel\Generator\Model\Table;
  17. use Propel\Generator\Platform\DefaultPlatform;
  18. class TableDiffTest extends TestCase
  19. {
  20. /**
  21. * @return void
  22. */
  23. public function testDefaultObjectState()
  24. {
  25. $fromTable = new Table('article');
  26. $toTable = new Table('article');
  27. $diff = $this->createTableDiff($fromTable, $toTable);
  28. $this->assertSame($fromTable, $diff->getFromTable());
  29. $this->assertSame($toTable, $diff->getToTable());
  30. $this->assertFalse($diff->hasAddedColumns());
  31. $this->assertFalse($diff->hasAddedFks());
  32. $this->assertFalse($diff->hasAddedIndices());
  33. $this->assertFalse($diff->hasAddedPkColumns());
  34. $this->assertFalse($diff->hasModifiedColumns());
  35. $this->assertFalse($diff->hasModifiedFks());
  36. $this->assertFalse($diff->hasModifiedIndices());
  37. $this->assertFalse($diff->hasModifiedPk());
  38. $this->assertFalse($diff->hasRemovedColumns());
  39. $this->assertFalse($diff->hasRemovedFks());
  40. $this->assertFalse($diff->hasRemovedIndices());
  41. $this->assertFalse($diff->hasRemovedPkColumns());
  42. $this->assertFalse($diff->hasRenamedColumns());
  43. $this->assertFalse($diff->hasRenamedPkColumns());
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function testSetAddedColumns()
  49. {
  50. $column = new Column('is_published', 'boolean');
  51. $diff = $this->createTableDiff();
  52. $diff->setAddedColumns([ $column ]);
  53. $this->assertCount(1, $diff->getAddedColumns());
  54. $this->assertSame($column, $diff->getAddedColumn('is_published'));
  55. $this->assertTrue($diff->hasAddedColumns());
  56. }
  57. /**
  58. * @return void
  59. */
  60. public function testRemoveAddedColumn()
  61. {
  62. $diff = $this->createTableDiff();
  63. $diff->addAddedColumn('is_published', new Column('is_published'));
  64. $diff->removeAddedColumn('is_published');
  65. $this->assertEmpty($diff->getAddedColumns());
  66. $this->assertNull($diff->getAddedColumn('is_published'));
  67. $this->assertFalse($diff->hasAddedColumns());
  68. }
  69. /**
  70. * @return void
  71. */
  72. public function testSetRemovedColumns()
  73. {
  74. $column = new Column('is_active');
  75. $diff = $this->createTableDiff();
  76. $diff->setRemovedColumns([ $column ]);
  77. $this->assertCount(1, $diff->getRemovedColumns());
  78. $this->assertSame($column, $diff->getRemovedColumn('is_active'));
  79. $this->assertTrue($diff->hasRemovedColumns());
  80. }
  81. /**
  82. * @return void
  83. */
  84. public function testSetRemoveRemovedColumn()
  85. {
  86. $diff = $this->createTableDiff();
  87. $this->assertNull($diff->getRemovedColumn('is_active'));
  88. $diff->addRemovedColumn('is_active', new Column('is_active'));
  89. $diff->removeRemovedColumn('is_active');
  90. $this->assertFalse($diff->hasRemovedColumns());
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testSetModifiedColumns()
  96. {
  97. $columnDiff = new ColumnDiff();
  98. $diff = $this->createTableDiff();
  99. $diff->setModifiedColumns([ 'title' => $columnDiff ]);
  100. $this->assertCount(1, $diff->getModifiedColumns());
  101. $this->assertTrue($diff->hasModifiedColumns());
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testAddRenamedColumn()
  107. {
  108. $fromColumn = new Column('is_published', 'boolean');
  109. $toColumn = new Column('is_active', 'boolean');
  110. $diff = $this->createTableDiff();
  111. $diff->setRenamedColumns([ [ $fromColumn, $toColumn ] ]);
  112. $this->assertCount(1, $diff->getRenamedColumns());
  113. $this->assertTrue($diff->hasRenamedColumns());
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testSetAddedPkColumns()
  119. {
  120. $column = new Column('id', 'integer', 7);
  121. $column->setPrimaryKey();
  122. $diff = $this->createTableDiff();
  123. $diff->setAddedPkColumns([ $column ]);
  124. $this->assertCount(1, $diff->getAddedPkColumns());
  125. $this->assertTrue($diff->hasAddedPkColumns());
  126. $this->assertTrue($diff->hasModifiedPk());
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testRemoveAddedPkColumn()
  132. {
  133. $column = new Column('id', 'integer', 7);
  134. $column->setPrimaryKey();
  135. $diff = $this->createTableDiff();
  136. $diff->setAddedPkColumns([ $column ]);
  137. $diff->removeAddedPkColumn('id');
  138. $this->assertEmpty($diff->getRemovedPkColumns());
  139. $this->assertFalse($diff->hasAddedPkColumns());
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testCantAddNonPrimaryKeyColumn()
  145. {
  146. $this->expectException(DiffException::class);
  147. $diff = $this->createTableDiff();
  148. $diff->addAddedPkColumn('id', new Column('id', 'integer'));
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testSetRemovedPkColumns()
  154. {
  155. $column = new Column('id', 'integer');
  156. $column->setPrimaryKey();
  157. $diff = $this->createTableDiff();
  158. $diff->setRemovedPkColumns([ $column ]);
  159. $this->assertCount(1, $diff->getRemovedPkColumns());
  160. $this->assertTrue($diff->hasModifiedPk());
  161. }
  162. /**
  163. * @return void
  164. */
  165. public function testRemoveRemovedPkColumn()
  166. {
  167. $diff = $this->createTableDiff();
  168. $diff->addRemovedPkColumn('id', new Column('id', 'integer'));
  169. $diff->removeRemovedPkColumn('id');
  170. $this->assertEmpty($diff->getRemovedPkColumns());
  171. }
  172. /**
  173. * @return void
  174. */
  175. public function testSetRenamedPkColumns()
  176. {
  177. $diff = $this->createTableDiff();
  178. $diff->setRenamedPkColumns([ [ new Column('id', 'integer'), new Column('post_id', 'integer') ] ]);
  179. $this->assertCount(1, $diff->getRenamedPkColumns());
  180. $this->assertTrue($diff->hasModifiedPk());
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testSetAddedIndices()
  186. {
  187. $table = new Table();
  188. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  189. $index = new Index('username_unique_idx');
  190. $index->setTable($table);
  191. $diff = $this->createTableDiff();
  192. $diff->setAddedIndices([ $index ]);
  193. $this->assertCount(1, $diff->getAddedIndices());
  194. $this->assertTrue($diff->hasAddedIndices());
  195. }
  196. /**
  197. * @return void
  198. */
  199. public function testSetRemovedIndices()
  200. {
  201. $table = new Table();
  202. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  203. $index = new Index('username_unique_idx');
  204. $index->setTable($table);
  205. $diff = $this->createTableDiff();
  206. $diff->setRemovedIndices([ $index ]);
  207. $this->assertCount(1, $diff->getRemovedIndices());
  208. $this->assertTrue($diff->hasRemovedIndices());
  209. }
  210. /**
  211. * @return void
  212. */
  213. public function testSetModifiedIndices()
  214. {
  215. $table = new Table('users');
  216. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  217. $fromIndex = new Index('username_unique_idx');
  218. $fromIndex->setTable($table);
  219. $fromIndex->setColumns([ new Column('username') ]);
  220. $toIndex = new Index('username_unique_idx');
  221. $toIndex->setTable($table);
  222. $toIndex->setColumns([ new Column('client_id'), new Column('username') ]);
  223. $diff = $this->createTableDiff();
  224. $diff->setModifiedIndices([ [ $fromIndex, $toIndex ]]);
  225. $this->assertCount(1, $diff->getModifiedIndices());
  226. $this->assertTrue($diff->hasModifiedIndices());
  227. }
  228. /**
  229. * @return void
  230. */
  231. public function testSetAddedFks()
  232. {
  233. $fk = new ForeignKey('fk_blog_author');
  234. $diff = $this->createTableDiff();
  235. $diff->setAddedFks([ $fk ]);
  236. $this->assertCount(1, $diff->getAddedFks());
  237. $this->assertTrue($diff->hasAddedFks());
  238. }
  239. /**
  240. * @return void
  241. */
  242. public function testRemoveAddedFk()
  243. {
  244. $diff = $this->createTableDiff();
  245. $diff->addAddedFk('fk_blog_author', new ForeignKey('fk_blog_author'));
  246. $diff->removeAddedFk('fk_blog_author');
  247. $this->assertEmpty($diff->getAddedFks());
  248. $this->assertFalse($diff->hasAddedFks());
  249. }
  250. /**
  251. * @return void
  252. */
  253. public function testSetRemovedFk()
  254. {
  255. $diff = $this->createTableDiff();
  256. $diff->setRemovedFks([ new ForeignKey('fk_blog_post_author') ]);
  257. $this->assertCount(1, $diff->getRemovedFks());
  258. $this->assertTrue($diff->hasRemovedFks());
  259. }
  260. /**
  261. * @return void
  262. */
  263. public function testRemoveRemovedFk()
  264. {
  265. $diff = $this->createTableDiff();
  266. $diff->addRemovedFk('blog_post_author', new ForeignKey('blog_post_author'));
  267. $diff->removeRemovedFk('blog_post_author');
  268. $this->assertEmpty($diff->getRemovedFks());
  269. $this->assertFalse($diff->hasRemovedFks());
  270. }
  271. /**
  272. * @return void
  273. */
  274. public function testSetModifiedFks()
  275. {
  276. $diff = $this->createTableDiff();
  277. $diff->setModifiedFks([ [ new ForeignKey('blog_post_author'), new ForeignKey('blog_post_has_author') ] ]);
  278. $this->assertCount(1, $diff->getModifiedFks());
  279. $this->assertTrue($diff->hasModifiedFks());
  280. }
  281. /**
  282. * @return void
  283. */
  284. public function testGetSimpleReverseDiff()
  285. {
  286. $tableA = new Table('users');
  287. $tableB = new Table('users');
  288. $diff = $this->createTableDiff($tableA, $tableB);
  289. $reverseDiff = $diff->getReverseDiff();
  290. $this->assertInstanceOf('Propel\Generator\Model\Diff\TableDiff', $reverseDiff);
  291. $this->assertSame($tableA, $reverseDiff->getToTable());
  292. $this->assertSame($tableB, $reverseDiff->getFromTable());
  293. }
  294. /**
  295. * @return void
  296. */
  297. public function testReverseDiffHasModifiedColumns()
  298. {
  299. $c1 = new Column('title', 'varchar', 50);
  300. $c2 = new Column('title', 'varchar', 100);
  301. $columnDiff = new ColumnDiff($c1, $c2);
  302. $reverseColumnDiff = $columnDiff->getReverseDiff();
  303. $diff = $this->createTableDiff();
  304. $diff->addModifiedColumn('title', $columnDiff);
  305. $reverseDiff = $diff->getReverseDiff();
  306. $this->assertTrue($reverseDiff->hasModifiedColumns());
  307. $this->assertEquals([ 'title' => $reverseColumnDiff ], $reverseDiff->getModifiedColumns());
  308. }
  309. /**
  310. * @return void
  311. */
  312. public function testReverseDiffHasRemovedColumns()
  313. {
  314. $column = new Column('slug', 'varchar', 100);
  315. $diff = $this->createTableDiff();
  316. $diff->addAddedColumn('slug', $column);
  317. $reverseDiff = $diff->getReverseDiff();
  318. $this->assertSame([ 'slug' => $column], $reverseDiff->getRemovedColumns());
  319. $this->assertSame($column, $reverseDiff->getRemovedColumn('slug'));
  320. }
  321. /**
  322. * @return void
  323. */
  324. public function testReverseDiffHasAddedColumns()
  325. {
  326. $column = new Column('slug', 'varchar', 100);
  327. $diff = $this->createTableDiff();
  328. $diff->addRemovedColumn('slug', $column);
  329. $reverseDiff = $diff->getReverseDiff();
  330. $this->assertSame([ 'slug' => $column], $reverseDiff->getAddedColumns());
  331. $this->assertSame($column, $reverseDiff->getAddedColumn('slug'));
  332. }
  333. /**
  334. * @return void
  335. */
  336. public function testReverseDiffHasRenamedColumns()
  337. {
  338. $columnA = new Column('login', 'varchar', 15);
  339. $columnB = new Column('username', 'varchar', 15);
  340. $diff = $this->createTableDiff();
  341. $diff->addRenamedColumn($columnA, $columnB);
  342. $reverseDiff = $diff->getReverseDiff();
  343. $this->assertSame([ [ $columnB, $columnA ] ], $reverseDiff->getRenamedColumns());
  344. }
  345. /**
  346. * @return void
  347. */
  348. public function testReverseDiffHasAddedPkColumns()
  349. {
  350. $column = new Column('client_id', 'integer');
  351. $column->setPrimaryKey();
  352. $diff = $this->createTableDiff();
  353. $diff->addRemovedPkColumn('client_id', $column);
  354. $reverseDiff = $diff->getReverseDiff();
  355. $this->assertCount(1, $reverseDiff->getAddedPkColumns());
  356. $this->assertTrue($reverseDiff->hasAddedPkColumns());
  357. }
  358. /**
  359. * @return void
  360. */
  361. public function testReverseDiffHasRemovedPkColumns()
  362. {
  363. $column = new Column('client_id', 'integer');
  364. $column->setPrimaryKey();
  365. $diff = $this->createTableDiff();
  366. $diff->addAddedPkColumn('client_id', $column);
  367. $reverseDiff = $diff->getReverseDiff();
  368. $this->assertCount(1, $reverseDiff->getRemovedPkColumns());
  369. $this->assertTrue($reverseDiff->hasRemovedPkColumns());
  370. }
  371. /**
  372. * @return void
  373. */
  374. public function testReverseDiffHasRenamedPkColumn()
  375. {
  376. $fromColumn = new Column('post_id', 'integer');
  377. $fromColumn->setPrimaryKey();
  378. $toColumn = new Column('id', 'integer');
  379. $toColumn->setPrimaryKey();
  380. $diff = $this->createTableDiff();
  381. $diff->addRenamedPkColumn($fromColumn, $toColumn);
  382. $reverseDiff = $diff->getReverseDiff();
  383. $this->assertTrue($reverseDiff->hasRenamedPkColumns());
  384. $this->assertSame([[ $toColumn, $fromColumn ]], $reverseDiff->getRenamedPkColumns());
  385. }
  386. /**
  387. * @return void
  388. */
  389. public function testReverseDiffHasAddedIndices()
  390. {
  391. $table = new Table();
  392. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  393. $index = new Index('username_unique_idx');
  394. $index->setTable($table);
  395. $diff = $this->createTableDiff();
  396. $diff->addRemovedIndex('username_unique_idx', $index);
  397. $reverseDiff = $diff->getReverseDiff();
  398. $this->assertTrue($reverseDiff->hasAddedIndices());
  399. $this->assertCount(1, $reverseDiff->getAddedIndices());
  400. }
  401. /**
  402. * @return void
  403. */
  404. public function testReverseDiffHasRemovedIndices()
  405. {
  406. $table = new Table();
  407. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  408. $index = new Index('username_unique_idx');
  409. $index->setTable($table);
  410. $diff = $this->createTableDiff();
  411. $diff->addAddedIndex('username_unique_idx', $index);
  412. $reverseDiff = $diff->getReverseDiff();
  413. $this->assertTrue($reverseDiff->hasRemovedIndices());
  414. $this->assertCount(1, $reverseDiff->getRemovedIndices());
  415. }
  416. /**
  417. * @return void
  418. */
  419. public function testReverseDiffHasModifiedIndices()
  420. {
  421. $table = new Table();
  422. $table->setDatabase(new Database('foo', new DefaultPlatform()));
  423. $fromIndex = new Index('i1');
  424. $fromIndex->setTable($table);
  425. $toIndex = new Index('i1');
  426. $toIndex->setTable($table);
  427. $diff = $this->createTableDiff();
  428. $diff->addModifiedIndex('i1', $fromIndex, $toIndex);
  429. $reverseDiff = $diff->getReverseDiff();
  430. $this->assertTrue($reverseDiff->hasModifiedIndices());
  431. $this->assertSame([ 'i1' => [ $toIndex, $fromIndex ]], $reverseDiff->getModifiedIndices());
  432. }
  433. /**
  434. * @return void
  435. */
  436. public function testReverseDiffHasRemovedFks()
  437. {
  438. $diff = $this->createTableDiff();
  439. $diff->addAddedFk('fk_post_author', new ForeignKey('fk_post_author'));
  440. $reverseDiff = $diff->getReverseDiff();
  441. $this->assertTrue($reverseDiff->hasRemovedFks());
  442. $this->assertCount(1, $reverseDiff->getRemovedFks());
  443. }
  444. /**
  445. * @return void
  446. */
  447. public function testReverseDiffHasAddedFks()
  448. {
  449. $diff = $this->createTableDiff();
  450. $diff->addRemovedFk('fk_post_author', new ForeignKey('fk_post_author'));
  451. $reverseDiff = $diff->getReverseDiff();
  452. $this->assertTrue($reverseDiff->hasAddedFks());
  453. $this->assertCount(1, $reverseDiff->getAddedFks());
  454. }
  455. /**
  456. * @return void
  457. */
  458. public function testReverseDiffHasModifiedFks()
  459. {
  460. $fromFk = new ForeignKey('fk_1');
  461. $toFk = new ForeignKey('fk_1');
  462. $diff = $this->createTableDiff();
  463. $diff->addModifiedFk('fk_1', $fromFk, $toFk);
  464. $reverseDiff = $diff->getReverseDiff();
  465. $this->assertTrue($reverseDiff->hasModifiedFks());
  466. $this->assertSame([ 'fk_1' => [ $toFk, $fromFk ]], $reverseDiff->getModifiedFks());
  467. }
  468. private function createTableDiff(?Table $fromTable = null, ?Table $toTable = null)
  469. {
  470. if (null === $fromTable) {
  471. $fromTable = new Table('users');
  472. }
  473. if (null === $toTable) {
  474. $toTable = new Table('users');
  475. }
  476. return new TableDiff($fromTable, $toTable);
  477. }
  478. /**
  479. * @return void
  480. */
  481. public function testToString()
  482. {
  483. $tableA = new Table('A');
  484. $tableB = new Table('B');
  485. $diff = new TableDiff($tableA, $tableB);
  486. $diff->addAddedColumn('id', new Column('id', 'integer'));
  487. $diff->addRemovedColumn('category_id', new Column('category_id', 'integer'));
  488. $colFoo = new Column('foo', 'integer');
  489. $colBar = new Column('bar', 'integer');
  490. $tableA->addColumn($colFoo);
  491. $tableA->addColumn($colBar);
  492. $diff->addRenamedColumn($colFoo, $colBar);
  493. $columnDiff = new ColumnDiff($colFoo, $colBar);
  494. $diff->addModifiedColumn('foo', $columnDiff);
  495. $fk = new ForeignKey('category');
  496. $fk->setTable($tableA);
  497. $fk->setForeignTableCommonName('B');
  498. $fk->addReference('category_id', 'id');
  499. $fkChanged = clone $fk;
  500. $fkChanged->setForeignTableCommonName('C');
  501. $fkChanged->addReference('bla', 'id2');
  502. $fkChanged->setOnDelete('cascade');
  503. $fkChanged->setOnUpdate('cascade');
  504. $diff->addAddedFk('category', $fk);
  505. $diff->addModifiedFk('category', $fk, $fkChanged);
  506. $diff->addRemovedFk('category', $fk);
  507. $index = new Index('test_index');
  508. $index->setTable($tableA);
  509. $index->setColumns([$colFoo]);
  510. $indexChanged = clone $index;
  511. $indexChanged->setColumns([$colBar]);
  512. $diff->addAddedIndex('test_index', $index);
  513. $diff->addModifiedIndex('test_index', $index, $indexChanged);
  514. $diff->addRemovedIndex('test_index', $index);
  515. $string = (string)$diff;
  516. $expected = ' A:
  517. addedColumns:
  518. - id
  519. removedColumns:
  520. - category_id
  521. modifiedColumns:
  522. A.FOO:
  523. modifiedProperties:
  524. renamedColumns:
  525. foo: bar
  526. addedIndices:
  527. - test_index
  528. removedIndices:
  529. - test_index
  530. modifiedIndices:
  531. - test_index
  532. addedFks:
  533. - category
  534. removedFks:
  535. - category
  536. modifiedFks:
  537. category:
  538. localColumns: from ["category_id"] to ["category_id","bla"]
  539. foreignColumns: from ["id"] to ["id","id2"]
  540. onUpdate: from to CASCADE
  541. onDelete: from to CASCADE
  542. ';
  543. $this->assertEquals($expected, $string);
  544. }
  545. /**
  546. * @return void
  547. */
  548. public function testMagicClone()
  549. {
  550. $diff = new TableDiff(new Table('A'), new Table('B'));
  551. $clonedDiff = clone $diff;
  552. $this->assertNotSame($clonedDiff, $diff);
  553. $this->assertNotSame($clonedDiff->getFromTable(), $diff->getFromTable());
  554. $this->assertNotSame($clonedDiff->getToTable(), $diff->getToTable());
  555. }
  556. }