Passed
Pull Request — master (#2945)
by Dorian
20:40 queued 09:56
created

testAlterTableRenameForeignKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

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 8
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
The assignment to $sql is dead and can be removed.
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', new \Doctrine\DBAL\Schema\Column(
341
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
342
            ),
343
            array('type', 'notnull', 'default')
344
        );
345
        $tableDiff->changedColumns['bloo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
346
            'bloo', new \Doctrine\DBAL\Schema\Column(
347
                'bloo', \Doctrine\DBAL\Types\Type::getType('boolean'), array('default' => false)
348
            ),
349
            array('type', 'notnull', 'default')
350
        );
351
352
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
353
354
        self::assertEquals($expectedSql, $sql);
355
    }
356
357
    public function testGetCustomColumnDeclarationSql()
358
    {
359
        $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
360
        self::assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
361
    }
362
363
    public function testGetCreateTableSqlDispatchEvent()
364
    {
365
        $listenerMock = $this->getMockBuilder('GetCreateTableSqlDispatchEvenListener')
366
            ->setMethods(array('onSchemaCreateTable', 'onSchemaCreateTableColumn'))
367
            ->getMock();
368
        $listenerMock
369
            ->expects($this->once())
370
            ->method('onSchemaCreateTable');
371
        $listenerMock
372
            ->expects($this->exactly(2))
373
            ->method('onSchemaCreateTableColumn');
374
375
        $eventManager = new EventManager();
376
        $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
377
378
        $this->_platform->setEventManager($eventManager);
379
380
        $table = new Table('test');
381
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
382
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
383
384
        $this->_platform->getCreateTableSQL($table);
385
    }
386
387
    public function testGetDropTableSqlDispatchEvent()
388
    {
389
        $listenerMock = $this->getMockBuilder('GetDropTableSqlDispatchEventListener')
390
            ->setMethods(array('onSchemaDropTable'))
391
            ->getMock();
392
        $listenerMock
393
            ->expects($this->once())
394
            ->method('onSchemaDropTable');
395
396
        $eventManager = new EventManager();
397
        $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);
398
399
        $this->_platform->setEventManager($eventManager);
400
401
        $this->_platform->getDropTableSQL('TABLE');
402
    }
403
404
    public function testGetAlterTableSqlDispatchEvent()
405
    {
406
        $events = array(
407
            'onSchemaAlterTable',
408
            'onSchemaAlterTableAddColumn',
409
            'onSchemaAlterTableRemoveColumn',
410
            'onSchemaAlterTableChangeColumn',
411
            'onSchemaAlterTableRenameColumn'
412
        );
413
414
        $listenerMock = $this->getMockBuilder('GetAlterTableSqlDispatchEvenListener')
415
            ->setMethods($events)
416
            ->getMock();
417
        $listenerMock
418
            ->expects($this->once())
419
            ->method('onSchemaAlterTable');
420
        $listenerMock
421
            ->expects($this->once())
422
            ->method('onSchemaAlterTableAddColumn');
423
        $listenerMock
424
            ->expects($this->once())
425
            ->method('onSchemaAlterTableRemoveColumn');
426
        $listenerMock
427
            ->expects($this->once())
428
            ->method('onSchemaAlterTableChangeColumn');
429
        $listenerMock
430
            ->expects($this->once())
431
            ->method('onSchemaAlterTableRenameColumn');
432
433
        $eventManager = new EventManager();
434
        $events = array(
435
            Events::onSchemaAlterTable,
436
            Events::onSchemaAlterTableAddColumn,
437
            Events::onSchemaAlterTableRemoveColumn,
438
            Events::onSchemaAlterTableChangeColumn,
439
            Events::onSchemaAlterTableRenameColumn
440
        );
441
        $eventManager->addEventListener($events, $listenerMock);
442
443
        $this->_platform->setEventManager($eventManager);
444
445
        $table = new Table('mytable');
446
        $table->addColumn('removed', 'integer');
447
        $table->addColumn('changed', 'integer');
448
        $table->addColumn('renamed', 'integer');
449
450
        $tableDiff = new TableDiff('mytable');
451
        $tableDiff->fromTable = $table;
452
        $tableDiff->addedColumns['added'] = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
453
        $tableDiff->removedColumns['removed'] = new \Doctrine\DBAL\Schema\Column('removed', \Doctrine\DBAL\Types\Type::getType('integer'), array());
454
        $tableDiff->changedColumns['changed'] = new \Doctrine\DBAL\Schema\ColumnDiff(
455
            'changed', new \Doctrine\DBAL\Schema\Column(
456
                'changed2', \Doctrine\DBAL\Types\Type::getType('string'), array()
457
            ),
458
            array()
459
        );
460
        $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());
461
462
        $this->_platform->getAlterTableSQL($tableDiff);
463
    }
464
465
    /**
466
     * @group DBAL-42
467
     */
468
    public function testCreateTableColumnComments()
469
    {
470
        $table = new Table('test');
471
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
472
        $table->setPrimaryKey(array('id'));
473
474
        self::assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
475
    }
476
477
    /**
478
     * @group DBAL-42
479
     */
480
    public function testAlterTableColumnComments()
481
    {
482
        $tableDiff = new TableDiff('mytable');
483
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
484
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
485
            'foo', new \Doctrine\DBAL\Schema\Column(
486
                'foo', \Doctrine\DBAL\Types\Type::getType('string')
487
            ),
488
            array('comment')
489
        );
490
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
491
            'bar', new \Doctrine\DBAL\Schema\Column(
492
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
493
            ),
494
            array('comment')
495
        );
496
497
        self::assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
498
    }
499
500 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...
501
    {
502
        $table = new Table('test');
503
        $table->addColumn('id', 'integer');
504
        $table->addColumn('data', 'array');
505
        $table->setPrimaryKey(array('id'));
506
507
        self::assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
508
    }
509
510
    public function getCreateTableColumnCommentsSQL()
511
    {
512
        $this->markTestSkipped('Platform does not support Column comments.');
513
    }
514
515
    public function getAlterTableColumnCommentsSQL()
516
    {
517
        $this->markTestSkipped('Platform does not support Column comments.');
518
    }
519
520
    public function getCreateTableColumnTypeCommentsSQL()
521
    {
522
        $this->markTestSkipped('Platform does not support Column comments.');
523
    }
524
525
    public function testGetDefaultValueDeclarationSQL()
526
    {
527
        // non-timestamp value will get single quotes
528
        $field = array(
529
            'type' => Type::getType('string'),
530
            'default' => 'non_timestamp'
531
        );
532
533
        self::assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field));
534
    }
535
536
    /**
537
     * @group 2859
538
     */
539
    public function testGetDefaultValueDeclarationSQLDateTime() : void
540
    {
541
        // timestamps on datetime types should not be quoted
542
        foreach (['datetime', 'datetimetz', 'datetime_immutable', 'datetimetz_immutable'] as $type) {
543
            $field = [
544
                'type'    => Type::getType($type),
545
                'default' => $this->_platform->getCurrentTimestampSQL(),
546
            ];
547
548
            self::assertSame(
549
                ' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(),
550
                $this->_platform->getDefaultValueDeclarationSQL($field)
551
            );
552
        }
553
    }
554
555
    public function testGetDefaultValueDeclarationSQLForIntegerTypes()
556
    {
557
        foreach(array('bigint', 'integer', 'smallint') as $type) {
558
            $field = array(
559
                'type'    => Type::getType($type),
560
                'default' => 1
561
            );
562
563
            self::assertEquals(
564
                ' DEFAULT 1',
565
                $this->_platform->getDefaultValueDeclarationSQL($field)
566
            );
567
        }
568
    }
569
570
    /**
571
     * @group 2859
572
     */
573 View Code Duplication
    public function testGetDefaultValueDeclarationSQLForDateType() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
574
    {
575
        $currentDateSql = $this->_platform->getCurrentDateSQL();
576
        foreach (['date', 'date_immutable'] as $type) {
577
            $field = [
578
                'type'    => Type::getType($type),
579
                'default' => $currentDateSql,
580
            ];
581
582
            self::assertSame(
583
                ' DEFAULT ' . $currentDateSql,
584
                $this->_platform->getDefaultValueDeclarationSQL($field)
585
            );
586
        }
587
    }
588
589
    /**
590
     * @group DBAL-45
591
     */
592
    public function testKeywordList()
593
    {
594
        $keywordList = $this->_platform->getReservedKeywordsList();
595
        self::assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
596
597
        self::assertTrue($keywordList->isKeyword('table'));
598
    }
599
600
    /**
601
     * @group DBAL-374
602
     */
603 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...
604
    {
605
        $table = new Table('`quoted`');
606
        $table->addColumn('create', 'string');
607
        $table->setPrimaryKey(array('create'));
608
609
        $sql = $this->_platform->getCreateTableSQL($table);
610
        self::assertEquals($this->getQuotedColumnInPrimaryKeySQL(), $sql);
611
    }
612
613
    abstract protected function getQuotedColumnInPrimaryKeySQL();
614
    abstract protected function getQuotedColumnInIndexSQL();
615
    abstract protected function getQuotedNameInIndexSQL();
616
    abstract protected function getQuotedColumnInForeignKeySQL();
617
618
    /**
619
     * @group DBAL-374
620
     */
621 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...
622
    {
623
        $table = new Table('`quoted`');
624
        $table->addColumn('create', 'string');
625
        $table->addIndex(array('create'));
626
627
        $sql = $this->_platform->getCreateTableSQL($table);
628
        self::assertEquals($this->getQuotedColumnInIndexSQL(), $sql);
629
    }
630
631 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...
632
    {
633
        $table = new Table('test');
634
        $table->addColumn('column1', 'string');
635
        $table->addIndex(array('column1'), '`key`');
636
637
        $sql = $this->_platform->getCreateTableSQL($table);
638
        self::assertEquals($this->getQuotedNameInIndexSQL(), $sql);
639
    }
640
641
    /**
642
     * @group DBAL-374
643
     */
644
    public function testQuotedColumnInForeignKeyPropagation()
645
    {
646
        $table = new Table('`quoted`');
647
        $table->addColumn('create', 'string');
648
        $table->addColumn('foo', 'string');
649
        $table->addColumn('`bar`', 'string');
650
651
        // Foreign table with reserved keyword as name (needs quotation).
652
        $foreignTable = new Table('foreign');
653
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
654
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
655
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
656
657
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');
658
659
        // Foreign table with non-reserved keyword as name (does not need quotation).
660
        $foreignTable = new Table('foo');
661
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
662
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
663
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
664
665
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');
666
667
        // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
668
        $foreignTable = new Table('`foo-bar`');
669
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
670
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
671
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
672
673
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
674
675
        $sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
676
        self::assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
677
    }
678
679
    /**
680
     * @group DBAL-1051
681
     */
682
    public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
683
    {
684
        $index = new Index('select', array('foo'), true);
685
686
        self::assertSame(
687
            $this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
688
            $this->_platform->getUniqueConstraintDeclarationSQL('select', $index)
689
        );
690
    }
691
692
    /**
693
     * @return string
694
     */
695
    abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL();
696
697
    /**
698
     * @group DBAL-2270
699
     */
700
    public function testQuotesReservedKeywordInTruncateTableSQL()
701
    {
702
        self::assertSame(
703
            $this->getQuotesReservedKeywordInTruncateTableSQL(),
704
            $this->_platform->getTruncateTableSQL('select')
705
        );
706
    }
707
708
    /**
709
     * @return string
710
     */
711
    abstract protected function getQuotesReservedKeywordInTruncateTableSQL();
712
713
    /**
714
     * @group DBAL-1051
715
     */
716
    public function testQuotesReservedKeywordInIndexDeclarationSQL()
717
    {
718
        $index = new Index('select', array('foo'));
719
720
        if (! $this->supportsInlineIndexDeclaration()) {
721
            $this->expectException('Doctrine\DBAL\DBALException');
722
        }
723
724
        self::assertSame(
725
            $this->getQuotesReservedKeywordInIndexDeclarationSQL(),
726
            $this->_platform->getIndexDeclarationSQL('select', $index)
727
        );
728
    }
729
730
    /**
731
     * @return string
732
     */
733
    abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL();
734
735
    /**
736
     * @return boolean
737
     */
738
    protected function supportsInlineIndexDeclaration()
739
    {
740
        return true;
741
    }
742
743
    public function testSupportsCommentOnStatement()
744
    {
745
        self::assertSame($this->supportsCommentOnStatement(), $this->_platform->supportsCommentOnStatement());
746
    }
747
748
    /**
749
     * @return bool
750
     */
751
    protected function supportsCommentOnStatement()
752
    {
753
        return false;
754
    }
755
756
    /**
757
     * @expectedException \Doctrine\DBAL\DBALException
758
     */
759
    public function testGetCreateSchemaSQL()
760
    {
761
        $this->_platform->getCreateSchemaSQL('schema');
762
    }
763
764
    /**
765
     * @group DBAL-585
766
     */
767
    public function testAlterTableChangeQuotedColumn()
768
    {
769
        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
770
        $tableDiff->fromTable = new \Doctrine\DBAL\Schema\Table('mytable');
771
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
772
            'select', new \Doctrine\DBAL\Schema\Column(
773
                'select', \Doctrine\DBAL\Types\Type::getType('string')
774
            ),
775
            array('type')
776
        );
777
778
        self::assertContains(
779
            $this->_platform->quoteIdentifier('select'),
780
            implode(';', $this->_platform->getAlterTableSQL($tableDiff))
781
        );
782
    }
783
784
    /**
785
     * @group DBAL-563
786
     */
787
    public function testUsesSequenceEmulatedIdentityColumns()
788
    {
789
        self::assertFalse($this->_platform->usesSequenceEmulatedIdentityColumns());
790
    }
791
792
    /**
793
     * @group DBAL-563
794
     * @expectedException \Doctrine\DBAL\DBALException
795
     */
796
    public function testReturnsIdentitySequenceName()
797
    {
798
        $this->_platform->getIdentitySequenceName('mytable', 'mycolumn');
799
    }
800
801
    public function testReturnsBinaryDefaultLength()
802
    {
803
        self::assertSame($this->getBinaryDefaultLength(), $this->_platform->getBinaryDefaultLength());
804
    }
805
806
    protected function getBinaryDefaultLength()
807
    {
808
        return 255;
809
    }
810
811
    public function testReturnsBinaryMaxLength()
812
    {
813
        self::assertSame($this->getBinaryMaxLength(), $this->_platform->getBinaryMaxLength());
814
    }
815
816
    protected function getBinaryMaxLength()
817
    {
818
        return 4000;
819
    }
820
821
    /**
822
     * @expectedException \Doctrine\DBAL\DBALException
823
     */
824
    public function testReturnsBinaryTypeDeclarationSQL()
825
    {
826
        $this->_platform->getBinaryTypeDeclarationSQL(array());
827
    }
828
829
    /**
830
     * @group DBAL-553
831
     */
832
    public function hasNativeJsonType()
833
    {
834
        self::assertFalse($this->_platform->hasNativeJsonType());
835
    }
836
837
    /**
838
     * @group DBAL-553
839
     */
840 View Code Duplication
    public function testReturnsJsonTypeDeclarationSQL()
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...
841
    {
842
        $column = array(
843
            'length'  => 666,
844
            'notnull' => true,
845
            'type'    => Type::getType('json_array'),
846
        );
847
848
        self::assertSame(
849
            $this->_platform->getClobTypeDeclarationSQL($column),
850
            $this->_platform->getJsonTypeDeclarationSQL($column)
851
        );
852
    }
853
854
    /**
855
     * @group DBAL-234
856
     */
857 View Code Duplication
    public function testAlterTableRenameForeignKey(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
858
    {
859
        $tableDiff = new TableDiff('mytable');
860
        $tableDiff->fromTable = new Table('mytable');
861
        $tableDiff->fromTable->addColumn('fk', 'integer');
862
        $tableDiff->renamedForeignKeys = array(
863
            'fk1' => new ForeignKeyConstraint(array('fk'), 'fk_table', array('id'), 'fk2')
864
        );
865
866
        self::assertSame(
867
            $this->getAlterTableRenameForeignKeySQL(),
868
            $this->_platform->getAlterTableSQL($tableDiff)
869
        );
870
    }
871
872
    /**
873
     * @group DBAL-234
874
     */
875
    protected function getAlterTableRenameForeignKeySQL(): array
876
    {
877
        return [
878
            'ALTER TABLE mytable DROP FOREIGN KEY fk1',
879
            'ALTER TABLE mytable ADD CONSTRAINT fk2 FOREIGN KEY (fk) REFERENCES fk_table (id)',
880
        ];
881
    }
882
883
    /**
884
     * @group DBAL-234
885
     */
886 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...
887
    {
888
        $tableDiff = new TableDiff('mytable');
889
        $tableDiff->fromTable = new Table('mytable');
890
        $tableDiff->fromTable->addColumn('id', 'integer');
891
        $tableDiff->fromTable->setPrimaryKey(array('id'));
892
        $tableDiff->renamedIndexes = array(
893
            'idx_foo' => new Index('idx_bar', array('id'))
894
        );
895
896
        self::assertSame(
897
            $this->getAlterTableRenameIndexSQL(),
898
            $this->_platform->getAlterTableSQL($tableDiff)
899
        );
900
    }
901
902
    /**
903
     * @group DBAL-234
904
     */
905
    protected function getAlterTableRenameIndexSQL()
906
    {
907
        return array(
908
            'DROP INDEX idx_foo',
909
            'CREATE INDEX idx_bar ON mytable (id)',
910
        );
911
    }
912
913
    /**
914
     * @group DBAL-234
915
     */
916 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...
917
    {
918
        $tableDiff = new TableDiff('table');
919
        $tableDiff->fromTable = new Table('table');
920
        $tableDiff->fromTable->addColumn('id', 'integer');
921
        $tableDiff->fromTable->setPrimaryKey(array('id'));
922
        $tableDiff->renamedIndexes = array(
923
            'create' => new Index('select', array('id')),
924
            '`foo`'  => new Index('`bar`', array('id')),
925
        );
926
927
        self::assertSame(
928
            $this->getQuotedAlterTableRenameIndexSQL(),
929
            $this->_platform->getAlterTableSQL($tableDiff)
930
        );
931
    }
932
933
    /**
934
     * @group DBAL-234
935
     */
936
    protected function getQuotedAlterTableRenameIndexSQL()
937
    {
938
        return array(
939
            'DROP INDEX "create"',
940
            'CREATE INDEX "select" ON "table" (id)',
941
            'DROP INDEX "foo"',
942
            'CREATE INDEX "bar" ON "table" (id)',
943
        );
944
    }
945
946
    /**
947
     * @group DBAL-835
948
     */
949
    public function testQuotesAlterTableRenameColumn()
950
    {
951
        $fromTable = new Table('mytable');
952
953
        $fromTable->addColumn('unquoted1', 'integer', array('comment' => 'Unquoted 1'));
954
        $fromTable->addColumn('unquoted2', 'integer', array('comment' => 'Unquoted 2'));
955
        $fromTable->addColumn('unquoted3', 'integer', array('comment' => 'Unquoted 3'));
956
957
        $fromTable->addColumn('create', 'integer', array('comment' => 'Reserved keyword 1'));
958
        $fromTable->addColumn('table', 'integer', array('comment' => 'Reserved keyword 2'));
959
        $fromTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword 3'));
960
961
        $fromTable->addColumn('`quoted1`', 'integer', array('comment' => 'Quoted 1'));
962
        $fromTable->addColumn('`quoted2`', 'integer', array('comment' => 'Quoted 2'));
963
        $fromTable->addColumn('`quoted3`', 'integer', array('comment' => 'Quoted 3'));
964
965
        $toTable = new Table('mytable');
966
967
        $toTable->addColumn('unquoted', 'integer', array('comment' => 'Unquoted 1')); // unquoted -> unquoted
968
        $toTable->addColumn('where', 'integer', array('comment' => 'Unquoted 2')); // unquoted -> reserved keyword
969
        $toTable->addColumn('`foo`', 'integer', array('comment' => 'Unquoted 3')); // unquoted -> quoted
970
971
        $toTable->addColumn('reserved_keyword', 'integer', array('comment' => 'Reserved keyword 1')); // reserved keyword -> unquoted
972
        $toTable->addColumn('from', 'integer', array('comment' => 'Reserved keyword 2')); // reserved keyword -> reserved keyword
973
        $toTable->addColumn('`bar`', 'integer', array('comment' => 'Reserved keyword 3')); // reserved keyword -> quoted
974
975
        $toTable->addColumn('quoted', 'integer', array('comment' => 'Quoted 1')); // quoted -> unquoted
976
        $toTable->addColumn('and', 'integer', array('comment' => 'Quoted 2')); // quoted -> reserved keyword
977
        $toTable->addColumn('`baz`', 'integer', array('comment' => 'Quoted 3')); // quoted -> quoted
978
979
        $comparator = new Comparator();
980
981
        self::assertEquals(
982
            $this->getQuotedAlterTableRenameColumnSQL(),
983
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
984
        );
985
    }
986
987
    /**
988
     * Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
989
     *
990
     * @return array
991
     *
992
     * @group DBAL-835
993
     */
994
    abstract protected function getQuotedAlterTableRenameColumnSQL();
995
996
    /**
997
     * @group DBAL-835
998
     */
999
    public function testQuotesAlterTableChangeColumnLength()
1000
    {
1001
        $fromTable = new Table('mytable');
1002
1003
        $fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
1004
        $fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
1005
        $fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));
1006
1007
        $fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
1008
        $fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
1009
        $fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));
1010
1011
        $toTable = new Table('mytable');
1012
1013
        $toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
1014
        $toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
1015
        $toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));
1016
1017
        $toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
1018
        $toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
1019
        $toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));
1020
1021
        $comparator = new Comparator();
1022
1023
        self::assertEquals(
1024
            $this->getQuotedAlterTableChangeColumnLengthSQL(),
1025
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
1026
        );
1027
    }
1028
1029
    /**
1030
     * Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
1031
     *
1032
     * @return array
1033
     *
1034
     * @group DBAL-835
1035
     */
1036
    abstract protected function getQuotedAlterTableChangeColumnLengthSQL();
1037
1038
    /**
1039
     * @group DBAL-807
1040
     */
1041 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...
1042
    {
1043
        $tableDiff = new TableDiff('myschema.mytable');
1044
        $tableDiff->fromTable = new Table('myschema.mytable');
1045
        $tableDiff->fromTable->addColumn('id', 'integer');
1046
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1047
        $tableDiff->renamedIndexes = array(
1048
            'idx_foo' => new Index('idx_bar', array('id'))
1049
        );
1050
1051
        self::assertSame(
1052
            $this->getAlterTableRenameIndexInSchemaSQL(),
1053
            $this->_platform->getAlterTableSQL($tableDiff)
1054
        );
1055
    }
1056
1057
    /**
1058
     * @group DBAL-807
1059
     */
1060
    protected function getAlterTableRenameIndexInSchemaSQL()
1061
    {
1062
        return array(
1063
            'DROP INDEX idx_foo',
1064
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
1065
        );
1066
    }
1067
1068
    /**
1069
     * @group DBAL-807
1070
     */
1071 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...
1072
    {
1073
        $tableDiff = new TableDiff('`schema`.table');
1074
        $tableDiff->fromTable = new Table('`schema`.table');
1075
        $tableDiff->fromTable->addColumn('id', 'integer');
1076
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1077
        $tableDiff->renamedIndexes = array(
1078
            'create' => new Index('select', array('id')),
1079
            '`foo`'  => new Index('`bar`', array('id')),
1080
        );
1081
1082
        self::assertSame(
1083
            $this->getQuotedAlterTableRenameIndexInSchemaSQL(),
1084
            $this->_platform->getAlterTableSQL($tableDiff)
1085
        );
1086
    }
1087
1088
    /**
1089
     * @group DBAL-234
1090
     */
1091
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
1092
    {
1093
        return array(
1094
            'DROP INDEX "schema"."create"',
1095
            'CREATE INDEX "select" ON "schema"."table" (id)',
1096
            'DROP INDEX "schema"."foo"',
1097
            'CREATE INDEX "bar" ON "schema"."table" (id)',
1098
        );
1099
    }
1100
1101
    /**
1102
     * @group DBAL-1237
1103
     */
1104
    public function testQuotesDropForeignKeySQL()
1105
    {
1106
        if (! $this->_platform->supportsForeignKeyConstraints()) {
1107
            $this->markTestSkipped(
1108
                sprintf('%s does not support foreign key constraints.', get_class($this->_platform))
1109
            );
1110
        }
1111
1112
        $tableName = 'table';
1113
        $table = new Table($tableName);
1114
        $foreignKeyName = 'select';
1115
        $foreignKey = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1116
        $expectedSql = $this->getQuotesDropForeignKeySQL();
1117
1118
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKeyName, $tableName));
1119
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKey, $table));
1120
    }
1121
1122
    protected function getQuotesDropForeignKeySQL()
1123
    {
1124
        return 'ALTER TABLE "table" DROP FOREIGN KEY "select"';
1125
    }
1126
1127
    /**
1128
     * @group DBAL-1237
1129
     */
1130
    public function testQuotesDropConstraintSQL()
1131
    {
1132
        $tableName = 'table';
1133
        $table = new Table($tableName);
1134
        $constraintName = 'select';
1135
        $constraint = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1136
        $expectedSql = $this->getQuotesDropConstraintSQL();
1137
1138
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraintName, $tableName));
1139
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraint, $table));
1140
    }
1141
1142
    protected function getQuotesDropConstraintSQL()
1143
    {
1144
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
1145
    }
1146
1147
    protected function getStringLiteralQuoteCharacter()
1148
    {
1149
        return "'";
1150
    }
1151
1152
    public function testGetStringLiteralQuoteCharacter()
1153
    {
1154
        self::assertSame($this->getStringLiteralQuoteCharacter(), $this->_platform->getStringLiteralQuoteCharacter());
1155
    }
1156
1157
    protected function getQuotedCommentOnColumnSQLWithoutQuoteCharacter()
1158
    {
1159
        return "COMMENT ON COLUMN mytable.id IS 'This is a comment'";
1160
    }
1161
1162
    public function testGetCommentOnColumnSQLWithoutQuoteCharacter()
1163
    {
1164
        self::assertEquals(
1165
            $this->getQuotedCommentOnColumnSQLWithoutQuoteCharacter(),
1166
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', 'This is a comment')
1167
        );
1168
    }
1169
1170
    protected function getQuotedCommentOnColumnSQLWithQuoteCharacter()
1171
    {
1172
        return "COMMENT ON COLUMN mytable.id IS 'It''s a quote !'";
1173
    }
1174
1175
    public function testGetCommentOnColumnSQLWithQuoteCharacter()
1176
    {
1177
        $c = $this->getStringLiteralQuoteCharacter();
1178
1179
        self::assertEquals(
1180
            $this->getQuotedCommentOnColumnSQLWithQuoteCharacter(),
1181
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', "It" . $c . "s a quote !")
1182
        );
1183
    }
1184
1185
    /**
1186
     * @return array
1187
     *
1188
     * @see testGetCommentOnColumnSQL
1189
     */
1190
    abstract protected function getCommentOnColumnSQL();
1191
1192
    /**
1193
     * @group DBAL-1004
1194
     */
1195
    public function testGetCommentOnColumnSQL()
1196
    {
1197
        self::assertSame(
1198
            $this->getCommentOnColumnSQL(),
1199
            array(
1200
                $this->_platform->getCommentOnColumnSQL('foo', 'bar', 'comment'), // regular identifiers
1201
                $this->_platform->getCommentOnColumnSQL('`Foo`', '`BAR`', 'comment'), // explicitly quoted identifiers
1202
                $this->_platform->getCommentOnColumnSQL('select', 'from', 'comment'), // reserved keyword identifiers
1203
            )
1204
        );
1205
    }
1206
1207
    /**
1208
     * @group DBAL-1176
1209
     *
1210
     * @dataProvider getGeneratesInlineColumnCommentSQL
1211
     */
1212
    public function testGeneratesInlineColumnCommentSQL($comment, $expectedSql)
1213
    {
1214
        if (! $this->_platform->supportsInlineColumnComments()) {
1215
            $this->markTestSkipped(sprintf('%s does not support inline column comments.', get_class($this->_platform)));
1216
        }
1217
1218
        self::assertSame($expectedSql, $this->_platform->getInlineColumnCommentSQL($comment));
1219
    }
1220
1221
    public function getGeneratesInlineColumnCommentSQL()
1222
    {
1223
        return array(
1224
            'regular comment' => array('Regular comment', $this->getInlineColumnRegularCommentSQL()),
1225
            'comment requiring escaping' => array(
1226
                sprintf(
1227
                    'Using inline comment delimiter %s works',
1228
                    $this->getInlineColumnCommentDelimiter()
1229
                ),
1230
                $this->getInlineColumnCommentRequiringEscapingSQL()
1231
            ),
1232
            'empty comment' => array('', $this->getInlineColumnEmptyCommentSQL()),
1233
        );
1234
    }
1235
1236
    protected function getInlineColumnCommentDelimiter()
1237
    {
1238
        return "'";
1239
    }
1240
1241
    protected function getInlineColumnRegularCommentSQL()
1242
    {
1243
        return "COMMENT 'Regular comment'";
1244
    }
1245
1246
    protected function getInlineColumnCommentRequiringEscapingSQL()
1247
    {
1248
        return "COMMENT 'Using inline comment delimiter '' works'";
1249
    }
1250
1251
    protected function getInlineColumnEmptyCommentSQL()
1252
    {
1253
        return "COMMENT ''";
1254
    }
1255
1256
    protected function getQuotedStringLiteralWithoutQuoteCharacter()
1257
    {
1258
        return "'No quote'";
1259
    }
1260
1261
    protected function getQuotedStringLiteralWithQuoteCharacter()
1262
    {
1263
        return "'It''s a quote'";
1264
    }
1265
1266
    protected function getQuotedStringLiteralQuoteCharacter()
1267
    {
1268
        return "''''";
1269
    }
1270
1271
    /**
1272
     * @group DBAL-1176
1273
     */
1274
    public function testThrowsExceptionOnGeneratingInlineColumnCommentSQLIfUnsupported()
1275
    {
1276
        if ($this->_platform->supportsInlineColumnComments()) {
1277
            $this->markTestSkipped(sprintf('%s supports inline column comments.', get_class($this->_platform)));
1278
        }
1279
1280
        $this->expectException(
1281
            'Doctrine\DBAL\DBALException',
1282
            "Operation 'Doctrine\\DBAL\\Platforms\\AbstractPlatform::getInlineColumnCommentSQL' is not supported by platform.",
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\TestCase::expectException() has too many arguments starting with 'Operation 'Doctrine\DBA...supported by platform.'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1282
        $this->/** @scrutinizer ignore-call */ 
1283
               expectException(

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. Please note the @ignore annotation hint above.

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