Failed Conditions
Pull Request — master (#2766)
by mon
09:56
created

testCreateSchemaNumberOfQueriesInvariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
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\Type;
8
use Doctrine\Tests\TestUtil;
9
10
class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
11
{
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
16
        if(!isset($GLOBALS['db_username'])) {
17
            $this->markTestSkipped('Foo');
18
        }
19
20
        $username = $GLOBALS['db_username'];
21
22
        $query = "GRANT ALL PRIVILEGES TO ".$username;
23
24
        $conn = \Doctrine\Tests\TestUtil::getTempConnection();
25
        $conn->executeUpdate($query);
26
    }
27
28
    public function testRenameTable()
29
    {
30
        $this->_sm->tryMethod('DropTable', 'list_tables_test');
31
        $this->_sm->tryMethod('DropTable', 'list_tables_test_new_name');
32
33
        $this->createTestTable('list_tables_test');
34
        $this->_sm->renameTable('list_tables_test', 'list_tables_test_new_name');
35
36
        $tables = $this->_sm->listTables();
37
38
        self::assertHasTable($tables, 'list_tables_test_new_name');
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\DBAL\Func...tCase::assertHasTable() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        self::/** @scrutinizer ignore-call */ 
39
              assertHasTable($tables, 'list_tables_test_new_name');
Loading history...
39
    }
40
41 View Code Duplication
    public function testListTableWithBinary()
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...
42
    {
43
        $tableName = 'test_binary_table';
44
45
        $table = new \Doctrine\DBAL\Schema\Table($tableName);
46
        $table->addColumn('id', 'integer');
47
        $table->addColumn('column_varbinary', 'binary', array());
48
        $table->addColumn('column_binary', 'binary', array('fixed' => true));
49
        $table->setPrimaryKey(array('id'));
50
51
        $this->_sm->createTable($table);
52
53
        $table = $this->_sm->listTableDetails($tableName);
54
55
        self::assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_varbinary')->getType());
56
        self::assertFalse($table->getColumn('column_varbinary')->getFixed());
57
58
        self::assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType());
59
        self::assertFalse($table->getColumn('column_binary')->getFixed());
60
    }
61
62
    /**
63
     * @group DBAL-472
64
     * @group DBAL-1001
65
     */
66
    public function testAlterTableColumnNotNull()
67
    {
68
        $comparator = new Schema\Comparator();
69
        $tableName  = 'list_table_column_notnull';
70
        $table      = new Schema\Table($tableName);
71
72
        $table->addColumn('id', 'integer');
73
        $table->addColumn('foo', 'integer');
74
        $table->addColumn('bar', 'string');
75
        $table->setPrimaryKey(array('id'));
76
77
        $this->_sm->dropAndCreateTable($table);
78
79
        $columns = $this->_sm->listTableColumns($tableName);
80
81
        self::assertTrue($columns['id']->getNotnull());
82
        self::assertTrue($columns['foo']->getNotnull());
83
        self::assertTrue($columns['bar']->getNotnull());
84
85
        $diffTable = clone $table;
86
        $diffTable->changeColumn('foo', array('notnull' => false));
87
        $diffTable->changeColumn('bar', array('length' => 1024));
88
89
        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
90
91
        $columns = $this->_sm->listTableColumns($tableName);
92
93
        self::assertTrue($columns['id']->getNotnull());
94
        self::assertFalse($columns['foo']->getNotnull());
95
        self::assertTrue($columns['bar']->getNotnull());
96
    }
97
98
    public function testListDatabases()
99
    {
100
        // We need the temp connection that has privileges to create a database.
101
        $sm = TestUtil::getTempConnection()->getSchemaManager();
102
103
        $sm->dropAndCreateDatabase('c##test_create_database');
104
105
        $databases = $this->_sm->listDatabases();
106
        $databases = array_map('strtolower', $databases);
107
108
        self::assertContains('c##test_create_database', $databases);
109
    }
110
111
    /**
112
     * @group DBAL-831
113
     */
114
    public function testListTableDetailsWithDifferentIdentifierQuotingRequirements()
115
    {
116
        $primaryTableName = '"Primary_Table"';
117
        $offlinePrimaryTable = new Schema\Table($primaryTableName);
118
        $offlinePrimaryTable->addColumn(
119
            '"Id"',
120
            'integer',
121
            array('autoincrement' => true, 'comment' => 'Explicit casing.')
122
        );
123
        $offlinePrimaryTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword.'));
124
        $offlinePrimaryTable->addColumn('foo', 'integer', array('comment' => 'Implicit uppercasing.'));
125
        $offlinePrimaryTable->addColumn('BAR', 'integer');
126
        $offlinePrimaryTable->addColumn('"BAZ"', 'integer');
127
        $offlinePrimaryTable->addIndex(array('select'), 'from');
128
        $offlinePrimaryTable->addIndex(array('foo'), 'foo_index');
129
        $offlinePrimaryTable->addIndex(array('BAR'), 'BAR_INDEX');
130
        $offlinePrimaryTable->addIndex(array('"BAZ"'), 'BAZ_INDEX');
131
        $offlinePrimaryTable->setPrimaryKey(array('"Id"'));
132
133
        $foreignTableName = 'foreign';
134
        $offlineForeignTable = new Schema\Table($foreignTableName);
135
        $offlineForeignTable->addColumn('id', 'integer', array('autoincrement' => true));
136
        $offlineForeignTable->addColumn('"Fk"', 'integer');
137
        $offlineForeignTable->addIndex(array('"Fk"'), '"Fk_index"');
138
        $offlineForeignTable->addForeignKeyConstraint(
139
            $primaryTableName,
140
            array('"Fk"'),
141
            array('"Id"'),
142
            array(),
143
            '"Primary_Table_Fk"'
144
        );
145
        $offlineForeignTable->setPrimaryKey(array('id'));
146
147
        $this->_sm->tryMethod('dropTable', $foreignTableName);
148
        $this->_sm->tryMethod('dropTable', $primaryTableName);
149
150
        $this->_sm->createTable($offlinePrimaryTable);
151
        $this->_sm->createTable($offlineForeignTable);
152
153
        $onlinePrimaryTable = $this->_sm->listTableDetails($primaryTableName);
154
        $onlineForeignTable = $this->_sm->listTableDetails($foreignTableName);
155
156
        $platform = $this->_sm->getDatabasePlatform();
157
158
        // Primary table assertions
159
        self::assertSame($primaryTableName, $onlinePrimaryTable->getQuotedName($platform));
160
161
        self::assertTrue($onlinePrimaryTable->hasColumn('"Id"'));
162
        self::assertSame('"Id"', $onlinePrimaryTable->getColumn('"Id"')->getQuotedName($platform));
163
        self::assertTrue($onlinePrimaryTable->hasPrimaryKey());
164
        self::assertSame(array('"Id"'), $onlinePrimaryTable->getPrimaryKey()->getQuotedColumns($platform));
165
166
        self::assertTrue($onlinePrimaryTable->hasColumn('select'));
167
        self::assertSame('"select"', $onlinePrimaryTable->getColumn('select')->getQuotedName($platform));
168
169
        self::assertTrue($onlinePrimaryTable->hasColumn('foo'));
170
        self::assertSame('FOO', $onlinePrimaryTable->getColumn('foo')->getQuotedName($platform));
171
172
        self::assertTrue($onlinePrimaryTable->hasColumn('BAR'));
173
        self::assertSame('BAR', $onlinePrimaryTable->getColumn('BAR')->getQuotedName($platform));
174
175
        self::assertTrue($onlinePrimaryTable->hasColumn('"BAZ"'));
176
        self::assertSame('BAZ', $onlinePrimaryTable->getColumn('"BAZ"')->getQuotedName($platform));
177
178
        self::assertTrue($onlinePrimaryTable->hasIndex('from'));
179
        self::assertTrue($onlinePrimaryTable->getIndex('from')->hasColumnAtPosition('"select"'));
180
        self::assertSame(array('"select"'), $onlinePrimaryTable->getIndex('from')->getQuotedColumns($platform));
181
182
        self::assertTrue($onlinePrimaryTable->hasIndex('foo_index'));
183
        self::assertTrue($onlinePrimaryTable->getIndex('foo_index')->hasColumnAtPosition('foo'));
184
        self::assertSame(array('FOO'), $onlinePrimaryTable->getIndex('foo_index')->getQuotedColumns($platform));
185
186
        self::assertTrue($onlinePrimaryTable->hasIndex('BAR_INDEX'));
187
        self::assertTrue($onlinePrimaryTable->getIndex('BAR_INDEX')->hasColumnAtPosition('BAR'));
188
        self::assertSame(array('BAR'), $onlinePrimaryTable->getIndex('BAR_INDEX')->getQuotedColumns($platform));
189
190
        self::assertTrue($onlinePrimaryTable->hasIndex('BAZ_INDEX'));
191
        self::assertTrue($onlinePrimaryTable->getIndex('BAZ_INDEX')->hasColumnAtPosition('"BAZ"'));
192
        self::assertSame(array('BAZ'), $onlinePrimaryTable->getIndex('BAZ_INDEX')->getQuotedColumns($platform));
193
194
        // Foreign table assertions
195
        self::assertTrue($onlineForeignTable->hasColumn('id'));
196
        self::assertSame('ID', $onlineForeignTable->getColumn('id')->getQuotedName($platform));
197
        self::assertTrue($onlineForeignTable->hasPrimaryKey());
198
        self::assertSame(array('ID'), $onlineForeignTable->getPrimaryKey()->getQuotedColumns($platform));
199
200
        self::assertTrue($onlineForeignTable->hasColumn('"Fk"'));
201
        self::assertSame('"Fk"', $onlineForeignTable->getColumn('"Fk"')->getQuotedName($platform));
202
203
        self::assertTrue($onlineForeignTable->hasIndex('"Fk_index"'));
204
        self::assertTrue($onlineForeignTable->getIndex('"Fk_index"')->hasColumnAtPosition('"Fk"'));
205
        self::assertSame(array('"Fk"'), $onlineForeignTable->getIndex('"Fk_index"')->getQuotedColumns($platform));
206
207
        self::assertTrue($onlineForeignTable->hasForeignKey('"Primary_Table_Fk"'));
208
        self::assertSame(
209
            $primaryTableName,
210
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignTableName($platform)
211
        );
212
        self::assertSame(
213
            array('"Fk"'),
214
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedLocalColumns($platform)
215
        );
216
        self::assertSame(
217
            array('"Id"'),
218
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignColumns($platform)
219
        );
220
    }
221
222
    public function testListTableColumnsSameTableNamesInDifferentSchemas()
223
    {
224
        $table = $this->createListTableColumns();
225
        $this->_sm->dropAndCreateTable($table);
226
227
        $otherTable = new Table($table->getName());
228
        $otherTable->addColumn('id', Type::STRING);
229
        TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);
230
231
        $columns = $this->_sm->listTableColumns($table->getName(), $this->_conn->getUsername());
232
        self::assertCount(7, $columns);
233
    }
234
235
    /**
236
     * @group DBAL-1234
237
     */
238
    public function testListTableIndexesPrimaryKeyConstraintNameDiffersFromIndexName()
239
    {
240
        $table = new Table('list_table_indexes_pk_id_test');
241
        $table->setSchemaConfig($this->_sm->createSchemaConfig());
242
        $table->addColumn('id', 'integer', array('notnull' => true));
243
        $table->addUniqueIndex(array('id'), 'id_unique_index');
244
        $this->_sm->dropAndCreateTable($table);
245
246
        // Adding a primary key on already indexed columns
247
        // Oracle will reuse the unique index, which cause a constraint name differing from the index name
248
        $this->_sm->createConstraint(new Schema\Index('id_pk_id_index', array('id'), true, true), 'list_table_indexes_pk_id_test');
249
250
        $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_pk_id_test');
251
252
        self::assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
253
        self::assertEquals(array('id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
254
        self::assertTrue($tableIndexes['primary']->isUnique());
255
        self::assertTrue($tableIndexes['primary']->isPrimary());
256
    }
257
258
    /**
259
     * @group DBAL-2555
260
     */
261
    public function testListTableDateTypeColumns()
262
    {
263
        $table = new Table('tbl_date');
264
        $table->addColumn('col_date', 'date');
265
        $table->addColumn('col_datetime', 'datetime');
266
        $table->addColumn('col_datetimetz', 'datetimetz');
267
268
        $this->_sm->dropAndCreateTable($table);
269
270
        $columns = $this->_sm->listTableColumns('tbl_date');
271
272
        self::assertSame('date', $columns['col_date']->getType()->getName());
273
        self::assertSame('datetime', $columns['col_datetime']->getType()->getName());
274
        self::assertSame('datetimetz', $columns['col_datetimetz']->getType()->getName());
275
    }
276
277
    /**
278
     * @group DBAL-2766
279
     */
280
    public function testCreateSchemaNumberOfQueriesInvariable(): void
281
    {
282
        // Introspect the db schema.
283
        $preCount = $this->_sqlLoggerStack->currentQuery;
284
        $schema = $this->_sm->createSchema();
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
285
        $firstQueryCount = $this->_sqlLoggerStack->currentQuery - $preCount;
286
287
        // Create a couple of additional tables.
288
        $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))");
289
        $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))");
290
        $this->_conn->executeUpdate("CREATE UNIQUE INDEX tbl_test_2766_uix_1 ON tbl_test_2766_1 (x_number)");
291
292
        // Introspect the db schema again.
293
        $preCount = $this->_sqlLoggerStack->currentQuery;
294
        $schema = $this->_sm->createSchema();
295
        $secondQueryCount = $this->_sqlLoggerStack->currentQuery - $preCount;
296
297
        // The number of queries needed to execute createSchema should be the
298
        // same regardless of additional tables added.
299
        $this->assertEquals($firstQueryCount, $secondQueryCount);
300
    }
301
}
302