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