Total Complexity | 33 |
Total Lines | 481 |
Duplicated Lines | 28.9 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase |
||
13 | { |
||
14 | protected function tearDown() |
||
15 | { |
||
16 | parent::tearDown(); |
||
17 | |||
18 | if (!$this->_conn) { |
||
19 | return; |
||
20 | } |
||
21 | |||
22 | $this->_conn->getConfiguration()->setFilterSchemaAssetsExpression(null); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @group DBAL-177 |
||
27 | */ |
||
28 | public function testGetSearchPath() |
||
29 | { |
||
30 | $params = $this->_conn->getParams(); |
||
31 | |||
32 | $paths = $this->_sm->getSchemaSearchPaths(); |
||
33 | self::assertEquals([$params['user'], 'public'], $paths); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @group DBAL-244 |
||
38 | */ |
||
39 | public function testGetSchemaNames() |
||
40 | { |
||
41 | $names = $this->_sm->getSchemaNames(); |
||
|
|||
42 | |||
43 | self::assertInternalType('array', $names); |
||
44 | self::assertTrue(count($names) > 0); |
||
45 | self::assertContains('public', $names, 'The public schema should be found.'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @group DBAL-21 |
||
50 | */ |
||
51 | public function testSupportDomainTypeFallback() |
||
52 | { |
||
53 | $createDomainTypeSQL = "CREATE DOMAIN MyMoney AS DECIMAL(18,2)"; |
||
54 | $this->_conn->exec($createDomainTypeSQL); |
||
55 | |||
56 | $createTableSQL = "CREATE TABLE domain_type_test (id INT PRIMARY KEY, value MyMoney)"; |
||
57 | $this->_conn->exec($createTableSQL); |
||
58 | |||
59 | $table = $this->_conn->getSchemaManager()->listTableDetails('domain_type_test'); |
||
60 | self::assertInstanceOf('Doctrine\DBAL\Types\DecimalType', $table->getColumn('value')->getType()); |
||
61 | |||
62 | Type::addType('MyMoney', 'Doctrine\Tests\DBAL\Functional\Schema\MoneyType'); |
||
63 | $this->_conn->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'MyMoney'); |
||
64 | |||
65 | $table = $this->_conn->getSchemaManager()->listTableDetails('domain_type_test'); |
||
66 | self::assertInstanceOf('Doctrine\Tests\DBAL\Functional\Schema\MoneyType', $table->getColumn('value')->getType()); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @group DBAL-37 |
||
71 | */ |
||
72 | public function testDetectsAutoIncrement() |
||
73 | { |
||
74 | $autoincTable = new \Doctrine\DBAL\Schema\Table('autoinc_table'); |
||
75 | $column = $autoincTable->addColumn('id', 'integer'); |
||
76 | $column->setAutoincrement(true); |
||
77 | $this->_sm->createTable($autoincTable); |
||
78 | $autoincTable = $this->_sm->listTableDetails('autoinc_table'); |
||
79 | |||
80 | self::assertTrue($autoincTable->getColumn('id')->getAutoincrement()); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @group DBAL-37 |
||
85 | */ |
||
86 | View Code Duplication | public function testAlterTableAutoIncrementAdd() |
|
87 | { |
||
88 | $tableFrom = new \Doctrine\DBAL\Schema\Table('autoinc_table_add'); |
||
89 | $column = $tableFrom->addColumn('id', 'integer'); |
||
90 | $this->_sm->createTable($tableFrom); |
||
91 | $tableFrom = $this->_sm->listTableDetails('autoinc_table_add'); |
||
92 | self::assertFalse($tableFrom->getColumn('id')->getAutoincrement()); |
||
93 | |||
94 | $tableTo = new \Doctrine\DBAL\Schema\Table('autoinc_table_add'); |
||
95 | $column = $tableTo->addColumn('id', 'integer'); |
||
96 | $column->setAutoincrement(true); |
||
97 | |||
98 | $c = new \Doctrine\DBAL\Schema\Comparator(); |
||
99 | $diff = $c->diffTable($tableFrom, $tableTo); |
||
100 | $sql = $this->_conn->getDatabasePlatform()->getAlterTableSQL($diff); |
||
101 | self::assertEquals(array( |
||
102 | "CREATE SEQUENCE autoinc_table_add_id_seq", |
||
103 | "SELECT setval('autoinc_table_add_id_seq', (SELECT MAX(id) FROM autoinc_table_add))", |
||
104 | "ALTER TABLE autoinc_table_add ALTER id SET DEFAULT nextval('autoinc_table_add_id_seq')", |
||
105 | ), $sql); |
||
106 | |||
107 | $this->_sm->alterTable($diff); |
||
108 | $tableFinal = $this->_sm->listTableDetails('autoinc_table_add'); |
||
109 | self::assertTrue($tableFinal->getColumn('id')->getAutoincrement()); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @group DBAL-37 |
||
114 | */ |
||
115 | View Code Duplication | public function testAlterTableAutoIncrementDrop() |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @group DBAL-75 |
||
139 | */ |
||
140 | public function testTableWithSchema() |
||
141 | { |
||
142 | $this->_conn->exec('CREATE SCHEMA nested'); |
||
143 | |||
144 | $nestedRelatedTable = new \Doctrine\DBAL\Schema\Table('nested.schemarelated'); |
||
145 | $column = $nestedRelatedTable->addColumn('id', 'integer'); |
||
146 | $column->setAutoincrement(true); |
||
147 | $nestedRelatedTable->setPrimaryKey(array('id')); |
||
148 | |||
149 | $nestedSchemaTable = new \Doctrine\DBAL\Schema\Table('nested.schematable'); |
||
150 | $column = $nestedSchemaTable->addColumn('id', 'integer'); |
||
151 | $column->setAutoincrement(true); |
||
152 | $nestedSchemaTable->setPrimaryKey(array('id')); |
||
153 | $nestedSchemaTable->addUnnamedForeignKeyConstraint($nestedRelatedTable, array('id'), array('id')); |
||
1 ignored issue
–
show
|
|||
154 | |||
155 | $this->_sm->createTable($nestedRelatedTable); |
||
156 | $this->_sm->createTable($nestedSchemaTable); |
||
157 | |||
158 | $tables = $this->_sm->listTableNames(); |
||
159 | self::assertContains('nested.schematable', $tables, "The table should be detected with its non-public schema."); |
||
160 | |||
161 | $nestedSchemaTable = $this->_sm->listTableDetails('nested.schematable'); |
||
162 | self::assertTrue($nestedSchemaTable->hasColumn('id')); |
||
163 | self::assertEquals(array('id'), $nestedSchemaTable->getPrimaryKey()->getColumns()); |
||
164 | |||
165 | $relatedFks = $nestedSchemaTable->getForeignKeys(); |
||
166 | self::assertEquals(1, count($relatedFks)); |
||
167 | $relatedFk = array_pop($relatedFks); |
||
168 | self::assertEquals("nested.schemarelated", $relatedFk->getForeignTableName()); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @group DBAL-91 |
||
173 | * @group DBAL-88 |
||
174 | */ |
||
175 | public function testReturnQuotedAssets() |
||
176 | { |
||
177 | $sql = 'create table dbal91_something ( id integer CONSTRAINT id_something PRIMARY KEY NOT NULL ,"table" integer );'; |
||
178 | $this->_conn->exec($sql); |
||
179 | |||
180 | $sql = 'ALTER TABLE dbal91_something ADD CONSTRAINT something_input FOREIGN KEY( "table" ) REFERENCES dbal91_something ON UPDATE CASCADE;'; |
||
181 | $this->_conn->exec($sql); |
||
182 | |||
183 | $table = $this->_sm->listTableDetails('dbal91_something'); |
||
184 | |||
185 | self::assertEquals( |
||
186 | array( |
||
187 | "CREATE TABLE dbal91_something (id INT NOT NULL, \"table\" INT DEFAULT NULL, PRIMARY KEY(id))", |
||
188 | "CREATE INDEX IDX_A9401304ECA7352B ON dbal91_something (\"table\")", |
||
189 | ), |
||
190 | $this->_conn->getDatabasePlatform()->getCreateTableSQL($table) |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @group DBAL-204 |
||
196 | */ |
||
197 | public function testFilterSchemaExpression() |
||
198 | { |
||
199 | $testTable = new \Doctrine\DBAL\Schema\Table('dbal204_test_prefix'); |
||
200 | $column = $testTable->addColumn('id', 'integer'); |
||
201 | $this->_sm->createTable($testTable); |
||
202 | $testTable = new \Doctrine\DBAL\Schema\Table('dbal204_without_prefix'); |
||
203 | $column = $testTable->addColumn('id', 'integer'); |
||
204 | $this->_sm->createTable($testTable); |
||
205 | |||
206 | $this->_conn->getConfiguration()->setFilterSchemaAssetsExpression('#^dbal204_#'); |
||
207 | $names = $this->_sm->listTableNames(); |
||
208 | self::assertEquals(2, count($names)); |
||
209 | |||
210 | $this->_conn->getConfiguration()->setFilterSchemaAssetsExpression('#^dbal204_test#'); |
||
211 | $names = $this->_sm->listTableNames(); |
||
212 | self::assertEquals(1, count($names)); |
||
213 | } |
||
214 | |||
215 | public function testListForeignKeys() |
||
216 | { |
||
217 | if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
218 | $this->markTestSkipped('Does not support foreign key constraints.'); |
||
219 | } |
||
220 | |||
221 | $fkOptions = array('SET NULL', 'SET DEFAULT', 'NO ACTION','CASCADE', 'RESTRICT'); |
||
222 | $foreignKeys = array(); |
||
223 | $fkTable = $this->getTestTable('test_create_fk1'); |
||
224 | for($i = 0; $i < count($fkOptions); $i++) { |
||
225 | $fkTable->addColumn("foreign_key_test$i", 'integer'); |
||
226 | $foreignKeys[] = new \Doctrine\DBAL\Schema\ForeignKeyConstraint( |
||
227 | array("foreign_key_test$i"), 'test_create_fk2', array('id'), "foreign_key_test_$i"."_fk", array('onDelete' => $fkOptions[$i])); |
||
228 | } |
||
229 | $this->_sm->dropAndCreateTable($fkTable); |
||
230 | $this->createTestTable('test_create_fk2'); |
||
231 | |||
232 | foreach($foreignKeys as $foreignKey) { |
||
233 | $this->_sm->createForeignKey($foreignKey, 'test_create_fk1'); |
||
234 | } |
||
235 | $fkeys = $this->_sm->listTableForeignKeys('test_create_fk1'); |
||
236 | self::assertEquals(count($foreignKeys), count($fkeys), "Table 'test_create_fk1' has to have " . count($foreignKeys) . " foreign keys."); |
||
237 | for ($i = 0; $i < count($fkeys); $i++) { |
||
238 | self::assertEquals(array("foreign_key_test$i"), array_map('strtolower', $fkeys[$i]->getLocalColumns())); |
||
239 | self::assertEquals(array('id'), array_map('strtolower', $fkeys[$i]->getForeignColumns())); |
||
240 | self::assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName())); |
||
241 | if ($foreignKeys[$i]->getOption('onDelete') == 'NO ACTION') { |
||
242 | self::assertFalse($fkeys[$i]->hasOption('onDelete'), 'Unexpected option: '. $fkeys[$i]->getOption('onDelete')); |
||
243 | } else { |
||
244 | self::assertEquals($foreignKeys[$i]->getOption('onDelete'), $fkeys[$i]->getOption('onDelete')); |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @group DBAL-511 |
||
251 | */ |
||
252 | public function testDefaultValueCharacterVarying() |
||
253 | { |
||
254 | $testTable = new \Doctrine\DBAL\Schema\Table('dbal511_default'); |
||
255 | $testTable->addColumn('id', 'integer'); |
||
256 | $testTable->addColumn('def', 'string', array('default' => 'foo')); |
||
257 | $testTable->setPrimaryKey(array('id')); |
||
258 | |||
259 | $this->_sm->createTable($testTable); |
||
260 | |||
261 | $databaseTable = $this->_sm->listTableDetails($testTable->getName()); |
||
262 | |||
263 | self::assertEquals('foo', $databaseTable->getColumn('def')->getDefault()); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @group DDC-2843 |
||
268 | */ |
||
269 | View Code Duplication | public function testBooleanDefault() |
|
270 | { |
||
271 | $table = new \Doctrine\DBAL\Schema\Table('ddc2843_bools'); |
||
272 | $table->addColumn('id', 'integer'); |
||
273 | $table->addColumn('checked', 'boolean', array('default' => false)); |
||
274 | |||
275 | $this->_sm->createTable($table); |
||
276 | |||
277 | $databaseTable = $this->_sm->listTableDetails($table->getName()); |
||
278 | |||
279 | $c = new \Doctrine\DBAL\Schema\Comparator(); |
||
280 | $diff = $c->diffTable($table, $databaseTable); |
||
281 | |||
282 | self::assertFalse($diff); |
||
283 | } |
||
284 | |||
285 | View Code Duplication | public function testListTableWithBinary() |
|
286 | { |
||
287 | $tableName = 'test_binary_table'; |
||
288 | |||
289 | $table = new \Doctrine\DBAL\Schema\Table($tableName); |
||
290 | $table->addColumn('id', 'integer'); |
||
291 | $table->addColumn('column_varbinary', 'binary', array()); |
||
292 | $table->addColumn('column_binary', 'binary', array('fixed' => true)); |
||
293 | $table->setPrimaryKey(array('id')); |
||
294 | |||
295 | $this->_sm->createTable($table); |
||
296 | |||
297 | $table = $this->_sm->listTableDetails($tableName); |
||
298 | |||
299 | self::assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_varbinary')->getType()); |
||
300 | self::assertFalse($table->getColumn('column_varbinary')->getFixed()); |
||
301 | |||
302 | self::assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_binary')->getType()); |
||
303 | self::assertFalse($table->getColumn('column_binary')->getFixed()); |
||
304 | } |
||
305 | |||
306 | public function testListQuotedTable() |
||
307 | { |
||
308 | $offlineTable = new Schema\Table('user'); |
||
309 | $offlineTable->addColumn('id', 'integer'); |
||
310 | $offlineTable->addColumn('username', 'string', array('unique' => true)); |
||
311 | $offlineTable->addColumn('fk', 'integer'); |
||
312 | $offlineTable->setPrimaryKey(array('id')); |
||
313 | $offlineTable->addForeignKeyConstraint($offlineTable, array('fk'), array('id')); |
||
314 | |||
315 | $this->_sm->dropAndCreateTable($offlineTable); |
||
316 | |||
317 | $onlineTable = $this->_sm->listTableDetails('"user"'); |
||
318 | |||
319 | $comparator = new Schema\Comparator(); |
||
320 | |||
321 | self::assertFalse($comparator->diffTable($offlineTable, $onlineTable)); |
||
322 | } |
||
323 | |||
324 | public function testListTablesExcludesViews() |
||
325 | { |
||
326 | $this->createTestTable('list_tables_excludes_views'); |
||
327 | |||
328 | $name = "list_tables_excludes_views_test_view"; |
||
329 | $sql = "SELECT * from list_tables_excludes_views"; |
||
330 | |||
331 | $view = new Schema\View($name, $sql); |
||
332 | |||
333 | $this->_sm->dropAndCreateView($view); |
||
334 | |||
335 | $tables = $this->_sm->listTables(); |
||
336 | |||
337 | $foundTable = false; |
||
338 | View Code Duplication | foreach ($tables as $table) { |
|
339 | self::assertInstanceOf('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.'); |
||
340 | if (strtolower($table->getName()) == 'list_tables_excludes_views_test_view') { |
||
341 | $foundTable = true; |
||
342 | } |
||
343 | } |
||
344 | |||
345 | self::assertFalse($foundTable, 'View "list_tables_excludes_views_test_view" must not be found in table list'); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @group DBAL-1033 |
||
350 | */ |
||
351 | public function testPartialIndexes() |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @dataProvider jsonbColumnTypeProvider |
||
373 | */ |
||
374 | public function testJsonbColumn(string $type): void |
||
375 | { |
||
376 | if (!$this->_sm->getDatabasePlatform() instanceof PostgreSQL94Platform) { |
||
377 | $this->markTestSkipped("Requires PostgresSQL 9.4+"); |
||
378 | return; |
||
379 | } |
||
380 | |||
381 | $table = new Schema\Table('test_jsonb'); |
||
382 | $table->addColumn('foo', $type)->setPlatformOption('jsonb', true); |
||
383 | $this->_sm->dropAndCreateTable($table); |
||
384 | |||
385 | /** @var Schema\Column[] $columns */ |
||
386 | $columns = $this->_sm->listTableColumns('test_jsonb'); |
||
387 | |||
388 | self::assertSame($type, $columns['foo']->getType()->getName()); |
||
389 | self::assertTrue(true, $columns['foo']->getPlatformOption('jsonb')); |
||
390 | } |
||
391 | |||
392 | public function jsonbColumnTypeProvider(): array |
||
393 | { |
||
394 | return [ |
||
395 | [Type::JSON], |
||
396 | [Type::JSON_ARRAY], |
||
397 | ]; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @group DBAL-2427 |
||
402 | */ |
||
403 | public function testListNegativeColumnDefaultValue() |
||
404 | { |
||
405 | $table = new Schema\Table('test_default_negative'); |
||
406 | $table->addColumn('col_smallint', 'smallint', array('default' => -1)); |
||
407 | $table->addColumn('col_integer', 'integer', array('default' => -1)); |
||
408 | $table->addColumn('col_bigint', 'bigint', array('default' => -1)); |
||
409 | $table->addColumn('col_float', 'float', array('default' => -1.1)); |
||
410 | $table->addColumn('col_decimal', 'decimal', array('default' => -1.1)); |
||
411 | $table->addColumn('col_string', 'string', array('default' => '(-1)')); |
||
412 | |||
413 | $this->_sm->dropAndCreateTable($table); |
||
414 | |||
415 | $columns = $this->_sm->listTableColumns('test_default_negative'); |
||
416 | |||
417 | self::assertEquals(-1, $columns['col_smallint']->getDefault()); |
||
418 | self::assertEquals(-1, $columns['col_integer']->getDefault()); |
||
419 | self::assertEquals(-1, $columns['col_bigint']->getDefault()); |
||
420 | self::assertEquals(-1.1, $columns['col_float']->getDefault()); |
||
421 | self::assertEquals(-1.1, $columns['col_decimal']->getDefault()); |
||
422 | self::assertEquals('(-1)', $columns['col_string']->getDefault()); |
||
423 | } |
||
424 | |||
425 | public static function serialTypes() : array |
||
426 | { |
||
427 | return [ |
||
428 | ['integer'], |
||
429 | ['bigint'], |
||
430 | ]; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @dataProvider serialTypes |
||
435 | * @group 2906 |
||
436 | */ |
||
437 | View Code Duplication | public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $type) : void |
|
449 | } |
||
450 | |||
451 | /** |
||
452 | * @dataProvider serialTypes |
||
453 | * @group 2906 |
||
454 | */ |
||
455 | View Code Duplication | public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenWhenDefaultIsSet(string $type) : void |
|
456 | { |
||
457 | $tableName = "test_serial_type_with_default_$type"; |
||
458 | |||
459 | $table = new Schema\Table($tableName); |
||
460 | $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]); |
||
461 | |||
462 | $this->_sm->dropAndCreateTable($table); |
||
463 | |||
464 | $columns = $this->_sm->listTableColumns($tableName); |
||
465 | |||
466 | self::assertNull($columns['id']->getDefault()); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @group 2916 |
||
471 | */ |
||
472 | View Code Duplication | public function testAlterTableAutoIncrementBigInt() : void |
|
493 | } |
||
494 | } |
||
495 | |||
496 | class MoneyType extends Type |
||
497 | { |
||
498 | |||
499 | public function getName() |
||
500 | { |
||
501 | return "MyMoney"; |
||
502 | } |
||
503 | |||
504 | public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
||
505 | { |
||
506 | return 'MyMoney'; |
||
507 | } |
||
508 | |||
509 | } |
||
510 |