Failed Conditions
Pull Request — master (#2849)
by Luís
63:28
created

testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
use Doctrine\DBAL\Schema\Index;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Types\Type;
11
12
class TableTest extends \Doctrine\Tests\DbalTestCase
13
{
14
    public function testCreateWithInvalidTableName()
15
    {
16
        $this->expectException(DBALException::class);
17
18
        new \Doctrine\DBAL\Schema\Table('');
19
    }
20
21
    public function testGetName()
22
    {
23
        $table =  new Table("foo", array(), array(), array());
24
        self::assertEquals("foo", $table->getName());
25
    }
26
27
    public function testColumns()
28
    {
29
        $type      = Type::getType('integer');
30
        $columns   = array();
31
        $columns[] = new Column("foo", $type);
32
        $columns[] = new Column("bar", $type);
33
        $table     = new Table("foo", $columns, array(), array());
34
35
        self::assertTrue($table->hasColumn("foo"));
36
        self::assertTrue($table->hasColumn("bar"));
37
        self::assertFalse($table->hasColumn("baz"));
38
39
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
40
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
41
42
        self::assertEquals(2, count($table->getColumns()));
43
    }
44
45
    public function testColumnsCaseInsensitive()
46
    {
47
        $table  = new Table("foo");
48
        $column = $table->addColumn('Foo', 'integer');
49
50
        self::assertTrue($table->hasColumn('Foo'));
51
        self::assertTrue($table->hasColumn('foo'));
52
        self::assertTrue($table->hasColumn('FOO'));
53
54
        self::assertSame($column, $table->getColumn('Foo'));
55
        self::assertSame($column, $table->getColumn('foo'));
56
        self::assertSame($column, $table->getColumn('FOO'));
57
    }
58
59
    public function testCreateColumn()
60
    {
61
        $type = Type::getType('integer');
62
63
        $table = new Table("foo");
64
65
        self::assertFalse($table->hasColumn("bar"));
66
        $table->addColumn("bar", 'integer');
67
        self::assertTrue($table->hasColumn("bar"));
68
        self::assertSame($type, $table->getColumn("bar")->getType());
69
    }
70
71
    public function testDropColumn()
72
    {
73
        $type      = Type::getType('integer');
74
        $columns   = array();
75
        $columns[] = new Column("foo", $type);
76
        $columns[] = new Column("bar", $type);
77
        $table     = new Table("foo", $columns, array(), array());
78
79
        self::assertTrue($table->hasColumn("foo"));
80
        self::assertTrue($table->hasColumn("bar"));
81
82
        $table->dropColumn("foo")->dropColumn("bar");
83
84
        self::assertFalse($table->hasColumn("foo"));
85
        self::assertFalse($table->hasColumn("bar"));
86
    }
87
88
    public function testGetUnknownColumnThrowsException()
89
    {
90
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
91
92
        $table = new Table("foo", array(), array(), array());
93
        $table->getColumn('unknown');
94
    }
95
96
    public function testAddColumnTwiceThrowsException()
97
    {
98
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
99
100
        $type      = \Doctrine\DBAL\Types\Type::getType('integer');
101
        $columns   = array();
102
        $columns[] = new Column("foo", $type);
103
        $columns[] = new Column("foo", $type);
104
        $table     = new Table("foo", $columns, array(), array());
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...
105
    }
106
107 View Code Duplication
    public function testCreateIndex()
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...
108
    {
109
        $type    = \Doctrine\DBAL\Types\Type::getType('integer');
110
        $columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
111
        $table   = new Table("foo", $columns);
112
113
        $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
114
        $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
115
116
        self::assertTrue($table->hasIndex("foo_foo_bar_idx"));
117
        self::assertTrue($table->hasIndex("foo_bar_baz_uniq"));
118
    }
119
120 View Code Duplication
    public function testIndexCaseInsensitive()
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...
121
    {
122
        $type    = \Doctrine\DBAL\Types\Type::getType('integer');
123
        $columns = array(
124
            new Column("foo", $type),
125
            new Column("bar", $type),
126
            new Column("baz", $type)
127
        );
128
        $table   = new Table("foo", $columns);
129
130
        $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
131
132
        self::assertTrue($table->hasIndex('foo_idx'));
133
        self::assertTrue($table->hasIndex('Foo_Idx'));
134
        self::assertTrue($table->hasIndex('FOO_IDX'));
135
    }
136
137
    public function testAddIndexes()
138
    {
139
        $type    = \Doctrine\DBAL\Types\Type::getType('integer');
140
        $columns = array(
141
            new Column("foo", $type),
142
            new Column("bar", $type),
143
        );
144
        $indexes = array(
145
            new Index("the_primary", array("foo"), true, true),
146
            new Index("bar_idx", array("bar"), false, false),
147
        );
148
        $table   = new Table("foo", $columns, $indexes, array());
149
150
        self::assertTrue($table->hasIndex("the_primary"));
151
        self::assertTrue($table->hasIndex("bar_idx"));
152
        self::assertFalse($table->hasIndex("some_idx"));
153
154
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
155
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
156
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
157
    }
158
159
    public function testGetUnknownIndexThrowsException()
160
    {
161
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
162
163
        $table = new Table("foo", array(), array(), array());
164
        $table->getIndex("unknownIndex");
165
    }
166
167 View Code Duplication
    public function testAddTwoPrimaryThrowsException()
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...
168
    {
169
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
170
171
        $type    = \Doctrine\DBAL\Types\Type::getType('integer');
172
        $columns = array(new Column("foo", $type), new Column("bar", $type));
173
        $indexes = array(
174
            new Index("the_primary", array("foo"), true, true),
175
            new Index("other_primary", array("bar"), true, true),
176
        );
177
        $table   = new Table("foo", $columns, $indexes, array());
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...
178
    }
179
180 View Code Duplication
    public function testAddTwoIndexesWithSameNameThrowsException()
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...
181
    {
182
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
183
184
        $type    = \Doctrine\DBAL\Types\Type::getType('integer');
185
        $columns = array(new Column("foo", $type), new Column("bar", $type));
186
        $indexes = array(
187
            new Index("an_idx", array("foo"), false, false),
188
            new Index("an_idx", array("bar"), false, false),
189
        );
190
        $table   = new Table("foo", $columns, $indexes, array());
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...
191
    }
192
193
    public function testConstraints()
194
    {
195
        $constraint = new ForeignKeyConstraint(array(), "foo", array());
196
197
        $tableA      = new Table("foo", array(), array(), array($constraint));
198
        $constraints = $tableA->getForeignKeys();
199
200
        self::assertEquals(1, count($constraints));
201
        self::assertSame($constraint, array_shift($constraints));
202
    }
203
204
    public function testOptions()
205
    {
206
        $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

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...
207
208
        self::assertTrue($table->hasOption("foo"));
209
        self::assertEquals("bar", $table->getOption("foo"));
210
    }
211
212 View Code Duplication
    public function testBuilderSetPrimaryKey()
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...
213
    {
214
        $table = new Table("foo");
215
216
        $table->addColumn("bar", 'integer');
217
        $table->setPrimaryKey(array("bar"));
218
219
        self::assertTrue($table->hasIndex("primary"));
220
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
221
        self::assertTrue($table->getIndex("primary")->isUnique());
222
        self::assertTrue($table->getIndex("primary")->isPrimary());
223
    }
224
225 View Code Duplication
    public function testBuilderAddUniqueIndex()
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->addUniqueIndex(array("bar"), "my_idx");
231
232
        self::assertTrue($table->hasIndex("my_idx"));
233
        self::assertTrue($table->getIndex("my_idx")->isUnique());
234
        self::assertFalse($table->getIndex("my_idx")->isPrimary());
235
    }
236
237 View Code Duplication
    public function testBuilderAddIndex()
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...
238
    {
239
        $table = new Table("foo");
240
241
        $table->addColumn("bar", 'integer');
242
        $table->addIndex(array("bar"), "my_idx");
243
244
        self::assertTrue($table->hasIndex("my_idx"));
245
        self::assertFalse($table->getIndex("my_idx")->isUnique());
246
        self::assertFalse($table->getIndex("my_idx")->isPrimary());
247
    }
248
249 View Code Duplication
    public function testBuilderAddIndexWithInvalidNameThrowsException()
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...
250
    {
251
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
252
253
        $table = new Table("foo");
254
        $table->addColumn("bar", 'integer');
255
        $table->addIndex(array("bar"), "invalid name %&/");
256
    }
257
258
    public function testBuilderAddIndexWithUnknownColumnThrowsException()
259
    {
260
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
261
262
        $table = new Table("foo");
263
        $table->addIndex(array("bar"), "invalidName");
264
    }
265
266
    public function testBuilderOptions()
267
    {
268
        $table = new Table("foo");
269
        $table->addOption("foo", "bar");
270
        self::assertTrue($table->hasOption("foo"));
271
        self::assertEquals("bar", $table->getOption("foo"));
272
    }
273
274 View Code Duplication
    public function testAddForeignKeyConstraintThrowsExceptionWhenUnknownLocalColumnIsGiven()
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...
275
    {
276
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
277
278
        $table = new Table("foo");
279
        $table->addColumn("id", 'integer');
280
281
        $foreignTable = new Table("bar");
282
        $foreignTable->addColumn("id", 'integer');
283
284
        $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
285
    }
286
287 View Code Duplication
    public function testAddForeignKeyConstraintThrowsExceptionWhenUnknownForeignColumnIsGiven()
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("Doctrine\DBAL\Schema\SchemaException");
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, array("id"), array("foo"));
298
    }
299
300
    public function testAddForeignKeyConstraint()
301
    {
302
        $table = new Table("foo");
303
        $table->addColumn("id", 'integer');
304
305
        $foreignTable = new Table("bar");
306
        $foreignTable->addColumn("id", 'integer');
307
308
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
309
310
        $constraints = $table->getForeignKeys();
311
        self::assertEquals(1, count($constraints));
312
        $constraint = current($constraints);
313
314
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
315
316
        self::assertTrue($constraint->hasOption("foo"));
317
        self::assertEquals("bar", $constraint->getOption("foo"));
318
    }
319
320
    public function testAddIndexWithCaseSensitiveColumnProblem()
321
    {
322
        $table = new Table("foo");
323
        $table->addColumn("id", 'integer');
324
325
        $table->addIndex(array("ID"), "my_idx");
326
327
        self::assertTrue($table->hasIndex('my_idx'));
328
        self::assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
329
        self::assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
330
    }
331
332 View Code Duplication
    public function testAddPrimaryKeyColumnsAreExplicitlySetToNotNull()
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...
333
    {
334
        $table  = new Table("foo");
335
        $column = $table->addColumn("id", 'integer', array('notnull' => false));
336
337
        self::assertFalse($column->getNotnull());
338
339
        $table->setPrimaryKey(array('id'));
340
341
        self::assertTrue($column->getNotnull());
342
    }
343
344
    /**
345
     * @group DDC-133
346
     */
347
    public function testAllowImplicitSchemaTableInAutoGeneratedIndexNames()
348
    {
349
        $table = new Table("foo.bar");
350
        $table->addColumn('baz', 'integer', array());
351
        $table->addIndex(array('baz'));
352
353
        self::assertEquals(1, count($table->getIndexes()));
354
    }
355
356
    /**
357
     * @group DBAL-50
358
     */
359
    public function testAddForeignKeyIndexImplicitly()
360
    {
361
        $table = new Table("foo");
362
        $table->addColumn("id", 'integer');
363
364
        $foreignTable = new Table("bar");
365
        $foreignTable->addColumn("id", 'integer');
366
367
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
368
369
        $indexes = $table->getIndexes();
370
        self::assertEquals(1, count($indexes));
371
        $index = current($indexes);
372
373
        self::assertTrue($table->hasIndex($index->getName()));
374
        self::assertEquals(array('id'), $index->getColumns());
375
    }
376
377
    /**
378
     * @group DBAL-1063
379
     */
380 View Code Duplication
    public function testAddForeignKeyDoesNotCreateDuplicateIndex()
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...
381
    {
382
        $table = new Table('foo');
383
        $table->addColumn('bar', 'integer');
384
        $table->addIndex(array('bar'), 'bar_idx');
385
386
        $foreignTable = new Table('bar');
387
        $foreignTable->addColumn('foo', 'integer');
388
389
        $table->addForeignKeyConstraint($foreignTable, array('bar'), array('foo'));
390
391
        self::assertCount(1, $table->getIndexes());
392
        self::assertTrue($table->hasIndex('bar_idx'));
393
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
394
    }
395
396
    /**
397
     * @group DBAL-1063
398
     */
399
    public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan()
400
    {
401
        $table = new Table('foo');
402
        $table->addColumn('bar', 'integer');
403
        $table->addColumn('baz', 'string');
404
        $table->addColumn('bloo', 'string');
405
        $table->addIndex(array('baz', 'bar'), 'composite_idx');
406
        $table->addIndex(array('bar', 'baz', 'bloo'), 'full_idx');
407
408
        $foreignTable = new Table('bar');
409
        $foreignTable->addColumn('foo', 'integer');
410
        $foreignTable->addColumn('baz', 'string');
411
412
        $table->addForeignKeyConstraint($foreignTable, array('bar', 'baz'), array('foo', 'baz'));
413
414
        self::assertCount(3, $table->getIndexes());
415
        self::assertTrue($table->hasIndex('composite_idx'));
416
        self::assertTrue($table->hasIndex('full_idx'));
417
        self::assertTrue($table->hasIndex('idx_8c73652176ff8caa78240498'));
418
        self::assertSame(array('baz', 'bar'), $table->getIndex('composite_idx')->getColumns());
419
        self::assertSame(array('bar', 'baz', 'bloo'), $table->getIndex('full_idx')->getColumns());
420
        self::assertSame(array('bar', 'baz'), $table->getIndex('idx_8c73652176ff8caa78240498')->getColumns());
421
    }
422
423
    /**
424
     * @group DBAL-50
425
     * @group DBAL-1063
426
     */
427
    public function testOverrulingIndexDoesNotDropOverruledIndex()
428
    {
429
        $table = new Table("bar");
430
        $table->addColumn('baz', 'integer', array());
431
        $table->addIndex(array('baz'));
432
433
        $indexes = $table->getIndexes();
434
        self::assertEquals(1, count($indexes));
435
        $index = current($indexes);
436
437
        $table->addUniqueIndex(array('baz'));
438
        self::assertEquals(2, count($table->getIndexes()));
439
        self::assertTrue($table->hasIndex($index->getName()));
440
    }
441
442
    /**
443
     * @group DBAL-1063
444
     */
445
    public function testAllowsAddingDuplicateIndexesBasedOnColumns()
446
    {
447
        $table = new Table('foo');
448
        $table->addColumn('bar', 'integer');
449
        $table->addIndex(array('bar'), 'bar_idx');
450
        $table->addIndex(array('bar'), 'duplicate_idx');
451
452
        self::assertCount(2, $table->getIndexes());
453
        self::assertTrue($table->hasIndex('bar_idx'));
454
        self::assertTrue($table->hasIndex('duplicate_idx'));
455
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
456
        self::assertSame(array('bar'), $table->getIndex('duplicate_idx')->getColumns());
457
    }
458
459
    /**
460
     * @group DBAL-1063
461
     */
462
    public function testAllowsAddingFulfillingIndexesBasedOnColumns()
463
    {
464
        $table = new Table('foo');
465
        $table->addColumn('bar', 'integer');
466
        $table->addColumn('baz', 'string');
467
        $table->addIndex(array('bar'), 'bar_idx');
468
        $table->addIndex(array('bar', 'baz'), 'fulfilling_idx');
469
470
        self::assertCount(2, $table->getIndexes());
471
        self::assertTrue($table->hasIndex('bar_idx'));
472
        self::assertTrue($table->hasIndex('fulfilling_idx'));
473
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
474
        self::assertSame(array('bar', 'baz'), $table->getIndex('fulfilling_idx')->getColumns());
475
    }
476
477
    /**
478
     * @group DBAL-50
479
     * @group DBAL-1063
480
     */
481
    public function testPrimaryKeyOverrulingUniqueIndexDoesNotDropUniqueIndex()
482
    {
483
        $table = new Table("bar");
484
        $table->addColumn('baz', 'integer', array());
485
        $table->addUniqueIndex(array('baz'), 'idx_unique');
486
487
        $table->setPrimaryKey(array('baz'));
488
489
        $indexes = $table->getIndexes();
490
        self::assertEquals(2, count($indexes), "Table should only contain both the primary key table index and the unique one, even though it was overruled.");
491
492
        self::assertTrue($table->hasPrimaryKey());
493
        self::assertTrue($table->hasIndex('idx_unique'));
494
    }
495
496 View Code Duplication
    public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex()
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...
497
    {
498
        $foreignTable = new Table('foreign');
499
        $foreignTable->addColumn('id', 'integer');
500
501
        $localTable = new Table('local');
502
        $localTable->addColumn('id', 'integer');
503
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));
504
505
        self::assertCount(1, $localTable->getIndexes());
506
507
        $localTable->addIndex(array('id'), 'explicit_idx');
508
509
        self::assertCount(1, $localTable->getIndexes());
510
        self::assertTrue($localTable->hasIndex('explicit_idx'));
511
    }
512
513 View Code Duplication
    public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex()
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...
514
    {
515
        $foreignTable = new Table('foreign');
516
        $foreignTable->addColumn('id', 'integer');
517
518
        $localTable = new Table('local');
519
        $localTable->addColumn('id', 'integer');
520
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));
521
522
        self::assertCount(1, $localTable->getIndexes());
523
524
        $localTable->addUniqueIndex(array('id'), 'explicit_idx');
525
526
        self::assertCount(1, $localTable->getIndexes());
527
        self::assertTrue($localTable->hasIndex('explicit_idx'));
528
    }
529
530 View Code Duplication
    public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex()
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...
531
    {
532
        $foreignTable = new Table('foreign');
533
        $foreignTable->addColumn('id', 'integer');
534
535
        $localTable = new Table('local');
536
        $localTable->addColumn('id', 'integer');
537
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));
538
539
        self::assertCount(1, $localTable->getIndexes());
540
541
        $localTable->setPrimaryKey(array('id'), 'explicit_idx');
542
543
        self::assertCount(1, $localTable->getIndexes());
544
        self::assertTrue($localTable->hasIndex('explicit_idx'));
545
    }
546
547
    public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException()
548
    {
549
        $foreignTable = new Table('foreign');
550
        $foreignTable->addColumn('id', 'integer');
551
552
        $localTable = new Table('local');
553
        $localTable->addColumn('id', 'integer');
554
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));
555
556
        self::assertCount(1, $localTable->getIndexes());
557
        self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
558
559
        $implicitIndex = $localTable->getIndex('IDX_8BD688E8BF396750');
560
561
        $localTable->addIndex(array('id'), 'IDX_8BD688E8BF396750');
562
563
        self::assertCount(1, $localTable->getIndexes());
564
        self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
565
        self::assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750'));
566
    }
567
568
    /**
569
     * @group DBAL-64
570
     */
571
    public function testQuotedTableName()
572
    {
573
        $table = new Table("`bar`");
574
575
        $mysqlPlatform  = new \Doctrine\DBAL\Platforms\MySqlPlatform();
576
        $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
577
578
        self::assertEquals('bar', $table->getName());
579
        self::assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
580
        self::assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
581
    }
582
583
    /**
584
     * @group DBAL-79
585
     */
586 View Code Duplication
    public function testTableHasPrimaryKey()
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...
587
    {
588
        $table = new Table("test");
589
590
        self::assertFalse($table->hasPrimaryKey());
591
592
        $table->addColumn("foo", "integer");
593
        $table->setPrimaryKey(array("foo"));
594
595
        self::assertTrue($table->hasPrimaryKey());
596
    }
597
598
    /**
599
     * @group DBAL-91
600
     */
601
    public function testAddIndexWithQuotedColumns()
602
    {
603
        $table = new Table("test");
604
        $table->addColumn('"foo"', 'integer');
605
        $table->addColumn('bar', 'integer');
606
        $table->addIndex(['"foo"', '"bar"']);
607
608
        self::assertTrue($table->columnsAreIndexed(['"foo"', '"bar"']));
609
    }
610
611
    /**
612
     * @group DBAL-91
613
     */
614 View Code Duplication
    public function testAddForeignKeyWithQuotedColumnsAndTable()
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...
615
    {
616
        $table = new Table("test");
617
        $table->addColumn('"foo"', 'integer');
618
        $table->addColumn('bar', 'integer');
619
        $table->addForeignKeyConstraint('"boing"', ['"foo"', '"bar"'], ["id"]);
620
621
        self::assertCount(1, $table->getForeignKeys());
622
    }
623
624
    /**
625
     * @group DBAL-177
626
     */
627
    public function testQuoteSchemaPrefixed()
628
    {
629
        $table = new Table("`test`.`test`");
630
        self::assertEquals("test.test", $table->getName());
631
        self::assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
632
    }
633
634
    /**
635
     * @group DBAL-204
636
     */
637
    public function testFullQualifiedTableName()
638
    {
639
        $table = new Table("`test`.`test`");
640
        self::assertEquals('test.test', $table->getFullQualifiedName("test"));
641
        self::assertEquals('test.test', $table->getFullQualifiedName("other"));
642
643
        $table = new Table("test");
644
        self::assertEquals('test.test', $table->getFullQualifiedName("test"));
645
        self::assertEquals('other.test', $table->getFullQualifiedName("other"));
646
    }
647
648
    /**
649
     * @group DBAL-224
650
     */
651
    public function testDropIndex()
652
    {
653
        $table = new Table("test");
654
        $table->addColumn('id', 'integer');
655
        $table->addIndex(array('id'), 'idx');
656
657
        self::assertTrue($table->hasIndex('idx'));
658
659
        $table->dropIndex('idx');
660
        self::assertFalse($table->hasIndex('idx'));
661
    }
662
663
    /**
664
     * @group DBAL-224
665
     */
666 View Code Duplication
    public function testDropPrimaryKey()
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...
667
    {
668
        $table = new Table("test");
669
        $table->addColumn('id', 'integer');
670
        $table->setPrimaryKey(array('id'));
671
672
        self::assertTrue($table->hasPrimaryKey());
673
674
        $table->dropPrimaryKey();
675
        self::assertFalse($table->hasPrimaryKey());
676
    }
677
678
    /**
679
     * @group DBAL-234
680
     */
681
    public function testRenameIndex()
682
    {
683
        $table = new Table("test");
684
        $table->addColumn('id', 'integer');
685
        $table->addColumn('foo', 'integer');
686
        $table->addColumn('bar', 'integer');
687
        $table->addColumn('baz', 'integer');
688
        $table->setPrimaryKey(array('id'), 'pk');
689
        $table->addIndex(array('foo'), 'idx', array('flag'));
690
        $table->addUniqueIndex(array('bar', 'baz'), 'uniq');
691
692
        // Rename to custom name.
693
        self::assertSame($table, $table->renameIndex('pk', 'pk_new'));
694
        self::assertSame($table, $table->renameIndex('idx', 'idx_new'));
695
        self::assertSame($table, $table->renameIndex('uniq', 'uniq_new'));
696
697
        self::assertTrue($table->hasPrimaryKey());
698
        self::assertTrue($table->hasIndex('pk_new'));
699
        self::assertTrue($table->hasIndex('idx_new'));
700
        self::assertTrue($table->hasIndex('uniq_new'));
701
702
        self::assertFalse($table->hasIndex('pk'));
703
        self::assertFalse($table->hasIndex('idx'));
704
        self::assertFalse($table->hasIndex('uniq'));
705
706
        self::assertEquals(new Index('pk_new', array('id'), true, true), $table->getPrimaryKey());
707
        self::assertEquals(new Index('pk_new', array('id'), true, true), $table->getIndex('pk_new'));
708
        self::assertEquals(
709
            new Index('idx_new', array('foo'), false, false, array('flag')),
710
            $table->getIndex('idx_new')
711
        );
712
        self::assertEquals(new Index('uniq_new', array('bar', 'baz'), true), $table->getIndex('uniq_new'));
713
714
        // Rename to auto-generated name.
715
        self::assertSame($table, $table->renameIndex('pk_new', null));
716
        self::assertSame($table, $table->renameIndex('idx_new', null));
717
        self::assertSame($table, $table->renameIndex('uniq_new', null));
718
719
        self::assertTrue($table->hasPrimaryKey());
720
        self::assertTrue($table->hasIndex('primary'));
721
        self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
722
        self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
723
724
        self::assertFalse($table->hasIndex('pk_new'));
725
        self::assertFalse($table->hasIndex('idx_new'));
726
        self::assertFalse($table->hasIndex('uniq_new'));
727
728
        self::assertEquals(new Index('primary', array('id'), true, true), $table->getPrimaryKey());
729
        self::assertEquals(new Index('primary', array('id'), true, true), $table->getIndex('primary'));
730
        self::assertEquals(
731
            new Index('IDX_D87F7E0C8C736521', array('foo'), false, false, array('flag')),
732
            $table->getIndex('IDX_D87F7E0C8C736521')
733
        );
734
        self::assertEquals(
735
            new Index('UNIQ_D87F7E0C76FF8CAA78240498', array('bar', 'baz'), true),
736
            $table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498')
737
        );
738
739
        // Rename to same name (changed case).
740
        self::assertSame($table, $table->renameIndex('primary', 'PRIMARY'));
741
        self::assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521'));
742
        self::assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498'));
743
744
        self::assertTrue($table->hasPrimaryKey());
745
        self::assertTrue($table->hasIndex('primary'));
746
        self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
747
        self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
748
    }
749
750
    /**
751
     * @group DBAL-2508
752
     */
753 View Code Duplication
    public function testKeepsIndexOptionsOnRenamingRegularIndex()
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...
754
    {
755
        $table = new Table('foo');
756
        $table->addColumn('id', 'integer');
757
        $table->addIndex(array('id'), 'idx_bar', array(), array('where' => '1 = 1'));
758
759
        $table->renameIndex('idx_bar', 'idx_baz');
760
761
        self::assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
762
    }
763
764
    /**
765
     * @group DBAL-2508
766
     */
767 View Code Duplication
    public function testKeepsIndexOptionsOnRenamingUniqueIndex()
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...
768
    {
769
        $table = new Table('foo');
770
        $table->addColumn('id', 'integer');
771
        $table->addUniqueIndex(array('id'), 'idx_bar', array('where' => '1 = 1'));
772
773
        $table->renameIndex('idx_bar', 'idx_baz');
774
775
        self::assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
776
    }
777
778
    /**
779
     * @group DBAL-234
780
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
781
     */
782 View Code Duplication
    public function testThrowsExceptionOnRenamingNonExistingIndex()
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...
783
    {
784
        $table = new Table("test");
785
        $table->addColumn('id', 'integer');
786
        $table->addIndex(array('id'), 'idx');
787
788
        $table->renameIndex('foo', 'bar');
789
    }
790
791
    /**
792
     * @group DBAL-234
793
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
794
     */
795
    public function testThrowsExceptionOnRenamingToAlreadyExistingIndex()
796
    {
797
        $table = new Table("test");
798
        $table->addColumn('id', 'integer');
799
        $table->addColumn('foo', 'integer');
800
        $table->addIndex(array('id'), 'idx_id');
801
        $table->addIndex(array('foo'), 'idx_foo');
802
803
        $table->renameIndex('idx_id', 'idx_foo');
804
    }
805
806
    /**
807
     * @dataProvider getNormalizesAssetNames
808
     * @group DBAL-831
809
     */
810
    public function testNormalizesColumnNames($assetName)
811
    {
812
        $table = new Table('test');
813
814
        $table->addColumn($assetName, 'integer');
815
        $table->addIndex(array($assetName), $assetName);
816
        $table->addForeignKeyConstraint('test', array($assetName), array($assetName), array(), $assetName);
817
818
        self::assertTrue($table->hasColumn($assetName));
819
        self::assertTrue($table->hasColumn('foo'));
820
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn($assetName));
821
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn('foo'));
822
823
        self::assertTrue($table->hasIndex($assetName));
824
        self::assertTrue($table->hasIndex('foo'));
825
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex($assetName));
826
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('foo'));
827
828
        self::assertTrue($table->hasForeignKey($assetName));
829
        self::assertTrue($table->hasForeignKey('foo'));
830
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey($assetName));
831
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey('foo'));
832
833
        $table->renameIndex($assetName, $assetName);
834
        self::assertTrue($table->hasIndex($assetName));
835
        self::assertTrue($table->hasIndex('foo'));
836
837
        $table->renameIndex($assetName, 'foo');
838
        self::assertTrue($table->hasIndex($assetName));
839
        self::assertTrue($table->hasIndex('foo'));
840
841
        $table->renameIndex('foo', $assetName);
842
        self::assertTrue($table->hasIndex($assetName));
843
        self::assertTrue($table->hasIndex('foo'));
844
845
        $table->renameIndex($assetName, 'bar');
846
        self::assertFalse($table->hasIndex($assetName));
847
        self::assertFalse($table->hasIndex('foo'));
848
        self::assertTrue($table->hasIndex('bar'));
849
850
        $table->renameIndex('bar', $assetName);
851
852
        $table->dropColumn($assetName);
853
        $table->dropIndex($assetName);
854
        $table->removeForeignKey($assetName);
855
856
        self::assertFalse($table->hasColumn($assetName));
857
        self::assertFalse($table->hasColumn('foo'));
858
        self::assertFalse($table->hasIndex($assetName));
859
        self::assertFalse($table->hasIndex('foo'));
860
        self::assertFalse($table->hasForeignKey($assetName));
861
        self::assertFalse($table->hasForeignKey('foo'));
862
    }
863
864
    public function getNormalizesAssetNames()
865
    {
866
        return array(
867
            array('foo'),
868
            array('FOO'),
869
            array('`foo`'),
870
            array('`FOO`'),
871
            array('"foo"'),
872
            array('"FOO"'),
873
            array('"foo"'),
874
            array('"FOO"'),
875
        );
876
    }
877
}
878