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