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