Failed Conditions
Pull Request — master (#2766)
by mon
18:50
created

testCreateSchemaNumberOfQueriesInvariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Types;
9
use Doctrine\Tests\TestUtil;
10
use function array_map;
11
12
class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
13
{
14
    /** @var bool */
15
    private static $privilegesGranted = false;
16
17
    protected function setUp() : void
18
    {
19
        parent::setUp();
20
21
        if (self::$privilegesGranted) {
22
            return;
23
        }
24
25
        if (! isset($GLOBALS['db_username'])) {
26
            self::markTestSkipped('Username must be explicitly specified in connection parameters for this test');
27
        }
28
29
        TestUtil::getTempConnection()
30
            ->exec('GRANT ALL PRIVILEGES TO ' . $GLOBALS['db_username']);
31
32
        self::$privilegesGranted = true;
33
    }
34
35
    public function testRenameTable() : void
36
    {
37
        $this->schemaManager->tryMethod('DropTable', 'list_tables_test');
38
        $this->schemaManager->tryMethod('DropTable', 'list_tables_test_new_name');
39
40
        $this->createTestTable('list_tables_test');
41
        $this->schemaManager->renameTable('list_tables_test', 'list_tables_test_new_name');
42
43
        $tables = $this->schemaManager->listTables();
44
45
        self::assertHasTable($tables, 'list_tables_test_new_name');
46
    }
47
48
    public function testListTableWithBinary() : void
49
    {
50
        $tableName = 'test_binary_table';
51
52
        $table = new Table($tableName);
53
        $table->addColumn('id', 'integer');
54
        $table->addColumn('column_varbinary', 'binary', []);
55
        $table->addColumn('column_binary', 'binary', ['fixed' => true]);
56
        $table->setPrimaryKey(['id']);
57
58
        $this->schemaManager->createTable($table);
59
60
        $table = $this->schemaManager->listTableDetails($tableName);
61
62
        self::assertInstanceOf(BinaryType::class, $table->getColumn('column_varbinary')->getType());
63
        self::assertFalse($table->getColumn('column_varbinary')->getFixed());
64
65
        self::assertInstanceOf(BinaryType::class, $table->getColumn('column_binary')->getType());
66
        self::assertFalse($table->getColumn('column_binary')->getFixed());
67
    }
68
69
    /**
70
     * @group DBAL-472
71
     * @group DBAL-1001
72
     */
73
    public function testAlterTableColumnNotNull() : void
74
    {
75
        $comparator = new Schema\Comparator();
76
        $tableName  = 'list_table_column_notnull';
77
        $table      = new Schema\Table($tableName);
78
79
        $table->addColumn('id', 'integer');
80
        $table->addColumn('foo', 'integer');
81
        $table->addColumn('bar', 'string');
82
        $table->setPrimaryKey(['id']);
83
84
        $this->schemaManager->dropAndCreateTable($table);
85
86
        $columns = $this->schemaManager->listTableColumns($tableName);
87
88
        self::assertTrue($columns['id']->getNotnull());
89
        self::assertTrue($columns['foo']->getNotnull());
90
        self::assertTrue($columns['bar']->getNotnull());
91
92
        $diffTable = clone $table;
93
        $diffTable->changeColumn('foo', ['notnull' => false]);
94
        $diffTable->changeColumn('bar', ['length' => 1024]);
95
96
        $this->schemaManager->alterTable($comparator->diffTable($table, $diffTable));
97
98
        $columns = $this->schemaManager->listTableColumns($tableName);
99
100
        self::assertTrue($columns['id']->getNotnull());
101
        self::assertFalse($columns['foo']->getNotnull());
102
        self::assertTrue($columns['bar']->getNotnull());
103
    }
104
105
    public function testListDatabases() : void
106
    {
107
        // We need the temp connection that has privileges to create a database.
108
        $sm = TestUtil::getTempConnection()->getSchemaManager();
109
110
        $sm->dropAndCreateDatabase('c##test_create_database');
111
112
        $databases = $this->schemaManager->listDatabases();
113
        $databases = array_map('strtolower', $databases);
114
115
        self::assertContains('c##test_create_database', $databases);
116
    }
117
118
    /**
119
     * @group DBAL-831
120
     */
121
    public function testListTableDetailsWithDifferentIdentifierQuotingRequirements() : void
122
    {
123
        $primaryTableName    = '"Primary_Table"';
124
        $offlinePrimaryTable = new Schema\Table($primaryTableName);
125
        $offlinePrimaryTable->addColumn(
126
            '"Id"',
127
            'integer',
128
            ['autoincrement' => true, 'comment' => 'Explicit casing.']
129
        );
130
        $offlinePrimaryTable->addColumn('select', 'integer', ['comment' => 'Reserved keyword.']);
131
        $offlinePrimaryTable->addColumn('foo', 'integer', ['comment' => 'Implicit uppercasing.']);
132
        $offlinePrimaryTable->addColumn('BAR', 'integer');
133
        $offlinePrimaryTable->addColumn('"BAZ"', 'integer');
134
        $offlinePrimaryTable->addIndex(['select'], 'from');
135
        $offlinePrimaryTable->addIndex(['foo'], 'foo_index');
136
        $offlinePrimaryTable->addIndex(['BAR'], 'BAR_INDEX');
137
        $offlinePrimaryTable->addIndex(['"BAZ"'], 'BAZ_INDEX');
138
        $offlinePrimaryTable->setPrimaryKey(['"Id"']);
139
140
        $foreignTableName    = 'foreign';
141
        $offlineForeignTable = new Schema\Table($foreignTableName);
142
        $offlineForeignTable->addColumn('id', 'integer', ['autoincrement' => true]);
143
        $offlineForeignTable->addColumn('"Fk"', 'integer');
144
        $offlineForeignTable->addIndex(['"Fk"'], '"Fk_index"');
145
        $offlineForeignTable->addForeignKeyConstraint(
146
            $primaryTableName,
147
            ['"Fk"'],
148
            ['"Id"'],
149
            [],
150
            '"Primary_Table_Fk"'
151
        );
152
        $offlineForeignTable->setPrimaryKey(['id']);
153
154
        $this->schemaManager->tryMethod('dropTable', $foreignTableName);
155
        $this->schemaManager->tryMethod('dropTable', $primaryTableName);
156
157
        $this->schemaManager->createTable($offlinePrimaryTable);
158
        $this->schemaManager->createTable($offlineForeignTable);
159
160
        $onlinePrimaryTable = $this->schemaManager->listTableDetails($primaryTableName);
161
        $onlineForeignTable = $this->schemaManager->listTableDetails($foreignTableName);
162
163
        $platform = $this->schemaManager->getDatabasePlatform();
164
165
        // Primary table assertions
166
        self::assertSame($primaryTableName, $onlinePrimaryTable->getQuotedName($platform));
167
168
        self::assertTrue($onlinePrimaryTable->hasColumn('"Id"'));
169
        self::assertSame('"Id"', $onlinePrimaryTable->getColumn('"Id"')->getQuotedName($platform));
170
        self::assertTrue($onlinePrimaryTable->hasPrimaryKey());
171
        self::assertSame(['"Id"'], $onlinePrimaryTable->getPrimaryKey()->getQuotedColumns($platform));
172
173
        self::assertTrue($onlinePrimaryTable->hasColumn('select'));
174
        self::assertSame('"select"', $onlinePrimaryTable->getColumn('select')->getQuotedName($platform));
175
176
        self::assertTrue($onlinePrimaryTable->hasColumn('foo'));
177
        self::assertSame('FOO', $onlinePrimaryTable->getColumn('foo')->getQuotedName($platform));
178
179
        self::assertTrue($onlinePrimaryTable->hasColumn('BAR'));
180
        self::assertSame('BAR', $onlinePrimaryTable->getColumn('BAR')->getQuotedName($platform));
181
182
        self::assertTrue($onlinePrimaryTable->hasColumn('"BAZ"'));
183
        self::assertSame('BAZ', $onlinePrimaryTable->getColumn('"BAZ"')->getQuotedName($platform));
184
185
        self::assertTrue($onlinePrimaryTable->hasIndex('from'));
186
        self::assertTrue($onlinePrimaryTable->getIndex('from')->hasColumnAtPosition('"select"'));
187
        self::assertSame(['"select"'], $onlinePrimaryTable->getIndex('from')->getQuotedColumns($platform));
188
189
        self::assertTrue($onlinePrimaryTable->hasIndex('foo_index'));
190
        self::assertTrue($onlinePrimaryTable->getIndex('foo_index')->hasColumnAtPosition('foo'));
191
        self::assertSame(['FOO'], $onlinePrimaryTable->getIndex('foo_index')->getQuotedColumns($platform));
192
193
        self::assertTrue($onlinePrimaryTable->hasIndex('BAR_INDEX'));
194
        self::assertTrue($onlinePrimaryTable->getIndex('BAR_INDEX')->hasColumnAtPosition('BAR'));
195
        self::assertSame(['BAR'], $onlinePrimaryTable->getIndex('BAR_INDEX')->getQuotedColumns($platform));
196
197
        self::assertTrue($onlinePrimaryTable->hasIndex('BAZ_INDEX'));
198
        self::assertTrue($onlinePrimaryTable->getIndex('BAZ_INDEX')->hasColumnAtPosition('"BAZ"'));
199
        self::assertSame(['BAZ'], $onlinePrimaryTable->getIndex('BAZ_INDEX')->getQuotedColumns($platform));
200
201
        // Foreign table assertions
202
        self::assertTrue($onlineForeignTable->hasColumn('id'));
203
        self::assertSame('ID', $onlineForeignTable->getColumn('id')->getQuotedName($platform));
204
        self::assertTrue($onlineForeignTable->hasPrimaryKey());
205
        self::assertSame(['ID'], $onlineForeignTable->getPrimaryKey()->getQuotedColumns($platform));
206
207
        self::assertTrue($onlineForeignTable->hasColumn('"Fk"'));
208
        self::assertSame('"Fk"', $onlineForeignTable->getColumn('"Fk"')->getQuotedName($platform));
209
210
        self::assertTrue($onlineForeignTable->hasIndex('"Fk_index"'));
211
        self::assertTrue($onlineForeignTable->getIndex('"Fk_index"')->hasColumnAtPosition('"Fk"'));
212
        self::assertSame(['"Fk"'], $onlineForeignTable->getIndex('"Fk_index"')->getQuotedColumns($platform));
213
214
        self::assertTrue($onlineForeignTable->hasForeignKey('"Primary_Table_Fk"'));
215
        self::assertSame(
216
            $primaryTableName,
217
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignTableName($platform)
218
        );
219
        self::assertSame(
220
            ['"Fk"'],
221
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedLocalColumns($platform)
222
        );
223
        self::assertSame(
224
            ['"Id"'],
225
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignColumns($platform)
226
        );
227
    }
228
229
    public function testListTableColumnsSameTableNamesInDifferentSchemas() : void
230
    {
231
        $table = $this->createListTableColumns();
232
        $this->schemaManager->dropAndCreateTable($table);
233
234
        $otherTable = new Table($table->getName());
235
        $otherTable->addColumn('id', Types::STRING);
236
        TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);
237
238
        $columns = $this->schemaManager->listTableColumns($table->getName(), $this->connection->getUsername());
239
        self::assertCount(7, $columns);
240
    }
241
242
    /**
243
     * @group DBAL-1234
244
     */
245
    public function testListTableIndexesPrimaryKeyConstraintNameDiffersFromIndexName() : void
246
    {
247
        $table = new Table('list_table_indexes_pk_id_test');
248
        $table->setSchemaConfig($this->schemaManager->createSchemaConfig());
249
        $table->addColumn('id', 'integer', ['notnull' => true]);
250
        $table->addUniqueIndex(['id'], 'id_unique_index');
251
        $this->schemaManager->dropAndCreateTable($table);
252
253
        // Adding a primary key on already indexed columns
254
        // Oracle will reuse the unique index, which cause a constraint name differing from the index name
255
        $this->schemaManager->createConstraint(new Schema\Index('id_pk_id_index', ['id'], true, true), 'list_table_indexes_pk_id_test');
256
257
        $tableIndexes = $this->schemaManager->listTableIndexes('list_table_indexes_pk_id_test');
258
259
        self::assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
260
        self::assertEquals(['id'], array_map('strtolower', $tableIndexes['primary']->getColumns()));
261
        self::assertTrue($tableIndexes['primary']->isUnique());
262
        self::assertTrue($tableIndexes['primary']->isPrimary());
263
    }
264
265
    /**
266
     * @group DBAL-2555
267
     */
268
    public function testListTableDateTypeColumns() : void
269
    {
270
        $table = new Table('tbl_date');
271
        $table->addColumn('col_date', 'date');
272
        $table->addColumn('col_datetime', 'datetime');
273
        $table->addColumn('col_datetimetz', 'datetimetz');
274
275
        $this->schemaManager->dropAndCreateTable($table);
276
277
        $columns = $this->schemaManager->listTableColumns('tbl_date');
278
279
        self::assertSame('date', $columns['col_date']->getType()->getName());
280
        self::assertSame('datetime', $columns['col_datetime']->getType()->getName());
281
        self::assertSame('datetimetz', $columns['col_datetimetz']->getType()->getName());
282
    }
283
284
    public function testCreateAndListSequences() : void
285
    {
286
        self::markTestSkipped("Skipped for uppercase letters are contained in sequences' names. Fix the schema manager in 3.0.");
287
    }
288
289
    /**
290
     * @group DBAL-2766
291
     */
292
    public function testCreateSchemaNumberOfQueriesInvariable() : void
293
    {
294
        // Introspect the db schema.
295
        $preCount        = $this->_sqlLoggerStack->currentQuery;
0 ignored issues
show
Bug introduced by
The property _sqlLoggerStack does not exist on Doctrine\Tests\DBAL\Func...OracleSchemaManagerTest. Did you mean sqlLoggerStack?
Loading history...
296
        $schema          = $this->_sm->createSchema();
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
Bug Best Practice introduced by
The property _sm does not exist on Doctrine\Tests\DBAL\Func...OracleSchemaManagerTest. Did you maybe forget to declare it?
Loading history...
297
        $firstQueryCount = $this->_sqlLoggerStack->currentQuery - $preCount;
298
299
        // Create a couple of additional tables.
300
        $this->_conn->executeUpdate("CREATE TABLE tbl_test_2766_0 (x_id VARCHAR2(255) DEFAULT 'x' NOT NULL, x_data CLOB DEFAULT NULL NULL, x_number NUMBER(10) DEFAULT 0 NOT NULL, PRIMARY KEY(x_id))");
0 ignored issues
show
Bug Best Practice introduced by
The property _conn does not exist on Doctrine\Tests\DBAL\Func...OracleSchemaManagerTest. Did you maybe forget to declare it?
Loading history...
301
        $this->_conn->executeUpdate("CREATE TABLE tbl_test_2766_1 (x_id VARCHAR2(255) DEFAULT 'x' NOT NULL, x_data CLOB DEFAULT NULL NULL, x_number NUMBER(10) DEFAULT 0 NOT NULL, x_parent_id VARCHAR2(255) DEFAULT 'x' NOT NULL, CONSTRAINT tbl_test_2766_fk_1 FOREIGN KEY (x_parent_id) REFERENCES tbl_test_2766_0(x_id), PRIMARY KEY(x_id))");
302
        $this->_conn->executeUpdate('CREATE UNIQUE INDEX tbl_test_2766_uix_1 ON tbl_test_2766_1 (x_number)');
303
304
        // Introspect the db schema again.
305
        $preCount         = $this->_sqlLoggerStack->currentQuery;
306
        $schema           = $this->_sm->createSchema();
307
        $secondQueryCount = $this->_sqlLoggerStack->currentQuery - $preCount;
308
309
        // The number of queries needed to execute createSchema should be the
310
        // same regardless of additional tables added.
311
        $this->assertEquals($firstQueryCount, $secondQueryCount);
312
    }
313
}
314