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