| Total Complexity | 60 |
| Total Lines | 1349 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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() : void |
||
| 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 testCompareColumnsMultipleTypeInstances() : void |
||
| 198 | { |
||
| 199 | $integerType1 = Type::getType('integer'); |
||
| 200 | Type::overrideType('integer', get_class($integerType1)); |
||
| 201 | $integerType2 = Type::getType('integer'); |
||
| 202 | |||
| 203 | $column1 = new Column('integerfield1', $integerType1); |
||
| 204 | $column2 = new Column('integerfield1', $integerType2); |
||
| 205 | |||
| 206 | $c = new Comparator(); |
||
| 207 | self::assertEquals([], $c->diffColumn($column1, $column2)); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function testCompareColumnsOverriddenType() : void |
||
| 211 | { |
||
| 212 | $oldStringInstance = Type::getType('string'); |
||
| 213 | $integerType = Type::getType('integer'); |
||
| 214 | |||
| 215 | Type::overrideType('string', get_class($integerType)); |
||
| 216 | $overriddenStringType = Type::getType('string'); |
||
| 217 | |||
| 218 | Type::overrideType('string', get_class($oldStringInstance)); |
||
| 219 | |||
| 220 | $column1 = new Column('integerfield1', $integerType); |
||
| 221 | $column2 = new Column('integerfield1', $overriddenStringType); |
||
| 222 | |||
| 223 | $c = new Comparator(); |
||
| 224 | self::assertEquals([], $c->diffColumn($column1, $column2)); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function testCompareChangedColumnsChangeCustomSchemaOption() : void |
||
| 228 | { |
||
| 229 | $column1 = new Column('charfield1', Type::getType('string')); |
||
| 230 | $column2 = new Column('charfield1', Type::getType('string')); |
||
| 231 | |||
| 232 | $column1->setCustomSchemaOption('foo', 'bar'); |
||
| 233 | $column2->setCustomSchemaOption('foo', 'bar'); |
||
| 234 | |||
| 235 | $column1->setCustomSchemaOption('foo1', 'bar1'); |
||
| 236 | $column2->setCustomSchemaOption('foo2', 'bar2'); |
||
| 237 | |||
| 238 | $c = new Comparator(); |
||
| 239 | self::assertEquals(['foo1', 'foo2'], $c->diffColumn($column1, $column2)); |
||
| 240 | self::assertEquals([], $c->diffColumn($column1, $column1)); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function testCompareChangeColumnsMultipleNewColumnsRename() : void |
||
| 244 | { |
||
| 245 | $tableA = new Table('foo'); |
||
| 246 | $tableA->addColumn('datefield1', 'datetime'); |
||
| 247 | |||
| 248 | $tableB = new Table('foo'); |
||
| 249 | $tableB->addColumn('new_datefield1', 'datetime'); |
||
| 250 | $tableB->addColumn('new_datefield2', 'datetime'); |
||
| 251 | |||
| 252 | $c = new Comparator(); |
||
| 253 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 254 | |||
| 255 | self::assertCount(1, $tableDiff->renamedColumns, 'we should have one rename datefield1 => new_datefield1.'); |
||
| 256 | self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1"); |
||
| 257 | self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added"); |
||
| 258 | self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!"); |
||
| 259 | self::assertCount(0, $tableDiff->removedColumns, 'Nothing should be removed.'); |
||
| 260 | self::assertCount(0, $tableDiff->changedColumns, 'Nothing should be changed as all fields old & new have diff names.'); |
||
| 261 | } |
||
| 262 | |||
| 263 | public function testCompareRemovedIndex() : void |
||
| 264 | { |
||
| 265 | $schema1 = new Schema([ |
||
| 266 | 'bugdb' => new Table( |
||
| 267 | 'bugdb', |
||
| 268 | [ |
||
| 269 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 270 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 271 | ], |
||
| 272 | [ |
||
| 273 | 'primary' => new Index( |
||
| 274 | 'primary', |
||
| 275 | ['integerfield1'], |
||
| 276 | true |
||
| 277 | ), |
||
| 278 | ] |
||
| 279 | ), |
||
| 280 | ]); |
||
| 281 | $schema2 = new Schema([ |
||
| 282 | 'bugdb' => new Table( |
||
| 283 | 'bugdb', |
||
| 284 | [ |
||
| 285 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 286 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 287 | ] |
||
| 288 | ), |
||
| 289 | ]); |
||
| 290 | |||
| 291 | $expected = new SchemaDiff( |
||
| 292 | [], |
||
| 293 | [ |
||
| 294 | 'bugdb' => new TableDiff( |
||
| 295 | 'bugdb', |
||
| 296 | [], |
||
| 297 | [], |
||
| 298 | [], |
||
| 299 | [], |
||
| 300 | [], |
||
| 301 | [ |
||
| 302 | 'primary' => new Index( |
||
| 303 | 'primary', |
||
| 304 | ['integerfield1'], |
||
| 305 | true |
||
| 306 | ), |
||
| 307 | ] |
||
| 308 | ), |
||
| 309 | ] |
||
| 310 | ); |
||
| 311 | $expected->fromSchema = $schema1; |
||
| 312 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
| 313 | |||
| 314 | self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function testCompareNewIndex() : void |
||
| 367 | } |
||
| 368 | |||
| 369 | public function testCompareChangedIndex() : void |
||
| 370 | { |
||
| 371 | $schema1 = new Schema([ |
||
| 372 | 'bugdb' => new Table( |
||
| 373 | 'bugdb', |
||
| 374 | [ |
||
| 375 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 376 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 377 | ], |
||
| 378 | [ |
||
| 379 | 'primary' => new Index( |
||
| 380 | 'primary', |
||
| 381 | ['integerfield1'], |
||
| 382 | true |
||
| 383 | ), |
||
| 384 | ] |
||
| 385 | ), |
||
| 386 | ]); |
||
| 387 | $schema2 = new Schema([ |
||
| 388 | 'bugdb' => new Table( |
||
| 389 | 'bugdb', |
||
| 390 | [ |
||
| 391 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 392 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 393 | ], |
||
| 394 | [ |
||
| 395 | 'primary' => new Index( |
||
| 396 | 'primary', |
||
| 397 | ['integerfield1', 'integerfield2'], |
||
| 398 | true |
||
| 399 | ), |
||
| 400 | ] |
||
| 401 | ), |
||
| 402 | ]); |
||
| 403 | |||
| 404 | $expected = new SchemaDiff( |
||
| 405 | [], |
||
| 406 | [ |
||
| 407 | 'bugdb' => new TableDiff( |
||
| 408 | 'bugdb', |
||
| 409 | [], |
||
| 410 | [], |
||
| 411 | [], |
||
| 412 | [], |
||
| 413 | [ |
||
| 414 | 'primary' => new Index( |
||
| 415 | 'primary', |
||
| 416 | [ |
||
| 417 | 'integerfield1', |
||
| 418 | 'integerfield2', |
||
| 419 | ], |
||
| 420 | true |
||
| 421 | ), |
||
| 422 | ] |
||
| 423 | ), |
||
| 424 | ] |
||
| 425 | ); |
||
| 426 | $expected->fromSchema = $schema1; |
||
| 427 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
| 428 | |||
| 429 | self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); |
||
| 430 | } |
||
| 431 | |||
| 432 | public function testCompareChangedIndexFieldPositions() : void |
||
| 433 | { |
||
| 434 | $schema1 = new Schema([ |
||
| 435 | 'bugdb' => new Table( |
||
| 436 | 'bugdb', |
||
| 437 | [ |
||
| 438 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 439 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 440 | ], |
||
| 441 | [ |
||
| 442 | 'primary' => new Index('primary', ['integerfield1', 'integerfield2'], true), |
||
| 443 | ] |
||
| 444 | ), |
||
| 445 | ]); |
||
| 446 | $schema2 = new Schema([ |
||
| 447 | 'bugdb' => new Table( |
||
| 448 | 'bugdb', |
||
| 449 | [ |
||
| 450 | 'integerfield1' => new Column('integerfield1', Type::getType('integer')), |
||
| 451 | 'integerfield2' => new Column('integerfield2', Type::getType('integer')), |
||
| 452 | ], |
||
| 453 | [ |
||
| 454 | 'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true), |
||
| 455 | ] |
||
| 456 | ), |
||
| 457 | ]); |
||
| 458 | |||
| 459 | $expected = new SchemaDiff( |
||
| 460 | [], |
||
| 461 | [ |
||
| 462 | 'bugdb' => new TableDiff( |
||
| 463 | 'bugdb', |
||
| 464 | [], |
||
| 465 | [], |
||
| 466 | [], |
||
| 467 | [], |
||
| 468 | [ |
||
| 469 | 'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true), |
||
| 470 | ] |
||
| 471 | ), |
||
| 472 | ] |
||
| 473 | ); |
||
| 474 | $expected->fromSchema = $schema1; |
||
| 475 | $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); |
||
| 476 | |||
| 477 | self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2)); |
||
| 478 | } |
||
| 479 | |||
| 480 | public function testCompareSequences() : void |
||
| 481 | { |
||
| 482 | $seq1 = new Sequence('foo', 1, 1); |
||
| 483 | $seq2 = new Sequence('foo', 1, 2); |
||
| 484 | $seq3 = new Sequence('foo', 2, 1); |
||
| 485 | $seq4 = new Sequence('foo', '1', '1'); |
||
| 486 | |||
| 487 | $c = new Comparator(); |
||
| 488 | |||
| 489 | self::assertTrue($c->diffSequence($seq1, $seq2)); |
||
| 490 | self::assertTrue($c->diffSequence($seq1, $seq3)); |
||
| 491 | self::assertFalse($c->diffSequence($seq1, $seq4)); |
||
| 492 | } |
||
| 493 | |||
| 494 | public function testRemovedSequence() : void |
||
| 495 | { |
||
| 496 | $schema1 = new Schema(); |
||
| 497 | $seq = $schema1->createSequence('foo'); |
||
| 498 | |||
| 499 | $schema2 = new Schema(); |
||
| 500 | |||
| 501 | $c = new Comparator(); |
||
| 502 | $diffSchema = $c->compare($schema1, $schema2); |
||
| 503 | |||
| 504 | self::assertCount(1, $diffSchema->removedSequences); |
||
| 505 | self::assertSame($seq, $diffSchema->removedSequences[0]); |
||
| 506 | } |
||
| 507 | |||
| 508 | public function testAddedSequence() : void |
||
| 509 | { |
||
| 510 | $schema1 = new Schema(); |
||
| 511 | |||
| 512 | $schema2 = new Schema(); |
||
| 513 | $seq = $schema2->createSequence('foo'); |
||
| 514 | |||
| 515 | $c = new Comparator(); |
||
| 516 | $diffSchema = $c->compare($schema1, $schema2); |
||
| 517 | |||
| 518 | self::assertCount(1, $diffSchema->newSequences); |
||
| 519 | self::assertSame($seq, $diffSchema->newSequences[0]); |
||
| 520 | } |
||
| 521 | |||
| 522 | public function testTableAddForeignKey() : void |
||
| 523 | { |
||
| 524 | $tableForeign = new Table('bar'); |
||
| 525 | $tableForeign->addColumn('id', 'integer'); |
||
| 526 | |||
| 527 | $table1 = new Table('foo'); |
||
| 528 | $table1->addColumn('fk', 'integer'); |
||
| 529 | |||
| 530 | $table2 = new Table('foo'); |
||
| 531 | $table2->addColumn('fk', 'integer'); |
||
| 532 | $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
| 533 | |||
| 534 | $c = new Comparator(); |
||
| 535 | $tableDiff = $c->diffTable($table1, $table2); |
||
| 536 | |||
| 537 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
| 538 | self::assertCount(1, $tableDiff->addedForeignKeys); |
||
| 539 | } |
||
| 540 | |||
| 541 | public function testTableRemoveForeignKey() : void |
||
| 558 | } |
||
| 559 | |||
| 560 | public function testTableUpdateForeignKey() : void |
||
| 561 | { |
||
| 562 | $tableForeign = new Table('bar'); |
||
| 563 | $tableForeign->addColumn('id', 'integer'); |
||
| 564 | |||
| 565 | $table1 = new Table('foo'); |
||
| 566 | $table1->addColumn('fk', 'integer'); |
||
| 567 | $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']); |
||
| 568 | |||
| 569 | $table2 = new Table('foo'); |
||
| 570 | $table2->addColumn('fk', 'integer'); |
||
| 571 | $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']); |
||
| 572 | |||
| 573 | $c = new Comparator(); |
||
| 574 | $tableDiff = $c->diffTable($table1, $table2); |
||
| 575 | |||
| 576 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
| 577 | self::assertCount(1, $tableDiff->changedForeignKeys); |
||
| 578 | } |
||
| 579 | |||
| 580 | public function testMovedForeignKeyForeignTable() : void |
||
| 601 | } |
||
| 602 | |||
| 603 | public function testTablesCaseInsensitive() : void |
||
| 604 | { |
||
| 605 | $schemaA = new Schema(); |
||
| 606 | $schemaA->createTable('foo'); |
||
| 607 | $schemaA->createTable('bAr'); |
||
| 608 | $schemaA->createTable('BAZ'); |
||
| 609 | $schemaA->createTable('new'); |
||
| 610 | |||
| 611 | $schemaB = new Schema(); |
||
| 612 | $schemaB->createTable('FOO'); |
||
| 613 | $schemaB->createTable('bar'); |
||
| 614 | $schemaB->createTable('Baz'); |
||
| 615 | $schemaB->createTable('old'); |
||
| 616 | |||
| 617 | $c = new Comparator(); |
||
| 618 | $diff = $c->compare($schemaA, $schemaB); |
||
| 619 | |||
| 620 | $this->assertSchemaTableChangeCount($diff, 1, 0, 1); |
||
| 621 | } |
||
| 622 | |||
| 623 | public function testSequencesCaseInsensitive() : void |
||
| 624 | { |
||
| 625 | $schemaA = new Schema(); |
||
| 626 | $schemaA->createSequence('foo'); |
||
| 627 | $schemaA->createSequence('BAR'); |
||
| 628 | $schemaA->createSequence('Baz'); |
||
| 629 | $schemaA->createSequence('new'); |
||
| 630 | |||
| 631 | $schemaB = new Schema(); |
||
| 632 | $schemaB->createSequence('FOO'); |
||
| 633 | $schemaB->createSequence('Bar'); |
||
| 634 | $schemaB->createSequence('baz'); |
||
| 635 | $schemaB->createSequence('old'); |
||
| 636 | |||
| 637 | $c = new Comparator(); |
||
| 638 | $diff = $c->compare($schemaA, $schemaB); |
||
| 639 | |||
| 640 | $this->assertSchemaSequenceChangeCount($diff, 1, 0, 1); |
||
| 641 | } |
||
| 642 | |||
| 643 | public function testCompareColumnCompareCaseInsensitive() : void |
||
| 644 | { |
||
| 645 | $tableA = new Table('foo'); |
||
| 646 | $tableA->addColumn('id', 'integer'); |
||
| 647 | |||
| 648 | $tableB = new Table('foo'); |
||
| 649 | $tableB->addColumn('ID', 'integer'); |
||
| 650 | |||
| 651 | $c = new Comparator(); |
||
| 652 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 653 | |||
| 654 | self::assertFalse($tableDiff); |
||
| 655 | } |
||
| 656 | |||
| 657 | public function testCompareIndexBasedOnPropertiesNotName() : void |
||
| 658 | { |
||
| 659 | $tableA = new Table('foo'); |
||
| 660 | $tableA->addColumn('id', 'integer'); |
||
| 661 | $tableA->addIndex(['id'], 'foo_bar_idx'); |
||
| 662 | |||
| 663 | $tableB = new Table('foo'); |
||
| 664 | $tableB->addColumn('ID', 'integer'); |
||
| 665 | $tableB->addIndex(['id'], 'bar_foo_idx'); |
||
| 666 | |||
| 667 | $c = new Comparator(); |
||
| 668 | $tableDiff = new TableDiff('foo'); |
||
| 669 | $tableDiff->fromTable = $tableA; |
||
| 670 | $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']); |
||
| 671 | |||
| 672 | self::assertEquals( |
||
| 673 | $tableDiff, |
||
| 674 | $c->diffTable($tableA, $tableB) |
||
| 675 | ); |
||
| 676 | } |
||
| 677 | |||
| 678 | public function testCompareForeignKeyBasedOnPropertiesNotName() : void |
||
| 679 | { |
||
| 680 | $tableA = new Table('foo'); |
||
| 681 | $tableA->addColumn('id', 'integer'); |
||
| 682 | $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', ['id'], ['id']); |
||
| 683 | |||
| 684 | $tableB = new Table('foo'); |
||
| 685 | $tableB->addColumn('ID', 'integer'); |
||
| 686 | $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', ['id'], ['id']); |
||
| 687 | |||
| 688 | $c = new Comparator(); |
||
| 689 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 690 | |||
| 691 | self::assertFalse($tableDiff); |
||
| 692 | } |
||
| 693 | |||
| 694 | public function testCompareForeignKeyRestrictNoActionAreTheSame() : void |
||
| 695 | { |
||
| 696 | $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
| 697 | $fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'RESTRICT']); |
||
| 698 | |||
| 699 | $c = new Comparator(); |
||
| 700 | self::assertFalse($c->diffForeignKey($fk1, $fk2)); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @group DBAL-492 |
||
| 705 | */ |
||
| 706 | public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable() : void |
||
| 713 | } |
||
| 714 | |||
| 715 | public function testDetectRenameColumn() : void |
||
| 716 | { |
||
| 717 | $tableA = new Table('foo'); |
||
| 718 | $tableA->addColumn('foo', 'integer'); |
||
| 719 | |||
| 730 | } |
||
| 731 | |||
| 732 | public function testDetectColumnIsNotRenamedIfDisabled() : void |
||
| 733 | { |
||
| 734 | $tableA = new Table('foo'); |
||
| 735 | $tableA->addColumn('foo', 'integer'); |
||
| 736 | |||
| 737 | $tableB = new Table('foo'); |
||
| 738 | $tableB->addColumn('bar', 'integer'); |
||
| 739 | |||
| 740 | $c = new Comparator(Comparator::DETECT_INDEX_RENAMINGS); |
||
| 741 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 742 | |||
| 743 | self::assertCount(1, $tableDiff->addedColumns); |
||
| 744 | self::assertCount(1, $tableDiff->removedColumns); |
||
| 745 | self::assertCount(0, $tableDiff->renamedColumns); |
||
| 746 | self::assertArrayHasKey('bar', $tableDiff->addedColumns); |
||
| 747 | self::assertArrayHasKey('foo', $tableDiff->removedColumns); |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * You can easily have ambiguities in the column renaming. If these |
||
| 752 | * are detected no renaming should take place, instead adding and dropping |
||
| 753 | * should be used exclusively. |
||
| 754 | * |
||
| 755 | * @group DBAL-24 |
||
| 756 | */ |
||
| 757 | public function testDetectRenameColumnAmbiguous() : void |
||
| 758 | { |
||
| 759 | $tableA = new Table('foo'); |
||
| 760 | $tableA->addColumn('foo', 'integer'); |
||
| 761 | $tableA->addColumn('bar', 'integer'); |
||
| 762 | |||
| 763 | $tableB = new Table('foo'); |
||
| 764 | $tableB->addColumn('baz', 'integer'); |
||
| 765 | |||
| 766 | $c = new Comparator(); |
||
| 767 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 768 | |||
| 769 | self::assertCount(1, $tableDiff->addedColumns, "'baz' should be added, not created through renaming!"); |
||
| 770 | self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!"); |
||
| 771 | self::assertCount(2, $tableDiff->removedColumns, "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'."); |
||
| 772 | self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed."); |
||
| 773 | self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed."); |
||
| 774 | self::assertCount(0, $tableDiff->renamedColumns, 'no renamings should take place.'); |
||
| 775 | } |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @group DBAL-1063 |
||
| 779 | */ |
||
| 780 | public function testDetectRenameIndex() : void |
||
| 781 | { |
||
| 782 | $table1 = new Table('foo'); |
||
| 783 | $table1->addColumn('foo', 'integer'); |
||
| 784 | |||
| 785 | $table2 = clone $table1; |
||
| 786 | |||
| 787 | $table1->addIndex(['foo'], 'idx_foo'); |
||
| 788 | |||
| 789 | $table2->addIndex(['foo'], 'idx_bar'); |
||
| 790 | |||
| 791 | $comparator = new Comparator(); |
||
| 792 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
| 793 | |||
| 794 | self::assertCount(0, $tableDiff->addedIndexes); |
||
| 795 | self::assertCount(0, $tableDiff->removedIndexes); |
||
| 796 | self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes); |
||
| 797 | self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName()); |
||
| 798 | } |
||
| 799 | |||
| 800 | /** |
||
| 801 | * @group DBAL-1063 |
||
| 802 | */ |
||
| 803 | public function testDetectIndexIsNotRenamedIfDisabled() : void |
||
| 804 | { |
||
| 805 | $table1 = new Table('foo'); |
||
| 806 | $table1->addColumn('foo', 'integer'); |
||
| 807 | |||
| 808 | $table2 = clone $table1; |
||
| 809 | |||
| 810 | $table1->addIndex(['foo'], 'idx_foo'); |
||
| 811 | |||
| 812 | $table2->addIndex(['foo'], 'idx_bar'); |
||
| 813 | |||
| 814 | $comparator = new Comparator(Comparator::DETECT_COLUMN_RENAMINGS); |
||
| 815 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
| 816 | |||
| 817 | self::assertCount(1, $tableDiff->addedIndexes); |
||
| 818 | self::assertCount(1, $tableDiff->removedIndexes); |
||
| 819 | self::assertCount(0, $tableDiff->renamedIndexes); |
||
| 820 | self::assertArrayHasKey('idx_bar', $tableDiff->addedIndexes); |
||
| 821 | self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * You can easily have ambiguities in the index renaming. If these |
||
| 826 | * are detected no renaming should take place, instead adding and dropping |
||
| 827 | * should be used exclusively. |
||
| 828 | * |
||
| 829 | * @group DBAL-1063 |
||
| 830 | */ |
||
| 831 | public function testDetectRenameIndexAmbiguous() : void |
||
| 832 | { |
||
| 833 | $table1 = new Table('foo'); |
||
| 834 | $table1->addColumn('foo', 'integer'); |
||
| 835 | |||
| 836 | $table2 = clone $table1; |
||
| 837 | |||
| 838 | $table1->addIndex(['foo'], 'idx_foo'); |
||
| 839 | $table1->addIndex(['foo'], 'idx_bar'); |
||
| 840 | |||
| 841 | $table2->addIndex(['foo'], 'idx_baz'); |
||
| 842 | |||
| 843 | $comparator = new Comparator(); |
||
| 844 | $tableDiff = $comparator->diffTable($table1, $table2); |
||
| 845 | |||
| 846 | self::assertCount(1, $tableDiff->addedIndexes); |
||
| 847 | self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes); |
||
| 848 | self::assertCount(2, $tableDiff->removedIndexes); |
||
| 849 | self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes); |
||
| 850 | self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes); |
||
| 851 | self::assertCount(0, $tableDiff->renamedIndexes); |
||
| 852 | } |
||
| 853 | |||
| 854 | public function testDetectChangeIdentifierType() : void |
||
| 855 | { |
||
| 856 | $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.'); |
||
| 857 | |||
| 858 | $tableA = new Table('foo'); |
||
| 859 | $tableA->addColumn('id', 'integer', ['autoincrement' => false]); |
||
| 860 | |||
| 861 | $tableB = new Table('foo'); |
||
| 862 | $tableB->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 863 | |||
| 864 | $c = new Comparator(); |
||
| 865 | $tableDiff = $c->diffTable($tableA, $tableB); |
||
| 866 | |||
| 867 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
| 868 | self::assertArrayHasKey('id', $tableDiff->changedColumns); |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @group DBAL-105 |
||
| 873 | */ |
||
| 874 | public function testDiff() : void |
||
| 875 | { |
||
| 876 | $table = new Table('twitter_users'); |
||
| 877 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 878 | $table->addColumn('twitterId', 'integer'); |
||
| 879 | $table->addColumn('displayName', 'string'); |
||
| 880 | $table->setPrimaryKey(['id']); |
||
| 881 | |||
| 882 | $newtable = new Table('twitter_users'); |
||
| 883 | $newtable->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 884 | $newtable->addColumn('twitter_id', 'integer'); |
||
| 885 | $newtable->addColumn('display_name', 'string'); |
||
| 886 | $newtable->addColumn('logged_in_at', 'datetime'); |
||
| 887 | $newtable->setPrimaryKey(['id']); |
||
| 888 | |||
| 889 | $c = new Comparator(); |
||
| 890 | $tableDiff = $c->diffTable($table, $newtable); |
||
| 891 | |||
| 892 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
| 893 | self::assertEquals(['twitterid', 'displayname'], array_keys($tableDiff->renamedColumns)); |
||
| 894 | self::assertEquals(['logged_in_at'], array_keys($tableDiff->addedColumns)); |
||
| 895 | self::assertCount(0, $tableDiff->removedColumns); |
||
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * @group DBAL-112 |
||
| 900 | */ |
||
| 901 | public function testChangedSequence() : void |
||
| 902 | { |
||
| 903 | $schema = new Schema(); |
||
| 904 | $sequence = $schema->createSequence('baz'); |
||
|
|
|||
| 905 | |||
| 906 | $schemaNew = clone $schema; |
||
| 907 | $schemaNew->getSequence('baz')->setAllocationSize(20); |
||
| 908 | |||
| 909 | $c = new Comparator(); |
||
| 910 | $diff = $c->compare($schema, $schemaNew); |
||
| 911 | |||
| 912 | self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz')); |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * @group DBAL-106 |
||
| 917 | */ |
||
| 918 | public function testDiffDecimalWithNullPrecision() : void |
||
| 919 | { |
||
| 920 | $column = new Column('foo', Type::getType('decimal')); |
||
| 921 | $column->setPrecision(null); |
||
| 922 | |||
| 923 | $column2 = new Column('foo', Type::getType('decimal')); |
||
| 924 | |||
| 925 | $c = new Comparator(); |
||
| 926 | self::assertEquals([], $c->diffColumn($column, $column2)); |
||
| 927 | } |
||
| 928 | |||
| 929 | /** |
||
| 930 | * @group DBAL-204 |
||
| 931 | */ |
||
| 932 | public function testFqnSchemaComparison() : void |
||
| 933 | { |
||
| 934 | $config = new SchemaConfig(); |
||
| 935 | $config->setName('foo'); |
||
| 936 | |||
| 937 | $oldSchema = new Schema([], [], $config); |
||
| 938 | $oldSchema->createTable('bar'); |
||
| 939 | |||
| 940 | $newSchema = new Schema([], [], $config); |
||
| 941 | $newSchema->createTable('foo.bar'); |
||
| 942 | |||
| 943 | $expected = new SchemaDiff(); |
||
| 944 | $expected->fromSchema = $oldSchema; |
||
| 945 | |||
| 946 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @group DBAL-669 |
||
| 951 | */ |
||
| 952 | public function testNamespacesComparison() : void |
||
| 953 | { |
||
| 954 | $config = new SchemaConfig(); |
||
| 955 | $config->setName('schemaName'); |
||
| 956 | |||
| 957 | $oldSchema = new Schema([], [], $config); |
||
| 958 | $oldSchema->createTable('taz'); |
||
| 959 | $oldSchema->createTable('war.tab'); |
||
| 960 | |||
| 961 | $newSchema = new Schema([], [], $config); |
||
| 962 | $newSchema->createTable('bar.tab'); |
||
| 963 | $newSchema->createTable('baz.tab'); |
||
| 964 | $newSchema->createTable('war.tab'); |
||
| 965 | |||
| 966 | $expected = new SchemaDiff(); |
||
| 967 | $expected->fromSchema = $oldSchema; |
||
| 968 | $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz']; |
||
| 969 | |||
| 970 | $diff = Comparator::compareSchemas($oldSchema, $newSchema); |
||
| 971 | |||
| 972 | self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces); |
||
| 973 | self::assertCount(2, $diff->newTables); |
||
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * @group DBAL-204 |
||
| 978 | */ |
||
| 979 | public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() : void |
||
| 980 | { |
||
| 981 | $config = new SchemaConfig(); |
||
| 982 | $config->setName('foo'); |
||
| 983 | |||
| 984 | $oldSchema = new Schema([], [], $config); |
||
| 985 | $oldSchema->createTable('foo.bar'); |
||
| 986 | |||
| 987 | $newSchema = new Schema(); |
||
| 988 | $newSchema->createTable('bar'); |
||
| 989 | |||
| 990 | $expected = new SchemaDiff(); |
||
| 991 | $expected->fromSchema = $oldSchema; |
||
| 992 | |||
| 993 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * @group DBAL-204 |
||
| 998 | */ |
||
| 999 | public function testFqnSchemaComparisonNoSchemaSame() : void |
||
| 1000 | { |
||
| 1001 | $config = new SchemaConfig(); |
||
| 1002 | $config->setName('foo'); |
||
| 1003 | $oldSchema = new Schema([], [], $config); |
||
| 1004 | $oldSchema->createTable('bar'); |
||
| 1005 | |||
| 1006 | $newSchema = new Schema(); |
||
| 1007 | $newSchema->createTable('bar'); |
||
| 1008 | |||
| 1009 | $expected = new SchemaDiff(); |
||
| 1010 | $expected->fromSchema = $oldSchema; |
||
| 1011 | |||
| 1012 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
| 1013 | } |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * @group DDC-1657 |
||
| 1017 | */ |
||
| 1018 | public function testAutoIncrementSequences() : void |
||
| 1019 | { |
||
| 1020 | $oldSchema = new Schema(); |
||
| 1021 | $table = $oldSchema->createTable('foo'); |
||
| 1022 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 1023 | $table->setPrimaryKey(['id']); |
||
| 1024 | $oldSchema->createSequence('foo_id_seq'); |
||
| 1025 | |||
| 1026 | $newSchema = new Schema(); |
||
| 1027 | $table = $newSchema->createTable('foo'); |
||
| 1028 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 1029 | $table->setPrimaryKey(['id']); |
||
| 1030 | |||
| 1031 | $c = new Comparator(); |
||
| 1032 | $diff = $c->compare($oldSchema, $newSchema); |
||
| 1033 | |||
| 1034 | self::assertCount(0, $diff->removedSequences); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Check that added autoincrement sequence is not populated in newSequences |
||
| 1039 | * |
||
| 1040 | * @group DBAL-562 |
||
| 1041 | */ |
||
| 1042 | public function testAutoIncrementNoSequences() : void |
||
| 1043 | { |
||
| 1044 | $oldSchema = new Schema(); |
||
| 1045 | $table = $oldSchema->createTable('foo'); |
||
| 1046 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 1047 | $table->setPrimaryKey(['id']); |
||
| 1048 | |||
| 1049 | $newSchema = new Schema(); |
||
| 1050 | $table = $newSchema->createTable('foo'); |
||
| 1051 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
| 1052 | $table->setPrimaryKey(['id']); |
||
| 1053 | $newSchema->createSequence('foo_id_seq'); |
||
| 1054 | |||
| 1055 | $c = new Comparator(); |
||
| 1056 | $diff = $c->compare($oldSchema, $newSchema); |
||
| 1057 | |||
| 1058 | self::assertCount(0, $diff->newSequences); |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * You can get multiple drops for a FK when a table referenced by a foreign |
||
| 1063 | * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys |
||
| 1064 | * array because of the dropped table, and once on changedTables array. We |
||
| 1065 | * now check that the key is present once. |
||
| 1066 | */ |
||
| 1067 | public function testAvoidMultipleDropForeignKey() : void |
||
| 1068 | { |
||
| 1069 | $oldSchema = new Schema(); |
||
| 1070 | |||
| 1071 | $tableA = $oldSchema->createTable('table_a'); |
||
| 1072 | $tableA->addColumn('id', 'integer'); |
||
| 1073 | |||
| 1074 | $tableB = $oldSchema->createTable('table_b'); |
||
| 1075 | $tableB->addColumn('id', 'integer'); |
||
| 1076 | |||
| 1077 | $tableC = $oldSchema->createTable('table_c'); |
||
| 1078 | $tableC->addColumn('id', 'integer'); |
||
| 1079 | $tableC->addColumn('table_a_id', 'integer'); |
||
| 1080 | $tableC->addColumn('table_b_id', 'integer'); |
||
| 1081 | |||
| 1082 | $tableC->addForeignKeyConstraint($tableA, ['table_a_id'], ['id']); |
||
| 1083 | $tableC->addForeignKeyConstraint($tableB, ['table_b_id'], ['id']); |
||
| 1084 | |||
| 1085 | $newSchema = new Schema(); |
||
| 1086 | |||
| 1087 | $tableB = $newSchema->createTable('table_b'); |
||
| 1088 | $tableB->addColumn('id', 'integer'); |
||
| 1089 | |||
| 1090 | $tableC = $newSchema->createTable('table_c'); |
||
| 1091 | $tableC->addColumn('id', 'integer'); |
||
| 1092 | |||
| 1093 | $comparator = new Comparator(); |
||
| 1094 | $schemaDiff = $comparator->compare($oldSchema, $newSchema); |
||
| 1095 | |||
| 1096 | self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys); |
||
| 1097 | self::assertCount(1, $schemaDiff->orphanedForeignKeys); |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | public function testCompareChangedColumn() : void |
||
| 1101 | { |
||
| 1102 | $oldSchema = new Schema(); |
||
| 1103 | |||
| 1104 | $tableFoo = $oldSchema->createTable('foo'); |
||
| 1105 | $tableFoo->addColumn('id', 'integer'); |
||
| 1106 | |||
| 1107 | $newSchema = new Schema(); |
||
| 1108 | $table = $newSchema->createTable('foo'); |
||
| 1109 | $table->addColumn('id', 'string'); |
||
| 1110 | |||
| 1111 | $expected = new SchemaDiff(); |
||
| 1112 | $expected->fromSchema = $oldSchema; |
||
| 1113 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
| 1114 | $tableDiff->fromTable = $tableFoo; |
||
| 1115 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
| 1116 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
| 1117 | $columnDiff->changedProperties = ['type']; |
||
| 1118 | |||
| 1119 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | public function testCompareChangedBinaryColumn() : void |
||
| 1123 | { |
||
| 1124 | $oldSchema = new Schema(); |
||
| 1125 | |||
| 1126 | $tableFoo = $oldSchema->createTable('foo'); |
||
| 1127 | $tableFoo->addColumn('id', 'binary'); |
||
| 1128 | |||
| 1129 | $newSchema = new Schema(); |
||
| 1130 | $table = $newSchema->createTable('foo'); |
||
| 1131 | $table->addColumn('id', 'binary', ['length' => 42, 'fixed' => true]); |
||
| 1132 | |||
| 1133 | $expected = new SchemaDiff(); |
||
| 1134 | $expected->fromSchema = $oldSchema; |
||
| 1135 | $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); |
||
| 1136 | $tableDiff->fromTable = $tableFoo; |
||
| 1137 | $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id')); |
||
| 1138 | $columnDiff->fromColumn = $tableFoo->getColumn('id'); |
||
| 1139 | $columnDiff->changedProperties = ['length', 'fixed']; |
||
| 1140 | |||
| 1141 | self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema)); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * @group DBAL-617 |
||
| 1146 | */ |
||
| 1147 | public function testCompareQuotedAndUnquotedForeignKeyColumns() : void |
||
| 1148 | { |
||
| 1149 | $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
| 1150 | $fk2 = new ForeignKeyConstraint(['`foo`'], 'bar', ['`baz`'], 'fk1', ['onDelete' => 'NO ACTION']); |
||
| 1151 | |||
| 1152 | $comparator = new Comparator(); |
||
| 1153 | $diff = $comparator->diffForeignKey($fk1, $fk2); |
||
| 1154 | |||
| 1155 | self::assertFalse($diff); |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | public function assertSchemaTableChangeCount(SchemaDiff $diff, int $newTableCount = 0, int $changeTableCount = 0, int $removeTableCount = 0) : void |
||
| 1159 | { |
||
| 1160 | self::assertCount($newTableCount, $diff->newTables); |
||
| 1161 | self::assertCount($changeTableCount, $diff->changedTables); |
||
| 1162 | self::assertCount($removeTableCount, $diff->removedTables); |
||
| 1163 | } |
||
| 1164 | |||
| 1165 | public function assertSchemaSequenceChangeCount( |
||
| 1166 | SchemaDiff $diff, |
||
| 1167 | int $newSequenceCount = 0, |
||
| 1168 | int $changeSequenceCount = 0, |
||
| 1169 | int $removeSequenceCount = 0 |
||
| 1170 | ) : void { |
||
| 1171 | self::assertCount($newSequenceCount, $diff->newSequences, 'Expected number of new sequences is wrong.'); |
||
| 1172 | self::assertCount($changeSequenceCount, $diff->changedSequences, 'Expected number of changed sequences is wrong.'); |
||
| 1173 | self::assertCount($removeSequenceCount, $diff->removedSequences, 'Expected number of removed sequences is wrong.'); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | public function testDiffColumnPlatformOptions() : void |
||
| 1177 | { |
||
| 1178 | $column1 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'bar']]); |
||
| 1179 | $column2 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'foobar' => 'foobar']]); |
||
| 1180 | $column3 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'rab']]); |
||
| 1181 | $column4 = new Column('foo', Type::getType('string')); |
||
| 1182 | |||
| 1183 | $comparator = new Comparator(); |
||
| 1184 | |||
| 1185 | self::assertEquals([], $comparator->diffColumn($column1, $column2)); |
||
| 1186 | self::assertEquals([], $comparator->diffColumn($column2, $column1)); |
||
| 1187 | self::assertEquals(['bar'], $comparator->diffColumn($column1, $column3)); |
||
| 1188 | self::assertEquals(['bar'], $comparator->diffColumn($column3, $column1)); |
||
| 1189 | self::assertEquals([], $comparator->diffColumn($column1, $column4)); |
||
| 1190 | self::assertEquals([], $comparator->diffColumn($column4, $column1)); |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | public function testComplexDiffColumn() : void |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * @group DBAL-669 |
||
| 1212 | */ |
||
| 1213 | public function testComparesNamespaces() : void |
||
| 1214 | { |
||
| 1215 | $comparator = new Comparator(); |
||
| 1216 | $fromSchema = $this->getMockBuilder(Schema::class) |
||
| 1217 | ->onlyMethods(['getNamespaces', 'hasNamespace']) |
||
| 1218 | ->getMock(); |
||
| 1219 | $toSchema = $this->getMockBuilder(Schema::class) |
||
| 1220 | ->onlyMethods(['getNamespaces', 'hasNamespace']) |
||
| 1221 | ->getMock(); |
||
| 1222 | |||
| 1223 | $fromSchema->expects($this->once()) |
||
| 1224 | ->method('getNamespaces') |
||
| 1225 | ->will($this->returnValue(['foo', 'bar'])); |
||
| 1226 | |||
| 1227 | $fromSchema->expects($this->at(0)) |
||
| 1228 | ->method('hasNamespace') |
||
| 1229 | ->with('bar') |
||
| 1230 | ->will($this->returnValue(true)); |
||
| 1231 | |||
| 1232 | $fromSchema->expects($this->at(1)) |
||
| 1233 | ->method('hasNamespace') |
||
| 1234 | ->with('baz') |
||
| 1235 | ->will($this->returnValue(false)); |
||
| 1236 | |||
| 1237 | $toSchema->expects($this->once()) |
||
| 1238 | ->method('getNamespaces') |
||
| 1239 | ->will($this->returnValue(['bar', 'baz'])); |
||
| 1240 | |||
| 1241 | $toSchema->expects($this->at(1)) |
||
| 1242 | ->method('hasNamespace') |
||
| 1243 | ->with('foo') |
||
| 1244 | ->will($this->returnValue(false)); |
||
| 1245 | |||
| 1246 | $toSchema->expects($this->at(2)) |
||
| 1247 | ->method('hasNamespace') |
||
| 1248 | ->with('bar') |
||
| 1249 | ->will($this->returnValue(true)); |
||
| 1250 | |||
| 1251 | $expected = new SchemaDiff(); |
||
| 1252 | $expected->fromSchema = $fromSchema; |
||
| 1253 | $expected->newNamespaces = ['baz' => 'baz']; |
||
| 1254 | $expected->removedNamespaces = ['foo' => 'foo']; |
||
| 1255 | |||
| 1256 | self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema)); |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | public function testCompareGuidColumns() : void |
||
| 1260 | { |
||
| 1261 | $comparator = new Comparator(); |
||
| 1262 | |||
| 1263 | $column1 = new Column('foo', Type::getType('guid'), ['comment' => 'GUID 1']); |
||
| 1264 | $column2 = new Column( |
||
| 1265 | 'foo', |
||
| 1266 | Type::getType('guid'), |
||
| 1267 | ['notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.'] |
||
| 1268 | ); |
||
| 1269 | |||
| 1270 | self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column1, $column2)); |
||
| 1271 | self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column2, $column1)); |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * @group DBAL-1009 |
||
| 1276 | * @dataProvider getCompareColumnComments |
||
| 1277 | */ |
||
| 1278 | public function testCompareColumnComments(?string $comment1, ?string $comment2, bool $equals) : void |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * @return mixed[][] |
||
| 1298 | */ |
||
| 1299 | public static function getCompareColumnComments() : iterable |
||
| 1300 | { |
||
| 1301 | return [ |
||
| 1302 | [null, null, true], |
||
| 1303 | ['', '', true], |
||
| 1304 | [' ', ' ', true], |
||
| 1305 | ['0', '0', true], |
||
| 1306 | ['foo', 'foo', true], |
||
| 1307 | |||
| 1308 | [null, '', true], |
||
| 1309 | [null, ' ', false], |
||
| 1310 | [null, '0', false], |
||
| 1311 | [null, 'foo', false], |
||
| 1312 | |||
| 1313 | ['', ' ', false], |
||
| 1314 | ['', '0', false], |
||
| 1315 | ['', 'foo', false], |
||
| 1316 | |||
| 1317 | [' ', '0', false], |
||
| 1318 | [' ', 'foo', false], |
||
| 1319 | |||
| 1320 | ['0', 'foo', false], |
||
| 1321 | ]; |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | public function testForeignKeyRemovalWithRenamedLocalColumn() : void |
||
| 1370 | } |
||
| 1371 | } |
||
| 1372 |