Passed
Pull Request — master (#2893)
by Šimon
14:29 queued 16s
created

testGetSequenceMetadataSQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Events;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Schema\Column;
10
use Doctrine\DBAL\Schema\ColumnDiff;
11
use Doctrine\DBAL\Schema\Comparator;
12
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
13
use Doctrine\DBAL\Schema\Index;
14
use Doctrine\DBAL\Schema\Table;
15
use Doctrine\DBAL\Schema\TableDiff;
16
use Doctrine\DBAL\Types\Type;
17
use Doctrine\Tests\Types\CommentedType;
18
19
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
20
{
21
    /**
22
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
23
     */
24
    protected $_platform;
25
26
    abstract public function createPlatform();
27
28
    protected function setUp()
29
    {
30
        $this->_platform = $this->createPlatform();
31
    }
32
33
    /**
34
     * @group DDC-1360
35
     */
36
    public function testQuoteIdentifier()
37
    {
38
        if ($this->_platform->getName() == "mssql") {
39
            $this->markTestSkipped('Not working this way on mssql.');
40
        }
41
42
        $c = $this->_platform->getIdentifierQuoteCharacter();
43
        self::assertEquals($c."test".$c, $this->_platform->quoteIdentifier("test"));
44
        self::assertEquals($c."test".$c.".".$c."test".$c, $this->_platform->quoteIdentifier("test.test"));
45
        self::assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
46
    }
47
48
    /**
49
     * @group DDC-1360
50
     */
51
    public function testQuoteSingleIdentifier()
52
    {
53
        if ($this->_platform->getName() == "mssql") {
54
            $this->markTestSkipped('Not working this way on mssql.');
55
        }
56
57
        $c = $this->_platform->getIdentifierQuoteCharacter();
58
        self::assertEquals($c."test".$c, $this->_platform->quoteSingleIdentifier("test"));
59
        self::assertEquals($c."test.test".$c, $this->_platform->quoteSingleIdentifier("test.test"));
60
        self::assertEquals(str_repeat($c, 4), $this->_platform->quoteSingleIdentifier($c));
61
    }
62
63
    /**
64
     * @group DBAL-1029
65
     *
66
     * @dataProvider getReturnsForeignKeyReferentialActionSQL
67
     */
68
    public function testReturnsForeignKeyReferentialActionSQL($action, $expectedSQL)
69
    {
70
        self::assertSame($expectedSQL, $this->_platform->getForeignKeyReferentialActionSQL($action));
71
    }
72
73
    /**
74
     * @return array
75
     */
76 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...
77
    {
78
        return array(
79
            array('CASCADE', 'CASCADE'),
80
            array('SET NULL', 'SET NULL'),
81
            array('NO ACTION', 'NO ACTION'),
82
            array('RESTRICT', 'RESTRICT'),
83
            array('SET DEFAULT', 'SET DEFAULT'),
84
            array('CaScAdE', 'CASCADE'),
85
        );
86
    }
87
88
    public function testGetInvalidForeignKeyReferentialActionSQL()
89
    {
90
        $this->expectException('InvalidArgumentException');
91
        $this->_platform->getForeignKeyReferentialActionSQL('unknown');
92
    }
93
94
    public function testGetUnknownDoctrineMappingType()
95
    {
96
        $this->expectException('Doctrine\DBAL\DBALException');
97
        $this->_platform->getDoctrineTypeMapping('foobar');
98
    }
99
100
    public function testRegisterDoctrineMappingType()
101
    {
102
        $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
103
        self::assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
104
    }
105
106
    public function testRegisterUnknownDoctrineMappingType()
107
    {
108
        $this->expectException('Doctrine\DBAL\DBALException');
109
        $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
110
    }
111
112
    /**
113
     * @group DBAL-2594
114
     */
115
    public function testRegistersCommentedDoctrineMappingTypeImplicitly()
116
    {
117
        if (!Type::hasType('my_commented')) {
118
            Type::addType('my_commented', CommentedType::class);
119
        }
120
121
        $type = Type::getType('my_commented');
122
        $this->_platform->registerDoctrineTypeMapping('foo', 'my_commented');
123
124
        self::assertTrue($this->_platform->isCommentedDoctrineType($type));
125
    }
126
127
    /**
128
     * @group DBAL-939
129
     *
130
     * @dataProvider getIsCommentedDoctrineType
131
     */
132
    public function testIsCommentedDoctrineType(Type $type, $commented)
133
    {
134
        self::assertSame($commented, $this->_platform->isCommentedDoctrineType($type));
135
    }
136
137
    public function getIsCommentedDoctrineType()
138
    {
139
        $this->setUp();
140
141
        $data = array();
142
143
        foreach (Type::getTypesMap() as $typeName => $className) {
144
            $type = Type::getType($typeName);
145
146
            $data[$typeName] = array(
147
                $type,
148
                $type->requiresSQLCommentHint($this->_platform),
149
            );
150
        }
151
152
        return $data;
153
    }
154
155
    public function testCreateWithNoColumns()
156
    {
157
        $table = new Table('test');
158
159
        $this->expectException('Doctrine\DBAL\DBALException');
160
        $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...
161
    }
162
163 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...
164
    {
165
        $table = new Table('test');
166
        $table->addColumn('id', 'integer', array('notnull' => true, 'autoincrement' => true));
167
        $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
168
        $table->setPrimaryKey(array('id'));
169
170
        $sql = $this->_platform->getCreateTableSQL($table);
171
        self::assertEquals($this->getGenerateTableSql(), $sql[0]);
172
    }
173
174
    abstract public function getGenerateTableSql();
175
176 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...
177
    {
178
        $table = new Table('test');
179
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
180
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
181
        $table->addUniqueIndex(array("foo", "bar"));
182
183
        $sql = $this->_platform->getCreateTableSQL($table);
184
        self::assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
185
    }
186
187
    abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
188
189 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...
190
    {
191
        $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
192
193
        self::assertEquals(
194
            $this->getGenerateIndexSql(),
195
            $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
196
        );
197
    }
198
199
    abstract public function getGenerateIndexSql();
200
201 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...
202
    {
203
        $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
204
205
        $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
206
        self::assertEquals($this->getGenerateUniqueIndexSql(), $sql);
207
    }
208
209
    abstract public function getGenerateUniqueIndexSql();
210
211
    public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
212
    {
213
        $where = 'test IS NULL AND test2 IS NOT NULL';
214
        $indexDef = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), array('where' => $where));
215
        $uniqueIndex = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), true, false, array(), array('where' => $where));
216
217
        $expected = ' WHERE ' . $where;
218
219
        $actuals = array();
220
221
        if ($this->supportsInlineIndexDeclaration()) {
222
            $actuals []= $this->_platform->getIndexDeclarationSQL('name', $indexDef);
223
        }
224
225
        $actuals []= $this->_platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
226
        $actuals []= $this->_platform->getCreateIndexSQL($indexDef, 'table');
227
228
        foreach ($actuals as $actual) {
229
            if ($this->_platform->supportsPartialIndexes()) {
230
                self::assertStringEndsWith($expected, $actual, 'WHERE clause should be present');
231
            } else {
232
                self::assertStringEndsNotWith($expected, $actual, 'WHERE clause should NOT be present');
233
            }
234
        }
235
    }
236
237
    public function testGeneratesForeignKeyCreationSql()
238
    {
239
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
240
241
        $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
242
        self::assertEquals($sql, $this->getGenerateForeignKeySql());
243
    }
244
245
    abstract public function getGenerateForeignKeySql();
246
247
    public function testGeneratesConstraintCreationSql()
248
    {
249
        $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
250
        $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
251
        self::assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
252
253
        $pk = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
254
        $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
255
        self::assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);
256
257
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
258
        $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
259
        self::assertEquals($this->getGenerateConstraintForeignKeySql($fk), $sql);
260
    }
261
262
    public function testGeneratesForeignKeySqlOnlyWhenSupportingForeignKeys()
263
    {
264
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
265
266
        if ($this->_platform->supportsForeignKeyConstraints()) {
267
            self::assertInternalType(
268
                'string',
269
                $this->_platform->getCreateForeignKeySQL($fk, 'test')
270
            );
271
        } else {
272
            $this->expectException('Doctrine\DBAL\DBALException');
273
            $this->_platform->getCreateForeignKeySQL($fk, 'test');
274
        }
275
    }
276
277
    protected function getBitAndComparisonExpressionSql($value1, $value2)
278
    {
279
        return '(' . $value1 . ' & ' . $value2 . ')';
280
    }
281
282
    /**
283
     * @group DDC-1213
284
     */
285
    public function testGeneratesBitAndComparisonExpressionSql()
286
    {
287
        $sql = $this->_platform->getBitAndComparisonExpression(2, 4);
288
        self::assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
289
    }
290
291
    protected  function getBitOrComparisonExpressionSql($value1, $value2)
292
    {
293
        return '(' . $value1 . ' | ' . $value2 . ')';
294
    }
295
296
    /**
297
     * @group DDC-1213
298
     */
299
    public function testGeneratesBitOrComparisonExpressionSql()
300
    {
301
        $sql = $this->_platform->getBitOrComparisonExpression(2, 4);
302
        self::assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
303
    }
304
305
    public function getGenerateConstraintUniqueIndexSql()
306
    {
307
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
308
    }
309
310
    public function getGenerateConstraintPrimaryIndexSql()
311
    {
312
        return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
313
    }
314
315
    public function getGenerateConstraintForeignKeySql(ForeignKeyConstraint $fk)
316
    {
317
        $quotedForeignTable = $fk->getQuotedForeignTableName($this->_platform);
318
319
        return "ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES $quotedForeignTable (id)";
320
    }
321
322
    abstract public function getGenerateAlterTableSql();
323
324
    public function testGeneratesTableAlterationSql()
325
    {
326
        $expectedSql = $this->getGenerateAlterTableSql();
327
328
        $table = new Table('mytable');
329
        $table->addColumn('id', 'integer', array('autoincrement' => true));
330
        $table->addColumn('foo', 'integer');
331
        $table->addColumn('bar', 'string');
332
        $table->addColumn('bloo', 'boolean');
333
        $table->setPrimaryKey(array('id'));
334
335
        $tableDiff = new TableDiff('mytable');
336
        $tableDiff->fromTable = $table;
337
        $tableDiff->newName = 'userlist';
338
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
339
        $tableDiff->removedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
340
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
341
            'bar', new \Doctrine\DBAL\Schema\Column(
342
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
343
            ),
344
            array('type', 'notnull', 'default')
345
        );
346
        $tableDiff->changedColumns['bloo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
347
            'bloo', new \Doctrine\DBAL\Schema\Column(
348
                'bloo', \Doctrine\DBAL\Types\Type::getType('boolean'), array('default' => false)
349
            ),
350
            array('type', 'notnull', 'default')
351
        );
352
353
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
354
355
        self::assertEquals($expectedSql, $sql);
356
    }
357
358
    public function testGetCustomColumnDeclarationSql()
359
    {
360
        $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
361
        self::assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
362
    }
363
364
    public function testGetCreateTableSqlDispatchEvent()
365
    {
366
        $listenerMock = $this->getMockBuilder('GetCreateTableSqlDispatchEvenListener')
367
            ->setMethods(array('onSchemaCreateTable', 'onSchemaCreateTableColumn'))
368
            ->getMock();
369
        $listenerMock
370
            ->expects($this->once())
371
            ->method('onSchemaCreateTable');
372
        $listenerMock
373
            ->expects($this->exactly(2))
374
            ->method('onSchemaCreateTableColumn');
375
376
        $eventManager = new EventManager();
377
        $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
378
379
        $this->_platform->setEventManager($eventManager);
380
381
        $table = new Table('test');
382
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
383
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
384
385
        $this->_platform->getCreateTableSQL($table);
386
    }
387
388
    public function testGetDropTableSqlDispatchEvent()
389
    {
390
        $listenerMock = $this->getMockBuilder('GetDropTableSqlDispatchEventListener')
391
            ->setMethods(array('onSchemaDropTable'))
392
            ->getMock();
393
        $listenerMock
394
            ->expects($this->once())
395
            ->method('onSchemaDropTable');
396
397
        $eventManager = new EventManager();
398
        $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);
399
400
        $this->_platform->setEventManager($eventManager);
401
402
        $this->_platform->getDropTableSQL('TABLE');
403
    }
404
405
    public function testGetAlterTableSqlDispatchEvent()
406
    {
407
        $events = array(
408
            'onSchemaAlterTable',
409
            'onSchemaAlterTableAddColumn',
410
            'onSchemaAlterTableRemoveColumn',
411
            'onSchemaAlterTableChangeColumn',
412
            'onSchemaAlterTableRenameColumn'
413
        );
414
415
        $listenerMock = $this->getMockBuilder('GetAlterTableSqlDispatchEvenListener')
416
            ->setMethods($events)
417
            ->getMock();
418
        $listenerMock
419
            ->expects($this->once())
420
            ->method('onSchemaAlterTable');
421
        $listenerMock
422
            ->expects($this->once())
423
            ->method('onSchemaAlterTableAddColumn');
424
        $listenerMock
425
            ->expects($this->once())
426
            ->method('onSchemaAlterTableRemoveColumn');
427
        $listenerMock
428
            ->expects($this->once())
429
            ->method('onSchemaAlterTableChangeColumn');
430
        $listenerMock
431
            ->expects($this->once())
432
            ->method('onSchemaAlterTableRenameColumn');
433
434
        $eventManager = new EventManager();
435
        $events = array(
436
            Events::onSchemaAlterTable,
437
            Events::onSchemaAlterTableAddColumn,
438
            Events::onSchemaAlterTableRemoveColumn,
439
            Events::onSchemaAlterTableChangeColumn,
440
            Events::onSchemaAlterTableRenameColumn
441
        );
442
        $eventManager->addEventListener($events, $listenerMock);
443
444
        $this->_platform->setEventManager($eventManager);
445
446
        $table = new Table('mytable');
447
        $table->addColumn('removed', 'integer');
448
        $table->addColumn('changed', 'integer');
449
        $table->addColumn('renamed', 'integer');
450
451
        $tableDiff = new TableDiff('mytable');
452
        $tableDiff->fromTable = $table;
453
        $tableDiff->addedColumns['added'] = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
454
        $tableDiff->removedColumns['removed'] = new \Doctrine\DBAL\Schema\Column('removed', \Doctrine\DBAL\Types\Type::getType('integer'), array());
455
        $tableDiff->changedColumns['changed'] = new \Doctrine\DBAL\Schema\ColumnDiff(
456
            'changed', new \Doctrine\DBAL\Schema\Column(
457
                'changed2', \Doctrine\DBAL\Types\Type::getType('string'), array()
458
            ),
459
            array()
460
        );
461
        $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());
462
463
        $this->_platform->getAlterTableSQL($tableDiff);
464
    }
465
466
    /**
467
     * @group DBAL-42
468
     */
469
    public function testCreateTableColumnComments()
470
    {
471
        $table = new Table('test');
472
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
473
        $table->setPrimaryKey(array('id'));
474
475
        self::assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
476
    }
477
478
    /**
479
     * @group DBAL-42
480
     */
481
    public function testAlterTableColumnComments()
482
    {
483
        $tableDiff = new TableDiff('mytable');
484
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
485
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
486
            'foo', new \Doctrine\DBAL\Schema\Column(
487
                'foo', \Doctrine\DBAL\Types\Type::getType('string')
488
            ),
489
            array('comment')
490
        );
491
        $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
492
            'bar', new \Doctrine\DBAL\Schema\Column(
493
                'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
494
            ),
495
            array('comment')
496
        );
497
498
        self::assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
499
    }
500
501 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...
502
    {
503
        $table = new Table('test');
504
        $table->addColumn('id', 'integer');
505
        $table->addColumn('data', 'array');
506
        $table->setPrimaryKey(array('id'));
507
508
        self::assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
509
    }
510
511
    public function getCreateTableColumnCommentsSQL()
512
    {
513
        $this->markTestSkipped('Platform does not support Column comments.');
514
    }
515
516
    public function getAlterTableColumnCommentsSQL()
517
    {
518
        $this->markTestSkipped('Platform does not support Column comments.');
519
    }
520
521
    public function getCreateTableColumnTypeCommentsSQL()
522
    {
523
        $this->markTestSkipped('Platform does not support Column comments.');
524
    }
525
526
    public function testGetDefaultValueDeclarationSQL()
527
    {
528
        // non-timestamp value will get single quotes
529
        $field = array(
530
            'type' => Type::getType('string'),
531
            'default' => 'non_timestamp'
532
        );
533
534
        self::assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field));
535
    }
536
537
    /**
538
     * @group 2859
539
     */
540
    public function testGetDefaultValueDeclarationSQLDateTime() : void
541
    {
542
        // timestamps on datetime types should not be quoted
543
        foreach (['datetime', 'datetimetz', 'datetime_immutable', 'datetimetz_immutable'] as $type) {
544
            $field = [
545
                'type'    => Type::getType($type),
546
                'default' => $this->_platform->getCurrentTimestampSQL(),
547
            ];
548
549
            self::assertSame(
550
                ' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(),
551
                $this->_platform->getDefaultValueDeclarationSQL($field)
552
            );
553
        }
554
    }
555
556
    public function testGetDefaultValueDeclarationSQLForIntegerTypes()
557
    {
558
        foreach(array('bigint', 'integer', 'smallint') as $type) {
559
            $field = array(
560
                'type'    => Type::getType($type),
561
                'default' => 1
562
            );
563
564
            self::assertEquals(
565
                ' DEFAULT 1',
566
                $this->_platform->getDefaultValueDeclarationSQL($field)
567
            );
568
        }
569
    }
570
571
    /**
572
     * @group 2859
573
     */
574 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...
575
    {
576
        $currentDateSql = $this->_platform->getCurrentDateSQL();
577
        foreach (['date', 'date_immutable'] as $type) {
578
            $field = [
579
                'type'    => Type::getType($type),
580
                'default' => $currentDateSql,
581
            ];
582
583
            self::assertSame(
584
                ' DEFAULT ' . $currentDateSql,
585
                $this->_platform->getDefaultValueDeclarationSQL($field)
586
            );
587
        }
588
    }
589
590
    /**
591
     * @group DBAL-45
592
     */
593
    public function testKeywordList()
594
    {
595
        $keywordList = $this->_platform->getReservedKeywordsList();
596
        self::assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
597
598
        self::assertTrue($keywordList->isKeyword('table'));
599
    }
600
601
    /**
602
     * @group DBAL-374
603
     */
604 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...
605
    {
606
        $table = new Table('`quoted`');
607
        $table->addColumn('create', 'string');
608
        $table->setPrimaryKey(array('create'));
609
610
        $sql = $this->_platform->getCreateTableSQL($table);
611
        self::assertEquals($this->getQuotedColumnInPrimaryKeySQL(), $sql);
612
    }
613
614
    abstract protected function getQuotedColumnInPrimaryKeySQL();
615
    abstract protected function getQuotedColumnInIndexSQL();
616
    abstract protected function getQuotedNameInIndexSQL();
617
    abstract protected function getQuotedColumnInForeignKeySQL();
618
619
    /**
620
     * @group DBAL-374
621
     */
622 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...
623
    {
624
        $table = new Table('`quoted`');
625
        $table->addColumn('create', 'string');
626
        $table->addIndex(array('create'));
627
628
        $sql = $this->_platform->getCreateTableSQL($table);
629
        self::assertEquals($this->getQuotedColumnInIndexSQL(), $sql);
630
    }
631
632 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...
633
    {
634
        $table = new Table('test');
635
        $table->addColumn('column1', 'string');
636
        $table->addIndex(array('column1'), '`key`');
637
638
        $sql = $this->_platform->getCreateTableSQL($table);
639
        self::assertEquals($this->getQuotedNameInIndexSQL(), $sql);
640
    }
641
642
    /**
643
     * @group DBAL-374
644
     */
645
    public function testQuotedColumnInForeignKeyPropagation()
646
    {
647
        $table = new Table('`quoted`');
648
        $table->addColumn('create', 'string');
649
        $table->addColumn('foo', 'string');
650
        $table->addColumn('`bar`', 'string');
651
652
        // Foreign table with reserved keyword as name (needs quotation).
653
        $foreignTable = new Table('foreign');
654
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
655
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
656
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
657
658
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');
659
660
        // Foreign table with non-reserved keyword as name (does not need quotation).
661
        $foreignTable = new Table('foo');
662
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
663
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
664
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
665
666
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');
667
668
        // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
669
        $foreignTable = new Table('`foo-bar`');
670
        $foreignTable->addColumn('create', 'string');    // Foreign column with reserved keyword as name (needs quotation).
671
        $foreignTable->addColumn('bar', 'string');       // Foreign column with non-reserved keyword as name (does not need quotation).
672
        $foreignTable->addColumn('`foo-bar`', 'string'); // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
673
674
        $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
675
676
        $sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
677
        self::assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
678
    }
679
680
    /**
681
     * @group DBAL-1051
682
     */
683
    public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
684
    {
685
        $index = new Index('select', array('foo'), true);
686
687
        self::assertSame(
688
            $this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
689
            $this->_platform->getUniqueConstraintDeclarationSQL('select', $index)
690
        );
691
    }
692
693
    /**
694
     * @return string
695
     */
696
    abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL();
697
698
    /**
699
     * @group DBAL-2270
700
     */
701
    public function testQuotesReservedKeywordInTruncateTableSQL()
702
    {
703
        self::assertSame(
704
            $this->getQuotesReservedKeywordInTruncateTableSQL(),
705
            $this->_platform->getTruncateTableSQL('select')
706
        );
707
    }
708
709
    /**
710
     * @return string
711
     */
712
    abstract protected function getQuotesReservedKeywordInTruncateTableSQL();
713
714
    /**
715
     * @group DBAL-1051
716
     */
717
    public function testQuotesReservedKeywordInIndexDeclarationSQL()
718
    {
719
        $index = new Index('select', array('foo'));
720
721
        if (! $this->supportsInlineIndexDeclaration()) {
722
            $this->expectException('Doctrine\DBAL\DBALException');
723
        }
724
725
        self::assertSame(
726
            $this->getQuotesReservedKeywordInIndexDeclarationSQL(),
727
            $this->_platform->getIndexDeclarationSQL('select', $index)
728
        );
729
    }
730
731
    /**
732
     * @return string
733
     */
734
    abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL();
735
736
    /**
737
     * @return boolean
738
     */
739
    protected function supportsInlineIndexDeclaration()
740
    {
741
        return true;
742
    }
743
744
    public function testSupportsCommentOnStatement()
745
    {
746
        self::assertSame($this->supportsCommentOnStatement(), $this->_platform->supportsCommentOnStatement());
747
    }
748
749
    /**
750
     * @return bool
751
     */
752
    protected function supportsCommentOnStatement()
753
    {
754
        return false;
755
    }
756
757
    /**
758
     * @expectedException \Doctrine\DBAL\DBALException
759
     */
760
    public function testGetCreateSchemaSQL()
761
    {
762
        $this->_platform->getCreateSchemaSQL('schema');
763
    }
764
765
    /**
766
     * @group DBAL-585
767
     */
768
    public function testAlterTableChangeQuotedColumn()
769
    {
770
        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
771
        $tableDiff->fromTable = new \Doctrine\DBAL\Schema\Table('mytable');
772
        $tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
773
            'select', new \Doctrine\DBAL\Schema\Column(
774
                'select', \Doctrine\DBAL\Types\Type::getType('string')
775
            ),
776
            array('type')
777
        );
778
779
        self::assertContains(
780
            $this->_platform->quoteIdentifier('select'),
781
            implode(';', $this->_platform->getAlterTableSQL($tableDiff))
782
        );
783
    }
784
785
    /**
786
     * @group DBAL-563
787
     */
788
    public function testUsesSequenceEmulatedIdentityColumns()
789
    {
790
        self::assertFalse($this->_platform->usesSequenceEmulatedIdentityColumns());
791
    }
792
793
    /**
794
     * @group DBAL-563
795
     * @expectedException \Doctrine\DBAL\DBALException
796
     */
797
    public function testReturnsIdentitySequenceName()
798
    {
799
        $this->_platform->getIdentitySequenceName('mytable', 'mycolumn');
800
    }
801
802
    public function testReturnsBinaryDefaultLength()
803
    {
804
        self::assertSame($this->getBinaryDefaultLength(), $this->_platform->getBinaryDefaultLength());
805
    }
806
807
    protected function getBinaryDefaultLength()
808
    {
809
        return 255;
810
    }
811
812
    public function testReturnsBinaryMaxLength()
813
    {
814
        self::assertSame($this->getBinaryMaxLength(), $this->_platform->getBinaryMaxLength());
815
    }
816
817
    protected function getBinaryMaxLength()
818
    {
819
        return 4000;
820
    }
821
822
    /**
823
     * @expectedException \Doctrine\DBAL\DBALException
824
     */
825
    public function testReturnsBinaryTypeDeclarationSQL()
826
    {
827
        $this->_platform->getBinaryTypeDeclarationSQL(array());
828
    }
829
830
    /**
831
     * @group DBAL-553
832
     */
833
    public function hasNativeJsonType()
834
    {
835
        self::assertFalse($this->_platform->hasNativeJsonType());
836
    }
837
838
    /**
839
     * @group DBAL-553
840
     */
841 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...
842
    {
843
        $column = array(
844
            'length'  => 666,
845
            'notnull' => true,
846
            'type'    => Type::getType('json_array'),
847
        );
848
849
        self::assertSame(
850
            $this->_platform->getClobTypeDeclarationSQL($column),
851
            $this->_platform->getJsonTypeDeclarationSQL($column)
852
        );
853
    }
854
855
    /**
856
     * @group DBAL-234
857
     */
858 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...
859
    {
860
        $tableDiff = new TableDiff('mytable');
861
        $tableDiff->fromTable = new Table('mytable');
862
        $tableDiff->fromTable->addColumn('id', 'integer');
863
        $tableDiff->fromTable->setPrimaryKey(array('id'));
864
        $tableDiff->renamedIndexes = array(
865
            'idx_foo' => new Index('idx_bar', array('id'))
866
        );
867
868
        self::assertSame(
869
            $this->getAlterTableRenameIndexSQL(),
870
            $this->_platform->getAlterTableSQL($tableDiff)
871
        );
872
    }
873
874
    /**
875
     * @group DBAL-234
876
     */
877
    protected function getAlterTableRenameIndexSQL()
878
    {
879
        return array(
880
            'DROP INDEX idx_foo',
881
            'CREATE INDEX idx_bar ON mytable (id)',
882
        );
883
    }
884
885
    /**
886
     * @group DBAL-234
887
     */
888 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...
889
    {
890
        $tableDiff = new TableDiff('table');
891
        $tableDiff->fromTable = new Table('table');
892
        $tableDiff->fromTable->addColumn('id', 'integer');
893
        $tableDiff->fromTable->setPrimaryKey(array('id'));
894
        $tableDiff->renamedIndexes = array(
895
            'create' => new Index('select', array('id')),
896
            '`foo`'  => new Index('`bar`', array('id')),
897
        );
898
899
        self::assertSame(
900
            $this->getQuotedAlterTableRenameIndexSQL(),
901
            $this->_platform->getAlterTableSQL($tableDiff)
902
        );
903
    }
904
905
    /**
906
     * @group DBAL-234
907
     */
908
    protected function getQuotedAlterTableRenameIndexSQL()
909
    {
910
        return array(
911
            'DROP INDEX "create"',
912
            'CREATE INDEX "select" ON "table" (id)',
913
            'DROP INDEX "foo"',
914
            'CREATE INDEX "bar" ON "table" (id)',
915
        );
916
    }
917
918
    /**
919
     * @group DBAL-835
920
     */
921
    public function testQuotesAlterTableRenameColumn()
922
    {
923
        $fromTable = new Table('mytable');
924
925
        $fromTable->addColumn('unquoted1', 'integer', array('comment' => 'Unquoted 1'));
926
        $fromTable->addColumn('unquoted2', 'integer', array('comment' => 'Unquoted 2'));
927
        $fromTable->addColumn('unquoted3', 'integer', array('comment' => 'Unquoted 3'));
928
929
        $fromTable->addColumn('create', 'integer', array('comment' => 'Reserved keyword 1'));
930
        $fromTable->addColumn('table', 'integer', array('comment' => 'Reserved keyword 2'));
931
        $fromTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword 3'));
932
933
        $fromTable->addColumn('`quoted1`', 'integer', array('comment' => 'Quoted 1'));
934
        $fromTable->addColumn('`quoted2`', 'integer', array('comment' => 'Quoted 2'));
935
        $fromTable->addColumn('`quoted3`', 'integer', array('comment' => 'Quoted 3'));
936
937
        $toTable = new Table('mytable');
938
939
        $toTable->addColumn('unquoted', 'integer', array('comment' => 'Unquoted 1')); // unquoted -> unquoted
940
        $toTable->addColumn('where', 'integer', array('comment' => 'Unquoted 2')); // unquoted -> reserved keyword
941
        $toTable->addColumn('`foo`', 'integer', array('comment' => 'Unquoted 3')); // unquoted -> quoted
942
943
        $toTable->addColumn('reserved_keyword', 'integer', array('comment' => 'Reserved keyword 1')); // reserved keyword -> unquoted
944
        $toTable->addColumn('from', 'integer', array('comment' => 'Reserved keyword 2')); // reserved keyword -> reserved keyword
945
        $toTable->addColumn('`bar`', 'integer', array('comment' => 'Reserved keyword 3')); // reserved keyword -> quoted
946
947
        $toTable->addColumn('quoted', 'integer', array('comment' => 'Quoted 1')); // quoted -> unquoted
948
        $toTable->addColumn('and', 'integer', array('comment' => 'Quoted 2')); // quoted -> reserved keyword
949
        $toTable->addColumn('`baz`', 'integer', array('comment' => 'Quoted 3')); // quoted -> quoted
950
951
        $comparator = new Comparator();
952
953
        self::assertEquals(
954
            $this->getQuotedAlterTableRenameColumnSQL(),
955
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
956
        );
957
    }
958
959
    /**
960
     * Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
961
     *
962
     * @return array
963
     *
964
     * @group DBAL-835
965
     */
966
    abstract protected function getQuotedAlterTableRenameColumnSQL();
967
968
    /**
969
     * @group DBAL-835
970
     */
971
    public function testQuotesAlterTableChangeColumnLength()
972
    {
973
        $fromTable = new Table('mytable');
974
975
        $fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
976
        $fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
977
        $fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));
978
979
        $fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
980
        $fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
981
        $fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));
982
983
        $toTable = new Table('mytable');
984
985
        $toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
986
        $toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
987
        $toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));
988
989
        $toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
990
        $toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
991
        $toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));
992
993
        $comparator = new Comparator();
994
995
        self::assertEquals(
996
            $this->getQuotedAlterTableChangeColumnLengthSQL(),
997
            $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
998
        );
999
    }
1000
1001
    /**
1002
     * Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
1003
     *
1004
     * @return array
1005
     *
1006
     * @group DBAL-835
1007
     */
1008
    abstract protected function getQuotedAlterTableChangeColumnLengthSQL();
1009
1010
    /**
1011
     * @group DBAL-807
1012
     */
1013 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...
1014
    {
1015
        $tableDiff = new TableDiff('myschema.mytable');
1016
        $tableDiff->fromTable = new Table('myschema.mytable');
1017
        $tableDiff->fromTable->addColumn('id', 'integer');
1018
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1019
        $tableDiff->renamedIndexes = array(
1020
            'idx_foo' => new Index('idx_bar', array('id'))
1021
        );
1022
1023
        self::assertSame(
1024
            $this->getAlterTableRenameIndexInSchemaSQL(),
1025
            $this->_platform->getAlterTableSQL($tableDiff)
1026
        );
1027
    }
1028
1029
    /**
1030
     * @group DBAL-807
1031
     */
1032
    protected function getAlterTableRenameIndexInSchemaSQL()
1033
    {
1034
        return array(
1035
            'DROP INDEX idx_foo',
1036
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
1037
        );
1038
    }
1039
1040
    /**
1041
     * @group DBAL-807
1042
     */
1043 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...
1044
    {
1045
        $tableDiff = new TableDiff('`schema`.table');
1046
        $tableDiff->fromTable = new Table('`schema`.table');
1047
        $tableDiff->fromTable->addColumn('id', 'integer');
1048
        $tableDiff->fromTable->setPrimaryKey(array('id'));
1049
        $tableDiff->renamedIndexes = array(
1050
            'create' => new Index('select', array('id')),
1051
            '`foo`'  => new Index('`bar`', array('id')),
1052
        );
1053
1054
        self::assertSame(
1055
            $this->getQuotedAlterTableRenameIndexInSchemaSQL(),
1056
            $this->_platform->getAlterTableSQL($tableDiff)
1057
        );
1058
    }
1059
1060
    /**
1061
     * @group DBAL-234
1062
     */
1063
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
1064
    {
1065
        return array(
1066
            'DROP INDEX "schema"."create"',
1067
            'CREATE INDEX "select" ON "schema"."table" (id)',
1068
            'DROP INDEX "schema"."foo"',
1069
            'CREATE INDEX "bar" ON "schema"."table" (id)',
1070
        );
1071
    }
1072
1073
    /**
1074
     * @group DBAL-1237
1075
     */
1076
    public function testQuotesDropForeignKeySQL()
1077
    {
1078
        if (! $this->_platform->supportsForeignKeyConstraints()) {
1079
            $this->markTestSkipped(
1080
                sprintf('%s does not support foreign key constraints.', get_class($this->_platform))
1081
            );
1082
        }
1083
1084
        $tableName = 'table';
1085
        $table = new Table($tableName);
1086
        $foreignKeyName = 'select';
1087
        $foreignKey = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1088
        $expectedSql = $this->getQuotesDropForeignKeySQL();
1089
1090
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKeyName, $tableName));
1091
        self::assertSame($expectedSql, $this->_platform->getDropForeignKeySQL($foreignKey, $table));
1092
    }
1093
1094
    protected function getQuotesDropForeignKeySQL()
1095
    {
1096
        return 'ALTER TABLE "table" DROP FOREIGN KEY "select"';
1097
    }
1098
1099
    /**
1100
     * @group DBAL-1237
1101
     */
1102
    public function testQuotesDropConstraintSQL()
1103
    {
1104
        $tableName = 'table';
1105
        $table = new Table($tableName);
1106
        $constraintName = 'select';
1107
        $constraint = new ForeignKeyConstraint(array(), 'foo', array(), 'select');
1108
        $expectedSql = $this->getQuotesDropConstraintSQL();
1109
1110
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraintName, $tableName));
1111
        self::assertSame($expectedSql, $this->_platform->getDropConstraintSQL($constraint, $table));
1112
    }
1113
1114
    protected function getQuotesDropConstraintSQL()
1115
    {
1116
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
1117
    }
1118
1119
    protected function getStringLiteralQuoteCharacter()
1120
    {
1121
        return "'";
1122
    }
1123
1124
    public function testGetStringLiteralQuoteCharacter()
1125
    {
1126
        self::assertSame($this->getStringLiteralQuoteCharacter(), $this->_platform->getStringLiteralQuoteCharacter());
1127
    }
1128
1129
    protected function getQuotedCommentOnColumnSQLWithoutQuoteCharacter()
1130
    {
1131
        return "COMMENT ON COLUMN mytable.id IS 'This is a comment'";
1132
    }
1133
1134
    public function testGetCommentOnColumnSQLWithoutQuoteCharacter()
1135
    {
1136
        self::assertEquals(
1137
            $this->getQuotedCommentOnColumnSQLWithoutQuoteCharacter(),
1138
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', 'This is a comment')
1139
        );
1140
    }
1141
1142
    protected function getQuotedCommentOnColumnSQLWithQuoteCharacter()
1143
    {
1144
        return "COMMENT ON COLUMN mytable.id IS 'It''s a quote !'";
1145
    }
1146
1147
    public function testGetCommentOnColumnSQLWithQuoteCharacter()
1148
    {
1149
        $c = $this->getStringLiteralQuoteCharacter();
1150
1151
        self::assertEquals(
1152
            $this->getQuotedCommentOnColumnSQLWithQuoteCharacter(),
1153
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', "It" . $c . "s a quote !")
1154
        );
1155
    }
1156
1157
    /**
1158
     * @return array
1159
     *
1160
     * @see testGetCommentOnColumnSQL
1161
     */
1162
    abstract protected function getCommentOnColumnSQL();
1163
1164
    /**
1165
     * @group DBAL-1004
1166
     */
1167
    public function testGetCommentOnColumnSQL()
1168
    {
1169
        self::assertSame(
1170
            $this->getCommentOnColumnSQL(),
1171
            array(
1172
                $this->_platform->getCommentOnColumnSQL('foo', 'bar', 'comment'), // regular identifiers
1173
                $this->_platform->getCommentOnColumnSQL('`Foo`', '`BAR`', 'comment'), // explicitly quoted identifiers
1174
                $this->_platform->getCommentOnColumnSQL('select', 'from', 'comment'), // reserved keyword identifiers
1175
            )
1176
        );
1177
    }
1178
1179
    /**
1180
     * @group DBAL-1176
1181
     *
1182
     * @dataProvider getGeneratesInlineColumnCommentSQL
1183
     */
1184
    public function testGeneratesInlineColumnCommentSQL($comment, $expectedSql)
1185
    {
1186
        if (! $this->_platform->supportsInlineColumnComments()) {
1187
            $this->markTestSkipped(sprintf('%s does not support inline column comments.', get_class($this->_platform)));
1188
        }
1189
1190
        self::assertSame($expectedSql, $this->_platform->getInlineColumnCommentSQL($comment));
1191
    }
1192
1193
    public function getGeneratesInlineColumnCommentSQL()
1194
    {
1195
        return array(
1196
            'regular comment' => array('Regular comment', $this->getInlineColumnRegularCommentSQL()),
1197
            'comment requiring escaping' => array(
1198
                sprintf(
1199
                    'Using inline comment delimiter %s works',
1200
                    $this->getInlineColumnCommentDelimiter()
1201
                ),
1202
                $this->getInlineColumnCommentRequiringEscapingSQL()
1203
            ),
1204
            'empty comment' => array('', $this->getInlineColumnEmptyCommentSQL()),
1205
        );
1206
    }
1207
1208
    protected function getInlineColumnCommentDelimiter()
1209
    {
1210
        return "'";
1211
    }
1212
1213
    protected function getInlineColumnRegularCommentSQL()
1214
    {
1215
        return "COMMENT 'Regular comment'";
1216
    }
1217
1218
    protected function getInlineColumnCommentRequiringEscapingSQL()
1219
    {
1220
        return "COMMENT 'Using inline comment delimiter '' works'";
1221
    }
1222
1223
    protected function getInlineColumnEmptyCommentSQL()
1224
    {
1225
        return "COMMENT ''";
1226
    }
1227
1228
    protected function getQuotedStringLiteralWithoutQuoteCharacter()
1229
    {
1230
        return "'No quote'";
1231
    }
1232
1233
    protected function getQuotedStringLiteralWithQuoteCharacter()
1234
    {
1235
        return "'It''s a quote'";
1236
    }
1237
1238
    protected function getQuotedStringLiteralQuoteCharacter()
1239
    {
1240
        return "''''";
1241
    }
1242
1243
    /**
1244
     * @group DBAL-1176
1245
     */
1246
    public function testThrowsExceptionOnGeneratingInlineColumnCommentSQLIfUnsupported()
1247
    {
1248
        if ($this->_platform->supportsInlineColumnComments()) {
1249
            $this->markTestSkipped(sprintf('%s supports inline column comments.', get_class($this->_platform)));
1250
        }
1251
1252
        $this->expectException(
1253
            'Doctrine\DBAL\DBALException',
1254
            "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

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