OracleSchemaManagerTest.php 10.4 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Functional\Schema;

use Doctrine\DBAL\Schema;
6 7
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
8
use Doctrine\Tests\TestUtil;
9 10

require_once __DIR__ . '/../../../TestInit.php';
11

romanb's avatar
romanb committed
12
class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
13
{
14
    public function setUp()
15
    {
16 17 18 19 20 21 22 23 24 25 26 27
        parent::setUp();

        if(!isset($GLOBALS['db_username'])) {
            $this->markTestSkipped('Foo');
        }

        $username = $GLOBALS['db_username'];

        $query = "GRANT ALL PRIVILEGES TO ".$username;

        $conn = \Doctrine\Tests\TestUtil::getTempConnection();
        $conn->executeUpdate($query);
28 29 30 31
    }

    public function testRenameTable()
    {
32 33
        $this->_sm->tryMethod('DropTable', 'list_tables_test');
        $this->_sm->tryMethod('DropTable', 'list_tables_test_new_name');
34 35 36 37 38

        $this->createTestTable('list_tables_test');
        $this->_sm->renameTable('list_tables_test', 'list_tables_test_new_name');

        $tables = $this->_sm->listTables();
39 40

        $this->assertHasTable($tables, 'list_tables_test_new_name');
41
    }
Steve Müller's avatar
Steve Müller committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    public function testListTableWithBinary()
    {
        $tableName = 'test_binary_table';

        $table = new \Doctrine\DBAL\Schema\Table($tableName);
        $table->addColumn('id', 'integer');
        $table->addColumn('column_varbinary', 'binary', array());
        $table->addColumn('column_binary', 'binary', array('fixed' => true));
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);

        $table = $this->_sm->listTableDetails($tableName);

        $this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_varbinary')->getType());
        $this->assertFalse($table->getColumn('column_varbinary')->getFixed());

        $this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType());
        $this->assertFalse($table->getColumn('column_binary')->getFixed());
    }
63 64 65

    /**
     * @group DBAL-472
66
     * @group DBAL-1001
67 68 69 70 71 72 73 74 75
     */
    public function testAlterTableColumnNotNull()
    {
        $comparator = new Schema\Comparator();
        $tableName  = 'list_table_column_notnull';
        $table      = new Schema\Table($tableName);

        $table->addColumn('id', 'integer');
        $table->addColumn('foo', 'integer');
76
        $table->addColumn('bar', 'string');
77 78 79 80 81 82 83 84
        $table->setPrimaryKey(array('id'));

        $this->_sm->dropAndCreateTable($table);

        $columns = $this->_sm->listTableColumns($tableName);

        $this->assertTrue($columns['id']->getNotnull());
        $this->assertTrue($columns['foo']->getNotnull());
85
        $this->assertTrue($columns['bar']->getNotnull());
86 87 88

        $diffTable = clone $table;
        $diffTable->changeColumn('foo', array('notnull' => false));
89
        $diffTable->changeColumn('bar', array('length' => 1024));
90 91 92 93 94 95 96

        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));

        $columns = $this->_sm->listTableColumns($tableName);

        $this->assertTrue($columns['id']->getNotnull());
        $this->assertFalse($columns['foo']->getNotnull());
97
        $this->assertTrue($columns['bar']->getNotnull());
98
    }
99 100 101 102 103 104 105 106 107

    public function testListDatabases()
    {
        // We need the temp connection that has privileges to create a database.
        $sm = TestUtil::getTempConnection()->getSchemaManager();

        $sm->dropAndCreateDatabase('c##test_create_database');

        $databases = $this->_sm->listDatabases();
108
        $databases = array_map('strtolower', $databases);
109

110
        $this->assertContains('c##test_create_database', $databases);
111
    }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    /**
     * @group DBAL-831
     */
    public function testListTableDetailsWithDifferentIdentifierQuotingRequirements()
    {
        $primaryTableName = '"Primary_Table"';
        $offlinePrimaryTable = new Schema\Table($primaryTableName);
        $offlinePrimaryTable->addColumn(
            '"Id"',
            'integer',
            array('autoincrement' => true, 'comment' => 'Explicit casing.')
        );
        $offlinePrimaryTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword.'));
        $offlinePrimaryTable->addColumn('foo', 'integer', array('comment' => 'Implicit uppercasing.'));
        $offlinePrimaryTable->addColumn('BAR', 'integer');
        $offlinePrimaryTable->addColumn('"BAZ"', 'integer');
        $offlinePrimaryTable->addIndex(array('select'), 'from');
        $offlinePrimaryTable->addIndex(array('foo'), 'foo_index');
        $offlinePrimaryTable->addIndex(array('BAR'), 'BAR_INDEX');
        $offlinePrimaryTable->addIndex(array('"BAZ"'), 'BAZ_INDEX');
        $offlinePrimaryTable->setPrimaryKey(array('"Id"'));

        $foreignTableName = 'foreign';
        $offlineForeignTable = new Schema\Table($foreignTableName);
        $offlineForeignTable->addColumn('id', 'integer', array('autoincrement' => true));
        $offlineForeignTable->addColumn('"Fk"', 'integer');
        $offlineForeignTable->addIndex(array('"Fk"'), '"Fk_index"');
        $offlineForeignTable->addForeignKeyConstraint(
            $primaryTableName,
            array('"Fk"'),
            array('"Id"'),
            array(),
            '"Primary_Table_Fk"'
        );
        $offlineForeignTable->setPrimaryKey(array('id'));

        $this->_sm->tryMethod('dropTable', $foreignTableName);
        $this->_sm->tryMethod('dropTable', $primaryTableName);

        $this->_sm->createTable($offlinePrimaryTable);
        $this->_sm->createTable($offlineForeignTable);

        $onlinePrimaryTable = $this->_sm->listTableDetails($primaryTableName);
        $onlineForeignTable = $this->_sm->listTableDetails($foreignTableName);

        $platform = $this->_sm->getDatabasePlatform();

        // Primary table assertions
        $this->assertSame($primaryTableName, $onlinePrimaryTable->getQuotedName($platform));

        $this->assertTrue($onlinePrimaryTable->hasColumn('"Id"'));
        $this->assertSame('"Id"', $onlinePrimaryTable->getColumn('"Id"')->getQuotedName($platform));
        $this->assertTrue($onlinePrimaryTable->hasPrimaryKey());
        $this->assertSame(array('"Id"'), $onlinePrimaryTable->getPrimaryKey()->getQuotedColumns($platform));

        $this->assertTrue($onlinePrimaryTable->hasColumn('select'));
        $this->assertSame('"select"', $onlinePrimaryTable->getColumn('select')->getQuotedName($platform));

        $this->assertTrue($onlinePrimaryTable->hasColumn('foo'));
        $this->assertSame('FOO', $onlinePrimaryTable->getColumn('foo')->getQuotedName($platform));

        $this->assertTrue($onlinePrimaryTable->hasColumn('BAR'));
        $this->assertSame('BAR', $onlinePrimaryTable->getColumn('BAR')->getQuotedName($platform));

        $this->assertTrue($onlinePrimaryTable->hasColumn('"BAZ"'));
        $this->assertSame('BAZ', $onlinePrimaryTable->getColumn('"BAZ"')->getQuotedName($platform));

        $this->assertTrue($onlinePrimaryTable->hasIndex('from'));
        $this->assertTrue($onlinePrimaryTable->getIndex('from')->hasColumnAtPosition('"select"'));
        $this->assertSame(array('"select"'), $onlinePrimaryTable->getIndex('from')->getQuotedColumns($platform));

        $this->assertTrue($onlinePrimaryTable->hasIndex('foo_index'));
        $this->assertTrue($onlinePrimaryTable->getIndex('foo_index')->hasColumnAtPosition('foo'));
        $this->assertSame(array('FOO'), $onlinePrimaryTable->getIndex('foo_index')->getQuotedColumns($platform));

        $this->assertTrue($onlinePrimaryTable->hasIndex('BAR_INDEX'));
        $this->assertTrue($onlinePrimaryTable->getIndex('BAR_INDEX')->hasColumnAtPosition('BAR'));
        $this->assertSame(array('BAR'), $onlinePrimaryTable->getIndex('BAR_INDEX')->getQuotedColumns($platform));

        $this->assertTrue($onlinePrimaryTable->hasIndex('BAZ_INDEX'));
        $this->assertTrue($onlinePrimaryTable->getIndex('BAZ_INDEX')->hasColumnAtPosition('"BAZ"'));
        $this->assertSame(array('BAZ'), $onlinePrimaryTable->getIndex('BAZ_INDEX')->getQuotedColumns($platform));

        // Foreign table assertions
        $this->assertTrue($onlineForeignTable->hasColumn('id'));
        $this->assertSame('ID', $onlineForeignTable->getColumn('id')->getQuotedName($platform));
        $this->assertTrue($onlineForeignTable->hasPrimaryKey());
        $this->assertSame(array('ID'), $onlineForeignTable->getPrimaryKey()->getQuotedColumns($platform));

        $this->assertTrue($onlineForeignTable->hasColumn('"Fk"'));
        $this->assertSame('"Fk"', $onlineForeignTable->getColumn('"Fk"')->getQuotedName($platform));

        $this->assertTrue($onlineForeignTable->hasIndex('"Fk_index"'));
        $this->assertTrue($onlineForeignTable->getIndex('"Fk_index"')->hasColumnAtPosition('"Fk"'));
        $this->assertSame(array('"Fk"'), $onlineForeignTable->getIndex('"Fk_index"')->getQuotedColumns($platform));

        $this->assertTrue($onlineForeignTable->hasForeignKey('"Primary_Table_Fk"'));
        $this->assertSame(
            $primaryTableName,
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignTableName($platform)
        );
        $this->assertSame(
            array('"Fk"'),
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedLocalColumns($platform)
        );
        $this->assertSame(
            array('"Id"'),
            $onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignColumns($platform)
        );
    }
223 224 225 226 227 228 229 230 231 232 233 234 235

    public function testListTableColumnsSameTableNamesInDifferentSchemas()
    {
        $table = $this->createListTableColumns();
        $this->_sm->dropAndCreateTable($table);

        $otherTable = new Table($table->getName());
        $otherTable->addColumn('id', Type::STRING);
        TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);

        $columns = $this->_sm->listTableColumns($table->getName(), $this->_conn->getUsername());
        $this->assertCount(7, $columns);
    }
236 237 238 239 240 241 242 243 244

    /**
     * @group DBAL-2555
     */
    public function testListTableDateTypeColumns()
    {
        $table = new Table('tbl_date');
        $table->addColumn('col_date', 'date');
        $table->addColumn('col_datetime', 'datetime');
245
        $table->addColumn('col_datetimetz', 'datetimetz');
246 247 248 249 250 251 252

        $this->_sm->dropAndCreateTable($table);

        $columns = $this->_sm->listTableColumns('tbl_date');

        $this->assertSame('date', $columns['col_date']->getType()->getName());
        $this->assertSame('datetime', $columns['col_datetime']->getType()->getName());
253
        $this->assertSame('datetimetz', $columns['col_datetimetz']->getType()->getName());
254
    }
Steve Müller's avatar
Steve Müller committed
255
}