Completed
Pull Request — master (#3610)
by Sergei
03:03
created

TableTest::testAddIndexWithQuotedColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Schema;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Platforms\MySqlPlatform;
9
use Doctrine\DBAL\Platforms\SqlitePlatform;
10
use Doctrine\DBAL\Schema\Column;
11
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
12
use Doctrine\DBAL\Schema\Index;
13
use Doctrine\DBAL\Schema\SchemaException;
14
use Doctrine\DBAL\Schema\Table;
15
use Doctrine\DBAL\Schema\UniqueConstraint;
16
use Doctrine\DBAL\Types\Type;
17
use Doctrine\Tests\DbalTestCase;
18
use function array_keys;
19
use function current;
20
21
class TableTest extends DbalTestCase
22
{
23
    public function testCreateWithInvalidTableName() : void
24
    {
25
        $this->expectException(DBALException::class);
26
27
        new Table('');
28
    }
29
30
    public function testGetName() : void
31
    {
32
        $table =  new Table('foo', [], [], []);
33
        self::assertEquals('foo', $table->getName());
34
    }
35
36
    public function testColumns() : void
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());
0 ignored issues
show
Documentation introduced by
$table->getColumns() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    public function testColumnsCaseInsensitive() : void
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() : void
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() : void
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() : void
98
    {
99
        $this->expectException(SchemaException::class);
100
101
        $table = new Table('foo', [], [], []);
102
        $table->getColumn('unknown');
103
    }
104
105
    public function testAddColumnTwiceThrowsException() : void
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, [], []);
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
    }
115
116 View Code Duplication
    public function testCreateIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testIndexCaseInsensitive() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() : void
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() : void
169
    {
170
        $this->expectException(SchemaException::class);
171
172
        $table = new Table('foo', [], [], []);
173
        $table->getIndex('unknownIndex');
174
    }
175
176 View Code Duplication
    public function testAddTwoPrimaryThrowsException() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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, []);
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
187
    }
188
189 View Code Duplication
    public function testAddTwoIndexesWithSameNameThrowsException() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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, []);
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
200
    }
201
202
    public function testConstraints() : void
203
    {
204
        $constraint = new ForeignKeyConstraint([], 'foo', []);
205
206
        $tableA      = new Table('foo', [], [], [], [$constraint]);
207
        $constraints = $tableA->getForeignKeys();
208
209
        self::assertCount(1, $constraints);
0 ignored issues
show
Documentation introduced by
$constraints is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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 View Code Duplication
    public function testBuilderSetPrimaryKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testBuilderAddUniqueIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testBuilderAddIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() : void
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() : void
272
    {
273
        $this->expectException(SchemaException::class);
274
275
        $table = new Table('foo');
276
        $table->addIndex(['bar'], 'invalidName');
277
    }
278
279
    public function testBuilderOptions() : void
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 View Code Duplication
    public function testAddForeignKeyConstraintUnknownLocalColumnThrowsException() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testAddForeignKeyConstraintUnknownForeignColumnThrowsException() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() : void
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);
0 ignored issues
show
Documentation introduced by
$constraints is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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 View Code Duplication
    public function testAddPrimaryKeyColumnsAreExplicitlySetToNotNull() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testAllowImplicitSchemaTableInAutogeneratedIndexNames() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
    {
362
        $table = new Table('foo.bar');
363
        $table->addColumn('baz', 'integer', []);
364
        $table->addIndex(['baz']);
365
366
        self::assertCount(1, $table->getIndexes());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
367
    }
368
369
    /**
370
     * @group DBAL-50
371
     */
372
    public function testAddForeignKeyIndexImplicitly() : void
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);
0 ignored issues
show
Documentation introduced by
$indexes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 View Code Duplication
    public function testAddForeignKeyDoesNotCreateDuplicateIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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);
0 ignored issues
show
Documentation introduced by
$indexes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
448
        $index = current($indexes);
449
450
        $table->addUniqueIndex(['baz']);
451
        self::assertCount(2, $table->getIndexes());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
452
        self::assertTrue($table->hasIndex($index->getName()));
453
    }
454
455
    /**
456
     * @group DBAL-1063
457
     */
458
    public function testAllowsAddingDuplicateIndexesBasedOnColumns() : void
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());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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());
0 ignored issues
show
Documentation introduced by
$table->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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.');
0 ignored issues
show
Documentation introduced by
$indexes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
504
505
        self::assertTrue($table->hasPrimaryKey());
506
        self::assertTrue($table->hasIndex('idx_unique'));
507
    }
508
509 View Code Duplication
    public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
519
520
        $localTable->addIndex(['id'], 'explicit_idx');
521
522
        self::assertCount(1, $localTable->getIndexes());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
523
        self::assertTrue($localTable->hasIndex('explicit_idx'));
524
    }
525
526 View Code Duplication
    public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
536
537
        $localTable->addUniqueIndex(['id'], 'explicit_idx');
538
539
        self::assertCount(1, $localTable->getIndexes());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
540
        self::assertTrue($localTable->hasIndex('explicit_idx'));
541
    }
542
543 View Code Duplication
    public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
553
554
        $localTable->setPrimaryKey(['id'], 'explicit_idx');
555
556
        self::assertCount(1, $localTable->getIndexes());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
557
        self::assertTrue($localTable->hasIndex('explicit_idx'));
558
    }
559
560
    public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException() : void
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());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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());
0 ignored issues
show
Documentation introduced by
$localTable->getIndexes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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 View Code Duplication
    public function testTableHasPrimaryKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
600
    {
601
        $table = new Table('test');
602
603
        self::assertFalse($table->hasPrimaryKey());
604
605
        $table->addColumn('foo', 'integer');
606
        $table->setPrimaryKey(['foo']);
607
608
        self::assertTrue($table->hasPrimaryKey());
609
    }
610
611
    /**
612
     * @group DBAL-91
613
     */
614
    public function testAddIndexWithQuotedColumns() : void
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() : void
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());
0 ignored issues
show
Documentation introduced by
$table->getForeignKeys() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
635
    }
636
637
    /**
638
     * @group DBAL-177
639
     */
640
    public function testQuoteSchemaPrefixed() : void
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() : void
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() : void
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 View Code Duplication
    public function testDropPrimaryKey() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
680
    {
681
        $table = new Table('test');
682
        $table->addColumn('id', 'integer');
683
        $table->setPrimaryKey(['id']);
684
685
        self::assertTrue($table->hasPrimaryKey());
686
687
        $table->dropPrimaryKey();
688
        self::assertFalse($table->hasPrimaryKey());
689
    }
690
691
    /**
692
     * @group DBAL-234
693
     */
694
    public function testRenameIndex() : void
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() : void
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 View Code Duplication
    public function testKeepsIndexOptionsOnRenamingUniqueIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testThrowsExceptionOnRenamingNonExistingIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testThrowsExceptionOnRenamingToAlreadyExistingIndex() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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(string $assetName) : void
826
    {
827
        $table = new Table('test');
828
829
        $table->addColumn($assetName, 'integer');
830
        $table->addIndex([$assetName], $assetName);
831
        $table->addForeignKeyConstraint('test', [$assetName], [$assetName], [], $assetName);
832
833
        self::assertTrue($table->hasColumn($assetName));
834
        self::assertTrue($table->hasColumn('foo'));
835
        self::assertInstanceOf(Column::class, $table->getColumn($assetName));
836
        self::assertInstanceOf(Column::class, $table->getColumn('foo'));
837
838
        self::assertTrue($table->hasIndex($assetName));
839
        self::assertTrue($table->hasIndex('foo'));
840
        self::assertInstanceOf(Index::class, $table->getIndex($assetName));
841
        self::assertInstanceOf(Index::class, $table->getIndex('foo'));
842
843
        self::assertTrue($table->hasForeignKey($assetName));
844
        self::assertTrue($table->hasForeignKey('foo'));
845
        self::assertInstanceOf(ForeignKeyConstraint::class, $table->getForeignKey($assetName));
846
        self::assertInstanceOf(ForeignKeyConstraint::class, $table->getForeignKey('foo'));
847
848
        $table->renameIndex($assetName, $assetName);
849
        self::assertTrue($table->hasIndex($assetName));
850
        self::assertTrue($table->hasIndex('foo'));
851
852
        $table->renameIndex($assetName, 'foo');
853
        self::assertTrue($table->hasIndex($assetName));
854
        self::assertTrue($table->hasIndex('foo'));
855
856
        $table->renameIndex('foo', $assetName);
857
        self::assertTrue($table->hasIndex($assetName));
858
        self::assertTrue($table->hasIndex('foo'));
859
860
        $table->renameIndex($assetName, 'bar');
861
        self::assertFalse($table->hasIndex($assetName));
862
        self::assertFalse($table->hasIndex('foo'));
863
        self::assertTrue($table->hasIndex('bar'));
864
865
        $table->renameIndex('bar', $assetName);
866
867
        $table->dropColumn($assetName);
868
        $table->dropIndex($assetName);
869
        $table->removeForeignKey($assetName);
870
871
        self::assertFalse($table->hasColumn($assetName));
872
        self::assertFalse($table->hasColumn('foo'));
873
        self::assertFalse($table->hasIndex($assetName));
874
        self::assertFalse($table->hasIndex('foo'));
875
        self::assertFalse($table->hasForeignKey($assetName));
876
        self::assertFalse($table->hasForeignKey('foo'));
877
    }
878
879
    /**
880
     * @return mixed[][]
881
     */
882
    public static function getNormalizesAssetNames() : iterable
883
    {
884
        return [
885
            ['foo'],
886
            ['FOO'],
887
            ['`foo`'],
888
            ['`FOO`'],
889
            ['"foo"'],
890
            ['"FOO"'],
891
            ['"foo"'],
892
            ['"FOO"'],
893
        ];
894
    }
895
896
    public function testTableComment() : void
897
    {
898
        $table = new Table('bar');
899
        self::assertNull($table->getComment());
900
901
        $table->setComment('foo');
902
        self::assertEquals('foo', $table->getComment());
903
    }
904
905
    public function testUniqueConstraintWithEmptyName() : void
906
    {
907
        $columns = [
908
            new Column('column1', Type::getType(Type::STRING)),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated with message: Use {@see Types::STRING} instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
909
            new Column('column2', Type::getType(Type::STRING)),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated with message: Use {@see Types::STRING} instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
910
            new Column('column3', Type::getType(Type::STRING)),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated with message: Use {@see Types::STRING} instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
911
            new Column('column4', Type::getType(Type::STRING)),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated with message: Use {@see Types::STRING} instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
912
        ];
913
914
        $uniqueConstraints = [
915
            new UniqueConstraint('', ['column1', 'column2']),
916
            new UniqueConstraint('', ['column3', 'column4']),
917
        ];
918
919
        $table = new Table('test', $columns, [], $uniqueConstraints);
920
921
        $constraints = $table->getUniqueConstraints();
922
923
        self::assertCount(2, $constraints);
0 ignored issues
show
Documentation introduced by
$constraints is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
924
925
        $constraintNames = array_keys($constraints);
926
927
        self::assertSame('fk_d87f7e0c341ce00bad15b1b1', $constraintNames[0]);
928
        self::assertSame('fk_d87f7e0cda12812744761484', $constraintNames[1]);
929
930
        self::assertSame($uniqueConstraints[0], $constraints['fk_d87f7e0c341ce00bad15b1b1']);
931
        self::assertSame($uniqueConstraints[1], $constraints['fk_d87f7e0cda12812744761484']);
932
    }
933
}
934