Passed
Pull Request — master (#2920)
by Luís
09:30
created

testSupportsIdentityColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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 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...
148
    {
149
        self::assertEquals(
150
            'INT',
151
            $this->_platform->getIntegerTypeDeclarationSQL(array())
152
        );
153
        self::assertEquals(
154
            'SERIAL',
155
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
156
        ));
157
        self::assertEquals(
158
            'SERIAL',
159
            $this->_platform->getIntegerTypeDeclarationSQL(
160
                array('autoincrement' => true, 'primary' => true)
161
        ));
162
    }
163
164 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...
165
    {
166
        self::assertEquals(
167
            'CHAR(10)',
168
            $this->_platform->getVarcharTypeDeclarationSQL(
169
                array('length' => 10, 'fixed' => true))
170
        );
171
        self::assertEquals(
172
            'VARCHAR(50)',
173
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
174
            'Variable string declaration is not correct'
175
        );
176
        self::assertEquals(
177
            'VARCHAR(255)',
178
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
179
            'Long string declaration is not correct'
180
        );
181
    }
182
183
    public function getGenerateUniqueIndexSql()
184
    {
185
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
186
    }
187
188 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...
189
    {
190
        $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
191
        self::assertEquals(
192
            'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
193
            $this->_platform->getCreateSequenceSQL($sequence)
194
        );
195
        self::assertEquals(
196
            'DROP SEQUENCE myseq CASCADE',
197
            $this->_platform->getDropSequenceSQL('myseq')
198
        );
199
        self::assertEquals(
200
            "SELECT NEXTVAL('myseq')",
201
            $this->_platform->getSequenceNextValSQL('myseq')
202
        );
203
    }
204
205
    public function testDoesNotPreferIdentityColumns()
206
    {
207
        self::assertFalse($this->_platform->prefersIdentityColumns());
208
    }
209
210
    public function testPrefersSequences()
211
    {
212
        self::assertTrue($this->_platform->prefersSequences());
213
    }
214
215
    public function testSupportsIdentityColumns()
216
    {
217
        self::assertTrue($this->_platform->supportsIdentityColumns());
218
    }
219
220
    public function testSupportsSavePoints()
221
    {
222
        self::assertTrue($this->_platform->supportsSavepoints());
223
    }
224
225
    public function testSupportsSequences()
226
    {
227
        self::assertTrue($this->_platform->supportsSequences());
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    protected function supportsCommentOnStatement()
234
    {
235
        return true;
236
    }
237
238
    public function testModifyLimitQuery()
239
    {
240
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
241
        self::assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
242
    }
243
244
    public function testModifyLimitQueryWithEmptyOffset()
245
    {
246
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
247
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
248
    }
249
250
    public function getCreateTableColumnCommentsSQL()
251
    {
252
        return array(
253
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
254
            "COMMENT ON COLUMN test.id IS 'This is a comment'",
255
        );
256
    }
257
258
    public function getAlterTableColumnCommentsSQL()
259
    {
260
        return array(
261
            "ALTER TABLE mytable ADD quota INT NOT NULL",
262
            "COMMENT ON COLUMN mytable.quota IS 'A comment'",
263
            "COMMENT ON COLUMN mytable.foo IS NULL",
264
            "COMMENT ON COLUMN mytable.baz IS 'B comment'",
265
        );
266
    }
267
268
    public function getCreateTableColumnTypeCommentsSQL()
269
    {
270
        return array(
271
            "CREATE TABLE test (id INT NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))",
272
            "COMMENT ON COLUMN test.data IS '(DC2Type:array)'"
273
        );
274
    }
275
276
    protected function getQuotedColumnInPrimaryKeySQL()
277
    {
278
        return array(
279
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, PRIMARY KEY("create"))',
280
        );
281
    }
282
283
    protected function getQuotedColumnInIndexSQL()
284
    {
285
        return array(
286
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL)',
287
            'CREATE INDEX IDX_22660D028FD6E0FB ON "quoted" ("create")',
288
        );
289
    }
290
291
    protected function getQuotedNameInIndexSQL()
292
    {
293
        return array(
294
            'CREATE TABLE test (column1 VARCHAR(255) NOT NULL)',
295
            'CREATE INDEX "key" ON test (column1)',
296
        );
297
    }
298
299
    protected function getQuotedColumnInForeignKeySQL()
300
    {
301
        return array(
302
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL, "bar" VARCHAR(255) NOT NULL)',
303
            'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar") REFERENCES "foreign" ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE',
304
            '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',
305
            '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',
306
        );
307
    }
308
309
    /**
310
     * @group DBAL-457
311
     * @dataProvider pgBooleanProvider
312
     *
313
     * @param string $databaseValue
314
     * @param string $preparedStatementValue
315
     * @param integer $integerValue
316
     * @param boolean $booleanValue
317
     */
318
    public function testConvertBooleanAsLiteralStrings(
319
        $databaseValue,
320
        $preparedStatementValue,
321
        $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

321
        /** @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...
322
        $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

322
        /** @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...
323
    ) {
324
        $platform = $this->createPlatform();
325
326
        self::assertEquals($preparedStatementValue, $platform->convertBooleans($databaseValue));
327
    }
328
329
    /**
330
     * @group DBAL-457
331
     */
332
    public function testConvertBooleanAsLiteralIntegers()
333
    {
334
        $platform = $this->createPlatform();
335
        $platform->setUseBooleanTrueFalseStrings(false);
336
337
        self::assertEquals(1, $platform->convertBooleans(true));
338
        self::assertEquals(1, $platform->convertBooleans('1'));
339
340
        self::assertEquals(0, $platform->convertBooleans(false));
341
        self::assertEquals(0, $platform->convertBooleans('0'));
342
    }
343
344
    /**
345
     * @group DBAL-630
346
     * @dataProvider pgBooleanProvider
347
     *
348
     * @param string $databaseValue
349
     * @param string $preparedStatementValue
350
     * @param integer $integerValue
351
     * @param boolean $booleanValue
352
     */
353
    public function testConvertBooleanAsDatabaseValueStrings(
354
        $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

354
        /** @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...
355
        $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

355
        /** @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...
356
        $integerValue,
357
        $booleanValue
358
    )
359
    {
360
        $platform = $this->createPlatform();
361
362
        self::assertSame($integerValue, $platform->convertBooleansToDatabaseValue($booleanValue));
363
    }
364
365
    /**
366
     * @group DBAL-630
367
     */
368
    public function testConvertBooleanAsDatabaseValueIntegers()
369
    {
370
        $platform = $this->createPlatform();
371
        $platform->setUseBooleanTrueFalseStrings(false);
372
373
        self::assertSame(1, $platform->convertBooleansToDatabaseValue(true));
374
        self::assertSame(0, $platform->convertBooleansToDatabaseValue(false));
375
    }
376
377
    /**
378
     * @dataProvider pgBooleanProvider
379
     *
380
     * @param string $databaseValue
381
     * @param string $prepareStatementValue
382
     * @param integer $integerValue
383
     * @param boolean $booleanValue
384
     */
385
    public function testConvertFromBoolean($databaseValue, $prepareStatementValue, $integerValue, $booleanValue)
0 ignored issues
show
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

385
    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...
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

385
    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...
386
    {
387
        $platform = $this->createPlatform();
388
389
        self::assertSame($booleanValue, $platform->convertFromBoolean($databaseValue));
390
    }
391
392
    /**
393
     * @expectedException        UnexpectedValueException
394
     * @expectedExceptionMessage Unrecognized boolean literal 'my-bool'
395
     */
396
    public function testThrowsExceptionWithInvalidBooleanLiteral()
397
    {
398
        $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...
399
    }
400
401
    public function testGetCreateSchemaSQL()
402
    {
403
        $schemaName = 'schema';
404
        $sql = $this->_platform->getCreateSchemaSQL($schemaName);
405
        self::assertEquals('CREATE SCHEMA ' . $schemaName, $sql);
406
    }
407
408
    public function testAlterDecimalPrecisionScale()
409
    {
410
411
        $table = new Table('mytable');
412
        $table->addColumn('dfoo1', 'decimal');
413
        $table->addColumn('dfoo2', 'decimal', array('precision' => 10, 'scale' => 6));
414
        $table->addColumn('dfoo3', 'decimal', array('precision' => 10, 'scale' => 6));
415
        $table->addColumn('dfoo4', 'decimal', array('precision' => 10, 'scale' => 6));
416
417
        $tableDiff = new TableDiff('mytable');
418
        $tableDiff->fromTable = $table;
419
420
        $tableDiff->changedColumns['dloo1'] = new \Doctrine\DBAL\Schema\ColumnDiff(
421
            'dloo1', new \Doctrine\DBAL\Schema\Column(
422
                'dloo1', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 16, 'scale' => 6)
423
            ),
424
            array('precision')
425
        );
426
        $tableDiff->changedColumns['dloo2'] = new \Doctrine\DBAL\Schema\ColumnDiff(
427
            'dloo2', new \Doctrine\DBAL\Schema\Column(
428
                'dloo2', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 10, 'scale' => 4)
429
            ),
430
            array('scale')
431
        );
432
        $tableDiff->changedColumns['dloo3'] = new \Doctrine\DBAL\Schema\ColumnDiff(
433
            'dloo3', new \Doctrine\DBAL\Schema\Column(
434
                'dloo3', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 10, 'scale' => 6)
435
            ),
436
            array()
437
        );
438
        $tableDiff->changedColumns['dloo4'] = new \Doctrine\DBAL\Schema\ColumnDiff(
439
            'dloo4', new \Doctrine\DBAL\Schema\Column(
440
                'dloo4', \Doctrine\DBAL\Types\Type::getType('decimal'), array('precision' => 16, 'scale' => 8)
441
            ),
442
            array('precision', 'scale')
443
        );
444
445
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
446
447
        $expectedSql = array(
448
            'ALTER TABLE mytable ALTER dloo1 TYPE NUMERIC(16, 6)',
449
            'ALTER TABLE mytable ALTER dloo2 TYPE NUMERIC(10, 4)',
450
            'ALTER TABLE mytable ALTER dloo4 TYPE NUMERIC(16, 8)',
451
        );
452
453
        self::assertEquals($expectedSql, $sql);
454
    }
455
456
    /**
457
     * @group DBAL-365
458
     */
459
    public function testDroppingConstraintsBeforeColumns()
460
    {
461
        $newTable = new Table('mytable');
462
        $newTable->addColumn('id', 'integer');
463
        $newTable->setPrimaryKey(array('id'));
464
465
        $oldTable = clone $newTable;
466
        $oldTable->addColumn('parent_id', 'integer');
467
        $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

467
        /** @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...
468
469
        $comparator = new \Doctrine\DBAL\Schema\Comparator();
470
        $tableDiff = $comparator->diffTable($oldTable, $newTable);
471
472
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
0 ignored issues
show
Bug introduced by
It seems like $tableDiff 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

472
        $sql = $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $tableDiff);
Loading history...
473
474
        $expectedSql = array(
475
            'ALTER TABLE mytable DROP CONSTRAINT FK_6B2BD609727ACA70',
476
            'DROP INDEX IDX_6B2BD609727ACA70',
477
            'ALTER TABLE mytable DROP parent_id',
478
        );
479
480
        self::assertEquals($expectedSql, $sql);
481
    }
482
483
    /**
484
     * @group DBAL-563
485
     */
486
    public function testUsesSequenceEmulatedIdentityColumns()
487
    {
488
        self::assertTrue($this->_platform->usesSequenceEmulatedIdentityColumns());
489
    }
490
491
    /**
492
     * @group DBAL-563
493
     */
494
    public function testReturnsIdentitySequenceName()
495
    {
496
        self::assertSame('mytable_mycolumn_seq', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
497
    }
498
499
    /**
500
     * @dataProvider dataCreateSequenceWithCache
501
     * @group DBAL-139
502
     */
503
    public function testCreateSequenceWithCache($cacheSize, $expectedSql)
504
    {
505
        $sequence = new \Doctrine\DBAL\Schema\Sequence('foo', 1, 1, $cacheSize);
506
        self::assertContains($expectedSql, $this->_platform->getCreateSequenceSQL($sequence));
507
    }
508
509
    public function dataCreateSequenceWithCache()
510
    {
511
        return array(
512
            array(3, 'CACHE 3')
513
        );
514
    }
515
516
    protected function getBinaryDefaultLength()
517
    {
518
        return 0;
519
    }
520
521
    protected function getBinaryMaxLength()
522
    {
523
        return 0;
524
    }
525
526 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...
527
    {
528
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array()));
529
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
530
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 9999999)));
531
532
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
533
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
534
        self::assertSame('BYTEA', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 9999999)));
535
    }
536
537
    public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType()
538
    {
539
        $table1 = new Table('mytable');
540
        $table1->addColumn('column_varbinary', 'binary');
541
        $table1->addColumn('column_binary', 'binary', array('fixed' => true));
542
        $table1->addColumn('column_blob', 'blob');
543
544
        $table2 = new Table('mytable');
545
        $table2->addColumn('column_varbinary', 'binary', array('fixed' => true));
546
        $table2->addColumn('column_binary', 'binary');
547
        $table2->addColumn('column_blob', 'binary');
548
549
        $comparator = new Comparator();
550
551
        // VARBINARY -> BINARY
552
        // BINARY    -> VARBINARY
553
        // BLOB      -> VARBINARY
554
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
0 ignored issues
show
Bug introduced by
It seems like $comparator->diffTable($table1, $table2) 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

554
        self::assertEmpty($this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $comparator->diffTable($table1, $table2)));
Loading history...
555
556
        $table2 = new Table('mytable');
557
        $table2->addColumn('column_varbinary', 'binary', array('length' => 42));
558
        $table2->addColumn('column_binary', 'blob');
559
        $table2->addColumn('column_blob', 'binary', array('length' => 11, 'fixed' => true));
560
561
        // VARBINARY -> VARBINARY with changed length
562
        // BINARY    -> BLOB
563
        // BLOB      -> BINARY
564
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
565
566
        $table2 = new Table('mytable');
567
        $table2->addColumn('column_varbinary', 'blob');
568
        $table2->addColumn('column_binary', 'binary', array('length' => 42, 'fixed' => true));
569
        $table2->addColumn('column_blob', 'blob');
570
571
        // VARBINARY -> BLOB
572
        // BINARY    -> BINARY with changed length
573
        // BLOB      -> BLOB
574
        self::assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
575
    }
576
577
    /**
578
     * @group DBAL-234
579
     */
580
    protected function getAlterTableRenameIndexSQL()
581
    {
582
        return array(
583
            'ALTER INDEX idx_foo RENAME TO idx_bar',
584
        );
585
    }
586
587
    /**
588
     * @group DBAL-234
589
     */
590
    protected function getQuotedAlterTableRenameIndexSQL()
591
    {
592
        return array(
593
            'ALTER INDEX "create" RENAME TO "select"',
594
            'ALTER INDEX "foo" RENAME TO "bar"',
595
        );
596
    }
597
598
    /**
599
     * PostgreSQL boolean strings provider
600
     * @return array
601
     */
602
    public function pgBooleanProvider()
603
    {
604
        return array(
605
            // Database value, prepared statement value, boolean integer value, boolean value.
606
            array(true, 'true', 1, true),
607
            array('t', 'true', 1, true),
608
            array('true', 'true', 1, true),
609
            array('y', 'true', 1, true),
610
            array('yes', 'true', 1, true),
611
            array('on', 'true', 1, true),
612
            array('1', 'true', 1, true),
613
614
            array(false, 'false', 0, false),
615
            array('f', 'false', 0, false),
616
            array('false', 'false', 0, false),
617
            array( 'n', 'false', 0, false),
618
            array('no', 'false', 0, false),
619
            array('off', 'false', 0, false),
620
            array('0', 'false', 0, false),
621
622
            array(null, 'NULL', null, null)
623
        );
624
    }
625
626
    /**
627
     * {@inheritdoc}
628
     */
629
    protected function getQuotedAlterTableRenameColumnSQL()
630
    {
631
        return array(
632
            'ALTER TABLE mytable RENAME COLUMN unquoted1 TO unquoted',
633
            'ALTER TABLE mytable RENAME COLUMN unquoted2 TO "where"',
634
            'ALTER TABLE mytable RENAME COLUMN unquoted3 TO "foo"',
635
            'ALTER TABLE mytable RENAME COLUMN "create" TO reserved_keyword',
636
            'ALTER TABLE mytable RENAME COLUMN "table" TO "from"',
637
            'ALTER TABLE mytable RENAME COLUMN "select" TO "bar"',
638
            'ALTER TABLE mytable RENAME COLUMN quoted1 TO quoted',
639
            'ALTER TABLE mytable RENAME COLUMN quoted2 TO "and"',
640
            'ALTER TABLE mytable RENAME COLUMN quoted3 TO "baz"',
641
        );
642
    }
643
644
    /**
645
     * {@inheritdoc}
646
     */
647
    protected function getQuotedAlterTableChangeColumnLengthSQL()
648
    {
649
        return array(
650
            'ALTER TABLE mytable ALTER unquoted1 TYPE VARCHAR(255)',
651
            'ALTER TABLE mytable ALTER unquoted2 TYPE VARCHAR(255)',
652
            'ALTER TABLE mytable ALTER unquoted3 TYPE VARCHAR(255)',
653
            'ALTER TABLE mytable ALTER "create" TYPE VARCHAR(255)',
654
            'ALTER TABLE mytable ALTER "table" TYPE VARCHAR(255)',
655
            'ALTER TABLE mytable ALTER "select" TYPE VARCHAR(255)',
656
        );
657
    }
658
659
    /**
660
     * @group DBAL-807
661
     */
662
    protected function getAlterTableRenameIndexInSchemaSQL()
663
    {
664
        return array(
665
            'ALTER INDEX myschema.idx_foo RENAME TO idx_bar',
666
        );
667
    }
668
669
    /**
670
     * @group DBAL-807
671
     */
672
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
673
    {
674
        return array(
675
            'ALTER INDEX "schema"."create" RENAME TO "select"',
676
            'ALTER INDEX "schema"."foo" RENAME TO "bar"',
677
        );
678
    }
679
680
    protected function getQuotesDropForeignKeySQL()
681
    {
682
        return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
683
    }
684
685
    public function testGetNullCommentOnColumnSQL()
686
    {
687
        self::assertEquals(
688
            "COMMENT ON COLUMN mytable.id IS NULL",
689
            $this->_platform->getCommentOnColumnSQL('mytable', 'id', null)
690
        );
691
    }
692
693
    /**
694
     * @group DBAL-423
695
     */
696
    public function testReturnsGuidTypeDeclarationSQL()
697
    {
698
        self::assertSame('UUID', $this->_platform->getGuidTypeDeclarationSQL(array()));
699
    }
700
701
    /**
702
     * {@inheritdoc}
703
     */
704
    public function getAlterTableRenameColumnSQL()
705
    {
706
        return array(
707
            'ALTER TABLE foo RENAME COLUMN bar TO baz',
708
        );
709
    }
710
711
    /**
712
     * {@inheritdoc}
713
     */
714 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...
715
    {
716
        return array(
717
            'ALTER TABLE "foo" DROP CONSTRAINT fk1',
718
            'ALTER TABLE "foo" DROP CONSTRAINT fk2',
719
            'ALTER TABLE "foo" ADD bloo INT NOT NULL',
720
            'ALTER TABLE "foo" DROP baz',
721
            'ALTER TABLE "foo" ALTER bar DROP NOT NULL',
722
            'ALTER TABLE "foo" RENAME COLUMN id TO war',
723
            'ALTER TABLE "foo" RENAME TO "table"',
724
            'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id) NOT DEFERRABLE ' .
725
            'INITIALLY IMMEDIATE',
726
            'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id) NOT DEFERRABLE ' .
727
            'INITIALLY IMMEDIATE',
728
        );
729
    }
730
731
    /**
732
     * {@inheritdoc}
733
     */
734
    protected function getCommentOnColumnSQL()
735
    {
736
        return array(
737
            'COMMENT ON COLUMN foo.bar IS \'comment\'',
738
            'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
739
            'COMMENT ON COLUMN "select"."from" IS \'comment\'',
740
        );
741
    }
742
743
    /**
744
     * @group DBAL-1004
745
     */
746 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...
747
    {
748
        $table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
749
        $table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
750
751
        $comparator = new Comparator();
752
753
        $tableDiff = $comparator->diffTable($table1, $table2);
754
755
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
756
        self::assertSame(
757
            array(
758
                'COMMENT ON COLUMN "foo"."bar" IS \'baz\'',
759
            ),
760
            $this->_platform->getAlterTableSQL($tableDiff)
0 ignored issues
show
Bug introduced by
It seems like $tableDiff 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

760
            $this->_platform->getAlterTableSQL(/** @scrutinizer ignore-type */ $tableDiff)
Loading history...
761
        );
762
    }
763
764
    /**
765
     * {@inheritdoc}
766
     */
767
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
768
    {
769
        return 'CONSTRAINT "select" UNIQUE (foo)';
770
    }
771
772
    /**
773
     * {@inheritdoc}
774
     */
775
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
776
    {
777
        return 'INDEX "select" (foo)';
778
    }
779
780
    /**
781
     * {@inheritdoc}
782
     */
783
    protected function getQuotesReservedKeywordInTruncateTableSQL()
784
    {
785
        return 'TRUNCATE "select"';
786
    }
787
788
    /**
789
     * {@inheritdoc}
790
     */
791
    protected function getAlterStringToFixedStringSQL()
792
    {
793
        return array(
794
            'ALTER TABLE mytable ALTER name TYPE CHAR(2)',
795
        );
796
    }
797
798
    /**
799
     * {@inheritdoc}
800
     */
801
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
802
    {
803
        return array(
804
            'ALTER INDEX idx_foo RENAME TO idx_foo_renamed',
805
        );
806
    }
807
808
    /**
809
     * @group DBAL-1142
810
     */
811
    public function testInitializesTsvectorTypeMapping()
812
    {
813
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('tsvector'));
814
        self::assertEquals('text', $this->_platform->getDoctrineTypeMapping('tsvector'));
815
    }
816
817
    /**
818
     * @group DBAL-1220
819
     */
820
    public function testReturnsDisallowDatabaseConnectionsSQL()
821
    {
822
        self::assertSame(
823
            "UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'foo'",
824
            $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

824
            $this->_platform->/** @scrutinizer ignore-call */ 
825
                              getDisallowDatabaseConnectionsSQL('foo')
Loading history...
825
        );
826
    }
827
828
    /**
829
     * @group DBAL-1220
830
     */
831
    public function testReturnsCloseActiveDatabaseConnectionsSQL()
832
    {
833
        self::assertSame(
834
            "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = 'foo'",
835
            $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

835
            $this->_platform->/** @scrutinizer ignore-call */ 
836
                              getCloseActiveDatabaseConnectionsSQL('foo')
Loading history...
836
        );
837
    }
838
839
    /**
840
     * @group DBAL-2436
841
     */
842
    public function testQuotesTableNameInListTableForeignKeysSQL()
843
    {
844
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
845
    }
846
847
    /**
848
     * @group DBAL-2436
849
     */
850
    public function testQuotesSchemaNameInListTableForeignKeysSQL()
851
    {
852
        self::assertContains(
853
            "'Foo''Bar\\\\'",
854
            $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
855
            '',
856
            true
857
        );
858
    }
859
860
    /**
861
     * @group DBAL-2436
862
     */
863
    public function testQuotesTableNameInListTableConstraintsSQL()
864
    {
865
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
866
    }
867
868
    /**
869
     * @group DBAL-2436
870
     */
871
    public function testQuotesTableNameInListTableIndexesSQL()
872
    {
873
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
874
    }
875
876
    /**
877
     * @group DBAL-2436
878
     */
879
    public function testQuotesSchemaNameInListTableIndexesSQL()
880
    {
881
        self::assertContains(
882
            "'Foo''Bar\\\\'",
883
            $this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
884
            '',
885
            true
886
        );
887
    }
888
889
    /**
890
     * @group DBAL-2436
891
     */
892
    public function testQuotesTableNameInListTableColumnsSQL()
893
    {
894
        self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
895
    }
896
897
    /**
898
     * @group DBAL-2436
899
     */
900
    public function testQuotesSchemaNameInListTableColumnsSQL()
901
    {
902
        self::assertContains(
903
            "'Foo''Bar\\\\'",
904
            $this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
905
            '',
906
            true
907
        );
908
    }
909
910
    /**
911
     * @group DBAL-2436
912
     */
913
    public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL()
914
    {
915
        self::assertContains(
916
            "'Foo''Bar\\\\'",
917
            $this->_platform->getCloseActiveDatabaseConnectionsSQL("Foo'Bar\\"),
918
            '',
919
            true
920
        );
921
    }
922
}
923