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

getAlterTableRenameForeignKeySQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\Comparator;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\TableDiff;
9
use Doctrine\DBAL\Types\Type;
10
11
abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCase
12
{
13
    public function getGenerateTableSql()
14
    {
15
        return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
16
    }
17
18
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
19
    {
20
        return array(
21
            'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
22
            'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)'
23
        );
24
    }
25
26
    public function getGenerateAlterTableSql()
27
    {
28
        return array(
29
            'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
30
            'ALTER TABLE mytable DROP foo',
31
            'ALTER TABLE mytable ALTER bar TYPE VARCHAR(255)',
32
            "ALTER TABLE mytable ALTER bar SET DEFAULT 'def'",
33
            'ALTER TABLE mytable ALTER bar SET NOT NULL',
34
            'ALTER TABLE mytable ALTER bloo TYPE BOOLEAN',
35
            "ALTER TABLE mytable ALTER bloo SET DEFAULT 'false'",
36
            'ALTER TABLE mytable ALTER bloo SET NOT NULL',
37
            'ALTER TABLE mytable RENAME TO userlist',
38
        );
39
    }
40
41
    public function getGenerateIndexSql()
42
    {
43
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
44
    }
45
46
    public function getGenerateForeignKeySql()
47
    {
48
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id) NOT DEFERRABLE INITIALLY IMMEDIATE';
49
    }
50
51
    public function testGeneratesForeignKeySqlForNonStandardOptions()
52
    {
53
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
54
                array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
55
        );
56
        self::assertEquals(
57
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
58
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
59
        );
60
61
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
62
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('match' => 'full')
63
        );
64
        self::assertEquals(
65
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) MATCH full NOT DEFERRABLE INITIALLY IMMEDIATE",
66
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
67
        );
68
69
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
70
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferrable' => true)
71
        );
72
        self::assertEquals(
73
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) DEFERRABLE INITIALLY IMMEDIATE",
74
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
75
        );
76
77
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
78
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferred' => true)
79
        );
80
        self::assertEquals(
81
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) NOT DEFERRABLE INITIALLY DEFERRED",
82
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
83
        );
84
85
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
86
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('feferred' => true)
87
        );
88
        self::assertEquals(
89
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) NOT DEFERRABLE INITIALLY DEFERRED",
90
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
91
        );
92
93
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
94
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferrable' => true, 'deferred' => true, 'match' => 'full')
95
        );
96
        self::assertEquals(
97
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) MATCH full DEFERRABLE INITIALLY DEFERRED",
98
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
99
        );
100
    }
101
102 View Code Duplication
    public function testGeneratesSqlSnippets()
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...
103
    {
104
        self::assertEquals('SIMILAR TO', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
105
        self::assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
106
        self::assertEquals('column1 || column2 || column3', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
107
        self::assertEquals('SUBSTRING(column FROM 5)', $this->_platform->getSubstringExpression('column', 5), 'Substring expression without length is not correct');
108
        self::assertEquals('SUBSTRING(column FROM 1 FOR 5)', $this->_platform->getSubstringExpression('column', 1, 5), 'Substring expression with length is not correct');
109
    }
110
111 View Code Duplication
    public function testGeneratesTransactionCommands()
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...
112
    {
113
        self::assertEquals(
114
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
115
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
116
        );
117
        self::assertEquals(
118
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
119
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
120
        );
121
        self::assertEquals(
122
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
123
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
124
        );
125
        self::assertEquals(
126
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
127
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
128
        );
129
    }
130
131
    public function testGeneratesDDLSnippets()
132
    {
133
        self::assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
134
        self::assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
135
        self::assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
136
    }
137
138 View Code Duplication
    public function testGenerateTableWithAutoincrement()
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...
139
    {
140
        $table = new \Doctrine\DBAL\Schema\Table('autoinc_table');
141
        $column = $table->addColumn('id', 'integer');
142
        $column->setAutoincrement(true);
143
144
        self::assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
145
    }
146
147
    public static function serialTypes() : array
148
    {
149
        return [
150
            ['integer', 'SERIAL'],
151
            ['bigint', 'BIGSERIAL'],
152
        ];
153
    }
154
155
    /**
156
     * @dataProvider serialTypes
157
     * @group 2906
158
     */
159 View Code Duplication
    public function testGenerateTableWithAutoincrementDoesNotSetDefault(string $type, string $definition) : 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...
160
    {
161
        $table  = new \Doctrine\DBAL\Schema\Table('autoinc_table_notnull');
162
        $column = $table->addColumn('id', $type);
163
        $column->setAutoIncrement(true);
164
        $column->setNotNull(false);
165
166
        $sql = $this->_platform->getCreateTableSQL($table);
167
168
        self::assertEquals(["CREATE TABLE autoinc_table_notnull (id $definition)"], $sql);
169
    }
170
171
    /**
172
     * @dataProvider serialTypes
173
     * @group 2906
174
     */
175 View Code Duplication
    public function testCreateTableWithAutoincrementAndNotNullAddsConstraint(string $type, string $definition) : 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...
176
    {
177
        $table  = new \Doctrine\DBAL\Schema\Table('autoinc_table_notnull_enabled');
178
        $column = $table->addColumn('id', $type);
179
        $column->setAutoIncrement(true);
180
        $column->setNotNull(true);
181
182
        $sql = $this->_platform->getCreateTableSQL($table);
183
184
        self::assertEquals(["CREATE TABLE autoinc_table_notnull_enabled (id $definition NOT NULL)"], $sql);
185
    }
186
187
    /**
188
     * @dataProvider serialTypes
189
     * @group 2906
190
     */
191 View Code Duplication
    public function testGetDefaultValueDeclarationSQLIgnoresTheDefaultKeyWhenTheFieldIsSerial(string $type) : 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...
192
    {
193
        $sql = $this->_platform->getDefaultValueDeclarationSQL(
194
            [
195
                'autoincrement' => true,
196
                'type'          => Type::getType($type),
197
                'default'       => 1,
198
            ]
199
        );
200
201
        self::assertSame('', $sql);
202
    }
203
204 View Code Duplication
    public function testGeneratesTypeDeclarationForIntegers()
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...
205
    {
206
        self::assertEquals(
207
            'INT',
208
            $this->_platform->getIntegerTypeDeclarationSQL(array())
209
        );
210
        self::assertEquals(
211
            'SERIAL',
212
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
213
        ));
214
        self::assertEquals(
215
            'SERIAL',
216
            $this->_platform->getIntegerTypeDeclarationSQL(
217
                array('autoincrement' => true, 'primary' => true)
218
        ));
219
    }
220
221 View Code Duplication
    public function testGeneratesTypeDeclarationForStrings()
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...
222
    {
223
        self::assertEquals(
224
            'CHAR(10)',
225
            $this->_platform->getVarcharTypeDeclarationSQL(
226
                array('length' => 10, 'fixed' => true))
227
        );
228
        self::assertEquals(
229
            'VARCHAR(50)',
230
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
231
            'Variable string declaration is not correct'
232
        );
233
        self::assertEquals(
234
            'VARCHAR(255)',
235
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
236
            'Long string declaration is not correct'
237
        );
238
    }
239
240
    public function getGenerateUniqueIndexSql()
241
    {
242
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
243
    }
244
245 View Code Duplication
    public function testGeneratesSequenceSqlCommands()
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...
246
    {
247
        $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
248
        self::assertEquals(
249
            'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
250
            $this->_platform->getCreateSequenceSQL($sequence)
251
        );
252
        self::assertEquals(
253
            'DROP SEQUENCE myseq CASCADE',
254
            $this->_platform->getDropSequenceSQL('myseq')
255
        );
256
        self::assertEquals(
257
            "SELECT NEXTVAL('myseq')",
258
            $this->_platform->getSequenceNextValSQL('myseq')
259
        );
260
    }
261
262
    public function testDoesNotPreferIdentityColumns()
263
    {
264
        self::assertFalse($this->_platform->prefersIdentityColumns());
265
    }
266
267
    public function testPrefersSequences()
268
    {
269
        self::assertTrue($this->_platform->prefersSequences());
270
    }
271
272
    public function testSupportsIdentityColumns()
273
    {
274
        self::assertTrue($this->_platform->supportsIdentityColumns());
275
    }
276
277
    public function testSupportsSavePoints()
278
    {
279
        self::assertTrue($this->_platform->supportsSavepoints());
280
    }
281
282
    public function testSupportsSequences()
283
    {
284
        self::assertTrue($this->_platform->supportsSequences());
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290
    protected function supportsCommentOnStatement()
291
    {
292
        return true;
293
    }
294
295
    public function testModifyLimitQuery()
296
    {
297
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
298
        self::assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
299
    }
300
301
    public function testModifyLimitQueryWithEmptyOffset()
302
    {
303
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
304
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
305
    }
306
307
    public function getCreateTableColumnCommentsSQL()
308
    {
309
        return array(
310
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
311
            "COMMENT ON COLUMN test.id IS 'This is a comment'",
312
        );
313
    }
314
315
    public function getAlterTableColumnCommentsSQL()
316
    {
317
        return array(
318
            "ALTER TABLE mytable ADD quota INT NOT NULL",
319
            "COMMENT ON COLUMN mytable.quota IS 'A comment'",
320
            "COMMENT ON COLUMN mytable.foo IS NULL",
321
            "COMMENT ON COLUMN mytable.baz IS 'B comment'",
322
        );
323
    }
324
325
    public function getCreateTableColumnTypeCommentsSQL()
326
    {
327
        return array(
328
            "CREATE TABLE test (id INT NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))",
329
            "COMMENT ON COLUMN test.data IS '(DC2Type:array)'"
330
        );
331
    }
332
333
    protected function getQuotedColumnInPrimaryKeySQL()
334
    {
335
        return array(
336
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, PRIMARY KEY("create"))',
337
        );
338
    }
339
340
    protected function getQuotedColumnInIndexSQL()
341
    {
342
        return array(
343
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL)',
344
            'CREATE INDEX IDX_22660D028FD6E0FB ON "quoted" ("create")',
345
        );
346
    }
347
348
    protected function getQuotedNameInIndexSQL()
349
    {
350
        return array(
351
            'CREATE TABLE test (column1 VARCHAR(255) NOT NULL)',
352
            'CREATE INDEX "key" ON test (column1)',
353
        );
354
    }
355
356
    protected function getQuotedColumnInForeignKeySQL()
357
    {
358
        return array(
359
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL, "bar" VARCHAR(255) NOT NULL)',
360
            'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar") REFERENCES "foreign" ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE',
361
            'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar") REFERENCES foo ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE',
362
            'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ("create", foo, "bar") REFERENCES "foo-bar" ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE',
363
        );
364
    }
365
366
    /**
367
     * @group DBAL-457
368
     * @dataProvider pgBooleanProvider
369
     *
370
     * @param string $databaseValue
371
     * @param string $preparedStatementValue
372
     * @param integer $integerValue
373
     * @param boolean $booleanValue
374
     */
375
    public function testConvertBooleanAsLiteralStrings(
376
        $databaseValue,
377
        $preparedStatementValue,
378
        $integerValue,
0 ignored issues
show
Unused Code introduced by
The parameter $integerValue is not used and could be removed. ( Ignorable by Annotation )

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

378
        /** @scrutinizer ignore-unused */ $integerValue,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
379
        $booleanValue
0 ignored issues
show
Unused Code introduced by
The parameter $booleanValue is not used and could be removed. ( Ignorable by Annotation )

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

379
        /** @scrutinizer ignore-unused */ $booleanValue

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
380
    ) {
381
        $platform = $this->createPlatform();
382
383
        self::assertEquals($preparedStatementValue, $platform->convertBooleans($databaseValue));
384
    }
385
386
    /**
387
     * @group DBAL-457
388
     */
389
    public function testConvertBooleanAsLiteralIntegers()
390
    {
391
        $platform = $this->createPlatform();
392
        $platform->setUseBooleanTrueFalseStrings(false);
393
394
        self::assertEquals(1, $platform->convertBooleans(true));
395
        self::assertEquals(1, $platform->convertBooleans('1'));
396
397
        self::assertEquals(0, $platform->convertBooleans(false));
398
        self::assertEquals(0, $platform->convertBooleans('0'));
399
    }
400
401
    /**
402
     * @group DBAL-630
403
     * @dataProvider pgBooleanProvider
404
     *
405
     * @param string $databaseValue
406
     * @param string $preparedStatementValue
407
     * @param integer $integerValue
408
     * @param boolean $booleanValue
409
     */
410
    public function testConvertBooleanAsDatabaseValueStrings(
411
        $databaseValue,
0 ignored issues
show
Unused Code introduced by
The parameter $databaseValue is not used and could be removed. ( Ignorable by Annotation )

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

411
        /** @scrutinizer ignore-unused */ $databaseValue,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
412
        $preparedStatementValue,
0 ignored issues
show
Unused Code introduced by
The parameter $preparedStatementValue is not used and could be removed. ( Ignorable by Annotation )

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

412
        /** @scrutinizer ignore-unused */ $preparedStatementValue,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
413
        $integerValue,
414
        $booleanValue
415
    )
416
    {
417
        $platform = $this->createPlatform();
418
419
        self::assertSame($integerValue, $platform->convertBooleansToDatabaseValue($booleanValue));
420
    }
421
422
    /**
423
     * @group DBAL-630
424
     */
425
    public function testConvertBooleanAsDatabaseValueIntegers()
426
    {
427
        $platform = $this->createPlatform();
428
        $platform->setUseBooleanTrueFalseStrings(false);
429
430
        self::assertSame(1, $platform->convertBooleansToDatabaseValue(true));
431
        self::assertSame(0, $platform->convertBooleansToDatabaseValue(false));
432
    }
433
434
    /**
435
     * @dataProvider pgBooleanProvider
436
     *
437
     * @param string $databaseValue
438
     * @param string $prepareStatementValue
439
     * @param integer $integerValue
440
     * @param boolean $booleanValue
441
     */
442
    public function testConvertFromBoolean($databaseValue, $prepareStatementValue, $integerValue, $booleanValue)
0 ignored issues
show
Unused Code introduced by
The parameter $integerValue is not used and could be removed. ( Ignorable by Annotation )

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

442
    public function testConvertFromBoolean($databaseValue, $prepareStatementValue, /** @scrutinizer ignore-unused */ $integerValue, $booleanValue)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $prepareStatementValue is not used and could be removed. ( Ignorable by Annotation )

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

442
    public function testConvertFromBoolean($databaseValue, /** @scrutinizer ignore-unused */ $prepareStatementValue, $integerValue, $booleanValue)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
443
    {
444
        $platform = $this->createPlatform();
445
446
        self::assertSame($booleanValue, $platform->convertFromBoolean($databaseValue));
447
    }
448
449
    /**
450
     * @expectedException        UnexpectedValueException
451
     * @expectedExceptionMessage Unrecognized boolean literal 'my-bool'
452
     */
453
    public function testThrowsExceptionWithInvalidBooleanLiteral()
454
    {
455
        $platform = $this->createPlatform()->convertBooleansToDatabaseValue("my-bool");
0 ignored issues
show
Unused Code introduced by
The assignment to $platform is dead and can be removed.
Loading history...
456
    }
457
458
    public function testGetCreateSchemaSQL()
459
    {
460
        $schemaName = 'schema';
461
        $sql = $this->_platform->getCreateSchemaSQL($schemaName);
462
        self::assertEquals('CREATE SCHEMA ' . $schemaName, $sql);
463
    }
464
465
    public function testAlterDecimalPrecisionScale()
466
    {
467
468
        $table = new Table('mytable');
469
        $table->addColumn('dfoo1', 'decimal');
470
        $table->addColumn('dfoo2', 'decimal', array('precision' => 10, 'scale' => 6));
471
        $table->addColumn('dfoo3', 'decimal', array('precision' => 10, 'scale' => 6));
472
        $table->addColumn('dfoo4', 'decimal', array('precision' => 10, 'scale' => 6));
473
474
        $tableDiff = new TableDiff('mytable');
475
        $tableDiff->fromTable = $table;
476
477
        $tableDiff->changedColumns['dloo1'] = new \Doctrine\DBAL\Schema\ColumnDiff(
478
            'dloo1', new \Doctrine\DBAL\Schema\Column(
479
                'dloo1', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 16, 'scale' => 6)
480
            ),
481
            array('precision')
482
        );
483
        $tableDiff->changedColumns['dloo2'] = new \Doctrine\DBAL\Schema\ColumnDiff(
484
            'dloo2', new \Doctrine\DBAL\Schema\Column(
485
                'dloo2', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 10, 'scale' => 4)
486
            ),
487
            array('scale')
488
        );
489
        $tableDiff->changedColumns['dloo3'] = new \Doctrine\DBAL\Schema\ColumnDiff(
490
            'dloo3', new \Doctrine\DBAL\Schema\Column(
491
                'dloo3', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 10, 'scale' => 6)
492
            ),
493
            array()
494
        );
495
        $tableDiff->changedColumns['dloo4'] = new \Doctrine\DBAL\Schema\ColumnDiff(
496
            'dloo4', new \Doctrine\DBAL\Schema\Column(
497
                'dloo4', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 16, 'scale' => 8)
498
            ),
499
            array('precision', 'scale')
500
        );
501
502
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
503
504
        $expectedSql = array(
505
            'ALTER TABLE mytable ALTER dloo1 TYPE NUMERIC(16, 6)',
506
            'ALTER TABLE mytable ALTER dloo2 TYPE NUMERIC(10, 4)',
507
            'ALTER TABLE mytable ALTER dloo4 TYPE NUMERIC(16, 8)',
508
        );
509
510
        self::assertEquals($expectedSql, $sql);
511
    }
512
513
    /**
514
     * @group DBAL-365
515
     */
516
    public function testDroppingConstraintsBeforeColumns()
517
    {
518
        $newTable = new Table('mytable');
519
        $newTable->addColumn('id', 'integer');
520
        $newTable->setPrimaryKey(array('id'));
521
522
        $oldTable = clone $newTable;
523
        $oldTable->addColumn('parent_id', 'integer');
524
        $oldTable->addUnnamedForeignKeyConstraint('mytable', array('parent_id'), array('id'));
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated: Use {@link addForeignKeyConstraint} ( Ignorable by Annotation )

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

524
        /** @scrutinizer ignore-deprecated */ $oldTable->addUnnamedForeignKeyConstraint('mytable', array('parent_id'), array('id'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
525
526
        $comparator = new \Doctrine\DBAL\Schema\Comparator();
527
        $tableDiff = $comparator->diffTable($oldTable, $newTable);
528
529
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
530
531
        $expectedSql = array(
532
            'ALTER TABLE mytable DROP CONSTRAINT FK_6B2BD609727ACA70',
533
            'DROP INDEX IDX_6B2BD609727ACA70',
534
            'ALTER TABLE mytable DROP parent_id',
535
        );
536
537
        self::assertEquals($expectedSql, $sql);
538
    }
539
540
    /**
541
     * @group DBAL-563
542
     */
543
    public function testUsesSequenceEmulatedIdentityColumns()
544
    {
545
        self::assertTrue($this->_platform->usesSequenceEmulatedIdentityColumns());
546
    }
547
548
    /**
549
     * @group DBAL-563
550
     */
551
    public function testReturnsIdentitySequenceName()
552
    {
553
        self::assertSame('mytable_mycolumn_seq', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
554
    }
555
556
    /**
557
     * @dataProvider dataCreateSequenceWithCache
558
     * @group DBAL-139
559
     */
560
    public function testCreateSequenceWithCache($cacheSize, $expectedSql)
561
    {
562
        $sequence = new \Doctrine\DBAL\Schema\Sequence('foo', 1, 1, $cacheSize);
563
        self::assertContains($expectedSql, $this->_platform->getCreateSequenceSQL($sequence));
564
    }
565
566
    public function dataCreateSequenceWithCache()
567
    {
568
        return array(
569
            array(3, 'CACHE 3')
570
        );
571
    }
572
573
    protected function getBinaryDefaultLength()
574
    {
575
        return 0;
576
    }
577
578
    protected function getBinaryMaxLength()
579
    {
580
        return 0;
581
    }
582
583 View Code Duplication
    public function testReturnsBinaryTypeDeclarationSQL()
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...
584
    {
585
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array()));
586
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
587
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 9999999)));
588
589
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
590
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
591
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 9999999)));
592
    }
593
594
    public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType()
595
    {
596
        $table1 = new Table('mytable');
597
        $table1->addColumn('column_varbinary', 'binary');
598
        $table1->addColumn('column_binary', 'binary', array('fixed' => true));
599
        $table1->addColumn('column_blob', 'blob');
600
601
        $table2 = new Table('mytable');
602
        $table2->addColumn('column_varbinary', 'binary', array('fixed' => true));
603
        $table2->addColumn('column_binary', 'binary');
604
        $table2->addColumn('column_blob', 'binary');
605
606
        $comparator = new Comparator();
607
608
        // VARBINARY -> BINARY
609
        // BINARY    -> VARBINARY
610
        // BLOB      -> VARBINARY
611
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
612
613
        $table2 = new Table('mytable');
614
        $table2->addColumn('column_varbinary', 'binary', array('length' => 42));
615
        $table2->addColumn('column_binary', 'blob');
616
        $table2->addColumn('column_blob', 'binary', array('length' => 11, 'fixed' => true));
617
618
        // VARBINARY -> VARBINARY with changed length
619
        // BINARY    -> BLOB
620
        // BLOB      -> BINARY
621
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
622
623
        $table2 = new Table('mytable');
624
        $table2->addColumn('column_varbinary', 'blob');
625
        $table2->addColumn('column_binary', 'binary', array('length' => 42, 'fixed' => true));
626
        $table2->addColumn('column_blob', 'blob');
627
628
        // VARBINARY -> BLOB
629
        // BINARY    -> BINARY with changed length
630
        // BLOB      -> BLOB
631
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
632
    }
633
634
    /**
635
     * @group DBAL-234
636
     */
637
    protected function getAlterTableRenameForeignKeySQL(): array
638
    {
639
        return [
640
            'ALTER TABLE mytable DROP CONSTRAINT fk1',
641
            'ALTER TABLE mytable ADD CONSTRAINT fk2 FOREIGN KEY (fk) REFERENCES fk_table (id) NOT DEFERRABLE INITIALLY IMMEDIATE',
642
        ];
643
    }
644
645
    /**
646
     * @group DBAL-234
647
     */
648
    protected function getAlterTableRenameIndexSQL()
649
    {
650
        return array(
651
            'ALTER INDEX idx_foo RENAME TO idx_bar',
652
        );
653
    }
654
655
    /**
656
     * @group DBAL-234
657
     */
658
    protected function getQuotedAlterTableRenameIndexSQL()
659
    {
660
        return array(
661
            'ALTER INDEX "create" RENAME TO "select"',
662
            'ALTER INDEX "foo" RENAME TO "bar"',
663
        );
664
    }
665
666
    /**
667
     * PostgreSQL boolean strings provider
668
     * @return array
669
     */
670
    public function pgBooleanProvider()
671
    {
672
        return array(
673
            // Database value, prepared statement value, boolean integer value, boolean value.
674
            array(true, 'true', 1, true),
675
            array('t', 'true', 1, true),
676
            array('true', 'true', 1, true),
677
            array('y', 'true', 1, true),
678
            array('yes', 'true', 1, true),
679
            array('on', 'true', 1, true),
680
            array('1', 'true', 1, true),
681
682
            array(false, 'false', 0, false),
683
            array('f', 'false', 0, false),
684
            array('false', 'false', 0, false),
685
            array( 'n', 'false', 0, false),
686
            array('no', 'false', 0, false),
687
            array('off', 'false', 0, false),
688
            array('0', 'false', 0, false),
689
690
            array(null, 'NULL', null, null)
691
        );
692
    }
693
694
    /**
695
     * {@inheritdoc}
696
     */
697
    protected function getQuotedAlterTableRenameColumnSQL()
698
    {
699
        return array(
700
            'ALTER TABLE mytable RENAME COLUMN unquoted1 TO unquoted',
701
            'ALTER TABLE mytable RENAME COLUMN unquoted2 TO "where"',
702
            'ALTER TABLE mytable RENAME COLUMN unquoted3 TO "foo"',
703
            'ALTER TABLE mytable RENAME COLUMN "create" TO reserved_keyword',
704
            'ALTER TABLE mytable RENAME COLUMN "table" TO "from"',
705
            'ALTER TABLE mytable RENAME COLUMN "select" TO "bar"',
706
            'ALTER TABLE mytable RENAME COLUMN quoted1 TO quoted',
707
            'ALTER TABLE mytable RENAME COLUMN quoted2 TO "and"',
708
            'ALTER TABLE mytable RENAME COLUMN quoted3 TO "baz"',
709
        );
710
    }
711
712
    /**
713
     * {@inheritdoc}
714
     */
715
    protected function getQuotedAlterTableChangeColumnLengthSQL()
716
    {
717
        return array(
718
            'ALTER TABLE mytable ALTER unquoted1 TYPE VARCHAR(255)',
719
            'ALTER TABLE mytable ALTER unquoted2 TYPE VARCHAR(255)',
720
            'ALTER TABLE mytable ALTER unquoted3 TYPE VARCHAR(255)',
721
            'ALTER TABLE mytable ALTER "create" TYPE VARCHAR(255)',
722
            'ALTER TABLE mytable ALTER "table" TYPE VARCHAR(255)',
723
            'ALTER TABLE mytable ALTER "select" TYPE VARCHAR(255)',
724
        );
725
    }
726
727
    /**
728
     * @group DBAL-807
729
     */
730
    protected function getAlterTableRenameIndexInSchemaSQL()
731
    {
732
        return array(
733
            'ALTER INDEX myschema.idx_foo RENAME TO idx_bar',
734
        );
735
    }
736
737
    /**
738
     * @group DBAL-807
739
     */
740
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
741
    {
742
        return array(
743
            'ALTER INDEX "schema"."create" RENAME TO "select"',
744
            'ALTER INDEX "schema"."foo" RENAME TO "bar"',
745
        );
746
    }
747
748
    protected function getQuotesDropForeignKeySQL()
749
    {
750
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
751
    }
752
753
    public function testGetNullCommentOnColumnSQL()
754
    {
755
        self::assertEquals(
756
            "COMMENT ON COLUMN mytable.id IS NULL",
757
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', null)
758
        );
759
    }
760
761
    /**
762
     * @group DBAL-423
763
     */
764
    public function testReturnsGuidTypeDeclarationSQL()
765
    {
766
        self::assertSame('UUID', $this->_platform->getGuidTypeDeclarationSQL(array()));
767
    }
768
769
    /**
770
     * {@inheritdoc}
771
     */
772
    public function getAlterTableRenameColumnSQL()
773
    {
774
        return array(
775
            'ALTER TABLE foo RENAME COLUMN bar TO baz',
776
        );
777
    }
778
779
    /**
780
     * {@inheritdoc}
781
     */
782 View Code Duplication
    protected function getQuotesTableIdentifiersInAlterTableSQL()
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...
783
    {
784
        return array(
785
            'ALTER TABLE "foo" DROP CONSTRAINT fk1',
786
            'ALTER TABLE "foo" DROP CONSTRAINT fk2',
787
            'ALTER TABLE "foo" ADD bloo INT NOT NULL',
788
            'ALTER TABLE "foo" DROP baz',
789
            'ALTER TABLE "foo" ALTER bar DROP NOT NULL',
790
            'ALTER TABLE "foo" RENAME COLUMN id TO war',
791
            'ALTER TABLE "foo" RENAME TO "table"',
792
            'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id) NOT DEFERRABLE ' .
793
            'INITIALLY IMMEDIATE',
794
            'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id) NOT DEFERRABLE ' .
795
            'INITIALLY IMMEDIATE',
796
        );
797
    }
798
799
    /**
800
     * {@inheritdoc}
801
     */
802
    protected function getCommentOnColumnSQL()
803
    {
804
        return array(
805
            'COMMENT ON COLUMN foo.bar IS \'comment\'',
806
            'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
807
            'COMMENT ON COLUMN "select"."from" IS \'comment\'',
808
        );
809
    }
810
811
    /**
812
     * @group DBAL-1004
813
     */
814 View Code Duplication
    public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers()
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...
815
    {
816
        $table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
817
        $table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
818
819
        $comparator = new Comparator();
820
821
        $tableDiff = $comparator->diffTable($table1, $table2);
822
823
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
824
        self::assertSame(
825
            array(
826
                'COMMENT ON COLUMN "foo"."bar" IS \'baz\'',
827
            ),
828
            $this->_platform->getAlterTableSQL($tableDiff)
829
        );
830
    }
831
832
    /**
833
     * {@inheritdoc}
834
     */
835
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
836
    {
837
        return 'CONSTRAINT "select" UNIQUE (foo)';
838
    }
839
840
    /**
841
     * {@inheritdoc}
842
     */
843
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
844
    {
845
        return 'INDEX "select" (foo)';
846
    }
847
848
    /**
849
     * {@inheritdoc}
850
     */
851
    protected function getQuotesReservedKeywordInTruncateTableSQL()
852
    {
853
        return 'TRUNCATE "select"';
854
    }
855
856
    /**
857
     * {@inheritdoc}
858
     */
859
    protected function getAlterStringToFixedStringSQL()
860
    {
861
        return array(
862
            'ALTER TABLE mytable ALTER name TYPE CHAR(2)',
863
        );
864
    }
865
866
    /**
867
     * {@inheritdoc}
868
     */
869
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
870
    {
871
        return array(
872
            'ALTER INDEX idx_foo RENAME TO idx_foo_renamed',
873
        );
874
    }
875
876
    /**
877
     * @group DBAL-1142
878
     */
879
    public function testInitializesTsvectorTypeMapping()
880
    {
881
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('tsvector'));
882
        self::assertEquals('text', $this->_platform->getDoctrineTypeMapping('tsvector'));
883
    }
884
885
    /**
886
     * @group DBAL-1220
887
     */
888
    public function testReturnsDisallowDatabaseConnectionsSQL()
889
    {
890
        self::assertSame(
891
            "UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'foo'",
892
            $this->_platform->getDisallowDatabaseConnectionsSQL('foo')
0 ignored issues
show
Bug introduced by
The method getDisallowDatabaseConnectionsSQL() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. It seems like you code against a sub-type of Doctrine\DBAL\Platforms\AbstractPlatform such as Doctrine\DBAL\Platforms\PostgreSqlPlatform. ( Ignorable by Annotation )

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

892
            $this->_platform->/** @scrutinizer ignore-call */ 
893
                              getDisallowDatabaseConnectionsSQL('foo')
Loading history...
893
        );
894
    }
895
896
    /**
897
     * @group DBAL-1220
898
     */
899
    public function testReturnsCloseActiveDatabaseConnectionsSQL()
900
    {
901
        self::assertSame(
902
            "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = 'foo'",
903
            $this->_platform->getCloseActiveDatabaseConnectionsSQL('foo')
0 ignored issues
show
Bug introduced by
The method getCloseActiveDatabaseConnectionsSQL() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. It seems like you code against a sub-type of Doctrine\DBAL\Platforms\AbstractPlatform such as Doctrine\DBAL\Platforms\PostgreSqlPlatform. ( Ignorable by Annotation )

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

903
            $this->_platform->/** @scrutinizer ignore-call */ 
904
                              getCloseActiveDatabaseConnectionsSQL('foo')
Loading history...
904
        );
905
    }
906
907
    /**
908
     * @group DBAL-2436
909
     */
910
    public function testQuotesTableNameInListTableForeignKeysSQL()
911
    {
912
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
913
    }
914
915
    /**
916
     * @group DBAL-2436
917
     */
918
    public function testQuotesSchemaNameInListTableForeignKeysSQL()
919
    {
920
        self::assertContains(
921
            "'Foo''Bar\\\\'",
922
            $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
923
            '',
924
            true
925
        );
926
    }
927
928
    /**
929
     * @group DBAL-2436
930
     */
931
    public function testQuotesTableNameInListTableConstraintsSQL()
932
    {
933
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
934
    }
935
936
    /**
937
     * @group DBAL-2436
938
     */
939
    public function testQuotesTableNameInListTableIndexesSQL()
940
    {
941
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
942
    }
943
944
    /**
945
     * @group DBAL-2436
946
     */
947
    public function testQuotesSchemaNameInListTableIndexesSQL()
948
    {
949
        self::assertContains(
950
            "'Foo''Bar\\\\'",
951
            $this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
952
            '',
953
            true
954
        );
955
    }
956
957
    /**
958
     * @group DBAL-2436
959
     */
960
    public function testQuotesTableNameInListTableColumnsSQL()
961
    {
962
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
963
    }
964
965
    /**
966
     * @group DBAL-2436
967
     */
968
    public function testQuotesSchemaNameInListTableColumnsSQL()
969
    {
970
        self::assertContains(
971
            "'Foo''Bar\\\\'",
972
            $this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
973
            '',
974
            true
975
        );
976
    }
977
978
    /**
979
     * @group DBAL-2436
980
     */
981
    public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL()
982
    {
983
        self::assertContains(
984
            "'Foo''Bar\\\\'",
985
            $this->_platform->getCloseActiveDatabaseConnectionsSQL("Foo'Bar\\"),
986
            '',
987
            true
988
        );
989
    }
990
}
991