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

https://github.com/hhamon/Propel2 · PHP · 276 lines · 237 code · 25 blank · 14 comment · 0 complexity · f692445d9c0a80914c47ef797c7a91a2 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: TableTest.php 1891 2010-08-09 15:03:18Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. use Propel\Generator\Model\Column;
  11. use Propel\Generator\Model\ColumnDefaultValue;
  12. use Propel\Generator\Model\Table;
  13. use Propel\Generator\Model\Diff\TableComparator;
  14. use Propel\Generator\Model\Diff\TableDiff;
  15. use Propel\Generator\Model\Diff\ColumnComparator;
  16. use Propel\Generator\Platform\MysqlPlatform;
  17. use \Propel\Tests\TestCase;
  18. /**
  19. * Tests for the Column methods of the TableComparator service class.
  20. *
  21. */
  22. class PropelTableColumnComparatorTest extends TestCase
  23. {
  24. public function setUp()
  25. {
  26. $this->platform = new MysqlPlatform();
  27. }
  28. public function testCompareSameColumns()
  29. {
  30. $t1 = new Table();
  31. $c1 = new Column('Foo');
  32. $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  33. $c1->getDomain()->replaceScale(2);
  34. $c1->getDomain()->replaceSize(3);
  35. $c1->setNotNull(true);
  36. $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  37. $t1->addColumn($c1);
  38. $t2 = new Table();
  39. $c2 = new Column('Foo');
  40. $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  41. $c2->getDomain()->replaceScale(2);
  42. $c2->getDomain()->replaceSize(3);
  43. $c2->setNotNull(true);
  44. $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  45. $t2->addColumn($c2);
  46. $this->assertFalse(TableComparator::computeDiff($t1, $t2));
  47. }
  48. public function testCompareNotSameColumns()
  49. {
  50. $t1 = new Table();
  51. $c1 = new Column('Foo');
  52. $t1->addColumn($c1);
  53. $t2 = new Table();
  54. $c2 = new Column('Bar');
  55. $t2->addColumn($c2);
  56. $diff = TableComparator::computeDiff($t1, $t2);
  57. $this->assertTrue($diff instanceof TableDiff);
  58. }
  59. public function testCompareCaseInsensitive()
  60. {
  61. $t1 = new Table();
  62. $c1 = new Column('Foo');
  63. $t1->addColumn($c1);
  64. $t2 = new Table();
  65. $c2 = new Column('fOO');
  66. $t2->addColumn($c2);
  67. $diff = TableComparator::computeDiff($t1, $t2);
  68. $this->assertTrue($diff instanceof TableDiff);
  69. $this->assertFalse(TableComparator::computeDiff($t1, $t2, true));
  70. }
  71. public function testCompareAddedColumn()
  72. {
  73. $t1 = new Table();
  74. $t2 = new Table();
  75. $c2 = new Column('Foo');
  76. $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  77. $c2->getDomain()->replaceScale(2);
  78. $c2->getDomain()->replaceSize(3);
  79. $c2->setNotNull(true);
  80. $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  81. $t2->addColumn($c2);
  82. $tc = new TableComparator();
  83. $tc->setFromTable($t1);
  84. $tc->setToTable($t2);
  85. $nbDiffs = $tc->compareColumns();
  86. $tableDiff = $tc->getTableDiff();
  87. $this->assertEquals(1, $nbDiffs);
  88. $this->assertEquals(1, count($tableDiff->getAddedColumns()));
  89. $this->assertEquals(array('Foo' => $c2), $tableDiff->getAddedColumns());
  90. }
  91. public function testCompareRemovedColumn()
  92. {
  93. $t1 = new Table();
  94. $c1 = new Column('Bar');
  95. $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  96. $c1->getDomain()->replaceScale(2);
  97. $c1->getDomain()->replaceSize(3);
  98. $c1->setNotNull(true);
  99. $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  100. $t1->addColumn($c1);
  101. $t2 = new Table();
  102. $tc = new TableComparator();
  103. $tc->setFromTable($t1);
  104. $tc->setToTable($t2);
  105. $nbDiffs = $tc->compareColumns();
  106. $tableDiff = $tc->getTableDiff();
  107. $this->assertEquals(1, $nbDiffs);
  108. $this->assertEquals(1, count($tableDiff->getRemovedColumns()));
  109. $this->assertEquals(array('Bar' => $c1), $tableDiff->getRemovedColumns());
  110. }
  111. public function testCompareModifiedColumn()
  112. {
  113. $t1 = new Table();
  114. $c1 = new Column('Foo');
  115. $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  116. $c1->getDomain()->replaceSize(255);
  117. $c1->setNotNull(false);
  118. $t1->addColumn($c1);
  119. $t2 = new Table();
  120. $c2 = new Column('Foo');
  121. $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  122. $c2->getDomain()->replaceScale(2);
  123. $c2->getDomain()->replaceSize(3);
  124. $c2->setNotNull(true);
  125. $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  126. $t2->addColumn($c2);
  127. $tc = new TableComparator();
  128. $tc->setFromTable($t1);
  129. $tc->setToTable($t2);
  130. $nbDiffs = $tc->compareColumns();
  131. $tableDiff = $tc->getTableDiff();
  132. $this->assertEquals(1, $nbDiffs);
  133. $this->assertEquals(1, count($tableDiff->getModifiedColumns()));
  134. $columnDiff = ColumnComparator::computeDiff($c1, $c2);
  135. $this->assertEquals(array('Foo' => $columnDiff), $tableDiff->getModifiedColumns());
  136. }
  137. public function testCompareRenamedColumn()
  138. {
  139. $t1 = new Table();
  140. $c1 = new Column('Foo');
  141. $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  142. $c1->getDomain()->replaceScale(2);
  143. $c1->getDomain()->replaceSize(3);
  144. $c1->setNotNull(true);
  145. $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  146. $t1->addColumn($c1);
  147. $t2 = new Table();
  148. $c2 = new Column('Bar');
  149. $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  150. $c2->getDomain()->replaceScale(2);
  151. $c2->getDomain()->replaceSize(3);
  152. $c2->setNotNull(true);
  153. $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  154. $t2->addColumn($c2);
  155. $tc = new TableComparator();
  156. $tc->setFromTable($t1);
  157. $tc->setToTable($t2);
  158. $nbDiffs = $tc->compareColumns();
  159. $tableDiff = $tc->getTableDiff();
  160. $this->assertEquals(1, $nbDiffs);
  161. $this->assertEquals(1, count($tableDiff->getRenamedColumns()));
  162. $this->assertEquals(array(array($c1, $c2)), $tableDiff->getRenamedColumns());
  163. $this->assertEquals(array(), $tableDiff->getAddedColumns());
  164. $this->assertEquals(array(), $tableDiff->getRemovedColumns());
  165. }
  166. public function testCompareSeveralColumnDifferences()
  167. {
  168. $t1 = new Table();
  169. $c1 = new Column('col1');
  170. $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  171. $c1->getDomain()->replaceSize(255);
  172. $c1->setNotNull(false);
  173. $t1->addColumn($c1);
  174. $c2 = new Column('col2');
  175. $c2->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
  176. $c2->setNotNull(true);
  177. $t1->addColumn($c2);
  178. $c3 = new Column('col3');
  179. $c3->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  180. $c3->getDomain()->replaceSize(255);
  181. $t1->addColumn($c3);
  182. $t2 = new Table();
  183. $c4 = new Column('col1');
  184. $c4->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
  185. $c4->getDomain()->replaceScale(2);
  186. $c4->getDomain()->replaceSize(3);
  187. $c4->setNotNull(true);
  188. $c4->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
  189. $t2->addColumn($c4);
  190. $c5 = new Column('col22');
  191. $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
  192. $c5->setNotNull(true);
  193. $t2->addColumn($c5);
  194. $c6 = new Column('col4');
  195. $c6->getDomain()->copy($this->platform->getDomainForType('LONGVARCHAR'));
  196. $c6->getDomain()->setDefaultValue(new ColumnDefaultValue('123', ColumnDefaultValue::TYPE_VALUE));
  197. $t2->addColumn($c6);
  198. // col1 was modified, col2 was renamed, col3 was removed, col4 was added
  199. $tc = new TableComparator();
  200. $tc->setFromTable($t1);
  201. $tc->setToTable($t2);
  202. $nbDiffs = $tc->compareColumns();
  203. $tableDiff = $tc->getTableDiff();
  204. $this->assertEquals(4, $nbDiffs);
  205. $this->assertEquals(array(array($c2, $c5)), $tableDiff->getRenamedColumns());
  206. $this->assertEquals(array('col4' => $c6), $tableDiff->getAddedColumns());
  207. $this->assertEquals(array('col3' => $c3), $tableDiff->getRemovedColumns());
  208. $columnDiff = ColumnComparator::computeDiff($c1, $c4);
  209. $this->assertEquals(array('col1' => $columnDiff), $tableDiff->getModifiedColumns());
  210. }
  211. public function testCompareSeveralRenamedSameColumns()
  212. {
  213. $t1 = new Table();
  214. $c1 = new Column('col1');
  215. $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  216. $c1->getDomain()->replaceSize(255);
  217. $t1->addColumn($c1);
  218. $c2 = new Column('col2');
  219. $c2->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  220. $c2->getDomain()->replaceSize(255);
  221. $t1->addColumn($c2);
  222. $c3 = new Column('col3');
  223. $c3->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  224. $c3->getDomain()->replaceSize(255);
  225. $t1->addColumn($c3);
  226. $t2 = new Table();
  227. $c4 = new Column('col4');
  228. $c4->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  229. $c4->getDomain()->replaceSize(255);
  230. $t2->addColumn($c4);
  231. $c5 = new Column('col5');
  232. $c5->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  233. $c5->getDomain()->replaceSize(255);
  234. $t2->addColumn($c5);
  235. $c6 = new Column('col3');
  236. $c6->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
  237. $c6->getDomain()->replaceSize(255);
  238. $t2->addColumn($c6);
  239. // col1 and col2 were renamed
  240. $tc = new TableComparator();
  241. $tc->setFromTable($t1);
  242. $tc->setToTable($t2);
  243. $nbDiffs = $tc->compareColumns();
  244. $tableDiff = $tc->getTableDiff();
  245. $this->assertEquals(2, $nbDiffs);
  246. $this->assertEquals(array(array($c1, $c4), array($c2, $c5)), $tableDiff->getRenamedColumns());
  247. $this->assertEquals(array(), $tableDiff->getAddedColumns());
  248. $this->assertEquals(array(), $tableDiff->getRemovedColumns());
  249. $this->assertEquals(array(), $tableDiff->getModifiedColumns());
  250. }
  251. }