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

testAlterTableColumnComments()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Events;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Schema\Column;
9
use Doctrine\DBAL\Schema\ColumnDiff;
10
use Doctrine\DBAL\Schema\Comparator;
11
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
12
use Doctrine\DBAL\Schema\Index;
13
use Doctrine\DBAL\Schema\Table;
14
use Doctrine\DBAL\Schema\TableDiff;
15
use Doctrine\DBAL\Types\Type;
16
use Doctrine\Tests\Types\CommentedType;
17
18
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
19
{
20
    /**
21
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
22
     */
23
    protected $_platform;
24
25
    abstract public function createPlatform();
26
27
    protected function setUp()
28
    {
29
        $this->_platform = $this->createPlatform();
30
    }
31
32
    /**
33
     * @group DDC-1360
34
     */
35
    public function testQuoteIdentifier()
36
    {
37
        if ($this->_platform->getName() == "mssql") {
38
            $this->markTestSkipped('Not working this way on mssql.');
39
        }
40
41
        $c = $this->_platform->getIdentifierQuoteCharacter();
42
        self::assertEquals($c . "test" . $c, $this->_platform->quoteIdentifier("test"));
43
        self::assertEquals($c . "test" . $c . "." . $c . "test" . $c, $this->_platform->quoteIdentifier("test.test"));
44
        self::assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
45
    }
46
47
    /**
48
     * @group DDC-1360
49
     */
50
    public function testQuoteSingleIdentifier()
51
    {
52
        if ($this->_platform->getName() == "mssql") {
53
            $this->markTestSkipped('Not working this way on mssql.');
54
        }
55
56
        $c = $this->_platform->getIdentifierQuoteCharacter();
57
        self::assertEquals($c . "test" . $c, $this->_platform->quoteSingleIdentifier("test"));
58
        self::assertEquals($c . "test.test" . $c, $this->_platform->quoteSingleIdentifier("test.test"));
59
        self::assertEquals(str_repeat($c, 4), $this->_platform->quoteSingleIdentifier($c));
60
    }
61
62
    /**
63
     * @group DBAL-1029
64
     *
65
     * @dataProvider getReturnsForeignKeyReferentialActionSQL
66
     */
67
    public function testReturnsForeignKeyReferentialActionSQL($action, $expectedSQL)
68
    {
69
        self::assertSame($expectedSQL, $this->_platform->getForeignKeyReferentialActionSQL($action));
70
    }
71
72
    /**
73
     * @return array
74
     */
75 View Code Duplication
    public function getReturnsForeignKeyReferentialActionSQL()
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...
76
    {
77
        return array(
78
            array('CASCADE', 'CASCADE'),
79
            array('SET NULL', 'SET NULL'),
80
            array('NO ACTION', 'NO ACTION'),
81
            array('RESTRICT', 'RESTRICT'),
82
            array('SET DEFAULT', 'SET DEFAULT'),
83
            array('CaScAdE', 'CASCADE'),
84
        );
85
    }
86
87
    public function testGetInvalidForeignKeyReferentialActionSQL()
88
    {
89
        $this->expectException('InvalidArgumentException');
90
        $this->_platform->getForeignKeyReferentialActionSQL('unknown');
91
    }
92
93
    public function testGetUnknownDoctrineMappingType()
94
    {
95
        $this->expectException('Doctrine\DBAL\DBALException');
96
        $this->_platform->getDoctrineTypeMapping('foobar');
97
    }
98
99
    public function testRegisterDoctrineMappingType()
100
    {
101
        $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
102
        self::assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
103
    }
104
105
    public function testRegisterUnknownDoctrineMappingType()
106
    {
107
        $this->expectException('Doctrine\DBAL\DBALException');
108
        $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
109
    }
110
111
    /**
112
     * @group DBAL-2594
113
     */
114
    public function testRegistersCommentedDoctrineMappingTypeImplicitly()
115
    {
116
        if ( ! Type::hasType('my_commented')) {
117
            Type::addType('my_commented', CommentedType::class);
118
        }
119
120
        $type = Type::getType('my_commented');
121
        $this->_platform->registerDoctrineTypeMapping('foo', 'my_commented');
122
123
        self::assertTrue($this->_platform->isCommentedDoctrineType($type));
124
    }
125
126
    /**
127
     * @group DBAL-939
128
     *
129
     * @dataProvider getIsCommentedDoctrineType
130
     */
131
    public function testIsCommentedDoctrineType(Type $type, $commented)
132
    {
133
        self::assertSame($commented, $this->_platform->isCommentedDoctrineType($type));
134
    }
135
136
    public function getIsCommentedDoctrineType()
137
    {
138
        $this->setUp();
139
140
        $data = array();
141
142
        foreach (Type::getTypesMap() as $typeName => $className) {
143
            $type = Type::getType($typeName);
144
145
            $data[$typeName] = array(
146
                $type,
147
                $type->requiresSQLCommentHint($this->_platform),
148
            );
149
        }
150
151
        return $data;
152
    }
153
154
    public function testCreateWithNoColumns()
155
    {
156
        $table = new Table('test');
157
158
        $this->expectException('Doctrine\DBAL\DBALException');
159
        $sql = $this->_platform->getCreateTableSQL($table);
0 ignored issues
show
Unused Code introduced by
$sql 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...
160
    }
161
162 View Code Duplication
    public function testGeneratesTableCreationSql()
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...
163
    {
164
        $table = new Table('test');
165
        $table->addColumn('id', 'integer', array('notnull' => true, 'autoincrement' => true));
166
        $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
167
        $table->setPrimaryKey(array('id'));
168
169
        $sql = $this->_platform->getCreateTableSQL($table);
170
        self::assertEquals($this->getGenerateTableSql(), $sql[0]);
171
    }
172
173
    abstract public function getGenerateTableSql();
174
175 View Code Duplication
    public function testGenerateTableWithMultiColumnUniqueIndex()
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...
176
    {
177
        $table = new Table('test');
178
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
179
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
180
        $table->addUniqueIndex(array("foo", "bar"));
181
182
        $sql = $this->_platform->getCreateTableSQL($table);
183
        self::assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
184
    }
185
186
    abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
187
188 View Code Duplication
    public function testGeneratesIndexCreationSql()
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...
189
    {
190
        $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
191
192
        self::assertEquals(
193
            $this->getGenerateIndexSql(),
194
            $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
195
        );
196
    }
197
198
    abstract public function getGenerateIndexSql();
199
200 View Code Duplication
    public function testGeneratesUniqueIndexCreationSql()
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...
201
    {
202
        $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
203
204
        $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
205
        self::assertEquals($this->getGenerateUniqueIndexSql(), $sql);
206
    }
207
208
    abstract public function getGenerateUniqueIndexSql();
209
210
    public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
211
    {
212
        $where       = 'test IS NULL AND test2 IS NOT NULL';
213
        $indexDef    = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), array('where' => $where));
214
        $uniqueIndex = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), true, false, array(), array('where' => $where));
215
216
        $expected = ' WHERE ' . $where;
217
218
        $actuals = array();
219
220
        if ($this->supportsInlineIndexDeclaration()) {
221
            $actuals [] = $this->_platform->getIndexDeclarationSQL('name', $indexDef);
222
        }
223
224
        $actuals [] = $this->_platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
225
        $actuals [] = $this->_platform->getCreateIndexSQL($indexDef, 'table');
226
227
        foreach ($actuals as $actual) {
228
            if ($this->_platform->supportsPartialIndexes()) {
229
                self::assertStringEndsWith($expected, $actual, 'WHERE clause should be present');
230
            } else {
231
                self::assertStringEndsNotWith($expected, $actual, 'WHERE clause should NOT be present');
232
            }
233
        }
234
    }
235
236
    public function testGeneratesForeignKeyCreationSql()
237
    {
238
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
239
240
        $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
241
        self::assertEquals($sql, $this->getGenerateForeignKeySql());
242
    }
243
244
    abstract public function getGenerateForeignKeySql();
245
246
    public function testGeneratesConstraintCreationSql()
247
    {
248
        $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
249
        $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
250
        self::assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
251
252
        $pk  = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
253
        $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
254
        self::assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);
255
256
        $fk  = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
257
        $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
258
        self::assertEquals($this->getGenerateConstraintForeignKeySql($fk), $sql);
259
    }
260
261
    public function testGeneratesForeignKeySqlOnlyWhenSupportingForeignKeys()
262
    {
263
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
264
265
        if ($this->_platform->supportsForeignKeyConstraints()) {
266
            self::assertInternalType(
267
                'string',
268
                $this->_platform->getCreateForeignKeySQL($fk, 'test')
269
            );
270
        } else {
271
            $this->expectException('Doctrine\DBAL\DBALException');
272
            $this->_platform->getCreateForeignKeySQL($fk, 'test');
273
        }
274
    }
275
276
    protected function getBitAndComparisonExpressionSql($value1, $value2)
277
    {
278
        return '(' . $value1 . ' & ' . $value2 . ')';
279
    }
280
281
    /**
282
     * @group DDC-1213
283
     */
284
    public function testGeneratesBitAndComparisonExpressionSql()
285
    {
286
        $sql = $this->_platform->getBitAndComparisonExpression(2, 4);
287
        self::assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
288
    }
289
290
    protected function getBitOrComparisonExpressionSql($value1, $value2)
291
    {
292
        return '(' . $value1 . ' | ' . $value2 . ')';
293
    }
294
295
    /**
296
     * @group DDC-1213
297
     */
298
    public function testGeneratesBitOrComparisonExpressionSql()
299
    {
300
        $sql = $this->_platform->getBitOrComparisonExpression(2, 4);
301
        self::assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
302
    }
303
304
    public function getGenerateConstraintUniqueIndexSql()
305
    {
306
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
307
    }
308
309
    public function getGenerateConstraintPrimaryIndexSql()
310
    {
311
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
312
    }
313
314
    public function getGenerateConstraintForeignKeySql(ForeignKeyConstraint $fk)
315
    {
316
        $quotedForeignTable = $fk->getQuotedForeignTableName($this->_platform);
317
318
        return "ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES $quotedForeignTable (id)";
319
    }
320
321
    abstract public function getGenerateAlterTableSql();
322
323
    public function testGeneratesTableAlterationSql()
324
    {
325
        $expectedSql = $this->getGenerateAlterTableSql();
326
327
        $table = new Table('mytable');
328
        $table->addColumn('id', 'integer', array('autoincrement' => true));
329
        $table->addColumn('foo', 'integer');
330
        $table->addColumn('bar', 'string');
331
        $table->addColumn('bloo', 'boolean');
332
        $table->setPrimaryKey(array('id'));
333
334
        $tableDiff                         = new TableDiff('mytable');
335
        $tableDiff->fromTable              = $table;
336
        $tableDiff->newName                = 'userlist';
337
        $tableDiff->addedColumns['quota']  = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
338
        $tableDiff->removedColumns['foo']  = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
339
        $tableDiff->changedColumns['bar']  = new \Doctrine\DBAL\Schema\ColumnDiff(
340
            'bar',
341
            new \Doctrine\DBAL\Schema\Column(
342
                'baz',
343
                \Doctrine\DBAL\Types\Type::getType('string'),
344
                array('default' => 'def')
345
            ),
346
            array('type', 'notnull', 'default')
347
        );
348
        $tableDiff->changedColumns['bloo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
349
            'bloo',
350
            new \Doctrine\DBAL\Schema\Column(
351
                'bloo',
352
                \Doctrine\DBAL\Types\Type::getType('boolean'),
353
                array('default' => false)
354
            ),
355
            array('type', 'notnull', 'default')
356
        );
357
358
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
359
360
        self::assertEquals($expectedSql, $sql);
361
    }
362
363
    public function testGetCustomColumnDeclarationSql()
364
    {
365
        $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
366
        self::assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
367
    }
368
369
    public function testGetCreateTableSqlDispatchEvent()
370
    {
371
        $listenerMock = $this->getMockBuilder('GetCreateTableSqlDispatchEvenListener')
372
            ->setMethods(array('onSchemaCreateTable', 'onSchemaCreateTableColumn'))
373
            ->getMock();
374
        $listenerMock
375
            ->expects($this->once())
376
            ->method('onSchemaCreateTable');
377
        $listenerMock
378
            ->expects($this->exactly(2))
379
            ->method('onSchemaCreateTableColumn');
380
381
        $eventManager = new EventManager();
382
        $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
383
384
        $this->_platform->setEventManager($eventManager);
385
386
        $table = new Table('test');
387
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
388
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
389
390
        $this->_platform->getCreateTableSQL($table);
391
    }
392
393
    public function testGetDropTableSqlDispatchEvent()
394
    {
395
        $listenerMock = $this->getMockBuilder('GetDropTableSqlDispatchEventListener')
396
            ->setMethods(array('onSchemaDropTable'))
397
            ->getMock();
398
        $listenerMock
399
            ->expects($this->once())
400
            ->method('onSchemaDropTable');
401
402
        $eventManager = new EventManager();
403
        $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);
404
405
        $this->_platform->setEventManager($eventManager);
406
407
        $this->_platform->getDropTableSQL('TABLE');
408
    }
409
410
    public function testGetAlterTableSqlDispatchEvent()
411
    {
412
        $events = array(
413
            'onSchemaAlterTable',
414
            'onSchemaAlterTableAddColumn',
415
            'onSchemaAlterTableRemoveColumn',
416
            'onSchemaAlterTableChangeColumn',
417
            'onSchemaAlterTableRenameColumn'
418
        );
419
420
        $listenerMock = $this->getMockBuilder('GetAlterTableSqlDispatchEvenListener')
421
            ->setMethods($events)
422
            ->getMock();
423
        $listenerMock
424
            ->expects($this->once())
425
            ->method('onSchemaAlterTable');
426
        $listenerMock
427
            ->expects($this->once())
428
            ->method('onSchemaAlterTableAddColumn');
429
        $listenerMock
430
            ->expects($this->once())
431
            ->method('onSchemaAlterTableRemoveColumn');
432
        $listenerMock
433
            ->expects($this->once())
434
            ->method('onSchemaAlterTableChangeColumn');
435
        $listenerMock
436
            ->expects($this->once())
437
            ->method('onSchemaAlterTableRenameColumn');
438
439
        $eventManager = new EventManager();
440
        $events       = array(
441
            Events::onSchemaAlterTable,
442
            Events::onSchemaAlterTableAddColumn,
443
            Events::onSchemaAlterTableRemoveColumn,
444
            Events::onSchemaAlterTableChangeColumn,
445
            Events::onSchemaAlterTableRenameColumn
446
        );
447
        $eventManager->addEventListener($events, $listenerMock);
448
449
        $this->_platform->setEventManager($eventManager);
450
451
        $table = new Table('mytable');
452
        $table->addColumn('removed', 'integer');
453
        $table->addColumn('changed', 'integer');
454
        $table->addColumn('renamed', 'integer');
455
456
        $tableDiff                            = new TableDiff('mytable');
457
        $tableDiff->fromTable                 = $table;
458
        $tableDiff->addedColumns['added']     = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
459
        $tableDiff->removedColumns['removed'] = new \Doctrine\DBAL\Schema\Column('removed', \Doctrine\DBAL\Types\Type::getType('integer'), array());
460
        $tableDiff->changedColumns['changed'] = new \Doctrine\DBAL\Schema\ColumnDiff(
461
            'changed',
462
            new \Doctrine\DBAL\Schema\Column(
463
                'changed2',
464
                \Doctrine\DBAL\Types\Type::getType('string'),
465
                array()
466
            ),
467
            array()
468
        );
469
        $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());
470
471
        $this->_platform->getAlterTableSQL($tableDiff);
472
    }
473
474
    /**
475
     * @group DBAL-42
476
     */
477
    public function testCreateTableColumnComments()
478
    {
479
        $table = new Table('test');
480
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
481
        $table->setPrimaryKey(array('id'));
482
483
        self::assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
484
    }
485
486
    /**
487
     * @group DBAL-42
488
     */
489
    public function testAlterTableColumnComments()
490
    {
491
        $tableDiff                        = new TableDiff('mytable');
492
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
493
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
494
            'foo',
495
            new \Doctrine\DBAL\Schema\Column(
496
                'foo',
497
                \Doctrine\DBAL\Types\Type::getType('string')
498
            ),
499
            array('comment')
500
        );
501
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
502
            'bar',
503
            new \Doctrine\DBAL\Schema\Column(
504
                'baz',
505
                \Doctrine\DBAL\Types\Type::getType('string'),
506
                array('comment' => 'B comment')
507
            ),
508
            array('comment')
509
        );
510
511
        self::assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
512
    }
513
514 View Code Duplication
    public function testCreateTableColumnTypeComments()
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...
515
    {
516
        $table = new Table('test');
517
        $table->addColumn('id', 'integer');
518
        $table->addColumn('data', 'array');
519
        $table->setPrimaryKey(array('id'));
520
521
        self::assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
522
    }
523
524
    public function getCreateTableColumnCommentsSQL()
525
    {
526
        $this->markTestSkipped('Platform does not support Column comments.');
527
    }
528
529
    public function getAlterTableColumnCommentsSQL()
530
    {
531
        $this->markTestSkipped('Platform does not support Column comments.');
532
    }
533
534
    public function getCreateTableColumnTypeCommentsSQL()
535
    {
536
        $this->markTestSkipped('Platform does not support Column comments.');
537
    }
538
539
    public function testGetDefaultValueDeclarationSQL()
540
    {
541
        // non-timestamp value will get single quotes
542
        $field = array(
543
            'type' => 'string',
544
            'default' => 'non_timestamp'
545
        );
546
547
        self::assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field));
548
    }
549
550
    public function testGetDefaultValueDeclarationSQLDateTime()
551
    {
552
        // timestamps on datetime types should not be quoted
553
        foreach (array('datetime', 'datetimetz') as $type) {
554
            $field = array(
555
                'type' => Type::getType($type),
556
                'default' => $this->_platform->getCurrentTimestampSQL()
557
            );
558
559
            self::assertEquals(' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(), $this->_platform->getDefaultValueDeclarationSQL($field));
560
        }
561
    }
562
563
    public function testGetDefaultValueDeclarationSQLForIntegerTypes()
564
    {
565
        foreach (array('bigint', 'integer', 'smallint') as $type) {
566
            $field = array(
567
                'type'    => Type::getType($type),
568
                'default' => 1
569
            );
570
571
            self::assertEquals(
572
                ' DEFAULT 1',
573
                $this->_platform->getDefaultValueDeclarationSQL($field)
574
            );
575
        }
576
    }
577
578
    /**
579
     * @group DBAL-45
580
     */
581
    public function testKeywordList()
582
    {
583
        $keywordList = $this->_platform->getReservedKeywordsList();
584
        self::assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
585
586
        self::assertTrue($keywordList->isKeyword('table'));
587
    }
588
589
    /**
590
     * @group DBAL-374
591
     */
592 View Code Duplication
    public function testQuotedColumnInPrimaryKeyPropagation()
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...
593
    {
594
        $table = new Table('`quoted`');
595
        $table->addColumn('create', 'string');
596
        $table->setPrimaryKey(array('create'));
597
598
        $sql = $this->_platform->getCreateTableSQL($table);
599
        self::assertEquals($this->getQuotedColumnInPrimaryKeySQL(), $sql);
600
    }
601
602
    abstract protected function getQuotedColumnInPrimaryKeySQL();
603
    abstract protected function getQuotedColumnInIndexSQL();
604
    abstract protected function getQuotedNameInIndexSQL();
605
    abstract protected function getQuotedColumnInForeignKeySQL();
606
607
    /**
608
     * @group DBAL-374
609
     */
610 View Code Duplication
    public function testQuotedColumnInIndexPropagation()
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...
611
    {
612
        $table = new Table('`quoted`');
613
        $table->addColumn('create', 'string');
614
        $table->addIndex(array('create'));
615
616
        $sql = $this->_platform->getCreateTableSQL($table);
617
        self::assertEquals($this->getQuotedColumnInIndexSQL(), $sql);
618
    }
619
620 View Code Duplication
    public function testQuotedNameInIndexSQL()
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...
621
    {
622
        $table = new Table('test');
623
        $table->addColumn('column1', 'string');
624
        $table->addIndex(array('column1'), '`key`');
625
626
        $sql = $this->_platform->getCreateTableSQL($table);
627
        self::assertEquals($this->getQuotedNameInIndexSQL(), $sql);
628
    }
629
630
    /**
631
     * @group DBAL-374
632
     */
633
    public function testQuotedColumnInForeignKeyPropagation()
634
    {
635
        $table = new Table('`quoted`');
636
        $table->addColumn('create', 'string');
637
        $table->addColumn('foo', 'string');
638
        $table->addColumn('`bar`', 'string');
639
640
        // Foreign table with reserved keyword as name (needs quotation).
641
        $foreignTable = new Table('foreign');
642
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
643
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
644
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
645
646
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');
647
648
        // Foreign table with non-reserved keyword as name (does not need quotation).
649
        $foreignTable = new Table('foo');
650
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
651
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
652
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
653
654
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');
655
656
        // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
657
        $foreignTable = new Table('`foo-bar`');
658
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
659
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
660
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
661
662
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
663
664
        $sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
665
        self::assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
666
    }
667
668
    /**
669
     * @group DBAL-1051
670
     */
671
    public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
672
    {
673
        $index = new Index('select', array('foo'), true);
674
675
        self::assertSame(
676
            $this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
677
            $this->_platform->getUniqueConstraintDeclarationSQL('select', $index)
678
        );
679
    }
680
681
    /**
682
     * @return string
683
     */
684
    abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL();
685
686
    /**
687
     * @group DBAL-2270
688
     */
689
    public function testQuotesReservedKeywordInTruncateTableSQL()
690
    {
691
        self::assertSame(
692
            $this->getQuotesReservedKeywordInTruncateTableSQL(),
693
            $this->_platform->getTruncateTableSQL('select')
694
        );
695
    }
696
697
    /**
698
     * @return string
699
     */
700
    abstract protected function getQuotesReservedKeywordInTruncateTableSQL();
701
702
    /**
703
     * @group DBAL-1051
704
     */
705
    public function testQuotesReservedKeywordInIndexDeclarationSQL()
706
    {
707
        $index = new Index('select', array('foo'));
708
709
        if ( ! $this->supportsInlineIndexDeclaration()) {
710
            $this->expectException('Doctrine\DBAL\DBALException');
711
        }
712
713
        self::assertSame(
714
            $this->getQuotesReservedKeywordInIndexDeclarationSQL(),
715
            $this->_platform->getIndexDeclarationSQL('select', $index)
716
        );
717
    }
718
719
    /**
720
     * @return string
721
     */
722
    abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL();
723
724
    /**
725
     * @return boolean
726
     */
727
    protected function supportsInlineIndexDeclaration()
728
    {
729
        return true;
730
    }
731
732
    public function testSupportsCommentOnStatement()
733
    {
734
        self::assertSame($this->supportsCommentOnStatement(), $this->_platform->supportsCommentOnStatement());
735
    }
736
737
    /**
738
     * @return bool
739
     */
740
    protected function supportsCommentOnStatement()
741
    {
742
        return false;
743
    }
744
745
    /**
746
     * @expectedException \Doctrine\DBAL\DBALException
747
     */
748
    public function testGetCreateSchemaSQL()
749
    {
750
        $this->_platform->getCreateSchemaSQL('schema');
751
    }
752
753
    /**
754
     * @group DBAL-585
755
     */
756
    public function testAlterTableChangeQuotedColumn()
757
    {
758
        $tableDiff                        = new \Doctrine\DBAL\Schema\TableDiff('mytable');
759
        $tableDiff->fromTable             = new \Doctrine\DBAL\Schema\Table('mytable');
760
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
761
            'select',
762
            new \Doctrine\DBAL\Schema\Column(
763
                'select',
764
                \Doctrine\DBAL\Types\Type::getType('string')
765
            ),
766
            array('type')
767
        );
768
769
        self::assertContains(
770
            $this->_platform->quoteIdentifier('select'),
771
            implode(';', $this->_platform->getAlterTableSQL($tableDiff))
772
        );
773
    }
774
775
    /**
776
     * @group DBAL-563
777
     */
778
    public function testUsesSequenceEmulatedIdentityColumns()
779
    {
780
        self::assertFalse($this->_platform->usesSequenceEmulatedIdentityColumns());
781
    }
782
783
    /**
784
     * @group DBAL-563
785
     * @expectedException \Doctrine\DBAL\DBALException
786
     */
787
    public function testReturnsIdentitySequenceName()
788
    {
789
        $this->_platform->getIdentitySequenceName('mytable', 'mycolumn');
790
    }
791
792
    public function testReturnsBinaryDefaultLength()
793
    {
794
        self::assertSame($this->getBinaryDefaultLength(), $this->_platform->getBinaryDefaultLength());
795
    }
796
797
    protected function getBinaryDefaultLength()
798
    {
799
        return 255;
800
    }
801
802
    public function testReturnsBinaryMaxLength()
803
    {
804
        self::assertSame($this->getBinaryMaxLength(), $this->_platform->getBinaryMaxLength());
805
    }
806
807
    protected function getBinaryMaxLength()
808
    {
809
        return 4000;
810
    }
811
812
    /**
813
     * @expectedException \Doctrine\DBAL\DBALException
814
     */
815
    public function testReturnsBinaryTypeDeclarationSQL()
816
    {
817
        $this->_platform->getBinaryTypeDeclarationSQL(array());
818
    }
819
820
    /**
821
     * @group DBAL-553
822
     */
823
    public function hasNativeJsonType()
824
    {
825
        self::assertFalse($this->_platform->hasNativeJsonType());
826
    }
827
828
    /**
829
     * @group DBAL-553
830
     */
831
    public function testReturnsJsonTypeDeclarationSQL()
832
    {
833
        $column = array(
834
            'length'  => 666,
835
            'notnull' => true,
836
            'type'    => Type::getType('json_array'),
837
        );
838
839
        self::assertSame(
840
            $this->_platform->getClobTypeDeclarationSQL($column),
841
            $this->_platform->getJsonTypeDeclarationSQL($column)
842
        );
843
    }
844
845
    /**
846
     * @group DBAL-234
847
     */
848 View Code Duplication
    public function testAlterTableRenameIndex()
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...
849
    {
850
        $tableDiff            = new TableDiff('mytable');
851
        $tableDiff->fromTable = new Table('mytable');
852
        $tableDiff->fromTable->addColumn('id', 'integer');
853
        $tableDiff->fromTable->setPrimaryKey(array('id'));
854
        $tableDiff->renamedIndexes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('idx_foo' => new \...idx_bar', array('id'))) of type array<string,object<Doct...DBAL\\Schema\\Index>"}> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $renamedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
855
            'idx_foo' => new Index('idx_bar', array('id'))
856
        );
857
858
        self::assertSame(
859
            $this->getAlterTableRenameIndexSQL(),
860
            $this->_platform->getAlterTableSQL($tableDiff)
861
        );
862
    }
863
864
    /**
865
     * @group DBAL-234
866
     */
867
    protected function getAlterTableRenameIndexSQL()
868
    {
869
        return array(
870
            'DROP INDEX idx_foo',
871
            'CREATE INDEX idx_bar ON mytable (id)',
872
        );
873
    }
874
875
    /**
876
     * @group DBAL-234
877
     */
878 View Code Duplication
    public function testQuotesAlterTableRenameIndex()
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...
879
    {
880
        $tableDiff            = new TableDiff('table');
881
        $tableDiff->fromTable = new Table('table');
882
        $tableDiff->fromTable->addColumn('id', 'integer');
883
        $tableDiff->fromTable->setPrimaryKey(array('id'));
884
        $tableDiff->renamedIndexes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('create' => new \D...('`bar`', array('id'))) of type array<string,object<Doct...DBAL\\Schema\\Index>"}> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $renamedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
885
            'create' => new Index('select', array('id')),
886
            '`foo`'  => new Index('`bar`', array('id')),
887
        );
888
889
        self::assertSame(
890
            $this->getQuotedAlterTableRenameIndexSQL(),
891
            $this->_platform->getAlterTableSQL($tableDiff)
892
        );
893
    }
894
895
    /**
896
     * @group DBAL-234
897
     */
898
    protected function getQuotedAlterTableRenameIndexSQL()
899
    {
900
        return array(
901
            'DROP INDEX "create"',
902
            'CREATE INDEX "select" ON "table" (id)',
903
            'DROP INDEX "foo"',
904
            'CREATE INDEX "bar" ON "table" (id)',
905
        );
906
    }
907
908
    /**
909
     * @group DBAL-835
910
     */
911
    public function testQuotesAlterTableRenameColumn()
912
    {
913
        $fromTable = new Table('mytable');
914
915
        $fromTable->addColumn('unquoted1', 'integer', array('comment' => 'Unquoted 1'));
916
        $fromTable->addColumn('unquoted2', 'integer', array('comment' => 'Unquoted 2'));
917
        $fromTable->addColumn('unquoted3', 'integer', array('comment' => 'Unquoted 3'));
918
919
        $fromTable->addColumn('create', 'integer', array('comment' => 'Reserved keyword 1'));
920
        $fromTable->addColumn('table', 'integer', array('comment' => 'Reserved keyword 2'));
921
        $fromTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword 3'));
922
923
        $fromTable->addColumn('`quoted1`', 'integer', array('comment' => 'Quoted 1'));
924
        $fromTable->addColumn('`quoted2`', 'integer', array('comment' => 'Quoted 2'));
925
        $fromTable->addColumn('`quoted3`', 'integer', array('comment' => 'Quoted 3'));
926
927
        $toTable = new Table('mytable');
928
929
        $toTable->addColumn('unquoted', 'integer', array('comment' => 'Unquoted 1')); // unquoted -> unquoted
930
        $toTable->addColumn('where', 'integer', array('comment' => 'Unquoted 2')); // unquoted -> reserved keyword
931
        $toTable->addColumn('`foo`', 'integer', array('comment' => 'Unquoted 3')); // unquoted -> quoted
932
933
        $toTable->addColumn('reserved_keyword', 'integer', array('comment' => 'Reserved keyword 1')); // reserved keyword -> unquoted
934
        $toTable->addColumn('from', 'integer', array('comment' => 'Reserved keyword 2')); // reserved keyword -> reserved keyword
935
        $toTable->addColumn('`bar`', 'integer', array('comment' => 'Reserved keyword 3')); // reserved keyword -> quoted
936
937
        $toTable->addColumn('quoted', 'integer', array('comment' => 'Quoted 1')); // quoted -> unquoted
938
        $toTable->addColumn('and', 'integer', array('comment' => 'Quoted 2')); // quoted -> reserved keyword
939
        $toTable->addColumn('`baz`', 'integer', array('comment' => 'Quoted 3')); // quoted -> quoted
940
941
        $comparator = new Comparator();
942
943
        self::assertEquals(
944
            $this->getQuotedAlterTableRenameColumnSQL(),
945
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($fromTable, $toTable) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
946
        );
947
    }
948
949
    /**
950
     * Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
951
     *
952
     * @return array
953
     *
954
     * @group DBAL-835
955
     */
956
    abstract protected function getQuotedAlterTableRenameColumnSQL();
957
958
    /**
959
     * @group DBAL-835
960
     */
961
    public function testQuotesAlterTableChangeColumnLength()
962
    {
963
        $fromTable = new Table('mytable');
964
965
        $fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
966
        $fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
967
        $fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));
968
969
        $fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
970
        $fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
971
        $fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));
972
973
        $toTable = new Table('mytable');
974
975
        $toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
976
        $toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
977
        $toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));
978
979
        $toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
980
        $toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
981
        $toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));
982
983
        $comparator = new Comparator();
984
985
        self::assertEquals(
986
            $this->getQuotedAlterTableChangeColumnLengthSQL(),
987
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($fromTable, $toTable) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
988
        );
989
    }
990
991
    /**
992
     * Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
993
     *
994
     * @return array
995
     *
996
     * @group DBAL-835
997
     */
998
    abstract protected function getQuotedAlterTableChangeColumnLengthSQL();
999
1000
    /**
1001
     * @group DBAL-807
1002
     */
1003 View Code Duplication
    public function testAlterTableRenameIndexInSchema()
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...
1004
    {
1005
        $tableDiff            = new TableDiff('myschema.mytable');
1006
        $tableDiff->fromTable = new Table('myschema.mytable');
1007
        $tableDiff->fromTable->addColumn('id', 'integer');
1008
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1009
        $tableDiff->renamedIndexes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('idx_foo' => new \...idx_bar', array('id'))) of type array<string,object<Doct...DBAL\\Schema\\Index>"}> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $renamedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1010
            'idx_foo' => new Index('idx_bar', array('id'))
1011
        );
1012
1013
        self::assertSame(
1014
            $this->getAlterTableRenameIndexInSchemaSQL(),
1015
            $this->_platform->getAlterTableSQL($tableDiff)
1016
        );
1017
    }
1018
1019
    /**
1020
     * @group DBAL-807
1021
     */
1022
    protected function getAlterTableRenameIndexInSchemaSQL()
1023
    {
1024
        return array(
1025
            'DROP INDEX idx_foo',
1026
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
1027
        );
1028
    }
1029
1030
    /**
1031
     * @group DBAL-807
1032
     */
1033 View Code Duplication
    public function testQuotesAlterTableRenameIndexInSchema()
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...
1034
    {
1035
        $tableDiff            = new TableDiff('`schema`.table');
1036
        $tableDiff->fromTable = new Table('`schema`.table');
1037
        $tableDiff->fromTable->addColumn('id', 'integer');
1038
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1039
        $tableDiff->renamedIndexes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('create' => new \D...('`bar`', array('id'))) of type array<string,object<Doct...DBAL\\Schema\\Index>"}> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $renamedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1040
            'create' => new Index('select', array('id')),
1041
            '`foo`'  => new Index('`bar`', array('id')),
1042
        );
1043
1044
        self::assertSame(
1045
            $this->getQuotedAlterTableRenameIndexInSchemaSQL(),
1046
            $this->_platform->getAlterTableSQL($tableDiff)
1047
        );
1048
    }
1049
1050
    /**
1051
     * @group DBAL-234
1052
     */
1053
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
1054
    {
1055
        return array(
1056
            'DROP INDEX "schema"."create"',
1057
            'CREATE INDEX "select" ON "schema"."table" (id)',
1058
            'DROP INDEX "schema"."foo"',
1059
            'CREATE INDEX "bar" ON "schema"."table" (id)',
1060
        );
1061
    }
1062
1063
    /**
1064
     * @group DBAL-1237
1065
     */
1066
    public function testQuotesDropForeignKeySQL()
1067
    {
1068
        if ( ! $this->_platform->supportsForeignKeyConstraints()) {
1069
            $this->markTestSkipped(
1070
                sprintf('%s does not support foreign key constraints.', get_class($this->_platform))
1071
            );
1072
        }
1073
1074
        $tableName      = 'table';
1075
        $table          = new Table($tableName);
1076
        $foreignKeyName = 'select';
1077
        $foreignKey     = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1078
        $expectedSql    = $this->getQuotesDropForeignKeySQL();
1079
1080
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKeyName, $tableName));
1081
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKey, $table));
1082
    }
1083
1084
    protected function getQuotesDropForeignKeySQL()
1085
    {
1086
        return 'ALTER TABLE "table" DROP FOREIGN KEY "select"';
1087
    }
1088
1089
    /**
1090
     * @group DBAL-1237
1091
     */
1092
    public function testQuotesDropConstraintSQL()
1093
    {
1094
        $tableName      = 'table';
1095
        $table          = new Table($tableName);
1096
        $constraintName = 'select';
1097
        $constraint     = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1098
        $expectedSql    = $this->getQuotesDropConstraintSQL();
1099
1100
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraintName, $tableName));
1101
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraint, $table));
1102
    }
1103
1104
    protected function getQuotesDropConstraintSQL()
1105
    {
1106
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
1107
    }
1108
1109
    protected function getStringLiteralQuoteCharacter()
1110
    {
1111
        return "'";
1112
    }
1113
1114
    public function testGetStringLiteralQuoteCharacter()
1115
    {
1116
        self::assertSame($this->getStringLiteralQuoteCharacter(), $this->_platform->getStringLiteralQuoteCharacter());
1117
    }
1118
1119
    protected function getQuotedCommentOnColumnSQLWithoutQuoteCharacter()
1120
    {
1121
        return "COMMENT ON COLUMN mytable.id IS 'This is a comment'";
1122
    }
1123
1124
    public function testGetCommentOnColumnSQLWithoutQuoteCharacter()
1125
    {
1126
        self::assertEquals(
1127
            $this->getQuotedCommentOnColumnSQLWithoutQuoteCharacter(),
1128
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', 'This is a comment')
1129
        );
1130
    }
1131
1132
    protected function getQuotedCommentOnColumnSQLWithQuoteCharacter()
1133
    {
1134
        return "COMMENT ON COLUMN mytable.id IS 'It''s a quote !'";
1135
    }
1136
1137
    public function testGetCommentOnColumnSQLWithQuoteCharacter()
1138
    {
1139
        $c = $this->getStringLiteralQuoteCharacter();
1140
1141
        self::assertEquals(
1142
            $this->getQuotedCommentOnColumnSQLWithQuoteCharacter(),
1143
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', "It" . $c . "s a quote !")
1144
        );
1145
    }
1146
1147
    /**
1148
     * @return array
1149
     *
1150
     * @see testGetCommentOnColumnSQL
1151
     */
1152
    abstract protected function getCommentOnColumnSQL();
1153
1154
    /**
1155
     * @group DBAL-1004
1156
     */
1157
    public function testGetCommentOnColumnSQL()
1158
    {
1159
        self::assertSame(
1160
            $this->getCommentOnColumnSQL(),
1161
            array(
1162
                $this->_platform->getCommentOnColumnSQL('foo', 'bar', 'comment'), // regular identifiers
1163
                $this->_platform->getCommentOnColumnSQL('`Foo`', '`BAR`', 'comment'), // explicitly quoted identifiers
1164
                $this->_platform->getCommentOnColumnSQL('select', 'from', 'comment'), // reserved keyword identifiers
1165
            )
1166
        );
1167
    }
1168
1169
    /**
1170
     * @group DBAL-1176
1171
     *
1172
     * @dataProvider getGeneratesInlineColumnCommentSQL
1173
     */
1174
    public function testGeneratesInlineColumnCommentSQL($comment, $expectedSql)
1175
    {
1176
        if ( ! $this->_platform->supportsInlineColumnComments()) {
1177
            $this->markTestSkipped(sprintf('%s does not support inline column comments.', get_class($this->_platform)));
1178
        }
1179
1180
        self::assertSame($expectedSql, $this->_platform->getInlineColumnCommentSQL($comment));
1181
    }
1182
1183
    public function getGeneratesInlineColumnCommentSQL()
1184
    {
1185
        return array(
1186
            'regular comment' => array('Regular comment', $this->getInlineColumnRegularCommentSQL()),
1187
            'comment requiring escaping' => array(
1188
                sprintf(
1189
                    'Using inline comment delimiter %s works',
1190
                    $this->getInlineColumnCommentDelimiter()
1191
                ),
1192
                $this->getInlineColumnCommentRequiringEscapingSQL()
1193
            ),
1194
            'empty comment' => array('', $this->getInlineColumnEmptyCommentSQL()),
1195
        );
1196
    }
1197
1198
    protected function getInlineColumnCommentDelimiter()
1199
    {
1200
        return "'";
1201
    }
1202
1203
    protected function getInlineColumnRegularCommentSQL()
1204
    {
1205
        return "COMMENT 'Regular comment'";
1206
    }
1207
1208
    protected function getInlineColumnCommentRequiringEscapingSQL()
1209
    {
1210
        return "COMMENT 'Using inline comment delimiter '' works'";
1211
    }
1212
1213
    protected function getInlineColumnEmptyCommentSQL()
1214
    {
1215
        return "COMMENT ''";
1216
    }
1217
1218
    protected function getQuotedStringLiteralWithoutQuoteCharacter()
1219
    {
1220
        return "'No quote'";
1221
    }
1222
1223
    protected function getQuotedStringLiteralWithQuoteCharacter()
1224
    {
1225
        return "'It''s a quote'";
1226
    }
1227
1228
    protected function getQuotedStringLiteralQuoteCharacter()
1229
    {
1230
        return "''''";
1231
    }
1232
1233
    /**
1234
     * @group DBAL-1176
1235
     */
1236
    public function testThrowsExceptionOnGeneratingInlineColumnCommentSQLIfUnsupported()
1237
    {
1238
        if ($this->_platform->supportsInlineColumnComments()) {
1239
            $this->markTestSkipped(sprintf('%s supports inline column comments.', get_class($this->_platform)));
1240
        }
1241
1242
        $this->expectException(
1243
            'Doctrine\DBAL\DBALException',
1244
            "Operation 'Doctrine\\DBAL\\Platforms\\AbstractPlatform::getInlineColumnCommentSQL' is not supported by platform.",
0 ignored issues
show
Unused Code introduced by
The call to AbstractPlatformTestCase::expectException() has too many arguments starting with 'Operation \'Doctrine\\D...supported by platform.'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1245
            0
1246
        );
1247
1248
        $this->_platform->getInlineColumnCommentSQL('unsupported');
1249
    }
1250
1251
    public function testQuoteStringLiteral()
1252
    {
1253
        $c = $this->getStringLiteralQuoteCharacter();
1254
1255
        self::assertEquals(
1256
            $this->getQuotedStringLiteralWithoutQuoteCharacter(),
1257
            $this->_platform->quoteStringLiteral('No quote')
1258
        );
1259
        self::assertEquals(
1260
            $this->getQuotedStringLiteralWithQuoteCharacter(),
1261
            $this->_platform->quoteStringLiteral('It' . $c . 's a quote')
1262
        );
1263
        self::assertEquals(
1264
            $this->getQuotedStringLiteralQuoteCharacter(),
1265
            $this->_platform->quoteStringLiteral($c)
1266
        );
1267
    }
1268
1269
    /**
1270
     * @group DBAL-423
1271
     *
1272
     * @expectedException \Doctrine\DBAL\DBALException
1273
     */
1274
    public function testReturnsGuidTypeDeclarationSQL()
1275
    {
1276
        $this->_platform->getGuidTypeDeclarationSQL(array());
1277
    }
1278
1279
    /**
1280
     * @group DBAL-1010
1281
     */
1282
    public function testGeneratesAlterTableRenameColumnSQL()
1283
    {
1284
        $table = new Table('foo');
1285
        $table->addColumn(
1286
            'bar',
1287
            'integer',
1288
            array('notnull' => true, 'default' => 666, 'comment' => 'rename test')
1289
        );
1290
1291
        $tableDiff                        = new TableDiff('foo');
1292
        $tableDiff->fromTable             = $table;
1293
        $tableDiff->renamedColumns['bar'] = new Column(
1294
            'baz',
1295
            Type::getType('integer'),
1296
            array('notnull' => true, 'default' => 666, 'comment' => 'rename test')
1297
        );
1298
1299
        self::assertSame($this->getAlterTableRenameColumnSQL(), $this->_platform->getAlterTableSQL($tableDiff));
1300
    }
1301
1302
    /**
1303
     * @return array
1304
     */
1305
    abstract public function getAlterTableRenameColumnSQL();
1306
1307
    /**
1308
     * @group DBAL-1016
1309
     */
1310
    public function testQuotesTableIdentifiersInAlterTableSQL()
1311
    {
1312
        $table = new Table('"foo"');
1313
        $table->addColumn('id', 'integer');
1314
        $table->addColumn('fk', 'integer');
1315
        $table->addColumn('fk2', 'integer');
1316
        $table->addColumn('fk3', 'integer');
1317
        $table->addColumn('bar', 'integer');
1318
        $table->addColumn('baz', 'integer');
1319
        $table->addForeignKeyConstraint('fk_table', array('fk'), array('id'), array(), 'fk1');
1320
        $table->addForeignKeyConstraint('fk_table', array('fk2'), array('id'), array(), 'fk2');
1321
1322
        $tableDiff                        = new TableDiff('"foo"');
1323
        $tableDiff->fromTable             = $table;
1324
        $tableDiff->newName               = 'table';
1325
        $tableDiff->addedColumns['bloo']  = new Column('bloo', Type::getType('integer'));
1326
        $tableDiff->changedColumns['bar'] = new ColumnDiff(
1327
            'bar',
1328
            new Column('bar', Type::getType('integer'), array('notnull' => false)),
1329
            array('notnull'),
1330
            $table->getColumn('bar')
1331
        );
1332
        $tableDiff->renamedColumns['id']  = new Column('war', Type::getType('integer'));
1333
        $tableDiff->removedColumns['baz'] = new Column('baz', Type::getType('integer'));
1334
        $tableDiff->addedForeignKeys[]    = new ForeignKeyConstraint(array('fk3'), 'fk_table', array('id'), 'fk_add');
1335
        $tableDiff->changedForeignKeys[]  = new ForeignKeyConstraint(array('fk2'), 'fk_table2', array('id'), 'fk2');
1336
        $tableDiff->removedForeignKeys[]  = new ForeignKeyConstraint(array('fk'), 'fk_table', array('id'), 'fk1');
1337
1338
        self::assertSame(
1339
            $this->getQuotesTableIdentifiersInAlterTableSQL(),
1340
            $this->_platform->getAlterTableSQL($tableDiff)
1341
        );
1342
    }
1343
1344
    /**
1345
     * @return array
1346
     */
1347
    abstract protected function getQuotesTableIdentifiersInAlterTableSQL();
1348
1349
    /**
1350
     * @group DBAL-1090
1351
     */
1352
    public function testAlterStringToFixedString()
1353
    {
1354
1355
        $table = new Table('mytable');
1356
        $table->addColumn('name', 'string', array('length' => 2));
1357
1358
        $tableDiff            = new TableDiff('mytable');
1359
        $tableDiff->fromTable = $table;
1360
1361
        $tableDiff->changedColumns['name'] = new \Doctrine\DBAL\Schema\ColumnDiff(
1362
            'name',
1363
            new \Doctrine\DBAL\Schema\Column(
1364
                'name',
1365
                \Doctrine\DBAL\Types\Type::getType('string'),
1366
                array('fixed' => true, 'length' => 2)
1367
            ),
1368
            array('fixed')
1369
        );
1370
1371
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
1372
1373
        $expectedSql = $this->getAlterStringToFixedStringSQL();
1374
1375
        self::assertEquals($expectedSql, $sql);
1376
    }
1377
1378
    /**
1379
     * @return array
1380
     */
1381
    abstract protected function getAlterStringToFixedStringSQL();
1382
1383
    /**
1384
     * @group DBAL-1062
1385
     */
1386
    public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
1387
    {
1388
        $foreignTable = new Table('foreign_table');
1389
        $foreignTable->addColumn('id', 'integer');
1390
        $foreignTable->setPrimaryKey(array('id'));
1391
1392
        $primaryTable = new Table('mytable');
1393
        $primaryTable->addColumn('foo', 'integer');
1394
        $primaryTable->addColumn('bar', 'integer');
1395
        $primaryTable->addColumn('baz', 'integer');
1396
        $primaryTable->addIndex(array('foo'), 'idx_foo');
1397
        $primaryTable->addIndex(array('bar'), 'idx_bar');
1398
        $primaryTable->addForeignKeyConstraint($foreignTable, array('foo'), array('id'), array(), 'fk_foo');
1399
        $primaryTable->addForeignKeyConstraint($foreignTable, array('bar'), array('id'), array(), 'fk_bar');
1400
1401
        $tableDiff                            = new TableDiff('mytable');
1402
        $tableDiff->fromTable                 = $primaryTable;
1403
        $tableDiff->renamedIndexes['idx_foo'] = new Index('idx_foo_renamed', array('foo'));
1404
1405
        self::assertSame(
1406
            $this->getGeneratesAlterTableRenameIndexUsedByForeignKeySQL(),
1407
            $this->_platform->getAlterTableSQL($tableDiff)
1408
        );
1409
    }
1410
1411
    /**
1412
     * @return array
1413
     */
1414
    abstract protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL();
1415
1416
    /**
1417
     * @group DBAL-1082
1418
     *
1419
     * @dataProvider getGeneratesDecimalTypeDeclarationSQL
1420
     */
1421
    public function testGeneratesDecimalTypeDeclarationSQL(array $column, $expectedSql)
1422
    {
1423
        self::assertSame($expectedSql, $this->_platform->getDecimalTypeDeclarationSQL($column));
1424
    }
1425
1426
    /**
1427
     * @return array
1428
     */
1429 View Code Duplication
    public function getGeneratesDecimalTypeDeclarationSQL()
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...
1430
    {
1431
        return array(
1432
            array(array(), 'NUMERIC(10, 0)'),
1433
            array(array('unsigned' => true), 'NUMERIC(10, 0)'),
1434
            array(array('unsigned' => false), 'NUMERIC(10, 0)'),
1435
            array(array('precision' => 5), 'NUMERIC(5, 0)'),
1436
            array(array('scale' => 5), 'NUMERIC(10, 5)'),
1437
            array(array('precision' => 8, 'scale' => 2), 'NUMERIC(8, 2)'),
1438
        );
1439
    }
1440
1441
    /**
1442
     * @group DBAL-1082
1443
     *
1444
     * @dataProvider getGeneratesFloatDeclarationSQL
1445
     */
1446
    public function testGeneratesFloatDeclarationSQL(array $column, $expectedSql)
1447
    {
1448
        self::assertSame($expectedSql, $this->_platform->getFloatDeclarationSQL($column));
1449
    }
1450
1451
    /**
1452
     * @return array
1453
     */
1454 View Code Duplication
    public function getGeneratesFloatDeclarationSQL()
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...
1455
    {
1456
        return array(
1457
            array(array(), 'DOUBLE PRECISION'),
1458
            array(array('unsigned' => true), 'DOUBLE PRECISION'),
1459
            array(array('unsigned' => false), 'DOUBLE PRECISION'),
1460
            array(array('precision' => 5), 'DOUBLE PRECISION'),
1461
            array(array('scale' => 5), 'DOUBLE PRECISION'),
1462
            array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
1463
        );
1464
    }
1465
}
1466