Total Complexity | 59 |
Total Lines | 1328 |
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() |
||
188 | { |
||
189 | $column1 = new Column('charfield1', Type::getType('string')); |
||
190 | $column2 = new Column('charfield1', Type::getType('integer')); |
||
191 | |||
192 | $c = new Comparator(); |
||
193 | self::assertEquals(['type'], $c->diffColumn($column1, $column2)); |
||
194 | self::assertEquals([], $c->diffColumn($column1, $column1)); |
||
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() |
||
507 | { |
||
508 | $fromSchema = new Schema(); |
||
509 | $fromSchema->createView('foo', 'bar'); |
||
510 | |||
511 | $toSchema = new Schema(); |
||
512 | $view2 = $toSchema->createView('foo', 'baz'); |
||
513 | |||
514 | $c = new Comparator(); |
||
515 | $diffSchema = $c->compare($fromSchema, $toSchema); |
||
516 | |||
517 | $this->assertEquals(1, count($diffSchema->changedViews)); |
||
518 | $this->assertSame($view2, $diffSchema->changedViews[$view2->getName()]); |
||
519 | } |
||
520 | |||
521 | public function testAddedViews() |
||
522 | { |
||
523 | $schema1 = new Schema(); |
||
524 | |||
525 | $schema2 = new Schema(); |
||
526 | $view = $schema2->createView('foo', 'bar'); |
||
527 | |||
528 | $c = new Comparator(); |
||
529 | $diffSchema = $c->compare($schema1, $schema2); |
||
530 | |||
531 | $this->assertEquals(1, count($diffSchema->newViews)); |
||
532 | $this->assertSame($view, $diffSchema->newViews[$view->getName()]); |
||
533 | } |
||
534 | |||
535 | public function testTableAddForeignKey() |
||
536 | { |
||
537 | $tableForeign = new Table('bar'); |
||
538 | $tableForeign->addColumn('id', 'integer'); |
||
539 | |||
540 | $table1 = new Table('foo'); |
||
541 | $table1->addColumn('fk', 'integer'); |
||
542 | |||
543 | $table2 = new Table('foo'); |
||
544 | $table2->addColumn('fk', 'integer'); |
||
545 | $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
546 | |||
547 | $c = new Comparator(); |
||
548 | $tableDiff = $c->diffTable($table1, $table2); |
||
549 | |||
550 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
551 | self::assertCount(1, $tableDiff->addedForeignKeys); |
||
552 | } |
||
553 | |||
554 | public function testTableRemoveForeignKey() |
||
555 | { |
||
556 | $tableForeign = new Table('bar'); |
||
557 | $tableForeign->addColumn('id', 'integer'); |
||
558 | |||
559 | $table1 = new Table('foo'); |
||
560 | $table1->addColumn('fk', 'integer'); |
||
561 | |||
562 | $table2 = new Table('foo'); |
||
563 | $table2->addColumn('fk', 'integer'); |
||
564 | $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
565 | |||
566 | $c = new Comparator(); |
||
567 | $tableDiff = $c->diffTable($table2, $table1); |
||
568 | |||
569 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
570 | self::assertCount(1, $tableDiff->removedForeignKeys); |
||
571 | } |
||
572 | |||
573 | public function testTableUpdateForeignKey() |
||
574 | { |
||
575 | $tableForeign = new Table('bar'); |
||
576 | $tableForeign->addColumn('id', 'integer'); |
||
577 | |||
578 | $table1 = new Table('foo'); |
||
579 | $table1->addColumn('fk', 'integer'); |
||
580 | $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
581 | |||
582 | $table2 = new Table('foo'); |
||
583 | $table2->addColumn('fk', 'integer'); |
||
584 | $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']); |
||
585 | |||
586 | $c = new Comparator(); |
||
587 | $tableDiff = $c->diffTable($table1, $table2); |
||
588 | |||
589 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
590 | self::assertCount(1, $tableDiff->changedForeignKeys); |
||
591 | } |
||
592 | |||
593 | public function testMovedForeignKeyForeignTable() |
||
594 | { |
||
595 | $tableForeign = new Table('bar'); |
||
596 | $tableForeign->addColumn('id', 'integer'); |
||
597 | |||
598 | $tableForeign2 = new Table('bar2'); |
||
599 | $tableForeign2->addColumn('id', 'integer'); |
||
600 | |||
601 | $table1 = new Table('foo'); |
||
602 | $table1->addColumn('fk', 'integer'); |
||
603 | $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
604 | |||
605 | $table2 = new Table('foo'); |
||
606 | $table2->addColumn('fk', 'integer'); |
||
607 | $table2->addForeignKeyConstraint($tableForeign2, ['fk'], ['id']); |
||
608 | |||
609 | $c = new Comparator(); |
||
610 | $tableDiff = $c->diffTable($table1, $table2); |
||
611 | |||
612 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
613 | self::assertCount(1, $tableDiff->changedForeignKeys); |
||
614 | } |
||
615 | |||
616 | public function testTablesCaseInsensitive() |
||
617 | { |
||
618 | $schemaA = new Schema(); |
||
619 | $schemaA->createTable('foo'); |
||
620 | $schemaA->createTable('bAr'); |
||
621 | $schemaA->createTable('BAZ'); |
||
622 | $schemaA->createTable('new'); |
||
623 | |||
624 | $schemaB = new Schema(); |
||
625 | $schemaB->createTable('FOO'); |
||
626 | $schemaB->createTable('bar'); |
||
627 | $schemaB->createTable('Baz'); |
||
628 | $schemaB->createTable('old'); |
||
629 | |||
630 | $c = new Comparator(); |
||
631 | $diff = $c->compare($schemaA, $schemaB); |
||
632 | |||
633 | self::assertSchemaTableChangeCount($diff, 1, 0, 1); |
||
|
|||
634 | } |
||
635 | |||
636 | public function testSequencesCaseInsensitive() |
||
637 | { |
||
638 | $schemaA = new Schema(); |
||
639 | $schemaA->createSequence('foo'); |
||
640 | $schemaA->createSequence('BAR'); |
||
641 | $schemaA->createSequence('Baz'); |
||
642 | $schemaA->createSequence('new'); |
||
643 | |||
644 | $schemaB = new Schema(); |
||
645 | $schemaB->createSequence('FOO'); |
||
646 | $schemaB->createSequence('Bar'); |
||
647 | $schemaB->createSequence('baz'); |
||
648 | $schemaB->createSequence('old'); |
||
649 | |||
650 | $c = new Comparator(); |
||
651 | $diff = $c->compare($schemaA, $schemaB); |
||
652 | |||
653 | self::assertSchemaSequenceChangeCount($diff, 1, 0, 1); |
||
654 | } |
||
655 | |||
656 | public function testCompareColumnCompareCaseInsensitive() |
||
657 | { |
||
658 | $tableA = new Table('foo'); |
||
659 | $tableA->addColumn('id', 'integer'); |
||
660 | |||
661 | $tableB = new Table('foo'); |
||
662 | $tableB->addColumn('ID', 'integer'); |
||
663 | |||
664 | $c = new Comparator(); |
||
665 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
666 | |||
667 | self::assertFalse($tableDiff); |
||
668 | } |
||
669 | |||
670 | public function testCompareIndexBasedOnPropertiesNotName() |
||
671 | { |
||
672 | $tableA = new Table('foo'); |
||
673 | $tableA->addColumn('id', 'integer'); |
||
674 | $tableA->addIndex(['id'], 'foo_bar_idx'); |
||
675 | |||
676 | $tableB = new Table('foo'); |
||
677 | $tableB->addColumn('ID', 'integer'); |
||
678 | $tableB->addIndex(['id'], 'bar_foo_idx'); |
||
679 | |||
680 | $c = new Comparator(); |
||
681 | $tableDiff = new TableDiff('foo'); |
||
682 | $tableDiff->fromTable = $tableA; |
||
683 | $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']); |
||
684 | |||
685 | self::assertEquals( |
||
686 | $tableDiff, |
||
687 | $c->diffTable($tableA, $tableB) |
||
688 | ); |
||
689 | } |
||
690 | |||
691 | public function testCompareForeignKeyBasedOnPropertiesNotName() |
||
692 | { |
||
693 | $tableA = new Table('foo'); |
||
694 | $tableA->addColumn('id', 'integer'); |
||
695 | $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', ['id'], ['id']); |
||
1 ignored issue
–
show
|
|||
696 | |||
697 | $tableB = new Table('foo'); |
||
698 | $tableB->addColumn('ID', 'integer'); |
||
699 | $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', ['id'], ['id']); |
||
1 ignored issue
–
show
|
|||
700 | |||
701 | $c = new Comparator(); |
||
702 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
703 | |||
704 | self::assertFalse($tableDiff); |
||
705 | } |
||
706 | |||
707 | public function testCompareForeignKeyRestrictNoActionAreTheSame() |
||
708 | { |
||
709 | $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
710 | $fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'RESTRICT']); |
||
711 | |||
712 | $c = new Comparator(); |
||
713 | self::assertFalse($c->diffForeignKey($fk1, $fk2)); |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * @group DBAL-492 |
||
718 | */ |
||
719 | public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable() |
||
720 | { |
||
721 | $fk1 = new ForeignKeyConstraint(['foo'], 'foo.bar', ['baz'], 'fk1'); |
||
722 | $fk2 = new ForeignKeyConstraint(['foo'], 'baz.bar', ['baz'], 'fk1'); |
||
723 | |||
724 | $c = new Comparator(); |
||
725 | self::assertFalse($c->diffForeignKey($fk1, $fk2)); |
||
726 | } |
||
727 | |||
728 | public function testDetectRenameColumn() |
||
729 | { |
||
730 | $tableA = new Table('foo'); |
||
731 | $tableA->addColumn('foo', 'integer'); |
||
732 | |||
733 | $tableB = new Table('foo'); |
||
734 | $tableB->addColumn('bar', 'integer'); |
||
735 | |||
736 | $c = new Comparator(); |
||
737 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
738 | |||
739 | self::assertCount(0, $tableDiff->addedColumns); |
||
740 | self::assertCount(0, $tableDiff->removedColumns); |
||
741 | self::assertArrayHasKey('foo', $tableDiff->renamedColumns); |
||
742 | self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName()); |
||
743 | } |
||
744 | |||
745 | /** |
||
746 | * You can easily have ambiguities in the column renaming. If these |
||
747 | * are detected no renaming should take place, instead adding and dropping |
||
748 | * should be used exclusively. |
||
749 | * |
||
750 | * @group DBAL-24 |
||
751 | */ |
||
752 | public function testDetectRenameColumnAmbiguous() |
||
753 | { |
||
754 | $tableA = new Table('foo'); |
||
755 | $tableA->addColumn('foo', 'integer'); |
||
756 | $tableA->addColumn('bar', 'integer'); |
||
757 | |||
758 | $tableB = new Table('foo'); |
||
759 | $tableB->addColumn('baz', 'integer'); |
||
760 | |||
761 | $c = new Comparator(); |
||
762 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
763 | |||
764 | self::assertCount(1, $tableDiff->addedColumns, "'baz' should be added, not created through renaming!"); |
||
765 | self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!"); |
||
766 | self::assertCount(2, $tableDiff->removedColumns, "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'."); |
||
767 | self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed."); |
||
768 | self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed."); |
||
769 | self::assertCount(0, $tableDiff->renamedColumns, 'no renamings should take place.'); |
||
770 | } |
||
771 | |||
772 | /** |
||
773 | * @group DBAL-1063 |
||
774 | */ |
||
775 | public function testDetectRenameIndex() |
||
776 | { |
||
777 | $table1 = new Table('foo'); |
||
778 | $table1->addColumn('foo', 'integer'); |
||
779 | |||
780 | $table2 = clone $table1; |
||
781 | |||
782 | $table1->addIndex(['foo'], 'idx_foo'); |
||
783 | |||
784 | $table2->addIndex(['foo'], 'idx_bar'); |
||
785 | |||
786 | $comparator = new Comparator(); |
||
787 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
788 | |||
789 | self::assertCount(0, $tableDiff->addedIndexes); |
||
790 | self::assertCount(0, $tableDiff->removedIndexes); |
||
791 | self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes); |
||
792 | self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName()); |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * You can easily have ambiguities in the index renaming. If these |
||
797 | * are detected no renaming should take place, instead adding and dropping |
||
798 | * should be used exclusively. |
||
799 | * |
||
800 | * @group DBAL-1063 |
||
801 | */ |
||
802 | public function testDetectRenameIndexAmbiguous() |
||
803 | { |
||
804 | $table1 = new Table('foo'); |
||
805 | $table1->addColumn('foo', 'integer'); |
||
806 | |||
807 | $table2 = clone $table1; |
||
808 | |||
809 | $table1->addIndex(['foo'], 'idx_foo'); |
||
810 | $table1->addIndex(['foo'], 'idx_bar'); |
||
811 | |||
812 | $table2->addIndex(['foo'], 'idx_baz'); |
||
813 | |||
814 | $comparator = new Comparator(); |
||
815 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
816 | |||
817 | self::assertCount(1, $tableDiff->addedIndexes); |
||
818 | self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes); |
||
819 | self::assertCount(2, $tableDiff->removedIndexes); |
||
820 | self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes); |
||
821 | self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes); |
||
822 | self::assertCount(0, $tableDiff->renamedIndexes); |
||
823 | } |
||
824 | |||
825 | public function testDetectChangeIdentifierType() |
||
826 | { |
||
827 | $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.'); |
||
828 | |||
829 | $tableA = new Table('foo'); |
||
830 | $tableA->addColumn('id', 'integer', ['autoincrement' => false]); |
||
831 | |||
832 | $tableB = new Table('foo'); |
||
833 | $tableB->addColumn('id', 'integer', ['autoincrement' => true]); |
||
834 | |||
835 | $c = new Comparator(); |
||
836 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
837 | |||
838 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
839 | self::assertArrayHasKey('id', $tableDiff->changedColumns); |
||
840 | } |
||
841 | |||
842 | |||
843 | /** |
||
844 | * @group DBAL-105 |
||
845 | */ |
||
846 | public function testDiff() |
||
847 | { |
||
848 | $table = new Table('twitter_users'); |
||
849 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
850 | $table->addColumn('twitterId', 'integer'); |
||
851 | $table->addColumn('displayName', 'string'); |
||
852 | $table->setPrimaryKey(['id']); |
||
853 | |||
854 | $newtable = new Table('twitter_users'); |
||
855 | $newtable->addColumn('id', 'integer', ['autoincrement' => true]); |
||
856 | $newtable->addColumn('twitter_id', 'integer'); |
||
857 | $newtable->addColumn('display_name', 'string'); |
||
858 | $newtable->addColumn('logged_in_at', 'datetime'); |
||
859 | $newtable->setPrimaryKey(['id']); |
||
860 | |||
861 | $c = new Comparator(); |
||
862 | $tableDiff = $c->diffTable($table, $newtable); |
||
863 | |||
864 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
865 | self::assertEquals(['twitterid', 'displayname'], array_keys($tableDiff->renamedColumns)); |
||
866 | self::assertEquals(['logged_in_at'], array_keys($tableDiff->addedColumns)); |
||
867 | self::assertCount(0, $tableDiff->removedColumns); |
||
868 | } |
||
869 | |||
870 | |||
871 | /** |
||
872 | * @group DBAL-112 |
||
873 | */ |
||
874 | public function testChangedSequence() |
||
875 | { |
||
876 | $schema = new Schema(); |
||
877 | $sequence = $schema->createSequence('baz'); |
||
878 | |||
879 | $schemaNew = clone $schema; |
||
880 | /** @var Schema $schemaNew */ |
||
881 | $schemaNew->getSequence('baz')->setAllocationSize(20); |
||
882 | |||
883 | $c = new Comparator(); |
||
884 | $diff = $c->compare($schema, $schemaNew); |
||
885 | |||
886 | self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz')); |
||
887 | } |
||
888 | |||
889 | /** |
||
890 | * @group DBAL-106 |
||
891 | */ |
||
892 | public function testDiffDecimalWithNullPrecision() |
||
893 | { |
||
894 | $column = new Column('foo', Type::getType('decimal')); |
||
895 | $column->setPrecision(null); |
||
896 | |||
897 | $column2 = new Column('foo', Type::getType('decimal')); |
||
898 | |||
899 | $c = new Comparator(); |
||
900 | self::assertEquals([], $c->diffColumn($column, $column2)); |
||
901 | } |
||
902 | |||
903 | /** |
||
904 | * @group DBAL-204 |
||
905 | */ |
||
906 | public function testFqnSchemaComparison() |
||
907 | { |
||
908 | $config = new SchemaConfig(); |
||
909 | $config->setName('foo'); |
||
910 | |||
911 | $oldSchema = new Schema([], [], $config); |
||
912 | $oldSchema->createTable('bar'); |
||
913 | |||
914 | $newSchema = new Schema([], [], $config); |
||
915 | $newSchema->createTable('foo.bar'); |
||
916 | |||
917 | $expected = new SchemaDiff(); |
||
918 | $expected->fromSchema = $oldSchema; |
||
919 | |||
920 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
921 | } |
||
922 | |||
923 | /** |
||
924 | * @group DBAL-669 |
||
925 | */ |
||
926 | public function testNamespacesComparison() |
||
927 | { |
||
928 | $config = new SchemaConfig(); |
||
929 | $config->setName('schemaName'); |
||
930 | |||
931 | $oldSchema = new Schema([], [], $config); |
||
932 | $oldSchema->createTable('taz'); |
||
933 | $oldSchema->createTable('war.tab'); |
||
934 | |||
935 | $newSchema = new Schema([], [], $config); |
||
936 | $newSchema->createTable('bar.tab'); |
||
937 | $newSchema->createTable('baz.tab'); |
||
938 | $newSchema->createTable('war.tab'); |
||
939 | |||
940 | $expected = new SchemaDiff(); |
||
941 | $expected->fromSchema = $oldSchema; |
||
942 | $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz']; |
||
943 | |||
944 | $diff = Comparator::compareSchemas($oldSchema, $newSchema); |
||
945 | |||
946 | self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces); |
||
947 | self::assertCount(2, $diff->newTables); |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * @group DBAL-204 |
||
952 | */ |
||
953 | public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() |
||
968 | } |
||
969 | |||
970 | /** |
||
971 | * @group DBAL-204 |
||
972 | */ |
||
973 | public function testFqnSchemaComparisonNoSchemaSame() |
||
974 | { |
||
975 | $config = new SchemaConfig(); |
||
976 | $config->setName('foo'); |
||
977 | $oldSchema = new Schema([], [], $config); |
||
978 | $oldSchema->createTable('bar'); |
||
979 | |||
980 | $newSchema = new Schema(); |
||
981 | $newSchema->createTable('bar'); |
||
982 | |||
983 | $expected = new SchemaDiff(); |
||
984 | $expected->fromSchema = $oldSchema; |
||
985 | |||
986 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
987 | } |
||
988 | |||
989 | /** |
||
990 | * @group DDC-1657 |
||
991 | */ |
||
992 | public function testAutoIncrementSequences() |
||
993 | { |
||
994 | $oldSchema = new Schema(); |
||
995 | $table = $oldSchema->createTable('foo'); |
||
996 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
997 | $table->setPrimaryKey(['id']); |
||
998 | $oldSchema->createSequence('foo_id_seq'); |
||
999 | |||
1000 | $newSchema = new Schema(); |
||
1001 | $table = $newSchema->createTable('foo'); |
||
1002 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1003 | $table->setPrimaryKey(['id']); |
||
1004 | |||
1005 | $c = new Comparator(); |
||
1006 | $diff = $c->compare($oldSchema, $newSchema); |
||
1007 | |||
1008 | self::assertCount(0, $diff->removedSequences); |
||
1009 | } |
||
1010 | |||
1011 | |||
1012 | /** |
||
1013 | * Check that added autoincrement sequence is not populated in newSequences |
||
1014 | * |
||
1015 | * @group DBAL-562 |
||
1016 | */ |
||
1017 | public function testAutoIncrementNoSequences() |
||
1018 | { |
||
1019 | $oldSchema = new Schema(); |
||
1020 | $table = $oldSchema->createTable('foo'); |
||
1021 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1022 | $table->setPrimaryKey(['id']); |
||
1023 | |||
1024 | $newSchema = new Schema(); |
||
1025 | $table = $newSchema->createTable('foo'); |
||
1026 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1027 | $table->setPrimaryKey(['id']); |
||
1028 | $newSchema->createSequence('foo_id_seq'); |
||
1029 | |||
1030 | $c = new Comparator(); |
||
1031 | $diff = $c->compare($oldSchema, $newSchema); |
||
1032 | |||
1033 | self::assertCount(0, $diff->newSequences); |
||
1034 | } |
||
1035 | /** |
||
1036 | * You can get multiple drops for a FK when a table referenced by a foreign |
||
1037 | * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys |
||
1038 | * array because of the dropped table, and once on changedTables array. We |
||
1039 | * now check that the key is present once. |
||
1040 | */ |
||
1041 | public function testAvoidMultipleDropForeignKey() |
||
1042 | { |
||
1043 | $oldSchema = new Schema(); |
||
1044 | |||
1045 | $tableA = $oldSchema->createTable('table_a'); |
||
1046 | $tableA->addColumn('id', 'integer'); |
||
1047 | |||
1048 | $tableB = $oldSchema->createTable('table_b'); |
||
1049 | $tableB->addColumn('id', 'integer'); |
||
1050 | |||
1051 | $tableC = $oldSchema->createTable('table_c'); |
||
1052 | $tableC->addColumn('id', 'integer'); |
||
1053 | $tableC->addColumn('table_a_id', 'integer'); |
||
1054 | $tableC->addColumn('table_b_id', 'integer'); |
||
1055 | |||
1056 | $tableC->addForeignKeyConstraint($tableA, ['table_a_id'], ['id']); |
||
1057 | $tableC->addForeignKeyConstraint($tableB, ['table_b_id'], ['id']); |
||
1058 | |||
1059 | $newSchema = new Schema(); |
||
1060 | |||
1061 | $tableB = $newSchema->createTable('table_b'); |
||
1062 | $tableB->addColumn('id', 'integer'); |
||
1063 | |||
1064 | $tableC = $newSchema->createTable('table_c'); |
||
1065 | $tableC->addColumn('id', 'integer'); |
||
1066 | |||
1067 | $comparator = new Comparator(); |
||
1068 | $schemaDiff = $comparator->compare($oldSchema, $newSchema); |
||
1069 | |||
1070 | self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys); |
||
1071 | self::assertCount(1, $schemaDiff->orphanedForeignKeys); |
||
1072 | } |
||
1073 | |||
1074 | public function testCompareChangedColumn() |
||
1075 | { |
||
1076 | $oldSchema = new Schema(); |
||
1077 | |||
1078 | $tableFoo = $oldSchema->createTable('foo'); |
||
1079 | $tableFoo->addColumn('id', 'integer'); |
||
1080 | |||
1081 | $newSchema = new Schema(); |
||
1082 | $table = $newSchema->createTable('foo'); |
||
1083 | $table->addColumn('id', 'string'); |
||
1084 | |||
1085 | $expected = new SchemaDiff(); |
||
1086 | $expected->fromSchema = $oldSchema; |
||
1087 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
1088 | $tableDiff->fromTable = $tableFoo; |
||
1089 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
1090 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
1091 | $columnDiff->changedProperties = ['type']; |
||
1092 | |||
1093 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
1094 | } |
||
1095 | |||
1096 | public function testCompareChangedBinaryColumn() |
||
1097 | { |
||
1098 | $oldSchema = new Schema(); |
||
1099 | |||
1100 | $tableFoo = $oldSchema->createTable('foo'); |
||
1101 | $tableFoo->addColumn('id', 'binary'); |
||
1102 | |||
1103 | $newSchema = new Schema(); |
||
1104 | $table = $newSchema->createTable('foo'); |
||
1105 | $table->addColumn('id', 'binary', ['length' => 42, 'fixed' => true]); |
||
1106 | |||
1107 | $expected = new SchemaDiff(); |
||
1108 | $expected->fromSchema = $oldSchema; |
||
1109 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
1110 | $tableDiff->fromTable = $tableFoo; |
||
1111 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
1112 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
1113 | $columnDiff->changedProperties = ['length', 'fixed']; |
||
1114 | |||
1115 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
1116 | } |
||
1117 | |||
1118 | /** |
||
1119 | * @group DBAL-617 |
||
1120 | */ |
||
1121 | public function testCompareQuotedAndUnquotedForeignKeyColumns() |
||
1122 | { |
||
1123 | $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
1124 | $fk2 = new ForeignKeyConstraint(['`foo`'], 'bar', ['`baz`'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
1125 | |||
1126 | $comparator = new Comparator(); |
||
1127 | $diff = $comparator->diffForeignKey($fk1, $fk2); |
||
1128 | |||
1129 | self::assertFalse($diff); |
||
1130 | } |
||
1131 | |||
1132 | /** |
||
1133 | * @param SchemaDiff $diff |
||
1134 | * @param int $newTableCount |
||
1135 | * @param int $changeTableCount |
||
1136 | * @param int $removeTableCount |
||
1137 | */ |
||
1138 | public function assertSchemaTableChangeCount($diff, $newTableCount = 0, $changeTableCount = 0, $removeTableCount = 0) |
||
1139 | { |
||
1140 | self::assertCount($newTableCount, $diff->newTables); |
||
1141 | self::assertCount($changeTableCount, $diff->changedTables); |
||
1142 | self::assertCount($removeTableCount, $diff->removedTables); |
||
1143 | } |
||
1144 | |||
1145 | /** |
||
1146 | * @param SchemaDiff $diff |
||
1147 | * @param int $newSequenceCount |
||
1148 | * @param int $changeSequenceCount |
||
1149 | * @param int $changeSequenceCount |
||
1150 | */ |
||
1151 | public function assertSchemaSequenceChangeCount($diff, $newSequenceCount = 0, $changeSequenceCount = 0, $removeSequenceCount = 0) |
||
1152 | { |
||
1153 | self::assertCount($newSequenceCount, $diff->newSequences, 'Expected number of new sequences is wrong.'); |
||
1154 | self::assertCount($changeSequenceCount, $diff->changedSequences, 'Expected number of changed sequences is wrong.'); |
||
1155 | self::assertCount($removeSequenceCount, $diff->removedSequences, 'Expected number of removed sequences is wrong.'); |
||
1156 | } |
||
1157 | |||
1158 | public function testDiffColumnPlatformOptions() |
||
1159 | { |
||
1160 | $column1 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'bar']]); |
||
1161 | $column2 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'foobar' => 'foobar']]); |
||
1162 | $column3 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'rab']]); |
||
1163 | $column4 = new Column('foo', Type::getType('string')); |
||
1164 | |||
1165 | $comparator = new Comparator(); |
||
1166 | |||
1167 | self::assertEquals([], $comparator->diffColumn($column1, $column2)); |
||
1168 | self::assertEquals([], $comparator->diffColumn($column2, $column1)); |
||
1169 | self::assertEquals(['bar'], $comparator->diffColumn($column1, $column3)); |
||
1170 | self::assertEquals(['bar'], $comparator->diffColumn($column3, $column1)); |
||
1171 | self::assertEquals([], $comparator->diffColumn($column1, $column4)); |
||
1172 | self::assertEquals([], $comparator->diffColumn($column4, $column1)); |
||
1173 | } |
||
1174 | |||
1175 | public function testComplexDiffColumn() |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * @group DBAL-669 |
||
1194 | */ |
||
1195 | public function testComparesNamespaces() |
||
1196 | { |
||
1197 | $comparator = new Comparator(); |
||
1198 | $fromSchema = $this->getMockBuilder(Schema::class) |
||
1199 | ->setMethods(['getNamespaces', 'hasNamespace']) |
||
1200 | ->getMock(); |
||
1201 | $toSchema = $this->getMockBuilder(Schema::class) |
||
1202 | ->setMethods(['getNamespaces', 'hasNamespace']) |
||
1203 | ->getMock(); |
||
1204 | |||
1205 | $fromSchema->expects($this->once()) |
||
1206 | ->method('getNamespaces') |
||
1207 | ->will($this->returnValue(['foo', 'bar'])); |
||
1208 | |||
1209 | $fromSchema->expects($this->at(0)) |
||
1210 | ->method('hasNamespace') |
||
1211 | ->with('bar') |
||
1212 | ->will($this->returnValue(true)); |
||
1213 | |||
1214 | $fromSchema->expects($this->at(1)) |
||
1215 | ->method('hasNamespace') |
||
1216 | ->with('baz') |
||
1217 | ->will($this->returnValue(false)); |
||
1218 | |||
1219 | $toSchema->expects($this->once()) |
||
1220 | ->method('getNamespaces') |
||
1221 | ->will($this->returnValue(['bar', 'baz'])); |
||
1222 | |||
1223 | $toSchema->expects($this->at(1)) |
||
1224 | ->method('hasNamespace') |
||
1225 | ->with('foo') |
||
1226 | ->will($this->returnValue(false)); |
||
1227 | |||
1228 | $toSchema->expects($this->at(2)) |
||
1229 | ->method('hasNamespace') |
||
1230 | ->with('bar') |
||
1231 | ->will($this->returnValue(true)); |
||
1232 | |||
1233 | $expected = new SchemaDiff(); |
||
1234 | $expected->fromSchema = $fromSchema; |
||
1 ignored issue
–
show
|
|||
1235 | $expected->newNamespaces = ['baz' => 'baz']; |
||
1236 | $expected->removedNamespaces = ['foo' => 'foo']; |
||
1237 | |||
1238 | self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema)); |
||
1239 | } |
||
1240 | |||
1241 | public function testCompareGuidColumns() |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * @group DBAL-1009 |
||
1258 | * @dataProvider getCompareColumnComments |
||
1259 | */ |
||
1260 | public function testCompareColumnComments($comment1, $comment2, $equals) |
||
1276 | } |
||
1277 | |||
1278 | public function getCompareColumnComments() |
||
1279 | { |
||
1280 | return [ |
||
1281 | [null, null, true], |
||
1282 | ['', '', true], |
||
1283 | [' ', ' ', true], |
||
1284 | ['0', '0', true], |
||
1285 | ['foo', 'foo', true], |
||
1286 | |||
1287 | [null, '', true], |
||
1288 | [null, ' ', false], |
||
1289 | [null, '0', false], |
||
1290 | [null, 'foo', false], |
||
1291 | |||
1292 | ['', ' ', false], |
||
1293 | ['', '0', false], |
||
1294 | ['', 'foo', false], |
||
1295 | |||
1296 | [' ', '0', false], |
||
1297 | [' ', 'foo', false], |
||
1298 | |||
1299 | ['0', 'foo', false], |
||
1300 | ]; |
||
1301 | } |
||
1302 | |||
1303 | public function testForeignKeyRemovalWithRenamedLocalColumn() |
||
1349 | } |
||
1350 | } |
||
1351 |