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 MIT license. For more information, see |
||
17 | * <http://www.doctrine-project.org>. |
||
18 | */ |
||
19 | |||
20 | namespace Doctrine\Tests\DBAL\Schema; |
||
21 | |||
22 | use Doctrine\DBAL\Schema\Column; |
||
23 | use Doctrine\DBAL\Schema\ColumnDiff; |
||
24 | use Doctrine\DBAL\Schema\Comparator; |
||
25 | use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
||
26 | use Doctrine\DBAL\Schema\Index; |
||
27 | use Doctrine\DBAL\Schema\Schema; |
||
28 | use Doctrine\DBAL\Schema\SchemaConfig; |
||
29 | use Doctrine\DBAL\Schema\SchemaDiff; |
||
30 | use Doctrine\DBAL\Schema\Sequence; |
||
31 | use Doctrine\DBAL\Schema\Table; |
||
32 | use Doctrine\DBAL\Schema\TableDiff; |
||
33 | use Doctrine\DBAL\Types\Type; |
||
34 | |||
35 | /** |
||
36 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
||
37 | * @link www.doctrine-project.org |
||
38 | * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. |
||
39 | * @license http://ez.no/licenses/new_bsd New BSD License |
||
40 | * @since 2.0 |
||
41 | * @version $Revision$ |
||
42 | * @author Benjamin Eberlei <[email protected]> |
||
43 | */ |
||
44 | class ComparatorTest extends \PHPUnit\Framework\TestCase |
||
45 | { |
||
46 | public function testCompareSame1() |
||
47 | { |
||
48 | $schema1 = new Schema( array( |
||
49 | 'bugdb' => new Table('bugdb', |
||
50 | array ( |
||
51 | 'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ), |
||
52 | ) |
||
53 | ), |
||
54 | ) ); |
||
55 | $schema2 = new Schema( array( |
||
56 | 'bugdb' => new Table('bugdb', |
||
57 | array ( |
||
58 | 'integerfield1' => new Column('integerfield1', Type::getType('integer') ), |
||
59 | ) |
||
60 | ), |
||
61 | ) ); |
||
62 | |||
63 | $expected = new SchemaDiff(); |
||
64 | $expected->fromSchema = $schema1; |
||
65 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
66 | } |
||
67 | |||
68 | public function testCompareSame2() |
||
69 | { |
||
70 | $schema1 = new Schema( array( |
||
71 | 'bugdb' => new Table('bugdb', |
||
72 | array ( |
||
73 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
74 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
75 | ) |
||
76 | ), |
||
77 | ) ); |
||
78 | $schema2 = new Schema( array( |
||
79 | 'bugdb' => new Table('bugdb', |
||
80 | array ( |
||
81 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
82 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
83 | ) |
||
84 | ), |
||
85 | ) ); |
||
86 | |||
87 | $expected = new SchemaDiff(); |
||
88 | $expected->fromSchema = $schema1; |
||
89 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
90 | } |
||
91 | |||
92 | View Code Duplication | public function testCompareMissingTable() |
|
93 | { |
||
94 | $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig; |
||
95 | $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer')))); |
||
96 | $table->setSchemaConfig($schemaConfig); |
||
97 | |||
98 | $schema1 = new Schema( array($table), array(), $schemaConfig ); |
||
99 | $schema2 = new Schema( array(), array(), $schemaConfig ); |
||
100 | |||
101 | $expected = new SchemaDiff( array(), array(), array('bugdb' => $table), $schema1 ); |
||
102 | |||
103 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
104 | } |
||
105 | |||
106 | View Code Duplication | public function testCompareNewTable() |
|
107 | { |
||
108 | $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig; |
||
109 | $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer')))); |
||
110 | $table->setSchemaConfig($schemaConfig); |
||
111 | |||
112 | $schema1 = new Schema( array(), array(), $schemaConfig ); |
||
113 | $schema2 = new Schema( array($table), array(), $schemaConfig ); |
||
114 | |||
115 | $expected = new SchemaDiff( array('bugdb' => $table), array(), array(), $schema1 ); |
||
116 | |||
117 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
118 | } |
||
119 | |||
120 | public function testCompareOnlyAutoincrementChanged() |
||
121 | { |
||
122 | $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true)); |
||
123 | $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false)); |
||
124 | |||
125 | $comparator = new Comparator(); |
||
126 | $changedProperties = $comparator->diffColumn($column1, $column2); |
||
127 | |||
128 | self::assertEquals(array('autoincrement'), $changedProperties); |
||
129 | } |
||
130 | |||
131 | public function testCompareMissingField() |
||
132 | { |
||
133 | $missingColumn = new Column('integerfield1', Type::getType('integer')); |
||
134 | $schema1 = new Schema( array( |
||
135 | 'bugdb' => new Table('bugdb', |
||
136 | array ( |
||
137 | 'integerfield1' => $missingColumn, |
||
138 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
139 | ) |
||
140 | ), |
||
141 | ) ); |
||
142 | $schema2 = new Schema( array( |
||
143 | 'bugdb' => new Table('bugdb', |
||
144 | array ( |
||
145 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
146 | ) |
||
147 | ), |
||
148 | ) ); |
||
149 | |||
150 | $expected = new SchemaDiff ( array(), |
||
151 | array ( |
||
152 | 'bugdb' => new TableDiff( 'bugdb', array(), array(), |
||
153 | array ( |
||
154 | 'integerfield1' => $missingColumn, |
||
155 | ) |
||
156 | ) |
||
157 | ) |
||
158 | ); |
||
159 | $expected->fromSchema = $schema1; |
||
160 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
161 | |||
162 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
163 | } |
||
164 | |||
165 | public function testCompareNewField() |
||
166 | { |
||
167 | $schema1 = new Schema( array( |
||
168 | 'bugdb' => new Table('bugdb', |
||
169 | array ( |
||
170 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
171 | ) |
||
172 | ), |
||
173 | ) ); |
||
174 | $schema2 = new Schema( array( |
||
175 | 'bugdb' => new Table('bugdb', |
||
176 | array ( |
||
177 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
178 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
179 | ) |
||
180 | ), |
||
181 | ) ); |
||
182 | |||
183 | $expected = new SchemaDiff ( array(), |
||
184 | array ( |
||
185 | 'bugdb' => new TableDiff ('bugdb', |
||
186 | array ( |
||
187 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
188 | ) |
||
189 | ), |
||
190 | ) |
||
191 | ); |
||
192 | $expected->fromSchema = $schema1; |
||
193 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
194 | |||
195 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
196 | } |
||
197 | |||
198 | View Code Duplication | public function testCompareChangedColumns_ChangeType() |
|
199 | { |
||
200 | $column1 = new Column('charfield1', Type::getType('string')); |
||
201 | $column2 = new Column('charfield1', Type::getType('integer')); |
||
202 | |||
203 | $c = new Comparator(); |
||
204 | self::assertEquals(array('type'), $c->diffColumn($column1, $column2)); |
||
205 | self::assertEquals(array(), $c->diffColumn($column1, $column1)); |
||
206 | } |
||
207 | |||
208 | public function testCompareChangedColumns_ChangeCustomSchemaOption() |
||
209 | { |
||
210 | $column1 = new Column('charfield1', Type::getType('string')); |
||
211 | $column2 = new Column('charfield1', Type::getType('string')); |
||
212 | |||
213 | $column1->setCustomSchemaOption('foo', 'bar'); |
||
214 | $column2->setCustomSchemaOption('foo', 'bar'); |
||
215 | |||
216 | $column1->setCustomSchemaOption('foo1', 'bar1'); |
||
217 | $column2->setCustomSchemaOption('foo2', 'bar2'); |
||
218 | |||
219 | $c = new Comparator(); |
||
220 | self::assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2)); |
||
221 | self::assertEquals(array(), $c->diffColumn($column1, $column1)); |
||
222 | } |
||
223 | |||
224 | public function testCompareChangeColumns_MultipleNewColumnsRename() |
||
225 | { |
||
226 | $tableA = new Table("foo"); |
||
227 | $tableA->addColumn('datefield1', 'datetime'); |
||
228 | |||
229 | $tableB = new Table("foo"); |
||
230 | $tableB->addColumn('new_datefield1', 'datetime'); |
||
231 | $tableB->addColumn('new_datefield2', 'datetime'); |
||
232 | |||
233 | $c = new Comparator(); |
||
234 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
235 | |||
236 | self::assertCount(1, $tableDiff->renamedColumns, "we should have one rename datefield1 => new_datefield1."); |
||
237 | self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1"); |
||
238 | self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added"); |
||
239 | self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!"); |
||
240 | self::assertCount(0, $tableDiff->removedColumns, "Nothing should be removed."); |
||
241 | self::assertCount(0, $tableDiff->changedColumns, "Nothing should be changed as all fields old & new have diff names."); |
||
242 | } |
||
243 | |||
244 | View Code Duplication | public function testCompareRemovedIndex() |
|
245 | { |
||
246 | $schema1 = new Schema( array( |
||
247 | 'bugdb' => new Table('bugdb', |
||
248 | array ( |
||
249 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
250 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
251 | ), |
||
252 | array ( |
||
253 | 'primary' => new Index('primary', |
||
254 | array( |
||
255 | 'integerfield1' |
||
256 | ), |
||
257 | true |
||
258 | ) |
||
259 | ) |
||
260 | ), |
||
261 | ) ); |
||
262 | $schema2 = new Schema( array( |
||
263 | 'bugdb' => new Table('bugdb', |
||
264 | array ( |
||
265 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
266 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
267 | ) |
||
268 | ), |
||
269 | ) ); |
||
270 | |||
271 | $expected = new SchemaDiff ( array(), |
||
272 | array ( |
||
273 | 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(), |
||
274 | array ( |
||
275 | 'primary' => new Index('primary', |
||
276 | array( |
||
277 | 'integerfield1' |
||
278 | ), |
||
279 | true |
||
280 | ) |
||
281 | ) |
||
282 | ), |
||
283 | ) |
||
284 | ); |
||
285 | $expected->fromSchema = $schema1; |
||
286 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
287 | |||
288 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
289 | } |
||
290 | |||
291 | View Code Duplication | public function testCompareNewIndex() |
|
292 | { |
||
293 | $schema1 = new Schema( array( |
||
294 | 'bugdb' => new Table('bugdb', |
||
295 | array ( |
||
296 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
297 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
298 | ) |
||
299 | ), |
||
300 | ) ); |
||
301 | $schema2 = new Schema( array( |
||
302 | 'bugdb' => new Table('bugdb', |
||
303 | array ( |
||
304 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
305 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
306 | ), |
||
307 | array ( |
||
308 | 'primary' => new Index('primary', |
||
309 | array( |
||
310 | 'integerfield1' |
||
311 | ), |
||
312 | true |
||
313 | ) |
||
314 | ) |
||
315 | ), |
||
316 | ) ); |
||
317 | |||
318 | $expected = new SchemaDiff ( array(), |
||
319 | array ( |
||
320 | 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), |
||
321 | array ( |
||
322 | 'primary' => new Index('primary', |
||
323 | array( |
||
324 | 'integerfield1' |
||
325 | ), |
||
326 | true |
||
327 | ) |
||
328 | ) |
||
329 | ), |
||
330 | ) |
||
331 | ); |
||
332 | $expected->fromSchema = $schema1; |
||
333 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
334 | |||
335 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); |
||
336 | } |
||
337 | |||
338 | View Code Duplication | public function testCompareChangedIndex() |
|
339 | { |
||
340 | $schema1 = new Schema( array( |
||
341 | 'bugdb' => new Table('bugdb', |
||
342 | array ( |
||
343 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
344 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
345 | ), |
||
346 | array ( |
||
347 | 'primary' => new Index('primary', |
||
348 | array( |
||
349 | 'integerfield1' |
||
350 | ), |
||
351 | true |
||
352 | ) |
||
353 | ) |
||
354 | ), |
||
355 | ) ); |
||
356 | $schema2 = new Schema( array( |
||
357 | 'bugdb' => new Table('bugdb', |
||
358 | array ( |
||
359 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
360 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
361 | ), |
||
362 | array ( |
||
363 | 'primary' => new Index('primary', |
||
364 | array('integerfield1', 'integerfield2'), |
||
365 | true |
||
366 | ) |
||
367 | ) |
||
368 | ), |
||
369 | ) ); |
||
370 | |||
371 | $expected = new SchemaDiff ( array(), |
||
372 | array ( |
||
373 | 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), |
||
374 | array ( |
||
375 | 'primary' => new Index('primary', |
||
376 | array( |
||
377 | 'integerfield1', |
||
378 | 'integerfield2' |
||
379 | ), |
||
380 | true |
||
381 | ) |
||
382 | ) |
||
383 | ), |
||
384 | ) |
||
385 | ); |
||
386 | $expected->fromSchema = $schema1; |
||
387 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
388 | |||
389 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 )); |
||
390 | } |
||
391 | |||
392 | View Code Duplication | public function testCompareChangedIndexFieldPositions() |
|
393 | { |
||
394 | $schema1 = new Schema( array( |
||
395 | 'bugdb' => new Table('bugdb', |
||
396 | array ( |
||
397 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
398 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
399 | ), |
||
400 | array ( |
||
401 | 'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true) |
||
402 | ) |
||
403 | ), |
||
404 | ) ); |
||
405 | $schema2 = new Schema( array( |
||
406 | 'bugdb' => new Table('bugdb', |
||
407 | array ( |
||
408 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
409 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
410 | ), |
||
411 | array ( |
||
412 | 'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true) |
||
413 | ) |
||
414 | ), |
||
415 | ) ); |
||
416 | |||
417 | $expected = new SchemaDiff ( array(), |
||
418 | array ( |
||
419 | 'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(), |
||
420 | array ( |
||
421 | 'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true) |
||
422 | ) |
||
423 | ), |
||
424 | ) |
||
425 | ); |
||
426 | $expected->fromSchema = $schema1; |
||
427 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
428 | |||
429 | self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 )); |
||
430 | } |
||
431 | |||
432 | public function testCompareSequences() |
||
433 | { |
||
434 | $seq1 = new Sequence('foo', 1, 1); |
||
435 | $seq2 = new Sequence('foo', 1, 2); |
||
436 | $seq3 = new Sequence('foo', 2, 1); |
||
437 | |||
438 | $c = new Comparator(); |
||
439 | |||
440 | self::assertTrue($c->diffSequence($seq1, $seq2)); |
||
441 | self::assertTrue($c->diffSequence($seq1, $seq3)); |
||
442 | } |
||
443 | |||
444 | View Code Duplication | public function testRemovedSequence() |
|
445 | { |
||
446 | $schema1 = new Schema(); |
||
447 | $seq = $schema1->createSequence('foo'); |
||
448 | |||
449 | $schema2 = new Schema(); |
||
450 | |||
451 | $c = new Comparator(); |
||
452 | $diffSchema = $c->compare($schema1, $schema2); |
||
453 | |||
454 | self::assertEquals(1, count($diffSchema->removedSequences)); |
||
455 | self::assertSame($seq, $diffSchema->removedSequences[0]); |
||
456 | } |
||
457 | |||
458 | View Code Duplication | public function testAddedSequence() |
|
459 | { |
||
460 | $schema1 = new Schema(); |
||
461 | |||
462 | $schema2 = new Schema(); |
||
463 | $seq = $schema2->createSequence('foo'); |
||
464 | |||
465 | $c = new Comparator(); |
||
466 | $diffSchema = $c->compare($schema1, $schema2); |
||
467 | |||
468 | self::assertEquals(1, count($diffSchema->newSequences)); |
||
469 | self::assertSame($seq, $diffSchema->newSequences[0]); |
||
470 | } |
||
471 | |||
472 | View Code Duplication | public function testTableAddForeignKey() |
|
473 | { |
||
474 | $tableForeign = new Table("bar"); |
||
475 | $tableForeign->addColumn('id', 'integer'); |
||
476 | |||
477 | $table1 = new Table("foo"); |
||
478 | $table1->addColumn('fk', 'integer'); |
||
479 | |||
480 | $table2 = new Table("foo"); |
||
481 | $table2->addColumn('fk', 'integer'); |
||
482 | $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id')); |
||
483 | |||
484 | $c = new Comparator(); |
||
485 | $tableDiff = $c->diffTable($table1, $table2); |
||
486 | |||
487 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
488 | self::assertEquals(1, count($tableDiff->addedForeignKeys)); |
||
489 | } |
||
490 | |||
491 | View Code Duplication | public function testTableRemoveForeignKey() |
|
492 | { |
||
493 | $tableForeign = new Table("bar"); |
||
494 | $tableForeign->addColumn('id', 'integer'); |
||
495 | |||
496 | $table1 = new Table("foo"); |
||
497 | $table1->addColumn('fk', 'integer'); |
||
498 | |||
499 | $table2 = new Table("foo"); |
||
500 | $table2->addColumn('fk', 'integer'); |
||
501 | $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id')); |
||
502 | |||
503 | $c = new Comparator(); |
||
504 | $tableDiff = $c->diffTable($table2, $table1); |
||
505 | |||
506 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
507 | self::assertEquals(1, count($tableDiff->removedForeignKeys)); |
||
508 | } |
||
509 | |||
510 | public function testTableUpdateForeignKey() |
||
511 | { |
||
512 | $tableForeign = new Table("bar"); |
||
513 | $tableForeign->addColumn('id', 'integer'); |
||
514 | |||
515 | $table1 = new Table("foo"); |
||
516 | $table1->addColumn('fk', 'integer'); |
||
517 | $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id')); |
||
518 | |||
519 | $table2 = new Table("foo"); |
||
520 | $table2->addColumn('fk', 'integer'); |
||
521 | $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE')); |
||
522 | |||
523 | $c = new Comparator(); |
||
524 | $tableDiff = $c->diffTable($table1, $table2); |
||
525 | |||
526 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
527 | self::assertEquals(1, count($tableDiff->changedForeignKeys)); |
||
528 | } |
||
529 | |||
530 | public function testMovedForeignKeyForeignTable() |
||
531 | { |
||
532 | $tableForeign = new Table("bar"); |
||
533 | $tableForeign->addColumn('id', 'integer'); |
||
534 | |||
535 | $tableForeign2 = new Table("bar2"); |
||
536 | $tableForeign2->addColumn('id', 'integer'); |
||
537 | |||
538 | $table1 = new Table("foo"); |
||
539 | $table1->addColumn('fk', 'integer'); |
||
540 | $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id')); |
||
541 | |||
542 | $table2 = new Table("foo"); |
||
543 | $table2->addColumn('fk', 'integer'); |
||
544 | $table2->addForeignKeyConstraint($tableForeign2, array('fk'), array('id')); |
||
545 | |||
546 | $c = new Comparator(); |
||
547 | $tableDiff = $c->diffTable($table1, $table2); |
||
548 | |||
549 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
550 | self::assertEquals(1, count($tableDiff->changedForeignKeys)); |
||
551 | } |
||
552 | |||
553 | View Code Duplication | public function testTablesCaseInsensitive() |
|
554 | { |
||
555 | $schemaA = new Schema(); |
||
556 | $schemaA->createTable('foo'); |
||
557 | $schemaA->createTable('bAr'); |
||
558 | $schemaA->createTable('BAZ'); |
||
559 | $schemaA->createTable('new'); |
||
560 | |||
561 | $schemaB = new Schema(); |
||
562 | $schemaB->createTable('FOO'); |
||
563 | $schemaB->createTable('bar'); |
||
564 | $schemaB->createTable('Baz'); |
||
565 | $schemaB->createTable('old'); |
||
566 | |||
567 | $c = new Comparator(); |
||
568 | $diff = $c->compare($schemaA, $schemaB); |
||
569 | |||
570 | self::assertSchemaTableChangeCount($diff, 1, 0, 1); |
||
571 | } |
||
572 | |||
573 | View Code Duplication | public function testSequencesCaseInsensitive() |
|
0 ignored issues
–
show
|
|||
574 | { |
||
575 | $schemaA = new Schema(); |
||
576 | $schemaA->createSequence('foo'); |
||
577 | $schemaA->createSequence('BAR'); |
||
578 | $schemaA->createSequence('Baz'); |
||
579 | $schemaA->createSequence('new'); |
||
580 | |||
581 | $schemaB = new Schema(); |
||
582 | $schemaB->createSequence('FOO'); |
||
583 | $schemaB->createSequence('Bar'); |
||
584 | $schemaB->createSequence('baz'); |
||
585 | $schemaB->createSequence('old'); |
||
586 | |||
587 | $c = new Comparator(); |
||
588 | $diff = $c->compare($schemaA, $schemaB); |
||
589 | |||
590 | self::assertSchemaSequenceChangeCount($diff, 1, 0, 1); |
||
591 | } |
||
592 | |||
593 | public function testCompareColumnCompareCaseInsensitive() |
||
594 | { |
||
595 | $tableA = new Table("foo"); |
||
596 | $tableA->addColumn('id', 'integer'); |
||
597 | |||
598 | $tableB = new Table("foo"); |
||
599 | $tableB->addColumn('ID', 'integer'); |
||
600 | |||
601 | $c = new Comparator(); |
||
602 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
603 | |||
604 | self::assertFalse($tableDiff); |
||
605 | } |
||
606 | |||
607 | public function testCompareIndexBasedOnPropertiesNotName() |
||
608 | { |
||
609 | $tableA = new Table("foo"); |
||
610 | $tableA->addColumn('id', 'integer'); |
||
611 | $tableA->addIndex(array("id"), "foo_bar_idx"); |
||
612 | |||
613 | $tableB = new Table("foo"); |
||
614 | $tableB->addColumn('ID', 'integer'); |
||
615 | $tableB->addIndex(array("id"), "bar_foo_idx"); |
||
616 | |||
617 | $c = new Comparator(); |
||
618 | $tableDiff = new TableDiff('foo'); |
||
619 | $tableDiff->fromTable = $tableA; |
||
620 | $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id')); |
||
621 | |||
622 | self::assertEquals( |
||
623 | $tableDiff, |
||
624 | $c->diffTable($tableA, $tableB) |
||
625 | ); |
||
626 | } |
||
627 | |||
628 | public function testCompareForeignKeyBasedOnPropertiesNotName() |
||
629 | { |
||
630 | $tableA = new Table("foo"); |
||
631 | $tableA->addColumn('id', 'integer'); |
||
632 | $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id')); |
||
633 | |||
634 | $tableB = new Table("foo"); |
||
635 | $tableB->addColumn('ID', 'integer'); |
||
636 | $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id')); |
||
637 | |||
638 | $c = new Comparator(); |
||
639 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
640 | |||
641 | self::assertFalse($tableDiff); |
||
642 | } |
||
643 | |||
644 | View Code Duplication | public function testCompareForeignKey_RestrictNoAction_AreTheSame() |
|
645 | { |
||
646 | $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION')); |
||
647 | $fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT')); |
||
648 | |||
649 | $c = new Comparator(); |
||
650 | self::assertFalse($c->diffForeignKey($fk1, $fk2)); |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * @group DBAL-492 |
||
655 | */ |
||
656 | public function testCompareForeignKeyNamesUnqualified_AsNoSchemaInformationIsAvailable() |
||
657 | { |
||
658 | $fk1 = new ForeignKeyConstraint(array("foo"), "foo.bar", array("baz"), "fk1"); |
||
659 | $fk2 = new ForeignKeyConstraint(array("foo"), "baz.bar", array("baz"), "fk1"); |
||
660 | |||
661 | $c = new Comparator(); |
||
662 | self::assertFalse($c->diffForeignKey($fk1, $fk2)); |
||
663 | } |
||
664 | |||
665 | public function testDetectRenameColumn() |
||
666 | { |
||
667 | $tableA = new Table("foo"); |
||
668 | $tableA->addColumn('foo', 'integer'); |
||
669 | |||
670 | $tableB = new Table("foo"); |
||
671 | $tableB->addColumn('bar', 'integer'); |
||
672 | |||
673 | $c = new Comparator(); |
||
674 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
675 | |||
676 | self::assertEquals(0, count($tableDiff->addedColumns)); |
||
677 | self::assertEquals(0, count($tableDiff->removedColumns)); |
||
678 | self::assertArrayHasKey('foo', $tableDiff->renamedColumns); |
||
679 | self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName()); |
||
680 | } |
||
681 | |||
682 | /** |
||
683 | * You can easily have ambiguities in the column renaming. If these |
||
684 | * are detected no renaming should take place, instead adding and dropping |
||
685 | * should be used exclusively. |
||
686 | * |
||
687 | * @group DBAL-24 |
||
688 | */ |
||
689 | public function testDetectRenameColumnAmbiguous() |
||
690 | { |
||
691 | $tableA = new Table("foo"); |
||
692 | $tableA->addColumn('foo', 'integer'); |
||
693 | $tableA->addColumn('bar', 'integer'); |
||
694 | |||
695 | $tableB = new Table("foo"); |
||
696 | $tableB->addColumn('baz', 'integer'); |
||
697 | |||
698 | $c = new Comparator(); |
||
699 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
700 | |||
701 | self::assertEquals(1, count($tableDiff->addedColumns), "'baz' should be added, not created through renaming!"); |
||
702 | self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!"); |
||
703 | self::assertEquals(2, count($tableDiff->removedColumns), "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'."); |
||
704 | self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed."); |
||
705 | self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed."); |
||
706 | self::assertEquals(0, count($tableDiff->renamedColumns), "no renamings should take place."); |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * @group DBAL-1063 |
||
711 | */ |
||
712 | public function testDetectRenameIndex() |
||
713 | { |
||
714 | $table1 = new Table('foo'); |
||
715 | $table1->addColumn('foo', 'integer'); |
||
716 | |||
717 | $table2 = clone $table1; |
||
718 | |||
719 | $table1->addIndex(array('foo'), 'idx_foo'); |
||
720 | |||
721 | $table2->addIndex(array('foo'), 'idx_bar'); |
||
722 | |||
723 | $comparator = new Comparator(); |
||
724 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
725 | |||
726 | self::assertCount(0, $tableDiff->addedIndexes); |
||
727 | self::assertCount(0, $tableDiff->removedIndexes); |
||
728 | self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes); |
||
729 | self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName()); |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * You can easily have ambiguities in the index renaming. If these |
||
734 | * are detected no renaming should take place, instead adding and dropping |
||
735 | * should be used exclusively. |
||
736 | * |
||
737 | * @group DBAL-1063 |
||
738 | */ |
||
739 | public function testDetectRenameIndexAmbiguous() |
||
740 | { |
||
741 | $table1 = new Table('foo'); |
||
742 | $table1->addColumn('foo', 'integer'); |
||
743 | |||
744 | $table2 = clone $table1; |
||
745 | |||
746 | $table1->addIndex(array('foo'), 'idx_foo'); |
||
747 | $table1->addIndex(array('foo'), 'idx_bar'); |
||
748 | |||
749 | $table2->addIndex(array('foo'), 'idx_baz'); |
||
750 | |||
751 | $comparator = new Comparator(); |
||
752 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
753 | |||
754 | self::assertCount(1, $tableDiff->addedIndexes); |
||
755 | self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes); |
||
756 | self::assertCount(2, $tableDiff->removedIndexes); |
||
757 | self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes); |
||
758 | self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes); |
||
759 | self::assertCount(0, $tableDiff->renamedIndexes); |
||
760 | } |
||
761 | |||
762 | View Code Duplication | public function testDetectChangeIdentifierType() |
|
763 | { |
||
764 | $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.'); |
||
765 | |||
766 | $tableA = new Table("foo"); |
||
767 | $tableA->addColumn('id', 'integer', array('autoincrement' => false)); |
||
768 | |||
769 | $tableB = new Table("foo"); |
||
770 | $tableB->addColumn('id', 'integer', array('autoincrement' => true)); |
||
771 | |||
772 | $c = new Comparator(); |
||
773 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
774 | |||
775 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
776 | self::assertArrayHasKey('id', $tableDiff->changedColumns); |
||
777 | } |
||
778 | |||
779 | |||
780 | /** |
||
781 | * @group DBAL-105 |
||
782 | */ |
||
783 | public function testDiff() |
||
784 | { |
||
785 | $table = new \Doctrine\DBAL\Schema\Table('twitter_users'); |
||
786 | $table->addColumn('id', 'integer', array('autoincrement' => true)); |
||
787 | $table->addColumn('twitterId', 'integer', array('nullable' => false)); |
||
788 | $table->addColumn('displayName', 'string', array('nullable' => false)); |
||
789 | $table->setPrimaryKey(array('id')); |
||
790 | |||
791 | $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users'); |
||
792 | $newtable->addColumn('id', 'integer', array('autoincrement' => true)); |
||
793 | $newtable->addColumn('twitter_id', 'integer', array('nullable' => false)); |
||
794 | $newtable->addColumn('display_name', 'string', array('nullable' => false)); |
||
795 | $newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true)); |
||
796 | $newtable->setPrimaryKey(array('id')); |
||
797 | |||
798 | $c = new Comparator(); |
||
799 | $tableDiff = $c->diffTable($table, $newtable); |
||
800 | |||
801 | self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); |
||
802 | self::assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns)); |
||
803 | self::assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns)); |
||
804 | self::assertEquals(0, count($tableDiff->removedColumns)); |
||
805 | } |
||
806 | |||
807 | |||
808 | /** |
||
809 | * @group DBAL-112 |
||
810 | */ |
||
811 | public function testChangedSequence() |
||
812 | { |
||
813 | $schema = new Schema(); |
||
814 | $sequence = $schema->createSequence('baz'); |
||
815 | |||
816 | $schemaNew = clone $schema; |
||
817 | /* @var $schemaNew Schema */ |
||
818 | $schemaNew->getSequence('baz')->setAllocationSize(20); |
||
819 | |||
820 | $c = new \Doctrine\DBAL\Schema\Comparator; |
||
821 | $diff = $c->compare($schema, $schemaNew); |
||
822 | |||
823 | self::assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz')); |
||
824 | } |
||
825 | |||
826 | /** |
||
827 | * @group DBAL-106 |
||
828 | */ |
||
829 | View Code Duplication | public function testDiffDecimalWithNullPrecision() |
|
830 | { |
||
831 | $column = new Column('foo', Type::getType('decimal')); |
||
832 | $column->setPrecision(null); |
||
833 | |||
834 | $column2 = new Column('foo', Type::getType('decimal')); |
||
835 | |||
836 | $c = new Comparator(); |
||
837 | self::assertEquals(array(), $c->diffColumn($column, $column2)); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * @group DBAL-204 |
||
842 | */ |
||
843 | View Code Duplication | public function testFqnSchemaComparison() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
844 | { |
||
845 | $config = new SchemaConfig(); |
||
846 | $config->setName("foo"); |
||
847 | |||
848 | $oldSchema = new Schema(array(), array(), $config); |
||
849 | $oldSchema->createTable('bar'); |
||
850 | |||
851 | $newSchema= new Schema(array(), array(), $config); |
||
852 | $newSchema->createTable('foo.bar'); |
||
853 | |||
854 | $expected = new SchemaDiff(); |
||
855 | $expected->fromSchema = $oldSchema; |
||
856 | |||
857 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
858 | } |
||
859 | |||
860 | /** |
||
861 | * @group DBAL-669 |
||
862 | */ |
||
863 | public function testNamespacesComparison() |
||
864 | { |
||
865 | $config = new SchemaConfig(); |
||
866 | $config->setName("schemaName"); |
||
867 | |||
868 | $oldSchema = new Schema(array(), array(), $config); |
||
869 | $oldSchema->createTable('taz'); |
||
870 | $oldSchema->createTable('war.tab'); |
||
871 | |||
872 | $newSchema= new Schema(array(), array(), $config); |
||
873 | $newSchema->createTable('bar.tab'); |
||
874 | $newSchema->createTable('baz.tab'); |
||
875 | $newSchema->createTable('war.tab'); |
||
876 | |||
877 | $expected = new SchemaDiff(); |
||
878 | $expected->fromSchema = $oldSchema; |
||
879 | $expected->newNamespaces = array('bar' => 'bar', 'baz' => 'baz'); |
||
880 | |||
881 | $diff = Comparator::compareSchemas($oldSchema, $newSchema); |
||
882 | |||
883 | self::assertEquals(array('bar' => 'bar', 'baz' => 'baz'), $diff->newNamespaces); |
||
884 | self::assertCount(2, $diff->newTables); |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * @group DBAL-204 |
||
889 | */ |
||
890 | View Code Duplication | public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
891 | { |
||
892 | $config = new SchemaConfig(); |
||
893 | $config->setName("foo"); |
||
894 | |||
895 | $oldSchema = new Schema(array(), array(), $config); |
||
896 | $oldSchema->createTable('foo.bar'); |
||
897 | |||
898 | $newSchema = new Schema(); |
||
899 | $newSchema->createTable('bar'); |
||
900 | |||
901 | $expected = new SchemaDiff(); |
||
902 | $expected->fromSchema = $oldSchema; |
||
903 | |||
904 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
905 | } |
||
906 | |||
907 | /** |
||
908 | * @group DBAL-204 |
||
909 | */ |
||
910 | View Code Duplication | public function testFqnSchemaComparisonNoSchemaSame() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
911 | { |
||
912 | $config = new SchemaConfig(); |
||
913 | $config->setName("foo"); |
||
914 | $oldSchema = new Schema(array(), array(), $config); |
||
915 | $oldSchema->createTable('bar'); |
||
916 | |||
917 | $newSchema = new Schema(); |
||
918 | $newSchema->createTable('bar'); |
||
919 | |||
920 | $expected = new SchemaDiff(); |
||
921 | $expected->fromSchema = $oldSchema; |
||
922 | |||
923 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
924 | } |
||
925 | |||
926 | /** |
||
927 | * @group DDC-1657 |
||
928 | */ |
||
929 | View Code Duplication | public function testAutoIncrementSequences() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
930 | { |
||
931 | $oldSchema = new Schema(); |
||
932 | $table = $oldSchema->createTable("foo"); |
||
933 | $table->addColumn("id", "integer", array("autoincrement" => true)); |
||
934 | $table->setPrimaryKey(array("id")); |
||
935 | $oldSchema->createSequence("foo_id_seq"); |
||
936 | |||
937 | $newSchema = new Schema(); |
||
938 | $table = $newSchema->createTable("foo"); |
||
939 | $table->addColumn("id", "integer", array("autoincrement" => true)); |
||
940 | $table->setPrimaryKey(array("id")); |
||
941 | |||
942 | $c = new Comparator(); |
||
943 | $diff = $c->compare($oldSchema, $newSchema); |
||
944 | |||
945 | self::assertCount(0, $diff->removedSequences); |
||
946 | } |
||
947 | |||
948 | |||
949 | /** |
||
950 | * Check that added autoincrement sequence is not populated in newSequences |
||
951 | * @group DBAL-562 |
||
952 | */ |
||
953 | View Code Duplication | public function testAutoIncrementNoSequences() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
954 | { |
||
955 | $oldSchema = new Schema(); |
||
956 | $table = $oldSchema->createTable("foo"); |
||
957 | $table->addColumn("id", "integer", array("autoincrement" => true)); |
||
958 | $table->setPrimaryKey(array("id")); |
||
959 | |||
960 | $newSchema = new Schema(); |
||
961 | $table = $newSchema->createTable("foo"); |
||
962 | $table->addColumn("id", "integer", array("autoincrement" => true)); |
||
963 | $table->setPrimaryKey(array("id")); |
||
964 | $newSchema->createSequence("foo_id_seq"); |
||
965 | |||
966 | $c = new Comparator(); |
||
967 | $diff = $c->compare($oldSchema, $newSchema); |
||
968 | |||
969 | self::assertCount(0, $diff->newSequences); |
||
970 | } |
||
971 | /** |
||
972 | * You can get multiple drops for a FK when a table referenced by a foreign |
||
973 | * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys |
||
974 | * array because of the dropped table, and once on changedTables array. We |
||
975 | * now check that the key is present once. |
||
976 | */ |
||
977 | public function testAvoidMultipleDropForeignKey() |
||
978 | { |
||
979 | $oldSchema = new Schema(); |
||
980 | |||
981 | $tableA = $oldSchema->createTable('table_a'); |
||
982 | $tableA->addColumn('id', 'integer'); |
||
983 | |||
984 | $tableB = $oldSchema->createTable('table_b'); |
||
985 | $tableB->addColumn('id', 'integer'); |
||
986 | |||
987 | $tableC = $oldSchema->createTable('table_c'); |
||
988 | $tableC->addColumn('id', 'integer'); |
||
989 | $tableC->addColumn('table_a_id', 'integer'); |
||
990 | $tableC->addColumn('table_b_id', 'integer'); |
||
991 | |||
992 | $tableC->addForeignKeyConstraint($tableA, array('table_a_id'), array('id')); |
||
993 | $tableC->addForeignKeyConstraint($tableB, array('table_b_id'), array('id')); |
||
994 | |||
995 | $newSchema = new Schema(); |
||
996 | |||
997 | $tableB = $newSchema->createTable('table_b'); |
||
998 | $tableB->addColumn('id', 'integer'); |
||
999 | |||
1000 | $tableC = $newSchema->createTable('table_c'); |
||
1001 | $tableC->addColumn('id', 'integer'); |
||
1002 | |||
1003 | $comparator = new Comparator(); |
||
1004 | $schemaDiff = $comparator->compare($oldSchema, $newSchema); |
||
1005 | |||
1006 | self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys); |
||
1007 | self::assertCount(1, $schemaDiff->orphanedForeignKeys); |
||
1008 | } |
||
1009 | |||
1010 | public function testCompareChangedColumn() |
||
1011 | { |
||
1012 | $oldSchema = new Schema(); |
||
1013 | |||
1014 | $tableFoo = $oldSchema->createTable('foo'); |
||
1015 | $tableFoo->addColumn('id', 'integer'); |
||
1016 | |||
1017 | $newSchema = new Schema(); |
||
1018 | $table = $newSchema->createTable('foo'); |
||
1019 | $table->addColumn('id', 'string'); |
||
1020 | |||
1021 | $expected = new SchemaDiff(); |
||
1022 | $expected->fromSchema = $oldSchema; |
||
1023 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
1024 | $tableDiff->fromTable = $tableFoo; |
||
1025 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
1026 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
1027 | $columnDiff->changedProperties = array('type'); |
||
1028 | |||
1029 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
1030 | } |
||
1031 | |||
1032 | public function testCompareChangedBinaryColumn() |
||
1033 | { |
||
1034 | $oldSchema = new Schema(); |
||
1035 | |||
1036 | $tableFoo = $oldSchema->createTable('foo'); |
||
1037 | $tableFoo->addColumn('id', 'binary'); |
||
1038 | |||
1039 | $newSchema = new Schema(); |
||
1040 | $table = $newSchema->createTable('foo'); |
||
1041 | $table->addColumn('id', 'binary', array('length' => 42, 'fixed' => true)); |
||
1042 | |||
1043 | $expected = new SchemaDiff(); |
||
1044 | $expected->fromSchema = $oldSchema; |
||
1045 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
1046 | $tableDiff->fromTable = $tableFoo; |
||
1047 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
1048 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
1049 | $columnDiff->changedProperties = array('length', 'fixed'); |
||
1050 | |||
1051 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
1052 | } |
||
1053 | |||
1054 | /** |
||
1055 | * @group DBAL-617 |
||
1056 | */ |
||
1057 | View Code Duplication | public function testCompareQuotedAndUnquotedForeignKeyColumns() |
|
1058 | { |
||
1059 | $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION')); |
||
1060 | $fk2 = new ForeignKeyConstraint(array("`foo`"), "bar", array("`baz`"), "fk1", array('onDelete' => 'NO ACTION')); |
||
1061 | |||
1062 | $comparator = new Comparator(); |
||
1063 | $diff = $comparator->diffForeignKey($fk1, $fk2); |
||
1064 | |||
1065 | self::assertFalse($diff); |
||
1066 | } |
||
1067 | |||
1068 | /** |
||
1069 | * @param SchemaDiff $diff |
||
1070 | * @param int $newTableCount |
||
1071 | * @param int $changeTableCount |
||
1072 | * @param int $removeTableCount |
||
1073 | */ |
||
1074 | public function assertSchemaTableChangeCount($diff, $newTableCount=0, $changeTableCount=0, $removeTableCount=0) |
||
1075 | { |
||
1076 | self::assertEquals($newTableCount, count($diff->newTables)); |
||
1077 | self::assertEquals($changeTableCount, count($diff->changedTables)); |
||
1078 | self::assertEquals($removeTableCount, count($diff->removedTables)); |
||
1079 | } |
||
1080 | |||
1081 | /** |
||
1082 | * @param SchemaDiff $diff |
||
1083 | * @param int $newSequenceCount |
||
1084 | * @param int $changeSequenceCount |
||
1085 | * @param int $changeSequenceCount |
||
1086 | */ |
||
1087 | public function assertSchemaSequenceChangeCount($diff, $newSequenceCount=0, $changeSequenceCount=0, $removeSequenceCount=0) |
||
1088 | { |
||
1089 | self::assertEquals($newSequenceCount, count($diff->newSequences), "Expected number of new sequences is wrong."); |
||
1090 | self::assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong."); |
||
1091 | self::assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong."); |
||
1092 | } |
||
1093 | |||
1094 | public function testDiffColumnPlatformOptions() |
||
1095 | { |
||
1096 | $column1 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'bar'))); |
||
1097 | $column2 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'foobar' => 'foobar'))); |
||
1098 | $column3 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'rab'))); |
||
1099 | $column4 = new Column('foo', Type::getType('string')); |
||
1100 | |||
1101 | $comparator = new Comparator(); |
||
1102 | |||
1103 | self::assertEquals(array(), $comparator->diffColumn($column1, $column2)); |
||
1104 | self::assertEquals(array(), $comparator->diffColumn($column2, $column1)); |
||
1105 | self::assertEquals(array('bar'), $comparator->diffColumn($column1, $column3)); |
||
1106 | self::assertEquals(array('bar'), $comparator->diffColumn($column3, $column1)); |
||
1107 | self::assertEquals(array(), $comparator->diffColumn($column1, $column4)); |
||
1108 | self::assertEquals(array(), $comparator->diffColumn($column4, $column1)); |
||
1109 | } |
||
1110 | |||
1111 | public function testComplexDiffColumn() |
||
1112 | { |
||
1113 | $column1 = new Column('foo', Type::getType('string'), array( |
||
1114 | 'platformOptions' => array('foo' => 'foo'), |
||
1115 | 'customSchemaOptions' => array('foo' => 'bar'), |
||
1116 | )); |
||
1117 | |||
1118 | $column2 = new Column('foo', Type::getType('string'), array( |
||
1119 | 'platformOptions' => array('foo' => 'bar'), |
||
1120 | )); |
||
1121 | |||
1122 | $comparator = new Comparator(); |
||
1123 | |||
1124 | self::assertEquals(array(), $comparator->diffColumn($column1, $column2)); |
||
1125 | self::assertEquals(array(), $comparator->diffColumn($column2, $column1)); |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * @group DBAL-669 |
||
1130 | */ |
||
1131 | public function testComparesNamespaces() |
||
1132 | { |
||
1133 | $comparator = new Comparator(); |
||
1134 | $fromSchema = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema') |
||
1135 | ->setMethods(array('getNamespaces', 'hasNamespace')) |
||
1136 | ->getMock(); |
||
1137 | $toSchema = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema') |
||
1138 | ->setMethods(array('getNamespaces', 'hasNamespace')) |
||
1139 | ->getMock(); |
||
1140 | |||
1141 | $fromSchema->expects($this->once()) |
||
1142 | ->method('getNamespaces') |
||
1143 | ->will($this->returnValue(array('foo', 'bar'))); |
||
1144 | |||
1145 | $fromSchema->expects($this->at(0)) |
||
1146 | ->method('hasNamespace') |
||
1147 | ->with('bar') |
||
1148 | ->will($this->returnValue(true)); |
||
1149 | |||
1150 | $fromSchema->expects($this->at(1)) |
||
1151 | ->method('hasNamespace') |
||
1152 | ->with('baz') |
||
1153 | ->will($this->returnValue(false)); |
||
1154 | |||
1155 | $toSchema->expects($this->once()) |
||
1156 | ->method('getNamespaces') |
||
1157 | ->will($this->returnValue(array('bar', 'baz'))); |
||
1158 | |||
1159 | $toSchema->expects($this->at(1)) |
||
1160 | ->method('hasNamespace') |
||
1161 | ->with('foo') |
||
1162 | ->will($this->returnValue(false)); |
||
1163 | |||
1164 | $toSchema->expects($this->at(2)) |
||
1165 | ->method('hasNamespace') |
||
1166 | ->with('bar') |
||
1167 | ->will($this->returnValue(true)); |
||
1168 | |||
1169 | $expected = new SchemaDiff(); |
||
1170 | $expected->fromSchema = $fromSchema; |
||
1171 | $expected->newNamespaces = array('baz' => 'baz'); |
||
1172 | $expected->removedNamespaces = array('foo' => 'foo'); |
||
1173 | |||
1174 | self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema)); |
||
1175 | } |
||
1176 | |||
1177 | public function testCompareGuidColumns() |
||
1178 | { |
||
1179 | $comparator = new Comparator(); |
||
1180 | |||
1181 | $column1 = new Column('foo', Type::getType('guid'), array('comment' => 'GUID 1')); |
||
1182 | $column2 = new Column( |
||
1183 | 'foo', |
||
1184 | Type::getType('guid'), |
||
1185 | array('notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.') |
||
1186 | ); |
||
1187 | |||
1188 | self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column1, $column2)); |
||
1189 | self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column2, $column1)); |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * @group DBAL-1009 |
||
1194 | * |
||
1195 | * @dataProvider getCompareColumnComments |
||
1196 | */ |
||
1197 | public function testCompareColumnComments($comment1, $comment2, $equals) |
||
1198 | { |
||
1199 | $column1 = new Column('foo', Type::getType('integer'), array('comment' => $comment1)); |
||
1200 | $column2 = new Column('foo', Type::getType('integer'), array('comment' => $comment2)); |
||
1201 | |||
1202 | $comparator = new Comparator(); |
||
1203 | |||
1204 | $expectedDiff = $equals ? array() : array('comment'); |
||
1205 | |||
1206 | $actualDiff = $comparator->diffColumn($column1, $column2); |
||
1207 | |||
1208 | self::assertSame($expectedDiff, $actualDiff); |
||
1209 | |||
1210 | $actualDiff = $comparator->diffColumn($column2, $column1); |
||
1211 | |||
1212 | self::assertSame($expectedDiff, $actualDiff); |
||
1213 | } |
||
1214 | |||
1215 | public function getCompareColumnComments() |
||
1216 | { |
||
1217 | return array( |
||
1218 | array(null, null, true), |
||
1219 | array('', '', true), |
||
1220 | array(' ', ' ', true), |
||
1221 | array('0', '0', true), |
||
1222 | array('foo', 'foo', true), |
||
1223 | |||
1224 | array(null, '', true), |
||
1225 | array(null, ' ', false), |
||
1226 | array(null, '0', false), |
||
1227 | array(null, 'foo', false), |
||
1228 | |||
1229 | array('', ' ', false), |
||
1230 | array('', '0', false), |
||
1231 | array('', 'foo', false), |
||
1232 | |||
1233 | array(' ', '0', false), |
||
1234 | array(' ', 'foo', false), |
||
1235 | |||
1236 | array('0', 'foo', false), |
||
1237 | ); |
||
1238 | } |
||
1239 | |||
1240 | public function testForeignKeyRemovalWithRenamedLocalColumn() |
||
1241 | { |
||
1242 | $fromSchema = new Schema( array( |
||
1243 | 'table1' => new Table('table1', |
||
1244 | array( |
||
1245 | 'id' => new Column('id', Type::getType('integer')), |
||
1246 | )), |
||
1247 | 'table2' => new Table('table2', |
||
1248 | array( |
||
1249 | 'id' => new Column('id', Type::getType('integer')), |
||
1250 | 'id_table1' => new Column('id_table1', Type::getType('integer')) |
||
1251 | ), |
||
1252 | array(), |
||
1253 | array( |
||
1254 | new ForeignKeyConstraint(array('id_table1'), 'table1', array('id'), 'fk_table2_table1') |
||
1255 | )) |
||
1256 | )); |
||
1257 | $toSchema = new Schema( array( |
||
1258 | 'table2' => new Table('table2', |
||
1259 | array( |
||
1260 | 'id' => new Column('id', Type::getType('integer')), |
||
1261 | 'id_table3' => new Column('id_table3', Type::getType('integer')) |
||
1262 | ), |
||
1263 | array(), |
||
1264 | array( |
||
1265 | new ForeignKeyConstraint(array('id_table3'), 'table3', array('id'), 'fk_table2_table3') |
||
1266 | )), |
||
1267 | 'table3' => new Table('table3', |
||
1268 | array( |
||
1269 | 'id' => new Column('id', Type::getType('integer')) |
||
1270 | )) |
||
1271 | )); |
||
1272 | $actual = Comparator::compareSchemas($fromSchema, $toSchema); |
||
1273 | self::assertArrayHasKey("table2", $actual->changedTables); |
||
1274 | self::assertCount(1, $actual->orphanedForeignKeys); |
||
1275 | self::assertEquals("fk_table2_table1", $actual->orphanedForeignKeys[0]->getName()); |
||
1276 | self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, "FK to table3 should be added."); |
||
1277 | self::assertEquals("table3", $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName()); |
||
1278 | } |
||
1279 | } |
||
1280 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.