Failed Conditions
Pull Request — master (#3339)
by Sergei
27:42
created

OracleSchemaManagerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 155
dl 0
loc 270
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testListDatabases() 0 11 1
A testAlterTableColumnNotNull() 0 30 1
A testCreateAndListSequences() 0 3 1
B testListTableDetailsWithDifferentIdentifierQuotingRequirements() 0 105 1
A testListTableDateTypeColumns() 0 14 1
A testListTableIndexesPrimaryKeyConstraintNameDiffersFromIndexName() 0 18 1
A testListTableWithBinary() 0 19 1
A testListTableColumnsSameTableNamesInDifferentSchemas() 0 11 1
A testRenameTable() 0 11 1
A setUpBeforeClass() 0 14 2
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Schema;
4
5
use Doctrine\DBAL\Schema;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Types\BinaryType;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\Tests\TestUtil;
10
use function array_map;
11
12
class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
13
{
14
    public static function setUpBeforeClass()
15
    {
16
        parent::setUpBeforeClass();
17
18
        if (! isset($GLOBALS['db_username'])) {
19
            self::markTestSkipped('Username must be explicitly specified in connection parameters for this test');
20
        }
21
22
        $username = $GLOBALS['db_username'];
23
24
        $query = 'GRANT ALL PRIVILEGES TO ' . $username;
25
26
        $conn = TestUtil::getTempConnection();
27
        $conn->exec($query);
28
    }
29
30
    public function testRenameTable()
31
    {
32
        $this->schemaManager->tryMethod('DropTable', 'list_tables_test');
33
        $this->schemaManager->tryMethod('DropTable', 'list_tables_test_new_name');
34
35
        $this->createTestTable('list_tables_test');
36
        $this->schemaManager->renameTable('list_tables_test', 'list_tables_test_new_name');
37
38
        $tables = $this->schemaManager->listTables();
39
40
        self::assertHasTable($tables, 'list_tables_test_new_name');
41
    }
42
43
    public function testListTableWithBinary()
44
    {
45
        $tableName = 'test_binary_table';
46
47
        $table = new Table($tableName);
48
        $table->addColumn('id', 'integer');
49
        $table->addColumn('column_varbinary', 'binary', []);
50
        $table->addColumn('column_binary', 'binary', ['fixed' => true]);
51
        $table->setPrimaryKey(['id']);
52
53
        $this->schemaManager->createTable($table);
54
55
        $table = $this->schemaManager->listTableDetails($tableName);
56
57
        self::assertInstanceOf(BinaryType::class, $table->getColumn('column_varbinary')->getType());
58
        self::assertFalse($table->getColumn('column_varbinary')->getFixed());
59
60
        self::assertInstanceOf(BinaryType::class, $table->getColumn('column_binary')->getType());
61
        self::assertFalse($table->getColumn('column_binary')->getFixed());
62
    }
63
64
    /**
65
     * @group DBAL-472
66
     * @group DBAL-1001
67
     */
68
    public function testAlterTableColumnNotNull()
69
    {
70
        $comparator = new Schema\Comparator();
71
        $tableName  = 'list_table_column_notnull';
72
        $table      = new Schema\Table($tableName);
73
74
        $table->addColumn('id', 'integer');
75
        $table->addColumn('foo', 'integer');
76
        $table->addColumn('bar', 'string');
77
        $table->setPrimaryKey(['id']);
78
79
        $this->schemaManager->dropAndCreateTable($table);
80
81
        $columns = $this->schemaManager->listTableColumns($tableName);
82
83
        self::assertTrue($columns['id']->getNotnull());
84
        self::assertTrue($columns['foo']->getNotnull());
85
        self::assertTrue($columns['bar']->getNotnull());
86
87
        $diffTable = clone $table;
88
        $diffTable->changeColumn('foo', ['notnull' => false]);
89
        $diffTable->changeColumn('bar', ['length' => 1024]);
90
91
        $this->schemaManager->alterTable($comparator->diffTable($table, $diffTable));
92
93
        $columns = $this->schemaManager->listTableColumns($tableName);
94
95
        self::assertTrue($columns['id']->getNotnull());
96
        self::assertFalse($columns['foo']->getNotnull());
97
        self::assertTrue($columns['bar']->getNotnull());
98
    }
99
100
    public function testListDatabases()
101
    {
102
        // We need the temp connection that has privileges to create a database.
103
        $sm = TestUtil::getTempConnection()->getSchemaManager();
104
105
        $sm->dropAndCreateDatabase('c##test_create_database');
106
107
        $databases = $this->schemaManager->listDatabases();
108
        $databases = array_map('strtolower', $databases);
109
110
        self::assertContains('c##test_create_database', $databases);
111
    }
112
113
    /**
114
     * @group DBAL-831
115
     */
116
    public function testListTableDetailsWithDifferentIdentifierQuotingRequirements()
117
    {
118
        $primaryTableName    = '"Primary_Table"';
119
        $offlinePrimaryTable = new Schema\Table($primaryTableName);
120
        $offlinePrimaryTable->addColumn(
121
            '"Id"',
122
            'integer',
123
            ['autoincrement' => true, 'comment' => 'Explicit casing.']
124
        );
125
        $offlinePrimaryTable->addColumn('select', 'integer', ['comment' => 'Reserved keyword.']);
126
        $offlinePrimaryTable->addColumn('foo', 'integer', ['comment' => 'Implicit uppercasing.']);
127
        $offlinePrimaryTable->addColumn('BAR', 'integer');
128
        $offlinePrimaryTable->addColumn('"BAZ"', 'integer');
129
        $offlinePrimaryTable->addIndex(['select'], 'from');
130
        $offlinePrimaryTable->addIndex(['foo'], 'foo_index');
131
        $offlinePrimaryTable->addIndex(['BAR'], 'BAR_INDEX');
132
        $offlinePrimaryTable->addIndex(['"BAZ"'], 'BAZ_INDEX');
133
        $offlinePrimaryTable->setPrimaryKey(['"Id"']);
134
135
        $foreignTableName    = 'foreign';
136
        $offlineForeignTable = new Schema\Table($foreignTableName);
137
        $offlineForeignTable->addColumn('id', 'integer', ['autoincrement' => true]);
138
        $offlineForeignTable->addColumn('"Fk"', 'integer');
139
        $offlineForeignTable->addIndex(['"Fk"'], '"Fk_index"');
140
        $offlineForeignTable->addForeignKeyConstraint(
141
            $primaryTableName,
142
            ['"Fk"'],
143
            ['"Id"'],
144
            [],
145
            '"Primary_Table_Fk"'
146
        );
147
        $offlineForeignTable->setPrimaryKey(['id']);
148
149
        $this->schemaManager->tryMethod('dropTable', $foreignTableName);
150
        $this->schemaManager->tryMethod('dropTable', $primaryTableName);
151
152
        $this->schemaManager->createTable($offlinePrimaryTable);
153
        $this->schemaManager->createTable($offlineForeignTable);
154
155
        $onlinePrimaryTable = $this->schemaManager->listTableDetails($primaryTableName);
156
        $onlineForeignTable = $this->schemaManager->listTableDetails($foreignTableName);
157
158
        $platform = $this->schemaManager->getDatabasePlatform();
159
160
        // Primary table assertions
161
        self::assertSame($primaryTableName, $onlinePrimaryTable->getQuotedName($platform));
162
163
        self::assertTrue($onlinePrimaryTable->hasColumn('"Id"'));
164
        self::assertSame('"Id"', $onlinePrimaryTable->getColumn('"Id"')->getQuotedName($platform));
165
        self::assertTrue($onlinePrimaryTable->hasPrimaryKey());
166
        self::assertSame(['"Id"'], $onlinePrimaryTable->getPrimaryKey()->getQuotedColumns($platform));
167
168
        self::assertTrue($onlinePrimaryTable->hasColumn('select'));
169
        self::assertSame('"select"', $onlinePrimaryTable->getColumn('select')->getQuotedName($platform));
170
171
        self::assertTrue($onlinePrimaryTable->hasColumn('foo'));
172
        self::assertSame('FOO', $onlinePrimaryTable->getColumn('foo')->getQuotedName($platform));
173
174
        self::assertTrue($onlinePrimaryTable->hasColumn('BAR'));
175
        self::assertSame('BAR', $onlinePrimaryTable->getColumn('BAR')->getQuotedName($platform));
176
177
        self::assertTrue($onlinePrimaryTable->hasColumn('"BAZ"'));
178
        self::assertSame('BAZ', $onlinePrimaryTable->getColumn('"BAZ"')->getQuotedName($platform));
179
180
        self::assertTrue($onlinePrimaryTable->hasIndex('from'));
181
        self::assertTrue($onlinePrimaryTable->getIndex('from')->hasColumnAtPosition('"select"'));
182
        self::assertSame(['"select"'], $onlinePrimaryTable->getIndex('from')->getQuotedColumns($platform));
183
184
        self::assertTrue($onlinePrimaryTable->hasIndex('foo_index'));
185
        self::assertTrue($onlinePrimaryTable->getIndex('foo_index')->hasColumnAtPosition('foo'));
186
        self::assertSame(['FOO'], $onlinePrimaryTable->getIndex('foo_index')->getQuotedColumns($platform));
187
188
        self::assertTrue($onlinePrimaryTable->hasIndex('BAR_INDEX'));
189
        self::assertTrue($onlinePrimaryTable->getIndex('BAR_INDEX')->hasColumnAtPosition('BAR'));
190
        self::assertSame(['BAR'], $onlinePrimaryTable->getIndex('BAR_INDEX')->getQuotedColumns($platform));
191
192
        self::assertTrue($onlinePrimaryTable->hasIndex('BAZ_INDEX'));
193
        self::assertTrue($onlinePrimaryTable->getIndex('BAZ_INDEX')->hasColumnAtPosition('"BAZ"'));
194
        self::assertSame(['BAZ'], $onlinePrimaryTable->getIndex('BAZ_INDEX')->getQuotedColumns($platform));
195
196
        // Foreign table assertions
197
        self::assertTrue($onlineForeignTable->hasColumn('id'));
198
        self::assertSame('ID', $onlineForeignTable->getColumn('id')->getQuotedName($platform));
199
        self::assertTrue($onlineForeignTable->hasPrimaryKey());
200
        self::assertSame(['ID'], $onlineForeignTable->getPrimaryKey()->getQuotedColumns($platform));
201
202
        self::assertTrue($onlineForeignTable->hasColumn('"Fk"'));
203
        self::assertSame('"Fk"', $onlineForeignTable->getColumn('"Fk"')->getQuotedName($platform));
204
205
        self::assertTrue($onlineForeignTable->hasIndex('"Fk_index"'));
206
        self::assertTrue($onlineForeignTable->getIndex('"Fk_index"')->hasColumnAtPosition('"Fk"'));
207
        self::assertSame(['"Fk"'], $onlineForeignTable->getIndex('"Fk_index"')->getQuotedColumns($platform));
208
209
        self::assertTrue($onlineForeignTable->hasForeignKey('"Primary_Table_Fk"'));
210
        self::assertSame(
211
            $primaryTableName,
212
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignTableName($platform)
213
        );
214
        self::assertSame(
215
            ['"Fk"'],
216
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedLocalColumns($platform)
217
        );
218
        self::assertSame(
219
            ['"Id"'],
220
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignColumns($platform)
221
        );
222
    }
223
224
    public function testListTableColumnsSameTableNamesInDifferentSchemas()
225
    {
226
        $table = $this->createListTableColumns();
227
        $this->schemaManager->dropAndCreateTable($table);
228
229
        $otherTable = new Table($table->getName());
230
        $otherTable->addColumn('id', Type::STRING);
231
        TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);
232
233
        $columns = $this->schemaManager->listTableColumns($table->getName(), $this->connection->getUsername());
234
        self::assertCount(7, $columns);
235
    }
236
237
    /**
238
     * @group DBAL-1234
239
     */
240
    public function testListTableIndexesPrimaryKeyConstraintNameDiffersFromIndexName()
241
    {
242
        $table = new Table('list_table_indexes_pk_id_test');
243
        $table->setSchemaConfig($this->schemaManager->createSchemaConfig());
244
        $table->addColumn('id', 'integer', ['notnull' => true]);
245
        $table->addUniqueIndex(['id'], 'id_unique_index');
246
        $this->schemaManager->dropAndCreateTable($table);
247
248
        // Adding a primary key on already indexed columns
249
        // Oracle will reuse the unique index, which cause a constraint name differing from the index name
250
        $this->schemaManager->createConstraint(new Schema\Index('id_pk_id_index', ['id'], true, true), 'list_table_indexes_pk_id_test');
251
252
        $tableIndexes = $this->schemaManager->listTableIndexes('list_table_indexes_pk_id_test');
253
254
        self::assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
255
        self::assertEquals(['id'], array_map('strtolower', $tableIndexes['primary']->getColumns()));
256
        self::assertTrue($tableIndexes['primary']->isUnique());
257
        self::assertTrue($tableIndexes['primary']->isPrimary());
258
    }
259
260
    /**
261
     * @group DBAL-2555
262
     */
263
    public function testListTableDateTypeColumns()
264
    {
265
        $table = new Table('tbl_date');
266
        $table->addColumn('col_date', 'date');
267
        $table->addColumn('col_datetime', 'datetime');
268
        $table->addColumn('col_datetimetz', 'datetimetz');
269
270
        $this->schemaManager->dropAndCreateTable($table);
271
272
        $columns = $this->schemaManager->listTableColumns('tbl_date');
273
274
        self::assertSame('date', $columns['col_date']->getType()->getName());
275
        self::assertSame('datetime', $columns['col_datetime']->getType()->getName());
276
        self::assertSame('datetimetz', $columns['col_datetimetz']->getType()->getName());
277
    }
278
279
    public function testCreateAndListSequences() : void
280
    {
281
        self::markTestSkipped("Skipped for uppercase letters are contained in sequences' names. Fix the schema manager in 3.0.");
282
    }
283
}
284