Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

DBAL/Platforms/AbstractMySQLPlatformTestCase.php (2 issues)

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Schema\Comparator;
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
use Doctrine\DBAL\Schema\Index;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Schema\TableDiff;
11
12
abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
13
{
14
    public function testModifyLimitQueryWitoutLimit()
15
    {
16
        $sql = $this->_platform->modifyLimitQuery('SELECT n FROM Foo', null , 10);
17
        self::assertEquals('SELECT n FROM Foo LIMIT 18446744073709551615 OFFSET 10',$sql);
18
    }
19
20
    public function testGenerateMixedCaseTableCreate()
21
    {
22
        $table = new Table("Foo");
23
        $table->addColumn("Bar", "integer");
24
25
        $sql = $this->_platform->getCreateTableSQL($table);
26
        self::assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', array_shift($sql));
27
    }
28
29
    public function getGenerateTableSql()
30
    {
31
        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';
32
    }
33
34
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
35
    {
36
        return array(
37
            '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'
38
        );
39
    }
40
41
    public function getGenerateAlterTableSql()
42
    {
43
        return array(
44
            "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"
45
        );
46
    }
47
48
    public function testGeneratesSqlSnippets()
49
    {
50
        self::assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
51
        self::assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
52
        self::assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
53
    }
54
55 View Code Duplication
    public function testGeneratesTransactionsCommands()
56
    {
57
        self::assertEquals(
58
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
59
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
60
            ''
61
        );
62
        self::assertEquals(
63
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
64
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
65
        );
66
        self::assertEquals(
67
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
68
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
69
        );
70
        self::assertEquals(
71
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
72
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
73
        );
74
    }
75
76
77 View Code Duplication
    public function testGeneratesDDLSnippets()
78
    {
79
        self::assertEquals('SHOW DATABASES', $this->_platform->getListDatabasesSQL());
80
        self::assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
81
        self::assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
82
        self::assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
83
    }
84
85 View Code Duplication
    public function testGeneratesTypeDeclarationForIntegers()
86
    {
87
        self::assertEquals(
88
            'INT',
89
            $this->_platform->getIntegerTypeDeclarationSQL(array())
90
        );
91
        self::assertEquals(
92
            'INT AUTO_INCREMENT',
93
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
94
        ));
95
        self::assertEquals(
96
            'INT AUTO_INCREMENT',
97
            $this->_platform->getIntegerTypeDeclarationSQL(
98
                array('autoincrement' => true, 'primary' => true)
99
        ));
100
    }
101
102 View Code Duplication
    public function testGeneratesTypeDeclarationForStrings()
103
    {
104
        self::assertEquals(
105
            'CHAR(10)',
106
            $this->_platform->getVarcharTypeDeclarationSQL(
107
                array('length' => 10, 'fixed' => true)
108
        ));
109
        self::assertEquals(
110
            'VARCHAR(50)',
111
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
112
            'Variable string declaration is not correct'
113
        );
114
        self::assertEquals(
115
            'VARCHAR(255)',
116
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
117
            'Long string declaration is not correct'
118
        );
119
    }
120
121
    public function testPrefersIdentityColumns()
122
    {
123
        self::assertTrue($this->_platform->prefersIdentityColumns());
124
    }
125
126
    public function testSupportsIdentityColumns()
127
    {
128
        self::assertTrue($this->_platform->supportsIdentityColumns());
129
    }
130
131
    public function testDoesSupportSavePoints()
132
    {
133
        self::assertTrue($this->_platform->supportsSavepoints());
134
    }
135
136
    public function getGenerateIndexSql()
137
    {
138
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
139
    }
140
141
    public function getGenerateUniqueIndexSql()
142
    {
143
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
144
    }
145
146
    public function getGenerateForeignKeySql()
147
    {
148
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
149
    }
150
151
    /**
152
     * @group DBAL-126
153
     */
154
    public function testUniquePrimaryKey()
155
    {
156
        $keyTable = new Table("foo");
157
        $keyTable->addColumn("bar", "integer");
158
        $keyTable->addColumn("baz", "string");
159
        $keyTable->setPrimaryKey(array("bar"));
160
        $keyTable->addUniqueIndex(array("baz"));
161
162
        $oldTable = new Table("foo");
163
        $oldTable->addColumn("bar", "integer");
164
        $oldTable->addColumn("baz", "string");
165
166
        $c = new \Doctrine\DBAL\Schema\Comparator;
167
        $diff = $c->diffTable($oldTable, $keyTable);
168
169
        $sql = $this->_platform->getAlterTableSQL($diff);
170
171
        self::assertEquals(array(
172
            "ALTER TABLE foo ADD PRIMARY KEY (bar)",
173
            "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
174
        ), $sql);
175
    }
176
177
    public function testModifyLimitQuery()
178
    {
179
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
180
        self::assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
181
    }
182
183
    public function testModifyLimitQueryWithEmptyOffset()
184
    {
185
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
186
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
187
    }
188
189
    /**
190
     * @group DDC-118
191
     */
192
    public function testGetDateTimeTypeDeclarationSql()
193
    {
194
        self::assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
195
        self::assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
196
        self::assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
197
    }
198
199
    public function getCreateTableColumnCommentsSQL()
200
    {
201
        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");
202
    }
203
204
    public function getAlterTableColumnCommentsSQL()
205
    {
206
        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'");
207
    }
208
209
    public function getCreateTableColumnTypeCommentsSQL()
210
    {
211
        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");
212
    }
213
214
    /**
215
     * @group DBAL-237
216
     */
217
    public function testChangeIndexWithForeignKeys()
218
    {
219
        $index = new Index("idx", array("col"), false);
220
        $unique = new Index("uniq", array("col"), true);
221
222
        $diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
223
        $sql = $this->_platform->getAlterTableSQL($diff);
224
        self::assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);
225
226
        $diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
227
        $sql = $this->_platform->getAlterTableSQL($diff);
228
        self::assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $sql);
229
    }
230
231
    protected function getQuotedColumnInPrimaryKeySQL()
232
    {
233
        return array(
234
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, PRIMARY KEY(`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
235
        );
236
    }
237
238
    protected function getQuotedColumnInIndexSQL()
239
    {
240
        return array(
241
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB (`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
242
        );
243
    }
244
245
    protected function getQuotedNameInIndexSQL()
246
    {
247
        return array(
248
            'CREATE TABLE test (column1 VARCHAR(255) NOT NULL, INDEX `key` (column1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
249
        );
250
    }
251
252
    protected function getQuotedColumnInForeignKeySQL()
253
    {
254
        return array(
255
            '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',
256
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foreign` (`create`, bar, `foo-bar`)',
257
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES foo (`create`, bar, `foo-bar`)',
258
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foo-bar` (`create`, bar, `foo-bar`)',
259
        );
260
    }
261
262 View Code Duplication
    public function testCreateTableWithFulltextIndex()
263
    {
264
        $table = new Table('fulltext_table');
265
        $table->addOption('engine', 'MyISAM');
266
        $table->addColumn('text', 'text');
267
        $table->addIndex(array('text'), 'fulltext_text');
268
269
        $index = $table->getIndex('fulltext_text');
270
        $index->addFlag('fulltext');
271
272
        $sql = $this->_platform->getCreateTableSQL($table);
273
        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);
274
    }
275
276 View Code Duplication
    public function testCreateTableWithSpatialIndex()
277
    {
278
        $table = new Table('spatial_table');
279
        $table->addOption('engine', 'MyISAM');
280
        $table->addColumn('point', 'text'); // This should be a point type
281
        $table->addIndex(array('point'), 'spatial_text');
282
283
        $index = $table->getIndex('spatial_text');
284
        $index->addFlag('spatial');
285
286
        $sql = $this->_platform->getCreateTableSQL($table);
287
        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);
288
    }
289
290 View Code Duplication
    public function testClobTypeDeclarationSQL()
291
    {
292
        self::assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 1)));
293
        self::assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 255)));
294
        self::assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 256)));
295
        self::assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65535)));
296
        self::assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65536)));
297
        self::assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777215)));
298
        self::assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777216)));
299
        self::assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array()));
300
    }
301
302 View Code Duplication
    public function testBlobTypeDeclarationSQL()
303
    {
304
        self::assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 1)));
305
        self::assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 255)));
306
        self::assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 256)));
307
        self::assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65535)));
308
        self::assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65536)));
309
        self::assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777215)));
310
        self::assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
311
        self::assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
312
    }
313
314
    /**
315
     * @group DBAL-400
316
     */
317
    public function testAlterTableAddPrimaryKey()
318
    {
319
        $table = new Table('alter_table_add_pk');
320
        $table->addColumn('id', 'integer');
321
        $table->addColumn('foo', 'integer');
322
        $table->addIndex(array('id'), 'idx_id');
323
324
        $comparator = new Comparator();
325
        $diffTable  = clone $table;
326
327
        $diffTable->dropIndex('idx_id');
328
        $diffTable->setPrimaryKey(array('id'));
329
330
        self::assertEquals(
331
            array('DROP INDEX idx_id ON alter_table_add_pk', 'ALTER TABLE alter_table_add_pk ADD PRIMARY KEY (id)'),
332
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
333
        );
334
    }
335
336
    /**
337
     * @group DBAL-1132
338
     */
339
    public function testAlterPrimaryKeyWithAutoincrementColumn()
340
    {
341
        $table = new Table("alter_primary_key");
342
        $table->addColumn('id', 'integer', array('autoincrement' => true));
343
        $table->addColumn('foo', 'integer');
344
        $table->setPrimaryKey(array('id'));
345
346
        $comparator = new Comparator();
347
        $diffTable = clone $table;
348
349
        $diffTable->dropPrimaryKey();
350
        $diffTable->setPrimaryKey(array('foo'));
351
352
        self::assertEquals(
353
            array(
354
                'ALTER TABLE alter_primary_key MODIFY id INT NOT NULL',
355
                'ALTER TABLE alter_primary_key DROP PRIMARY KEY',
356
                'ALTER TABLE alter_primary_key ADD PRIMARY KEY (foo)'
357
            ),
358
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
359
        );
360
    }
361
362
    /**
363
     * @group DBAL-464
364
     */
365
    public function testDropPrimaryKeyWithAutoincrementColumn()
366
    {
367
        $table = new Table("drop_primary_key");
368
        $table->addColumn('id', 'integer', array('autoincrement' => true));
369
        $table->addColumn('foo', 'integer');
370
        $table->addColumn('bar', 'integer');
371
        $table->setPrimaryKey(array('id', 'foo'));
372
373
        $comparator = new Comparator();
374
        $diffTable = clone $table;
375
376
        $diffTable->dropPrimaryKey();
377
378
        self::assertEquals(
379
            array(
380
                'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
381
                'ALTER TABLE drop_primary_key DROP PRIMARY KEY'
382
            ),
383
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
384
        );
385
    }
386
387
    /**
388
     * @group DBAL-2302
389
     */
390 View Code Duplication
    public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn()
391
    {
392
        $table = new Table("tbl");
393
        $table->addColumn('id', 'integer', array('autoincrement' => true));
394
        $table->addColumn('foo', 'integer');
395
        $table->addColumn('bar', 'integer');
396
        $table->setPrimaryKey(array('id', 'foo'));
397
398
        $comparator = new Comparator();
399
        $diffTable = clone $table;
400
401
        $diffTable->dropPrimaryKey();
402
        $diffTable->setPrimaryKey(array('id'));
403
404
        self::assertSame(
405
            array(
406
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
407
                'ALTER TABLE tbl DROP PRIMARY KEY',
408
                'ALTER TABLE tbl ADD PRIMARY KEY (id)',
409
            ),
410
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
411
        );
412
    }
413
414
    /**
415
     * @group DBAL-2302
416
     */
417 View Code Duplication
    public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn()
418
    {
419
        $table = new Table("tbl");
420
        $table->addColumn('id', 'integer', array('autoincrement' => true));
421
        $table->addColumn('foo', 'integer');
422
        $table->addColumn('bar', 'integer');
423
        $table->setPrimaryKey(array('id'));
424
425
        $comparator = new Comparator();
426
        $diffTable = clone $table;
427
428
        $diffTable->dropPrimaryKey();
429
        $diffTable->setPrimaryKey(array('id', 'foo'));
430
431
        self::assertSame(
432
            array(
433
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
434
                'ALTER TABLE tbl DROP PRIMARY KEY',
435
                'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)',
436
            ),
437
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
438
        );
439
    }
440
441
    /**
442
     * @group DBAL-586
443
     */
444
    public function testAddAutoIncrementPrimaryKey()
445
    {
446
        $keyTable = new Table("foo");
447
        $keyTable->addColumn("id", "integer", array('autoincrement' => true));
448
        $keyTable->addColumn("baz", "string");
449
        $keyTable->setPrimaryKey(array("id"));
450
451
        $oldTable = new Table("foo");
452
        $oldTable->addColumn("baz", "string");
453
454
        $c = new \Doctrine\DBAL\Schema\Comparator;
455
        $diff = $c->diffTable($oldTable, $keyTable);
456
457
        $sql = $this->_platform->getAlterTableSQL($diff);
458
459
        self::assertEquals(array(
460
            "ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)",
461
        ), $sql);
462
    }
463
464
    public function testNamedPrimaryKey()
465
    {
466
        $diff = new TableDiff('mytable');
467
        $diff->changedIndexes['foo_index'] = new Index('foo_index', array('foo'), true, true);
468
469
        $sql = $this->_platform->getAlterTableSQL($diff);
470
471
        self::assertEquals(array(
472
            "ALTER TABLE mytable DROP PRIMARY KEY",
473
            "ALTER TABLE mytable ADD PRIMARY KEY (foo)",
474
        ), $sql);
475
    }
476
    
477 View Code Duplication
    public function testAlterPrimaryKeyWithNewColumn()
0 ignored issues
show
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...
478
    {
479
        $table = new Table("yolo");
480
        $table->addColumn('pkc1', 'integer');
481
        $table->addColumn('col_a', 'integer');
482
        $table->setPrimaryKey(array('pkc1'));
483
484
        $comparator = new Comparator();
485
        $diffTable = clone $table;
486
        
487
        $diffTable->addColumn('pkc2', 'integer');
488
        $diffTable->dropPrimaryKey();
489
        $diffTable->setPrimaryKey(array('pkc1', 'pkc2'));
490
491
        self::assertSame(
492
            array(
493
                'ALTER TABLE yolo DROP PRIMARY KEY',
494
                'ALTER TABLE yolo ADD pkc2 INT NOT NULL',
495
                'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)',
496
            ),
497
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
0 ignored issues
show
It seems like $comparator->diffTable($table, $diffTable) can also be of type boolean; 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

497
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table, $diffTable))
Loading history...
498
        );      
499
    }
500
501
    public function testInitializesDoctrineTypeMappings()
502
    {
503
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('binary'));
504
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('binary'));
505
506
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varbinary'));
507
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('varbinary'));
508
    }
509
510
    protected function getBinaryMaxLength()
511
    {
512
        return 65535;
513
    }
514
515
    public function testReturnsBinaryTypeDeclarationSQL()
516
    {
517
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
518
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
519
        self::assertSame('VARBINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65535)));
520
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65536)));
521
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 16777215)));
522
        self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 16777216)));
523
524
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
525
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
526
        self::assertSame('BINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65535)));
527
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65536)));
528
        self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 16777215)));
529
        self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 16777216)));
530
    }
531
532
    public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines()
533
    {
534
        $table = new Table("foreign_table");
535
        $table->addColumn('id', 'integer');
536
        $table->addColumn('fk_id', 'integer');
537
        $table->addForeignKeyConstraint('foreign_table', array('fk_id'), array('id'));
538
        $table->setPrimaryKey(array('id'));
539
        $table->addOption('engine', 'MyISAM');
540
541
        self::assertSame(
542
            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'),
543
            $this->_platform->getCreateTableSQL(
544
                $table,
545
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
546
            )
547
        );
548
549
        $table = clone $table;
550
        $table->addOption('engine', 'InnoDB');
551
552
        self::assertSame(
553
            array(
554
                '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',
555
                'ALTER TABLE foreign_table ADD CONSTRAINT FK_5690FFE2A57719D0 FOREIGN KEY (fk_id) REFERENCES foreign_table (id)'
556
            ),
557
            $this->_platform->getCreateTableSQL(
558
                $table,
559
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
560
            )
561
        );
562
    }
563
564
    public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines()
565
    {
566
        $table = new Table("foreign_table");
567
        $table->addColumn('id', 'integer');
568
        $table->addColumn('fk_id', 'integer');
569
        $table->addForeignKeyConstraint('foreign_table', array('fk_id'), array('id'));
570
        $table->setPrimaryKey(array('id'));
571
        $table->addOption('engine', 'MyISAM');
572
573
        $addedForeignKeys   = array(new ForeignKeyConstraint(array('fk_id'), 'foo', array('id'), 'fk_add'));
574
        $changedForeignKeys = array(new ForeignKeyConstraint(array('fk_id'), 'bar', array('id'), 'fk_change'));
575
        $removedForeignKeys = array(new ForeignKeyConstraint(array('fk_id'), 'baz', array('id'), 'fk_remove'));
576
577
        $tableDiff = new TableDiff('foreign_table');
578
        $tableDiff->fromTable = $table;
579
        $tableDiff->addedForeignKeys = $addedForeignKeys;
580
        $tableDiff->changedForeignKeys = $changedForeignKeys;
581
        $tableDiff->removedForeignKeys = $removedForeignKeys;
582
583
        self::assertEmpty($this->_platform->getAlterTableSQL($tableDiff));
584
585
        $table->addOption('engine', 'InnoDB');
586
587
        $tableDiff = new TableDiff('foreign_table');
588
        $tableDiff->fromTable = $table;
589
        $tableDiff->addedForeignKeys = $addedForeignKeys;
590
        $tableDiff->changedForeignKeys = $changedForeignKeys;
591
        $tableDiff->removedForeignKeys = $removedForeignKeys;
592
593
        self::assertSame(
594
            array(
595
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_remove',
596
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_change',
597
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_add FOREIGN KEY (fk_id) REFERENCES foo (id)',
598
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_change FOREIGN KEY (fk_id) REFERENCES bar (id)',
599
            ),
600
            $this->_platform->getAlterTableSQL($tableDiff)
601
        );
602
    }
603
604
    /**
605
     * @group DBAL-234
606
     */
607
    protected function getAlterTableRenameIndexSQL()
608
    {
609
        return array(
610
            'DROP INDEX idx_foo ON mytable',
611
            'CREATE INDEX idx_bar ON mytable (id)',
612
        );
613
    }
614
615
    /**
616
     * @group DBAL-234
617
     */
618
    protected function getQuotedAlterTableRenameIndexSQL()
619
    {
620
        return array(
621
            'DROP INDEX `create` ON `table`',
622
            'CREATE INDEX `select` ON `table` (id)',
623
            'DROP INDEX `foo` ON `table`',
624
            'CREATE INDEX `bar` ON `table` (id)',
625
        );
626
    }
627
628
    /**
629
     * @group DBAL-807
630
     */
631
    protected function getAlterTableRenameIndexInSchemaSQL()
632
    {
633
        return array(
634
            'DROP INDEX idx_foo ON myschema.mytable',
635
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
636
        );
637
    }
638
639
    /**
640
     * @group DBAL-807
641
     */
642
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
643
    {
644
        return array(
645
            'DROP INDEX `create` ON `schema`.`table`',
646
            'CREATE INDEX `select` ON `schema`.`table` (id)',
647
            'DROP INDEX `foo` ON `schema`.`table`',
648
            'CREATE INDEX `bar` ON `schema`.`table` (id)',
649
        );
650
    }
651
652
    protected function getQuotesDropForeignKeySQL()
653
    {
654
        return 'ALTER TABLE `table` DROP FOREIGN KEY `select`';
655
    }
656
657
    protected function getQuotesDropConstraintSQL()
658
    {
659
        return 'ALTER TABLE `table` DROP CONSTRAINT `select`';
660
    }
661
662
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
663
    {
664
        $table = new Table("text_blob_default_value");
665
        $table->addColumn('def_text', 'text', array('default' => 'def'));
666
        $table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
667
        $table->addColumn('def_blob', 'blob', array('default' => 'def'));
668
        $table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
669
670
        self::assertSame(
671
            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'),
672
            $this->_platform->getCreateTableSQL($table)
673
        );
674
675
        $diffTable = clone $table;
676
        $diffTable->changeColumn('def_text', array('default' => null));
677
        $diffTable->changeColumn('def_text_null', array('default' => null));
678
        $diffTable->changeColumn('def_blob', array('default' => null));
679
        $diffTable->changeColumn('def_blob_null', array('default' => null));
680
681
        $comparator = new Comparator();
682
683
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
684
    }
685
686
    /**
687
     * {@inheritdoc}
688
     */
689 View Code Duplication
    protected function getQuotedAlterTableRenameColumnSQL()
690
    {
691
        return array(
692
            "ALTER TABLE mytable " .
693
            "CHANGE unquoted1 unquoted INT NOT NULL COMMENT 'Unquoted 1', " .
694
            "CHANGE unquoted2 `where` INT NOT NULL COMMENT 'Unquoted 2', " .
695
            "CHANGE unquoted3 `foo` INT NOT NULL COMMENT 'Unquoted 3', " .
696
            "CHANGE `create` reserved_keyword INT NOT NULL COMMENT 'Reserved keyword 1', " .
697
            "CHANGE `table` `from` INT NOT NULL COMMENT 'Reserved keyword 2', " .
698
            "CHANGE `select` `bar` INT NOT NULL COMMENT 'Reserved keyword 3', " .
699
            "CHANGE quoted1 quoted INT NOT NULL COMMENT 'Quoted 1', " .
700
            "CHANGE quoted2 `and` INT NOT NULL COMMENT 'Quoted 2', " .
701
            "CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'"
702
        );
703
    }
704
705
    /**
706
     * {@inheritdoc}
707
     */
708
    protected function getQuotedAlterTableChangeColumnLengthSQL()
709
    {
710
        return array(
711
            "ALTER TABLE mytable " .
712
            "CHANGE unquoted1 unquoted1 VARCHAR(255) NOT NULL COMMENT 'Unquoted 1', " .
713
            "CHANGE unquoted2 unquoted2 VARCHAR(255) NOT NULL COMMENT 'Unquoted 2', " .
714
            "CHANGE unquoted3 unquoted3 VARCHAR(255) NOT NULL COMMENT 'Unquoted 3', " .
715
            "CHANGE `create` `create` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 1', " .
716
            "CHANGE `table` `table` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 2', " .
717
            "CHANGE `select` `select` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 3'"
718
        );
719
    }
720
721
    /**
722
     * @group DBAL-423
723
     */
724
    public function testReturnsGuidTypeDeclarationSQL()
725
    {
726
        self::assertSame('CHAR(36)', $this->_platform->getGuidTypeDeclarationSQL(array()));
727
    }
728
729
    /**
730
     * {@inheritdoc}
731
     */
732
    public function getAlterTableRenameColumnSQL()
733
    {
734
        return array(
735
            "ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'",
736
        );
737
    }
738
739
    /**
740
     * {@inheritdoc}
741
     */
742
    protected function getQuotesTableIdentifiersInAlterTableSQL()
743
    {
744
        return array(
745
            'ALTER TABLE `foo` DROP FOREIGN KEY fk1',
746
            'ALTER TABLE `foo` DROP FOREIGN KEY fk2',
747
            'ALTER TABLE `foo` RENAME TO `table`, ADD bloo INT NOT NULL, DROP baz, CHANGE bar bar INT DEFAULT NULL, ' .
748
            'CHANGE id war INT NOT NULL',
749
            'ALTER TABLE `table` ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
750
            'ALTER TABLE `table` ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
751
        );
752
    }
753
754
    /**
755
     * {@inheritdoc}
756
     */
757
    protected function getCommentOnColumnSQL()
758
    {
759
        return array(
760
            "COMMENT ON COLUMN foo.bar IS 'comment'",
761
            "COMMENT ON COLUMN `Foo`.`BAR` IS 'comment'",
762
            "COMMENT ON COLUMN `select`.`from` IS 'comment'",
763
        );
764
    }
765
766
    /**
767
     * {@inheritdoc}
768
     */
769
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
770
    {
771
        return 'CONSTRAINT `select` UNIQUE (foo)';
772
    }
773
774
    /**
775
     * {@inheritdoc}
776
     */
777
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
778
    {
779
        return 'INDEX `select` (foo)';
780
    }
781
782
    /**
783
     * {@inheritdoc}
784
     */
785
    protected function getQuotesReservedKeywordInTruncateTableSQL()
786
    {
787
        return 'TRUNCATE `select`';
788
    }
789
790
    /**
791
     * {@inheritdoc}
792
     */
793
    protected function getAlterStringToFixedStringSQL()
794
    {
795
        return array(
796
            'ALTER TABLE mytable CHANGE name name CHAR(2) NOT NULL',
797
        );
798
    }
799
800
    /**
801
     * {@inheritdoc}
802
     */
803
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
804
    {
805
        return array(
806
            'ALTER TABLE mytable DROP FOREIGN KEY fk_foo',
807
            'DROP INDEX idx_foo ON mytable',
808
            'CREATE INDEX idx_foo_renamed ON mytable (foo)',
809
            'ALTER TABLE mytable ADD CONSTRAINT fk_foo FOREIGN KEY (foo) REFERENCES foreign_table (id)',
810
        );
811
    }
812
813
    /**
814
     * {@inheritdoc}
815
     */
816 View Code Duplication
    public function getGeneratesDecimalTypeDeclarationSQL()
817
    {
818
        return array(
819
            array(array(), 'NUMERIC(10, 0)'),
820
            array(array('unsigned' => true), 'NUMERIC(10, 0) UNSIGNED'),
821
            array(array('unsigned' => false), 'NUMERIC(10, 0)'),
822
            array(array('precision' => 5), 'NUMERIC(5, 0)'),
823
            array(array('scale' => 5), 'NUMERIC(10, 5)'),
824
            array(array('precision' => 8, 'scale' => 2), 'NUMERIC(8, 2)'),
825
        );
826
    }
827
828
    /**
829
     * {@inheritdoc}
830
     */
831 View Code Duplication
    public function getGeneratesFloatDeclarationSQL()
832
    {
833
        return array(
834
            array(array(), 'DOUBLE PRECISION'),
835
            array(array('unsigned' => true), 'DOUBLE PRECISION UNSIGNED'),
836
            array(array('unsigned' => false), 'DOUBLE PRECISION'),
837
            array(array('precision' => 5), 'DOUBLE PRECISION'),
838
            array(array('scale' => 5), 'DOUBLE PRECISION'),
839
            array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
840
        );
841
    }
842
843
    /**
844
     * @group DBAL-2436
845
     */
846
    public function testQuotesTableNameInListTableIndexesSQL()
847
    {
848
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\", 'foo_db'), '', true);
849
    }
850
851
    /**
852
     * @group DBAL-2436
853
     */
854
    public function testQuotesDatabaseNameInListTableIndexesSQL()
855
    {
856
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL('foo_table', "Foo'Bar\\"), '', true);
857
    }
858
859
    /**
860
     * @group DBAL-2436
861
     */
862
    public function testQuotesDatabaseNameInListViewsSQL()
863
    {
864
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListViewsSQL("Foo'Bar\\"), '', true);
865
    }
866
867
    /**
868
     * @group DBAL-2436
869
     */
870
    public function testQuotesTableNameInListTableForeignKeysSQL()
871
    {
872
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
873
    }
874
875
    /**
876
     * @group DBAL-2436
877
     */
878
    public function testQuotesDatabaseNameInListTableForeignKeysSQL()
879
    {
880
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL('foo_table', "Foo'Bar\\"), '', true);
881
    }
882
883
    /**
884
     * @group DBAL-2436
885
     */
886
    public function testQuotesTableNameInListTableColumnsSQL()
887
    {
888
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
889
    }
890
891
    /**
892
     * @group DBAL-2436
893
     */
894
    public function testQuotesDatabaseNameInListTableColumnsSQL()
895
    {
896
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
897
    }
898
899
    public function testListTableForeignKeysSQLEvaluatesDatabase()
900
    {
901
        $sql = $this->_platform->getListTableForeignKeysSQL('foo');
902
903
        self::assertContains('DATABASE()', $sql);
904
905
        $sql = $this->_platform->getListTableForeignKeysSQL('foo', 'bar');
906
907
        self::assertContains('bar', $sql);
908
        self::assertNotContains('DATABASE()', $sql);
909
    }
910
}
911