Completed
Pull Request — master (#2667)
by Adam
14:43
created

testNotDuplicateDropForeignKeySql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
use Doctrine\DBAL\Schema\Index;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Schema\TableDiff;
12
use Doctrine\DBAL\TransactionIsolationLevel;
13
use Doctrine\DBAL\Types\Type;
14
use function array_shift;
15
16
abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
17
{
18
    public function testModifyLimitQueryWitoutLimit()
19
    {
20
        $sql = $this->_platform->modifyLimitQuery('SELECT n FROM Foo', null , 10);
21
        self::assertEquals('SELECT n FROM Foo LIMIT 18446744073709551615 OFFSET 10',$sql);
22
    }
23
24
    public function testGenerateMixedCaseTableCreate()
25
    {
26
        $table = new Table("Foo");
27
        $table->addColumn("Bar", "integer");
28
29
        $sql = $this->_platform->getCreateTableSQL($table);
30
        self::assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', array_shift($sql));
31
    }
32
33
    public function getGenerateTableSql()
34
    {
35
        return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB';
36
    }
37
38
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
39
    {
40
        return array(
41
            'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
42
        );
43
    }
44
45
    public function getGenerateAlterTableSql()
46
    {
47
        return array(
48
            "ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL, CHANGE bloo bloo TINYINT(1) DEFAULT '0' NOT NULL"
49
        );
50
    }
51
52
    public function testGeneratesSqlSnippets()
53
    {
54
        self::assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
55
        self::assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
56
        self::assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
57
    }
58
59
    public function testGeneratesTransactionsCommands()
60
    {
61
        self::assertEquals(
62
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
63
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_UNCOMMITTED),
64
            ''
65
        );
66
        self::assertEquals(
67
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
68
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED)
69
        );
70
        self::assertEquals(
71
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
72
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
73
        );
74
        self::assertEquals(
75
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
76
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
77
        );
78
    }
79
80
81
    public function testGeneratesDDLSnippets()
82
    {
83
        self::assertEquals('SHOW DATABASES', $this->_platform->getListDatabasesSQL());
84
        self::assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
85
        self::assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
86
        self::assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
87
    }
88
89
    public function testGeneratesTypeDeclarationForIntegers()
90
    {
91
        self::assertEquals(
92
            'INT',
93
            $this->_platform->getIntegerTypeDeclarationSQL(array())
94
        );
95
        self::assertEquals(
96
            'INT AUTO_INCREMENT',
97
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
98
        ));
99
        self::assertEquals(
100
            'INT AUTO_INCREMENT',
101
            $this->_platform->getIntegerTypeDeclarationSQL(
102
                array('autoincrement' => true, 'primary' => true)
103
        ));
104
    }
105
106
    public function testGeneratesTypeDeclarationForStrings()
107
    {
108
        self::assertEquals(
109
            'CHAR(10)',
110
            $this->_platform->getVarcharTypeDeclarationSQL(
111
                array('length' => 10, 'fixed' => true)
112
        ));
113
        self::assertEquals(
114
            'VARCHAR(50)',
115
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
116
            'Variable string declaration is not correct'
117
        );
118
        self::assertEquals(
119
            'VARCHAR(255)',
120
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
121
            'Long string declaration is not correct'
122
        );
123
    }
124
125
    public function testPrefersIdentityColumns()
126
    {
127
        self::assertTrue($this->_platform->prefersIdentityColumns());
128
    }
129
130
    public function testSupportsIdentityColumns()
131
    {
132
        self::assertTrue($this->_platform->supportsIdentityColumns());
133
    }
134
135
    public function testDoesSupportSavePoints()
136
    {
137
        self::assertTrue($this->_platform->supportsSavepoints());
138
    }
139
140
    public function getGenerateIndexSql()
141
    {
142
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
143
    }
144
145
    public function getGenerateUniqueIndexSql()
146
    {
147
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
148
    }
149
150
    public function getGenerateForeignKeySql()
151
    {
152
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
153
    }
154
155
    /**
156
     * @group DBAL-126
157
     */
158
    public function testUniquePrimaryKey()
159
    {
160
        $keyTable = new Table("foo");
161
        $keyTable->addColumn("bar", "integer");
162
        $keyTable->addColumn("baz", "string");
163
        $keyTable->setPrimaryKey(array("bar"));
164
        $keyTable->addUniqueIndex(array("baz"));
165
166
        $oldTable = new Table("foo");
167
        $oldTable->addColumn("bar", "integer");
168
        $oldTable->addColumn("baz", "string");
169
170
        $c = new \Doctrine\DBAL\Schema\Comparator;
171
        $diff = $c->diffTable($oldTable, $keyTable);
172
173
        $sql = $this->_platform->getAlterTableSQL($diff);
0 ignored issues
show
Bug introduced by
It seems like $diff can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

173
        $sql = $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $diff);
Loading history...
174
175
        self::assertEquals(array(
176
            "ALTER TABLE foo ADD PRIMARY KEY (bar)",
177
            "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
178
        ), $sql);
179
    }
180
181
    public function testModifyLimitQuery()
182
    {
183
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
184
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
185
    }
186
187
    public function testModifyLimitQueryWithEmptyOffset()
188
    {
189
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
190
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
191
    }
192
193
    /**
194
     * @group DDC-118
195
     */
196
    public function testGetDateTimeTypeDeclarationSql()
197
    {
198
        self::assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
199
        self::assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
200
        self::assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
201
    }
202
203
    public function getCreateTableColumnCommentsSQL()
204
    {
205
        return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
206
    }
207
208
    public function getAlterTableColumnCommentsSQL()
209
    {
210
        return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE foo foo VARCHAR(255) NOT NULL, CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
211
    }
212
213
    public function getCreateTableColumnTypeCommentsSQL()
214
    {
215
        return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
216
    }
217
218
    /**
219
     * @group DBAL-237
220
     */
221
    public function testChangeIndexWithForeignKeys()
222
    {
223
        $index = new Index("idx", array("col"), false);
224
        $unique = new Index("uniq", array("col"), true);
225
226
        $diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
227
        $sql = $this->_platform->getAlterTableSQL($diff);
228
        self::assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);
229
230
        $diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
231
        $sql = $this->_platform->getAlterTableSQL($diff);
232
        self::assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $sql);
233
    }
234
235
    protected function getQuotedColumnInPrimaryKeySQL()
236
    {
237
        return array(
238
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, PRIMARY KEY(`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
239
        );
240
    }
241
242
    protected function getQuotedColumnInIndexSQL()
243
    {
244
        return array(
245
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB (`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
246
        );
247
    }
248
249
    protected function getQuotedNameInIndexSQL()
250
    {
251
        return array(
252
            'CREATE TABLE test (column1 VARCHAR(255) NOT NULL, INDEX `key` (column1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
253
        );
254
    }
255
256
    protected function getQuotedColumnInForeignKeySQL()
257
    {
258
        return array(
259
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL, `bar` VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB',
260
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foreign` (`create`, bar, `foo-bar`)',
261
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES foo (`create`, bar, `foo-bar`)',
262
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foo-bar` (`create`, bar, `foo-bar`)',
263
        );
264
    }
265
266
    public function testCreateTableWithFulltextIndex()
267
    {
268
        $table = new Table('fulltext_table');
269
        $table->addOption('engine', 'MyISAM');
270
        $table->addColumn('text', 'text');
271
        $table->addIndex(array('text'), 'fulltext_text');
272
273
        $index = $table->getIndex('fulltext_text');
274
        $index->addFlag('fulltext');
275
276
        $sql = $this->_platform->getCreateTableSQL($table);
277
        self::assertEquals(array('CREATE TABLE fulltext_table (text LONGTEXT NOT NULL, FULLTEXT INDEX fulltext_text (text)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'), $sql);
278
    }
279
280
    public function testCreateTableWithSpatialIndex()
281
    {
282
        $table = new Table('spatial_table');
283
        $table->addOption('engine', 'MyISAM');
284
        $table->addColumn('point', 'text'); // This should be a point type
285
        $table->addIndex(array('point'), 'spatial_text');
286
287
        $index = $table->getIndex('spatial_text');
288
        $index->addFlag('spatial');
289
290
        $sql = $this->_platform->getCreateTableSQL($table);
291
        self::assertEquals(array('CREATE TABLE spatial_table (point LONGTEXT NOT NULL, SPATIAL INDEX spatial_text (point)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'), $sql);
292
    }
293
294
    public function testClobTypeDeclarationSQL()
295
    {
296
        self::assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 1)));
297
        self::assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 255)));
298
        self::assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 256)));
299
        self::assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65535)));
300
        self::assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65536)));
301
        self::assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777215)));
302
        self::assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777216)));
303
        self::assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array()));
304
    }
305
306
    public function testBlobTypeDeclarationSQL()
307
    {
308
        self::assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 1)));
309
        self::assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 255)));
310
        self::assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 256)));
311
        self::assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65535)));
312
        self::assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65536)));
313
        self::assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777215)));
314
        self::assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
315
        self::assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
316
    }
317
318
    /**
319
     * @group DBAL-400
320
     */
321
    public function testAlterTableAddPrimaryKey()
322
    {
323
        $table = new Table('alter_table_add_pk');
324
        $table->addColumn('id', 'integer');
325
        $table->addColumn('foo', 'integer');
326
        $table->addIndex(array('id'), 'idx_id');
327
328
        $comparator = new Comparator();
329
        $diffTable  = clone $table;
330
331
        $diffTable->dropIndex('idx_id');
332
        $diffTable->setPrimaryKey(array('id'));
333
334
        self::assertEquals(
335
            array('DROP INDEX idx_id ON alter_table_add_pk', 'ALTER TABLE alter_table_add_pk ADD PRIMARY KEY (id)'),
336
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

336
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
337
        );
338
    }
339
340
    /**
341
     * @group DBAL-1132
342
     */
343
    public function testAlterPrimaryKeyWithAutoincrementColumn()
344
    {
345
        $table = new Table("alter_primary_key");
346
        $table->addColumn('id', 'integer', array('autoincrement' => true));
347
        $table->addColumn('foo', 'integer');
348
        $table->setPrimaryKey(array('id'));
349
350
        $comparator = new Comparator();
351
        $diffTable = clone $table;
352
353
        $diffTable->dropPrimaryKey();
354
        $diffTable->setPrimaryKey(array('foo'));
355
356
        self::assertEquals(
357
            array(
358
                'ALTER TABLE alter_primary_key MODIFY id INT NOT NULL',
359
                'ALTER TABLE alter_primary_key DROP PRIMARY KEY',
360
                'ALTER TABLE alter_primary_key ADD PRIMARY KEY (foo)'
361
            ),
362
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

362
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
363
        );
364
    }
365
366
    /**
367
     * @group DBAL-464
368
     */
369
    public function testDropPrimaryKeyWithAutoincrementColumn()
370
    {
371
        $table = new Table("drop_primary_key");
372
        $table->addColumn('id', 'integer', array('autoincrement' => true));
373
        $table->addColumn('foo', 'integer');
374
        $table->addColumn('bar', 'integer');
375
        $table->setPrimaryKey(array('id', 'foo'));
376
377
        $comparator = new Comparator();
378
        $diffTable = clone $table;
379
380
        $diffTable->dropPrimaryKey();
381
382
        self::assertEquals(
383
            array(
384
                'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
385
                'ALTER TABLE drop_primary_key DROP PRIMARY KEY'
386
            ),
387
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

387
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
388
        );
389
    }
390
391
    /**
392
     * @group DBAL-2302
393
     */
394
    public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn()
395
    {
396
        $table = new Table("tbl");
397
        $table->addColumn('id', 'integer', array('autoincrement' => true));
398
        $table->addColumn('foo', 'integer');
399
        $table->addColumn('bar', 'integer');
400
        $table->setPrimaryKey(array('id', 'foo'));
401
402
        $comparator = new Comparator();
403
        $diffTable = clone $table;
404
405
        $diffTable->dropPrimaryKey();
406
        $diffTable->setPrimaryKey(array('id'));
407
408
        self::assertSame(
409
            array(
410
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
411
                'ALTER TABLE tbl DROP PRIMARY KEY',
412
                'ALTER TABLE tbl ADD PRIMARY KEY (id)',
413
            ),
414
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

414
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
415
        );
416
    }
417
418
    /**
419
     * @group DBAL-2302
420
     */
421
    public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn()
422
    {
423
        $table = new Table("tbl");
424
        $table->addColumn('id', 'integer', array('autoincrement' => true));
425
        $table->addColumn('foo', 'integer');
426
        $table->addColumn('bar', 'integer');
427
        $table->setPrimaryKey(array('id'));
428
429
        $comparator = new Comparator();
430
        $diffTable = clone $table;
431
432
        $diffTable->dropPrimaryKey();
433
        $diffTable->setPrimaryKey(array('id', 'foo'));
434
435
        self::assertSame(
436
            array(
437
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
438
                'ALTER TABLE tbl DROP PRIMARY KEY',
439
                'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)',
440
            ),
441
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

441
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
442
        );
443
    }
444
445
    /**
446
     * @group DBAL-586
447
     */
448
    public function testAddAutoIncrementPrimaryKey()
449
    {
450
        $keyTable = new Table("foo");
451
        $keyTable->addColumn("id", "integer", array('autoincrement' => true));
452
        $keyTable->addColumn("baz", "string");
453
        $keyTable->setPrimaryKey(array("id"));
454
455
        $oldTable = new Table("foo");
456
        $oldTable->addColumn("baz", "string");
457
458
        $c = new \Doctrine\DBAL\Schema\Comparator;
459
        $diff = $c->diffTable($oldTable, $keyTable);
460
461
        $sql = $this->_platform->getAlterTableSQL($diff);
0 ignored issues
show
Bug introduced by
It seems like $diff can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

461
        $sql = $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $diff);
Loading history...
462
463
        self::assertEquals(array(
464
            "ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)",
465
        ), $sql);
466
    }
467
468
    public function testNamedPrimaryKey()
469
    {
470
        $diff = new TableDiff('mytable');
471
        $diff->changedIndexes['foo_index'] = new Index('foo_index', array('foo'), true, true);
472
473
        $sql = $this->_platform->getAlterTableSQL($diff);
474
475
        self::assertEquals(array(
476
            "ALTER TABLE mytable DROP PRIMARY KEY",
477
            "ALTER TABLE mytable ADD PRIMARY KEY (foo)",
478
        ), $sql);
479
    }
480
481
    public function testAlterPrimaryKeyWithNewColumn()
482
    {
483
        $table = new Table("yolo");
484
        $table->addColumn('pkc1', 'integer');
485
        $table->addColumn('col_a', 'integer');
486
        $table->setPrimaryKey(array('pkc1'));
487
488
        $comparator = new Comparator();
489
        $diffTable = clone $table;
490
491
        $diffTable->addColumn('pkc2', 'integer');
492
        $diffTable->dropPrimaryKey();
493
        $diffTable->setPrimaryKey(array('pkc1', 'pkc2'));
494
495
        self::assertSame(
496
            array(
497
                'ALTER TABLE yolo DROP PRIMARY KEY',
498
                'ALTER TABLE yolo ADD pkc2 INT NOT NULL',
499
                'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)',
500
            ),
501
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

501
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
502
        );
503
    }
504
505
    public function testInitializesDoctrineTypeMappings()
506
    {
507
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('binary'));
508
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('binary'));
509
510
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varbinary'));
511
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('varbinary'));
512
    }
513
514
    protected function getBinaryMaxLength()
515
    {
516
        return 65535;
517
    }
518
519
    public function testReturnsBinaryTypeDeclarationSQL()
520
    {
521
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
522
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
523
        self::assertSame('VARBINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65535)));
524
525
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
526
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
527
        self::assertSame('BINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65535)));
528
    }
529
530
    /**
531
     * @group legacy
532
     * @expectedDeprecation Binary field length 65536 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
533
     * @expectedDeprecation Binary field length 16777215 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
534
     * @expectedDeprecation Binary field length 16777216 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
535
     */
536
    public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
537
    {
538
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
539
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777215]));
540
        self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777216]));
541
542
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 65536]));
543
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777215]));
544
        self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777216]));
545
    }
546
547
    public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines()
548
    {
549
        $table = new Table("foreign_table");
550
        $table->addColumn('id', 'integer');
551
        $table->addColumn('fk_id', 'integer');
552
        $table->addForeignKeyConstraint('foreign_table', array('fk_id'), array('id'));
553
        $table->setPrimaryKey(array('id'));
554
        $table->addOption('engine', 'MyISAM');
555
556
        self::assertSame(
557
            array('CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'),
558
            $this->_platform->getCreateTableSQL(
559
                $table,
560
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
561
            )
562
        );
563
564
        $table = clone $table;
565
        $table->addOption('engine', 'InnoDB');
566
567
        self::assertSame(
568
            array(
569
                'CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB',
570
                'ALTER TABLE foreign_table ADD CONSTRAINT FK_5690FFE2A57719D0 FOREIGN KEY (fk_id) REFERENCES foreign_table (id)'
571
            ),
572
            $this->_platform->getCreateTableSQL(
573
                $table,
574
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
575
            )
576
        );
577
    }
578
579
    public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines()
580
    {
581
        $table = new Table("foreign_table");
582
        $table->addColumn('id', 'integer');
583
        $table->addColumn('fk_id', 'integer');
584
        $table->addForeignKeyConstraint('foreign_table', array('fk_id'), array('id'));
585
        $table->setPrimaryKey(array('id'));
586
        $table->addOption('engine', 'MyISAM');
587
588
        $addedForeignKeys   = array(new ForeignKeyConstraint(array('fk_id'), 'foo', array('id'), 'fk_add'));
589
        $changedForeignKeys = array(new ForeignKeyConstraint(array('fk_id'), 'bar', array('id'), 'fk_change'));
590
        $removedForeignKeys = array(new ForeignKeyConstraint(array('fk_id'), 'baz', array('id'), 'fk_remove'));
591
592
        $tableDiff = new TableDiff('foreign_table');
593
        $tableDiff->fromTable = $table;
594
        $tableDiff->addedForeignKeys = $addedForeignKeys;
595
        $tableDiff->changedForeignKeys = $changedForeignKeys;
596
        $tableDiff->removedForeignKeys = $removedForeignKeys;
597
598
        self::assertEmpty($this->_platform->getAlterTableSQL($tableDiff));
599
600
        $table->addOption('engine', 'InnoDB');
601
602
        $tableDiff = new TableDiff('foreign_table');
603
        $tableDiff->fromTable = $table;
604
        $tableDiff->addedForeignKeys = $addedForeignKeys;
605
        $tableDiff->changedForeignKeys = $changedForeignKeys;
606
        $tableDiff->removedForeignKeys = $removedForeignKeys;
607
608
        self::assertSame(
609
            array(
610
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_remove',
611
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_change',
612
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_add FOREIGN KEY (fk_id) REFERENCES foo (id)',
613
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_change FOREIGN KEY (fk_id) REFERENCES bar (id)',
614
            ),
615
            $this->_platform->getAlterTableSQL($tableDiff)
616
        );
617
    }
618
619
    /**
620
     * @group DBAL-234
621
     */
622
    protected function getAlterTableRenameIndexSQL()
623
    {
624
        return array(
625
            'DROP INDEX idx_foo ON mytable',
626
            'CREATE INDEX idx_bar ON mytable (id)',
627
        );
628
    }
629
630
    /**
631
     * @group DBAL-234
632
     */
633
    protected function getQuotedAlterTableRenameIndexSQL()
634
    {
635
        return array(
636
            'DROP INDEX `create` ON `table`',
637
            'CREATE INDEX `select` ON `table` (id)',
638
            'DROP INDEX `foo` ON `table`',
639
            'CREATE INDEX `bar` ON `table` (id)',
640
        );
641
    }
642
643
    /**
644
     * @group DBAL-807
645
     */
646
    protected function getAlterTableRenameIndexInSchemaSQL()
647
    {
648
        return array(
649
            'DROP INDEX idx_foo ON myschema.mytable',
650
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
651
        );
652
    }
653
654
    /**
655
     * @group DBAL-807
656
     */
657
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
658
    {
659
        return array(
660
            'DROP INDEX `create` ON `schema`.`table`',
661
            'CREATE INDEX `select` ON `schema`.`table` (id)',
662
            'DROP INDEX `foo` ON `schema`.`table`',
663
            'CREATE INDEX `bar` ON `schema`.`table` (id)',
664
        );
665
    }
666
667
    protected function getQuotesDropForeignKeySQL()
668
    {
669
        return 'ALTER TABLE `table` DROP FOREIGN KEY `select`';
670
    }
671
672
    protected function getQuotesDropConstraintSQL()
673
    {
674
        return 'ALTER TABLE `table` DROP CONSTRAINT `select`';
675
    }
676
677
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
678
    {
679
        $table = new Table("text_blob_default_value");
680
        $table->addColumn('def_text', 'text', array('default' => 'def'));
681
        $table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
682
        $table->addColumn('def_blob', 'blob', array('default' => 'def'));
683
        $table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
684
685
        self::assertSame(
686
            array('CREATE TABLE text_blob_default_value (def_text LONGTEXT NOT NULL, def_text_null LONGTEXT DEFAULT NULL, def_blob LONGBLOB NOT NULL, def_blob_null LONGBLOB DEFAULT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'),
687
            $this->_platform->getCreateTableSQL($table)
688
        );
689
690
        $diffTable = clone $table;
691
        $diffTable->changeColumn('def_text', array('default' => null));
692
        $diffTable->changeColumn('def_text_null', array('default' => null));
693
        $diffTable->changeColumn('def_blob', array('default' => null));
694
        $diffTable->changeColumn('def_blob_null', array('default' => null));
695
696
        $comparator = new Comparator();
697
698
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) can also be of type false; however, parameter $diff of Doctrine\DBAL\Platforms\...orm::getAlterTableSQL() does only seem to accept Doctrine\DBAL\Schema\TableDiff, maybe add an additional type check? ( Ignorable by Annotation )

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

698
        self::assertEmpty($this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable)));
Loading history...
699
    }
700
701
    /**
702
     * {@inheritdoc}
703
     */
704
    protected function getQuotedAlterTableRenameColumnSQL()
705
    {
706
        return array(
707
            "ALTER TABLE mytable " .
708
            "CHANGE unquoted1 unquoted INT NOT NULL COMMENT 'Unquoted 1', " .
709
            "CHANGE unquoted2 `where` INT NOT NULL COMMENT 'Unquoted 2', " .
710
            "CHANGE unquoted3 `foo` INT NOT NULL COMMENT 'Unquoted 3', " .
711
            "CHANGE `create` reserved_keyword INT NOT NULL COMMENT 'Reserved keyword 1', " .
712
            "CHANGE `table` `from` INT NOT NULL COMMENT 'Reserved keyword 2', " .
713
            "CHANGE `select` `bar` INT NOT NULL COMMENT 'Reserved keyword 3', " .
714
            "CHANGE quoted1 quoted INT NOT NULL COMMENT 'Quoted 1', " .
715
            "CHANGE quoted2 `and` INT NOT NULL COMMENT 'Quoted 2', " .
716
            "CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'"
717
        );
718
    }
719
720
    /**
721
     * {@inheritdoc}
722
     */
723
    protected function getQuotedAlterTableChangeColumnLengthSQL()
724
    {
725
        return array(
726
            "ALTER TABLE mytable " .
727
            "CHANGE unquoted1 unquoted1 VARCHAR(255) NOT NULL COMMENT 'Unquoted 1', " .
728
            "CHANGE unquoted2 unquoted2 VARCHAR(255) NOT NULL COMMENT 'Unquoted 2', " .
729
            "CHANGE unquoted3 unquoted3 VARCHAR(255) NOT NULL COMMENT 'Unquoted 3', " .
730
            "CHANGE `create` `create` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 1', " .
731
            "CHANGE `table` `table` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 2', " .
732
            "CHANGE `select` `select` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 3'"
733
        );
734
    }
735
736
    /**
737
     * @group DBAL-423
738
     */
739
    public function testReturnsGuidTypeDeclarationSQL()
740
    {
741
        self::assertSame('CHAR(36)', $this->_platform->getGuidTypeDeclarationSQL(array()));
742
    }
743
744
    /**
745
     * {@inheritdoc}
746
     */
747
    public function getAlterTableRenameColumnSQL()
748
    {
749
        return array(
750
            "ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'",
751
        );
752
    }
753
754
    /**
755
     * {@inheritdoc}
756
     */
757
    protected function getQuotesTableIdentifiersInAlterTableSQL()
758
    {
759
        return array(
760
            'ALTER TABLE `foo` DROP FOREIGN KEY fk1',
761
            'ALTER TABLE `foo` DROP FOREIGN KEY fk2',
762
            'ALTER TABLE `foo` RENAME TO `table`, ADD bloo INT NOT NULL, DROP baz, CHANGE bar bar INT DEFAULT NULL, ' .
763
            'CHANGE id war INT NOT NULL',
764
            'ALTER TABLE `table` ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
765
            'ALTER TABLE `table` ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
766
        );
767
    }
768
769
    /**
770
     * {@inheritdoc}
771
     */
772
    protected function getCommentOnColumnSQL()
773
    {
774
        return array(
775
            "COMMENT ON COLUMN foo.bar IS 'comment'",
776
            "COMMENT ON COLUMN `Foo`.`BAR` IS 'comment'",
777
            "COMMENT ON COLUMN `select`.`from` IS 'comment'",
778
        );
779
    }
780
781
    /**
782
     * {@inheritdoc}
783
     */
784
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
785
    {
786
        return 'CONSTRAINT `select` UNIQUE (foo)';
787
    }
788
789
    /**
790
     * {@inheritdoc}
791
     */
792
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
793
    {
794
        return 'INDEX `select` (foo)';
795
    }
796
797
    /**
798
     * {@inheritdoc}
799
     */
800
    protected function getQuotesReservedKeywordInTruncateTableSQL()
801
    {
802
        return 'TRUNCATE `select`';
803
    }
804
805
    /**
806
     * {@inheritdoc}
807
     */
808
    protected function getAlterStringToFixedStringSQL()
809
    {
810
        return array(
811
            'ALTER TABLE mytable CHANGE name name CHAR(2) NOT NULL',
812
        );
813
    }
814
815
    /**
816
     * {@inheritdoc}
817
     */
818
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
819
    {
820
        return array(
821
            'ALTER TABLE mytable DROP FOREIGN KEY fk_foo',
822
            'DROP INDEX idx_foo ON mytable',
823
            'CREATE INDEX idx_foo_renamed ON mytable (foo)',
824
            'ALTER TABLE mytable ADD CONSTRAINT fk_foo FOREIGN KEY (foo) REFERENCES foreign_table (id)',
825
        );
826
    }
827
828
    /**
829
     * {@inheritdoc}
830
     */
831
    public function getGeneratesDecimalTypeDeclarationSQL()
832
    {
833
        return array(
834
            array(array(), 'NUMERIC(10, 0)'),
835
            array(array('unsigned' => true), 'NUMERIC(10, 0) UNSIGNED'),
836
            array(array('unsigned' => false), 'NUMERIC(10, 0)'),
837
            array(array('precision' => 5), 'NUMERIC(5, 0)'),
838
            array(array('scale' => 5), 'NUMERIC(10, 5)'),
839
            array(array('precision' => 8, 'scale' => 2), 'NUMERIC(8, 2)'),
840
        );
841
    }
842
843
    /**
844
     * {@inheritdoc}
845
     */
846
    public function getGeneratesFloatDeclarationSQL()
847
    {
848
        return array(
849
            array(array(), 'DOUBLE PRECISION'),
850
            array(array('unsigned' => true), 'DOUBLE PRECISION UNSIGNED'),
851
            array(array('unsigned' => false), 'DOUBLE PRECISION'),
852
            array(array('precision' => 5), 'DOUBLE PRECISION'),
853
            array(array('scale' => 5), 'DOUBLE PRECISION'),
854
            array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
855
        );
856
    }
857
858
    /**
859
     * @group DBAL-2436
860
     */
861
    public function testQuotesTableNameInListTableIndexesSQL()
862
    {
863
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\", 'foo_db'), '', true);
864
    }
865
866
    /**
867
     * @group DBAL-2436
868
     */
869
    public function testQuotesDatabaseNameInListTableIndexesSQL()
870
    {
871
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL('foo_table', "Foo'Bar\\"), '', true);
872
    }
873
874
    /**
875
     * @group DBAL-2436
876
     */
877
    public function testQuotesDatabaseNameInListViewsSQL()
878
    {
879
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListViewsSQL("Foo'Bar\\"), '', true);
880
    }
881
882
    /**
883
     * @group DBAL-2436
884
     */
885
    public function testQuotesTableNameInListTableForeignKeysSQL()
886
    {
887
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
888
    }
889
890
    /**
891
     * @group DBAL-2436
892
     */
893
    public function testQuotesDatabaseNameInListTableForeignKeysSQL()
894
    {
895
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL('foo_table', "Foo'Bar\\"), '', true);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Platforms\...stTableForeignKeysSQL() has too many arguments starting with 'Foo'Bar\'. ( Ignorable by Annotation )

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

895
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->/** @scrutinizer ignore-call */ getListTableForeignKeysSQL('foo_table', "Foo'Bar\\"), '', true);

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...
896
    }
897
898
    /**
899
     * @group DBAL-2436
900
     */
901
    public function testQuotesTableNameInListTableColumnsSQL()
902
    {
903
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
904
    }
905
906
    /**
907
     * @group DBAL-2436
908
     */
909
    public function testQuotesDatabaseNameInListTableColumnsSQL()
910
    {
911
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
912
    }
913
914
    public function testListTableForeignKeysSQLEvaluatesDatabase()
915
    {
916
        $sql = $this->_platform->getListTableForeignKeysSQL('foo');
917
918
        self::assertContains('DATABASE()', $sql);
919
920
        $sql = $this->_platform->getListTableForeignKeysSQL('foo', 'bar');
921
922
        self::assertContains('bar', $sql);
923
        self::assertNotContains('DATABASE()', $sql);
924
    }
925
926
    public function testSupportsColumnCollation() : void
927
    {
928
        self::assertTrue($this->_platform->supportsColumnCollation());
929
    }
930
931
    public function testColumnCollationDeclarationSQL() : void
932
    {
933
        self::assertSame(
934
            'COLLATE ascii_general_ci',
935
            $this->_platform->getColumnCollationDeclarationSQL('ascii_general_ci')
936
        );
937
    }
938
939
    public function testGetCreateTableSQLWithColumnCollation() : void
940
    {
941
        $table = new Table('foo');
942
        $table->addColumn('no_collation', 'string');
943
        $table->addColumn('column_collation', 'string')->setPlatformOption('collation', 'ascii_general_ci');
944
945
        self::assertSame(
946
            ['CREATE TABLE foo (no_collation VARCHAR(255) NOT NULL, column_collation VARCHAR(255) NOT NULL COLLATE ascii_general_ci) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'],
947
            $this->_platform->getCreateTableSQL($table),
948
            'Column "no_collation" will use the default collation from the table/database and "column_collation" overwrites the collation on this column'
949
        );
950
    }
951
952
    /**
953
     * @group DBAL-2576
954
     */
955
    protected function createNotDuplicateDropForeignKeySql()
956
    {
957
        $diff = new TableDiff('documents');
958
959
        // Create offices table
960
        $offices = new Table('offices');
961
        $offices->addColumn('id', 'integer');
962
963
        // Create documents table
964
        $documents = new Table('documents');
965
        $documents->addColumn('id', 'integer');
966
        $documents->addColumn('office_id', 'integer');
967
        $documents->addForeignKeyConstraint('offices', [ 'office_id' ], [ 'id' ], [ 'onDelete' => 'CASCADE' ], 'FK_a788692ffa0c224');
968
        $documents->setPrimaryKey([ 'id' ]);
969
970
        // Added foreign key
971
        $diff->addedForeignKeys['FK_a788692ffa0c224'] = $documents->getForeignKey('FK_a788692ffa0c224');
972
973
        // Set removed foreign keys
974
        $documents = new Table('documents');
975
        $documents->addColumn('id', 'integer');
976
        $documents->addColumn('office_id', 'integer');
977
        $documents->setPrimaryKey([ 'id' ], 'PRIMARY');
978
        $documents->addIndex([ 'office_id' ], 'office_id');
979
        $documents->addForeignKeyConstraint('offices', [ 'office_id' ], [ 'id' ], [ 'onDelete' => 'RESTRICT', 'onUpdate' => 'RESTRICT' ], 'documents_ibfk_1');
980
981
        $diff->removedForeignKeys['documents_ibfk_1'] = $documents->getForeignKey('documents_ibfk_1');
982
983
        $diff->renamedIndexes = [
984
            'office_id' => new Index('IDX_A788692FFA0C224', [ 'office_id' ])
985
        ];
986
987
        // Set fromTable
988
        $documents = new Table('documents', [
989
            new Column('id', Type::getType('integer')),
990
            new Column('office_id', Type::getType('integer')),
991
        ], [
992
            'primary'        => new Index('PRIMARY', [ 'id' ], true, true),
993
            'office_id'      => new Index('office_id', [ 'office_id' ]),
994
        ], [
995
            'jos_documents_ibfk_1' => new ForeignKeyConstraint([ 'office_id' ], 'offices', [ 'id' ], 'documents_ibfk_1', [ 'onDelete' => 'RESTRICT', 'onUpdate' => 'RESTRICT' ])
996
        ]);
997
998
        $diff->fromTable = $documents;
999
1000
        // Return diff
1001
        return $diff;
1002
    }
1003
1004
    /**
1005
     * @group DBAL-2576
1006
     */
1007
    public function testNotDuplicateDropForeignKeySql()
1008
    {
1009
        $diff = $this->createNotDuplicateDropForeignKeySql();
1010
1011
        // Run through alter table method
1012
        $sql = $this->_platform->getAlterTableSQL($diff);
1013
1014
        // Assert that there are no duplicates in the results
1015
        $this->assertSame(array(
1016
            'ALTER TABLE documents DROP FOREIGN KEY documents_ibfk_1',
1017
            'ALTER TABLE documents ADD CONSTRAINT FK_a788692ffa0c224 FOREIGN KEY (office_id) REFERENCES offices (id) ON DELETE CASCADE',
1018
            'DROP INDEX office_id ON documents',
1019
            'CREATE INDEX IDX_A788692FFA0C224 ON documents (office_id)'
1020
        ), $sql);
1021
    }
1022
}
1023