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