| Total Complexity | 55 |
| Total Lines | 899 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TableTest 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 TableTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class TableTest extends DbalTestCase |
||
| 22 | { |
||
| 23 | public function testCreateWithInvalidTableName() |
||
| 24 | { |
||
| 25 | $this->expectException(DBALException::class); |
||
| 26 | |||
| 27 | new Table(''); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testGetName() |
||
| 31 | { |
||
| 32 | $table = new Table('foo', [], [], []); |
||
| 33 | self::assertEquals('foo', $table->getName()); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testColumns() |
||
| 37 | { |
||
| 38 | $type = Type::getType('integer'); |
||
| 39 | $columns = []; |
||
| 40 | $columns[] = new Column('foo', $type); |
||
| 41 | $columns[] = new Column('bar', $type); |
||
| 42 | $table = new Table('foo', $columns, [], []); |
||
| 43 | |||
| 44 | self::assertTrue($table->hasColumn('foo')); |
||
| 45 | self::assertTrue($table->hasColumn('bar')); |
||
| 46 | self::assertFalse($table->hasColumn('baz')); |
||
| 47 | |||
| 48 | self::assertInstanceOf(Column::class, $table->getColumn('foo')); |
||
| 49 | self::assertInstanceOf(Column::class, $table->getColumn('bar')); |
||
| 50 | |||
| 51 | self::assertCount(2, $table->getColumns()); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testColumnsCaseInsensitive() |
||
| 55 | { |
||
| 56 | $table = new Table('foo'); |
||
| 57 | $column = $table->addColumn('Foo', 'integer'); |
||
| 58 | |||
| 59 | self::assertTrue($table->hasColumn('Foo')); |
||
| 60 | self::assertTrue($table->hasColumn('foo')); |
||
| 61 | self::assertTrue($table->hasColumn('FOO')); |
||
| 62 | |||
| 63 | self::assertSame($column, $table->getColumn('Foo')); |
||
| 64 | self::assertSame($column, $table->getColumn('foo')); |
||
| 65 | self::assertSame($column, $table->getColumn('FOO')); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testCreateColumn() |
||
| 69 | { |
||
| 70 | $type = Type::getType('integer'); |
||
| 71 | |||
| 72 | $table = new Table('foo'); |
||
| 73 | |||
| 74 | self::assertFalse($table->hasColumn('bar')); |
||
| 75 | $table->addColumn('bar', 'integer'); |
||
| 76 | self::assertTrue($table->hasColumn('bar')); |
||
| 77 | self::assertSame($type, $table->getColumn('bar')->getType()); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testDropColumn() |
||
| 81 | { |
||
| 82 | $type = Type::getType('integer'); |
||
| 83 | $columns = []; |
||
| 84 | $columns[] = new Column('foo', $type); |
||
| 85 | $columns[] = new Column('bar', $type); |
||
| 86 | $table = new Table('foo', $columns, [], []); |
||
| 87 | |||
| 88 | self::assertTrue($table->hasColumn('foo')); |
||
| 89 | self::assertTrue($table->hasColumn('bar')); |
||
| 90 | |||
| 91 | $table->dropColumn('foo')->dropColumn('bar'); |
||
| 92 | |||
| 93 | self::assertFalse($table->hasColumn('foo')); |
||
| 94 | self::assertFalse($table->hasColumn('bar')); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testGetUnknownColumnThrowsException() |
||
| 98 | { |
||
| 99 | $this->expectException(SchemaException::class); |
||
| 100 | |||
| 101 | $table = new Table('foo', [], [], []); |
||
| 102 | $table->getColumn('unknown'); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testAddColumnTwiceThrowsException() |
||
| 106 | { |
||
| 107 | $this->expectException(SchemaException::class); |
||
| 108 | |||
| 109 | $type = Type::getType('integer'); |
||
| 110 | $columns = []; |
||
| 111 | $columns[] = new Column('foo', $type); |
||
| 112 | $columns[] = new Column('foo', $type); |
||
| 113 | $table = new Table('foo', $columns, [], []); |
||
|
|
|||
| 114 | } |
||
| 115 | |||
| 116 | public function testCreateIndex() |
||
| 117 | { |
||
| 118 | $type = Type::getType('integer'); |
||
| 119 | $columns = [new Column('foo', $type), new Column('bar', $type), new Column('baz', $type)]; |
||
| 120 | $table = new Table('foo', $columns); |
||
| 121 | |||
| 122 | $table->addIndex(['foo', 'bar'], 'foo_foo_bar_idx'); |
||
| 123 | $table->addUniqueIndex(['bar', 'baz'], 'foo_bar_baz_uniq'); |
||
| 124 | |||
| 125 | self::assertTrue($table->hasIndex('foo_foo_bar_idx')); |
||
| 126 | self::assertTrue($table->hasIndex('foo_bar_baz_uniq')); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testIndexCaseInsensitive() |
||
| 130 | { |
||
| 131 | $type = Type::getType('integer'); |
||
| 132 | $columns = [ |
||
| 133 | new Column('foo', $type), |
||
| 134 | new Column('bar', $type), |
||
| 135 | new Column('baz', $type), |
||
| 136 | ]; |
||
| 137 | $table = new Table('foo', $columns); |
||
| 138 | |||
| 139 | $table->addIndex(['foo', 'bar', 'baz'], 'Foo_Idx'); |
||
| 140 | |||
| 141 | self::assertTrue($table->hasIndex('foo_idx')); |
||
| 142 | self::assertTrue($table->hasIndex('Foo_Idx')); |
||
| 143 | self::assertTrue($table->hasIndex('FOO_IDX')); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testAddIndexes() |
||
| 147 | { |
||
| 148 | $type = Type::getType('integer'); |
||
| 149 | $columns = [ |
||
| 150 | new Column('foo', $type), |
||
| 151 | new Column('bar', $type), |
||
| 152 | ]; |
||
| 153 | $indexes = [ |
||
| 154 | new Index('the_primary', ['foo'], true, true), |
||
| 155 | new Index('bar_idx', ['bar'], false, false), |
||
| 156 | ]; |
||
| 157 | $table = new Table('foo', $columns, $indexes, []); |
||
| 158 | |||
| 159 | self::assertTrue($table->hasIndex('the_primary')); |
||
| 160 | self::assertTrue($table->hasIndex('bar_idx')); |
||
| 161 | self::assertFalse($table->hasIndex('some_idx')); |
||
| 162 | |||
| 163 | self::assertInstanceOf(Index::class, $table->getPrimaryKey()); |
||
| 164 | self::assertInstanceOf(Index::class, $table->getIndex('the_primary')); |
||
| 165 | self::assertInstanceOf(Index::class, $table->getIndex('bar_idx')); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function testGetUnknownIndexThrowsException() |
||
| 169 | { |
||
| 170 | $this->expectException(SchemaException::class); |
||
| 171 | |||
| 172 | $table = new Table('foo', [], [], []); |
||
| 173 | $table->getIndex('unknownIndex'); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function testAddTwoPrimaryThrowsException() |
||
| 177 | { |
||
| 178 | $this->expectException(SchemaException::class); |
||
| 179 | |||
| 180 | $type = Type::getType('integer'); |
||
| 181 | $columns = [new Column('foo', $type), new Column('bar', $type)]; |
||
| 182 | $indexes = [ |
||
| 183 | new Index('the_primary', ['foo'], true, true), |
||
| 184 | new Index('other_primary', ['bar'], true, true), |
||
| 185 | ]; |
||
| 186 | $table = new Table('foo', $columns, $indexes, []); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testAddTwoIndexesWithSameNameThrowsException() |
||
| 190 | { |
||
| 191 | $this->expectException(SchemaException::class); |
||
| 192 | |||
| 193 | $type = Type::getType('integer'); |
||
| 194 | $columns = [new Column('foo', $type), new Column('bar', $type)]; |
||
| 195 | $indexes = [ |
||
| 196 | new Index('an_idx', ['foo'], false, false), |
||
| 197 | new Index('an_idx', ['bar'], false, false), |
||
| 198 | ]; |
||
| 199 | $table = new Table('foo', $columns, $indexes, []); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function testConstraints() |
||
| 203 | { |
||
| 204 | $constraint = new ForeignKeyConstraint([], 'foo', []); |
||
| 205 | |||
| 206 | $tableA = new Table('foo', [], [], [], [$constraint]); |
||
| 207 | $constraints = $tableA->getForeignKeys(); |
||
| 208 | |||
| 209 | self::assertCount(1, $constraints); |
||
| 210 | |||
| 211 | $constraintNames = array_keys($constraints); |
||
| 212 | |||
| 213 | self::assertSame('fk_8c736521', $constraintNames[0]); |
||
| 214 | self::assertSame($constraint, $constraints['fk_8c736521']); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testOptions() |
||
| 218 | { |
||
| 219 | $table = new Table('foo', [], [], [], [], ['foo' => 'bar']); |
||
| 220 | |||
| 221 | self::assertTrue($table->hasOption('foo')); |
||
| 222 | self::assertEquals('bar', $table->getOption('foo')); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function testBuilderSetPrimaryKey() |
||
| 226 | { |
||
| 227 | $table = new Table('foo'); |
||
| 228 | |||
| 229 | $table->addColumn('bar', 'integer'); |
||
| 230 | $table->setPrimaryKey(['bar']); |
||
| 231 | |||
| 232 | self::assertTrue($table->hasIndex('primary')); |
||
| 233 | self::assertInstanceOf(Index::class, $table->getPrimaryKey()); |
||
| 234 | self::assertTrue($table->getIndex('primary')->isUnique()); |
||
| 235 | self::assertTrue($table->getIndex('primary')->isPrimary()); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function testBuilderAddUniqueIndex() |
||
| 239 | { |
||
| 240 | $table = new Table('foo'); |
||
| 241 | |||
| 242 | $table->addColumn('bar', 'integer'); |
||
| 243 | $table->addUniqueIndex(['bar'], 'my_idx'); |
||
| 244 | |||
| 245 | self::assertTrue($table->hasIndex('my_idx')); |
||
| 246 | self::assertTrue($table->getIndex('my_idx')->isUnique()); |
||
| 247 | self::assertFalse($table->getIndex('my_idx')->isPrimary()); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function testBuilderAddIndex() |
||
| 251 | { |
||
| 252 | $table = new Table('foo'); |
||
| 253 | |||
| 254 | $table->addColumn('bar', 'integer'); |
||
| 255 | $table->addIndex(['bar'], 'my_idx'); |
||
| 256 | |||
| 257 | self::assertTrue($table->hasIndex('my_idx')); |
||
| 258 | self::assertFalse($table->getIndex('my_idx')->isUnique()); |
||
| 259 | self::assertFalse($table->getIndex('my_idx')->isPrimary()); |
||
| 260 | } |
||
| 261 | |||
| 262 | public function testBuilderAddIndexWithInvalidNameThrowsException() |
||
| 263 | { |
||
| 264 | $this->expectException(SchemaException::class); |
||
| 265 | |||
| 266 | $table = new Table('foo'); |
||
| 267 | $table->addColumn('bar', 'integer'); |
||
| 268 | $table->addIndex(['bar'], 'invalid name %&/'); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function testBuilderAddIndexWithUnknownColumnThrowsException() |
||
| 272 | { |
||
| 273 | $this->expectException(SchemaException::class); |
||
| 274 | |||
| 275 | $table = new Table('foo'); |
||
| 276 | $table->addIndex(['bar'], 'invalidName'); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function testBuilderOptions() |
||
| 280 | { |
||
| 281 | $table = new Table('foo'); |
||
| 282 | $table->addOption('foo', 'bar'); |
||
| 283 | self::assertTrue($table->hasOption('foo')); |
||
| 284 | self::assertEquals('bar', $table->getOption('foo')); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function testAddForeignKeyConstraintUnknownLocalColumnThrowsException() |
||
| 288 | { |
||
| 289 | $this->expectException(SchemaException::class); |
||
| 290 | |||
| 291 | $table = new Table('foo'); |
||
| 292 | $table->addColumn('id', 'integer'); |
||
| 293 | |||
| 294 | $foreignTable = new Table('bar'); |
||
| 295 | $foreignTable->addColumn('id', 'integer'); |
||
| 296 | |||
| 297 | $table->addForeignKeyConstraint($foreignTable, ['foo'], ['id']); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testAddForeignKeyConstraintUnknownForeignColumnThrowsException() |
||
| 301 | { |
||
| 302 | $this->expectException(SchemaException::class); |
||
| 303 | |||
| 304 | $table = new Table('foo'); |
||
| 305 | $table->addColumn('id', 'integer'); |
||
| 306 | |||
| 307 | $foreignTable = new Table('bar'); |
||
| 308 | $foreignTable->addColumn('id', 'integer'); |
||
| 309 | |||
| 310 | $table->addForeignKeyConstraint($foreignTable, ['id'], ['foo']); |
||
| 311 | } |
||
| 312 | |||
| 313 | public function testAddForeignKeyConstraint() |
||
| 314 | { |
||
| 315 | $table = new Table('foo'); |
||
| 316 | $table->addColumn('id', 'integer'); |
||
| 317 | |||
| 318 | $foreignTable = new Table('bar'); |
||
| 319 | $foreignTable->addColumn('id', 'integer'); |
||
| 320 | |||
| 321 | $table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']); |
||
| 322 | |||
| 323 | $constraints = $table->getForeignKeys(); |
||
| 324 | self::assertCount(1, $constraints); |
||
| 325 | $constraint = current($constraints); |
||
| 326 | |||
| 327 | self::assertInstanceOf(ForeignKeyConstraint::class, $constraint); |
||
| 328 | |||
| 329 | self::assertTrue($constraint->hasOption('foo')); |
||
| 330 | self::assertEquals('bar', $constraint->getOption('foo')); |
||
| 331 | } |
||
| 332 | |||
| 333 | public function testAddIndexWithCaseSensitiveColumnProblem() |
||
| 334 | { |
||
| 335 | $table = new Table('foo'); |
||
| 336 | $table->addColumn('id', 'integer'); |
||
| 337 | |||
| 338 | $table->addIndex(['ID'], 'my_idx'); |
||
| 339 | |||
| 340 | self::assertTrue($table->hasIndex('my_idx')); |
||
| 341 | self::assertEquals(['ID'], $table->getIndex('my_idx')->getColumns()); |
||
| 342 | self::assertTrue($table->getIndex('my_idx')->spansColumns(['id'])); |
||
| 343 | } |
||
| 344 | |||
| 345 | public function testAddPrimaryKeyColumnsAreExplicitlySetToNotNull() |
||
| 346 | { |
||
| 347 | $table = new Table('foo'); |
||
| 348 | $column = $table->addColumn('id', 'integer', ['notnull' => false]); |
||
| 349 | |||
| 350 | self::assertFalse($column->getNotnull()); |
||
| 351 | |||
| 352 | $table->setPrimaryKey(['id']); |
||
| 353 | |||
| 354 | self::assertTrue($column->getNotnull()); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @group DDC-133 |
||
| 359 | */ |
||
| 360 | public function testAllowImplicitSchemaTableInAutogeneratedIndexNames() |
||
| 361 | { |
||
| 362 | $table = new Table('foo.bar'); |
||
| 363 | $table->addColumn('baz', 'integer', []); |
||
| 364 | $table->addIndex(['baz']); |
||
| 365 | |||
| 366 | self::assertCount(1, $table->getIndexes()); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @group DBAL-50 |
||
| 371 | */ |
||
| 372 | public function testAddForeignKeyIndexImplicitly() |
||
| 373 | { |
||
| 374 | $table = new Table('foo'); |
||
| 375 | $table->addColumn('id', 'integer'); |
||
| 376 | |||
| 377 | $foreignTable = new Table('bar'); |
||
| 378 | $foreignTable->addColumn('id', 'integer'); |
||
| 379 | |||
| 380 | $table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']); |
||
| 381 | |||
| 382 | $indexes = $table->getIndexes(); |
||
| 383 | self::assertCount(1, $indexes); |
||
| 384 | $index = current($indexes); |
||
| 385 | |||
| 386 | self::assertTrue($table->hasIndex($index->getName())); |
||
| 387 | self::assertEquals(['id'], $index->getColumns()); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @group DBAL-1063 |
||
| 392 | */ |
||
| 393 | public function testAddForeignKeyDoesNotCreateDuplicateIndex() |
||
| 394 | { |
||
| 395 | $table = new Table('foo'); |
||
| 396 | $table->addColumn('bar', 'integer'); |
||
| 397 | $table->addIndex(['bar'], 'bar_idx'); |
||
| 398 | |||
| 399 | $foreignTable = new Table('bar'); |
||
| 400 | $foreignTable->addColumn('foo', 'integer'); |
||
| 401 | |||
| 402 | $table->addForeignKeyConstraint($foreignTable, ['bar'], ['foo']); |
||
| 403 | |||
| 404 | self::assertCount(1, $table->getIndexes()); |
||
| 405 | self::assertTrue($table->hasIndex('bar_idx')); |
||
| 406 | self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @group DBAL-1063 |
||
| 411 | */ |
||
| 412 | public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan() |
||
| 413 | { |
||
| 414 | $table = new Table('foo'); |
||
| 415 | $table->addColumn('bar', 'integer'); |
||
| 416 | $table->addColumn('baz', 'string'); |
||
| 417 | $table->addColumn('bloo', 'string'); |
||
| 418 | $table->addIndex(['baz', 'bar'], 'composite_idx'); |
||
| 419 | $table->addIndex(['bar', 'baz', 'bloo'], 'full_idx'); |
||
| 420 | |||
| 421 | $foreignTable = new Table('bar'); |
||
| 422 | $foreignTable->addColumn('foo', 'integer'); |
||
| 423 | $foreignTable->addColumn('baz', 'string'); |
||
| 424 | |||
| 425 | $table->addForeignKeyConstraint($foreignTable, ['bar', 'baz'], ['foo', 'baz']); |
||
| 426 | |||
| 427 | self::assertCount(3, $table->getIndexes()); |
||
| 428 | self::assertTrue($table->hasIndex('composite_idx')); |
||
| 429 | self::assertTrue($table->hasIndex('full_idx')); |
||
| 430 | self::assertTrue($table->hasIndex('idx_8c73652176ff8caa78240498')); |
||
| 431 | self::assertSame(['baz', 'bar'], $table->getIndex('composite_idx')->getColumns()); |
||
| 432 | self::assertSame(['bar', 'baz', 'bloo'], $table->getIndex('full_idx')->getColumns()); |
||
| 433 | self::assertSame(['bar', 'baz'], $table->getIndex('idx_8c73652176ff8caa78240498')->getColumns()); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @group DBAL-50 |
||
| 438 | * @group DBAL-1063 |
||
| 439 | */ |
||
| 440 | public function testOverrulingIndexDoesNotDropOverruledIndex() |
||
| 441 | { |
||
| 442 | $table = new Table('bar'); |
||
| 443 | $table->addColumn('baz', 'integer', []); |
||
| 444 | $table->addIndex(['baz']); |
||
| 445 | |||
| 446 | $indexes = $table->getIndexes(); |
||
| 447 | self::assertCount(1, $indexes); |
||
| 448 | $index = current($indexes); |
||
| 449 | |||
| 450 | $table->addUniqueIndex(['baz']); |
||
| 451 | self::assertCount(2, $table->getIndexes()); |
||
| 452 | self::assertTrue($table->hasIndex($index->getName())); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @group DBAL-1063 |
||
| 457 | */ |
||
| 458 | public function testAllowsAddingDuplicateIndexesBasedOnColumns() |
||
| 459 | { |
||
| 460 | $table = new Table('foo'); |
||
| 461 | $table->addColumn('bar', 'integer'); |
||
| 462 | $table->addIndex(['bar'], 'bar_idx'); |
||
| 463 | $table->addIndex(['bar'], 'duplicate_idx'); |
||
| 464 | |||
| 465 | self::assertCount(2, $table->getIndexes()); |
||
| 466 | self::assertTrue($table->hasIndex('bar_idx')); |
||
| 467 | self::assertTrue($table->hasIndex('duplicate_idx')); |
||
| 468 | self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
||
| 469 | self::assertSame(['bar'], $table->getIndex('duplicate_idx')->getColumns()); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @group DBAL-1063 |
||
| 474 | */ |
||
| 475 | public function testAllowsAddingFulfillingIndexesBasedOnColumns() |
||
| 476 | { |
||
| 477 | $table = new Table('foo'); |
||
| 478 | $table->addColumn('bar', 'integer'); |
||
| 479 | $table->addColumn('baz', 'string'); |
||
| 480 | $table->addIndex(['bar'], 'bar_idx'); |
||
| 481 | $table->addIndex(['bar', 'baz'], 'fulfilling_idx'); |
||
| 482 | |||
| 483 | self::assertCount(2, $table->getIndexes()); |
||
| 484 | self::assertTrue($table->hasIndex('bar_idx')); |
||
| 485 | self::assertTrue($table->hasIndex('fulfilling_idx')); |
||
| 486 | self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
||
| 487 | self::assertSame(['bar', 'baz'], $table->getIndex('fulfilling_idx')->getColumns()); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @group DBAL-50 |
||
| 492 | * @group DBAL-1063 |
||
| 493 | */ |
||
| 494 | public function testPrimaryKeyOverrulingUniqueIndexDoesNotDropUniqueIndex() |
||
| 495 | { |
||
| 496 | $table = new Table('bar'); |
||
| 497 | $table->addColumn('baz', 'integer', []); |
||
| 498 | $table->addUniqueIndex(['baz'], 'idx_unique'); |
||
| 499 | |||
| 500 | $table->setPrimaryKey(['baz']); |
||
| 501 | |||
| 502 | $indexes = $table->getIndexes(); |
||
| 503 | self::assertCount(2, $indexes, 'Table should only contain both the primary key table index and the unique one, even though it was overruled.'); |
||
| 504 | |||
| 505 | self::assertTrue($table->hasPrimaryKey()); |
||
| 506 | self::assertTrue($table->hasIndex('idx_unique')); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex() |
||
| 510 | { |
||
| 511 | $foreignTable = new Table('foreign'); |
||
| 512 | $foreignTable->addColumn('id', 'integer'); |
||
| 513 | |||
| 514 | $localTable = new Table('local'); |
||
| 515 | $localTable->addColumn('id', 'integer'); |
||
| 516 | $localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
||
| 517 | |||
| 518 | self::assertCount(1, $localTable->getIndexes()); |
||
| 519 | |||
| 520 | $localTable->addIndex(['id'], 'explicit_idx'); |
||
| 521 | |||
| 522 | self::assertCount(1, $localTable->getIndexes()); |
||
| 523 | self::assertTrue($localTable->hasIndex('explicit_idx')); |
||
| 524 | } |
||
| 525 | |||
| 526 | public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex() |
||
| 527 | { |
||
| 528 | $foreignTable = new Table('foreign'); |
||
| 529 | $foreignTable->addColumn('id', 'integer'); |
||
| 530 | |||
| 531 | $localTable = new Table('local'); |
||
| 532 | $localTable->addColumn('id', 'integer'); |
||
| 533 | $localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
||
| 534 | |||
| 535 | self::assertCount(1, $localTable->getIndexes()); |
||
| 536 | |||
| 537 | $localTable->addUniqueIndex(['id'], 'explicit_idx'); |
||
| 538 | |||
| 539 | self::assertCount(1, $localTable->getIndexes()); |
||
| 540 | self::assertTrue($localTable->hasIndex('explicit_idx')); |
||
| 541 | } |
||
| 542 | |||
| 543 | public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex() |
||
| 544 | { |
||
| 545 | $foreignTable = new Table('foreign'); |
||
| 546 | $foreignTable->addColumn('id', 'integer'); |
||
| 547 | |||
| 548 | $localTable = new Table('local'); |
||
| 549 | $localTable->addColumn('id', 'integer'); |
||
| 550 | $localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
||
| 551 | |||
| 552 | self::assertCount(1, $localTable->getIndexes()); |
||
| 553 | |||
| 554 | $localTable->setPrimaryKey(['id'], 'explicit_idx'); |
||
| 555 | |||
| 556 | self::assertCount(1, $localTable->getIndexes()); |
||
| 557 | self::assertTrue($localTable->hasIndex('explicit_idx')); |
||
| 558 | } |
||
| 559 | |||
| 560 | public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException() |
||
| 561 | { |
||
| 562 | $foreignTable = new Table('foreign'); |
||
| 563 | $foreignTable->addColumn('id', 'integer'); |
||
| 564 | |||
| 565 | $localTable = new Table('local'); |
||
| 566 | $localTable->addColumn('id', 'integer'); |
||
| 567 | $localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
||
| 568 | |||
| 569 | self::assertCount(1, $localTable->getIndexes()); |
||
| 570 | self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750')); |
||
| 571 | |||
| 572 | $implicitIndex = $localTable->getIndex('IDX_8BD688E8BF396750'); |
||
| 573 | |||
| 574 | $localTable->addIndex(['id'], 'IDX_8BD688E8BF396750'); |
||
| 575 | |||
| 576 | self::assertCount(1, $localTable->getIndexes()); |
||
| 577 | self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750')); |
||
| 578 | self::assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750')); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @group DBAL-64 |
||
| 583 | */ |
||
| 584 | public function testQuotedTableName() |
||
| 585 | { |
||
| 586 | $table = new Table('`bar`'); |
||
| 587 | |||
| 588 | $mysqlPlatform = new MySqlPlatform(); |
||
| 589 | $sqlitePlatform = new SqlitePlatform(); |
||
| 590 | |||
| 591 | self::assertEquals('bar', $table->getName()); |
||
| 592 | self::assertEquals('`bar`', $table->getQuotedName($mysqlPlatform)); |
||
| 593 | self::assertEquals('"bar"', $table->getQuotedName($sqlitePlatform)); |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @group DBAL-79 |
||
| 598 | */ |
||
| 599 | public function testTableHasPrimaryKey() |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @group DBAL-91 |
||
| 613 | */ |
||
| 614 | public function testAddIndexWithQuotedColumns() |
||
| 615 | { |
||
| 616 | $table = new Table('test'); |
||
| 617 | $table->addColumn('"foo"', 'integer'); |
||
| 618 | $table->addColumn('bar', 'integer'); |
||
| 619 | $table->addIndex(['"foo"', '"bar"']); |
||
| 620 | |||
| 621 | self::assertTrue($table->columnsAreIndexed(['"foo"', '"bar"'])); |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @group DBAL-91 |
||
| 626 | */ |
||
| 627 | public function testAddForeignKeyWithQuotedColumnsAndTable() |
||
| 628 | { |
||
| 629 | $table = new Table('test'); |
||
| 630 | $table->addColumn('"foo"', 'integer'); |
||
| 631 | $table->addColumn('bar', 'integer'); |
||
| 632 | $table->addForeignKeyConstraint('"boing"', ['"foo"', '"bar"'], ['id']); |
||
| 633 | |||
| 634 | self::assertCount(1, $table->getForeignKeys()); |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @group DBAL-177 |
||
| 639 | */ |
||
| 640 | public function testQuoteSchemaPrefixed() |
||
| 641 | { |
||
| 642 | $table = new Table('`test`.`test`'); |
||
| 643 | self::assertEquals('test.test', $table->getName()); |
||
| 644 | self::assertEquals('`test`.`test`', $table->getQuotedName(new MySqlPlatform())); |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @group DBAL-204 |
||
| 649 | */ |
||
| 650 | public function testFullQualifiedTableName() |
||
| 651 | { |
||
| 652 | $table = new Table('`test`.`test`'); |
||
| 653 | self::assertEquals('test.test', $table->getFullQualifiedName('test')); |
||
| 654 | self::assertEquals('test.test', $table->getFullQualifiedName('other')); |
||
| 655 | |||
| 656 | $table = new Table('test'); |
||
| 657 | self::assertEquals('test.test', $table->getFullQualifiedName('test')); |
||
| 658 | self::assertEquals('other.test', $table->getFullQualifiedName('other')); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @group DBAL-224 |
||
| 663 | */ |
||
| 664 | public function testDropIndex() |
||
| 665 | { |
||
| 666 | $table = new Table('test'); |
||
| 667 | $table->addColumn('id', 'integer'); |
||
| 668 | $table->addIndex(['id'], 'idx'); |
||
| 669 | |||
| 670 | self::assertTrue($table->hasIndex('idx')); |
||
| 671 | |||
| 672 | $table->dropIndex('idx'); |
||
| 673 | self::assertFalse($table->hasIndex('idx')); |
||
| 674 | } |
||
| 675 | |||
| 676 | /** |
||
| 677 | * @group DBAL-224 |
||
| 678 | */ |
||
| 679 | public function testDropPrimaryKey() |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @group DBAL-234 |
||
| 693 | */ |
||
| 694 | public function testRenameIndex() |
||
| 695 | { |
||
| 696 | $table = new Table('test'); |
||
| 697 | $table->addColumn('id', 'integer'); |
||
| 698 | $table->addColumn('foo', 'integer'); |
||
| 699 | $table->addColumn('bar', 'integer'); |
||
| 700 | $table->addColumn('baz', 'integer'); |
||
| 701 | $table->setPrimaryKey(['id'], 'pk'); |
||
| 702 | $table->addIndex(['foo'], 'idx', ['flag']); |
||
| 703 | $table->addUniqueIndex(['bar', 'baz'], 'uniq'); |
||
| 704 | |||
| 705 | // Rename to custom name. |
||
| 706 | self::assertSame($table, $table->renameIndex('pk', 'pk_new')); |
||
| 707 | self::assertSame($table, $table->renameIndex('idx', 'idx_new')); |
||
| 708 | self::assertSame($table, $table->renameIndex('uniq', 'uniq_new')); |
||
| 709 | |||
| 710 | self::assertTrue($table->hasPrimaryKey()); |
||
| 711 | self::assertTrue($table->hasIndex('pk_new')); |
||
| 712 | self::assertTrue($table->hasIndex('idx_new')); |
||
| 713 | self::assertTrue($table->hasIndex('uniq_new')); |
||
| 714 | |||
| 715 | self::assertFalse($table->hasIndex('pk')); |
||
| 716 | self::assertFalse($table->hasIndex('idx')); |
||
| 717 | self::assertFalse($table->hasIndex('uniq')); |
||
| 718 | |||
| 719 | self::assertEquals(new Index('pk_new', ['id'], true, true), $table->getPrimaryKey()); |
||
| 720 | self::assertEquals(new Index('pk_new', ['id'], true, true), $table->getIndex('pk_new')); |
||
| 721 | self::assertEquals( |
||
| 722 | new Index('idx_new', ['foo'], false, false, ['flag']), |
||
| 723 | $table->getIndex('idx_new') |
||
| 724 | ); |
||
| 725 | self::assertEquals(new Index('uniq_new', ['bar', 'baz'], true), $table->getIndex('uniq_new')); |
||
| 726 | |||
| 727 | // Rename to auto-generated name. |
||
| 728 | self::assertSame($table, $table->renameIndex('pk_new', null)); |
||
| 729 | self::assertSame($table, $table->renameIndex('idx_new', null)); |
||
| 730 | self::assertSame($table, $table->renameIndex('uniq_new', null)); |
||
| 731 | |||
| 732 | self::assertTrue($table->hasPrimaryKey()); |
||
| 733 | self::assertTrue($table->hasIndex('primary')); |
||
| 734 | self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521')); |
||
| 735 | self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498')); |
||
| 736 | |||
| 737 | self::assertFalse($table->hasIndex('pk_new')); |
||
| 738 | self::assertFalse($table->hasIndex('idx_new')); |
||
| 739 | self::assertFalse($table->hasIndex('uniq_new')); |
||
| 740 | |||
| 741 | self::assertEquals(new Index('primary', ['id'], true, true), $table->getPrimaryKey()); |
||
| 742 | self::assertEquals(new Index('primary', ['id'], true, true), $table->getIndex('primary')); |
||
| 743 | self::assertEquals( |
||
| 744 | new Index('IDX_D87F7E0C8C736521', ['foo'], false, false, ['flag']), |
||
| 745 | $table->getIndex('IDX_D87F7E0C8C736521') |
||
| 746 | ); |
||
| 747 | self::assertEquals( |
||
| 748 | new Index('UNIQ_D87F7E0C76FF8CAA78240498', ['bar', 'baz'], true), |
||
| 749 | $table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498') |
||
| 750 | ); |
||
| 751 | |||
| 752 | // Rename to same name (changed case). |
||
| 753 | self::assertSame($table, $table->renameIndex('primary', 'PRIMARY')); |
||
| 754 | self::assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521')); |
||
| 755 | self::assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498')); |
||
| 756 | |||
| 757 | self::assertTrue($table->hasPrimaryKey()); |
||
| 758 | self::assertTrue($table->hasIndex('primary')); |
||
| 759 | self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521')); |
||
| 760 | self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498')); |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @group DBAL-2508 |
||
| 765 | */ |
||
| 766 | public function testKeepsIndexOptionsOnRenamingRegularIndex() |
||
| 767 | { |
||
| 768 | $table = new Table('foo'); |
||
| 769 | $table->addColumn('id', 'integer'); |
||
| 770 | $table->addIndex(['id'], 'idx_bar', [], ['where' => '1 = 1']); |
||
| 771 | |||
| 772 | $table->renameIndex('idx_bar', 'idx_baz'); |
||
| 773 | |||
| 774 | self::assertSame(['where' => '1 = 1'], $table->getIndex('idx_baz')->getOptions()); |
||
| 775 | } |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @group DBAL-2508 |
||
| 779 | */ |
||
| 780 | public function testKeepsIndexOptionsOnRenamingUniqueIndex() |
||
| 781 | { |
||
| 782 | $table = new Table('foo'); |
||
| 783 | $table->addColumn('id', 'integer'); |
||
| 784 | $table->addUniqueIndex(['id'], 'idx_bar', ['where' => '1 = 1']); |
||
| 785 | |||
| 786 | $table->renameIndex('idx_bar', 'idx_baz'); |
||
| 787 | |||
| 788 | self::assertSame(['where' => '1 = 1'], $table->getIndex('idx_baz')->getOptions()); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @group DBAL-234 |
||
| 793 | */ |
||
| 794 | public function testThrowsExceptionOnRenamingNonExistingIndex() |
||
| 795 | { |
||
| 796 | $table = new Table('test'); |
||
| 797 | $table->addColumn('id', 'integer'); |
||
| 798 | $table->addIndex(['id'], 'idx'); |
||
| 799 | |||
| 800 | $this->expectException(SchemaException::class); |
||
| 801 | |||
| 802 | $table->renameIndex('foo', 'bar'); |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @group DBAL-234 |
||
| 807 | */ |
||
| 808 | public function testThrowsExceptionOnRenamingToAlreadyExistingIndex() |
||
| 809 | { |
||
| 810 | $table = new Table('test'); |
||
| 811 | $table->addColumn('id', 'integer'); |
||
| 812 | $table->addColumn('foo', 'integer'); |
||
| 813 | $table->addIndex(['id'], 'idx_id'); |
||
| 814 | $table->addIndex(['foo'], 'idx_foo'); |
||
| 815 | |||
| 816 | $this->expectException(SchemaException::class); |
||
| 817 | |||
| 818 | $table->renameIndex('idx_id', 'idx_foo'); |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * @dataProvider getNormalizesAssetNames |
||
| 823 | * @group DBAL-831 |
||
| 824 | */ |
||
| 825 | public function testNormalizesColumnNames($assetName) |
||
| 877 | } |
||
| 878 | |||
| 879 | public function getNormalizesAssetNames() |
||
| 880 | { |
||
| 881 | return [ |
||
| 882 | ['foo'], |
||
| 883 | ['FOO'], |
||
| 884 | ['`foo`'], |
||
| 885 | ['`FOO`'], |
||
| 886 | ['"foo"'], |
||
| 887 | ['"FOO"'], |
||
| 888 | ['"foo"'], |
||
| 889 | ['"FOO"'], |
||
| 890 | ]; |
||
| 891 | } |
||
| 892 | |||
| 893 | public function testUniqueConstraintWithEmptyName() : void |
||
| 894 | { |
||
| 895 | $columns = [ |
||
| 920 | } |
||
| 921 | } |
||
| 922 |