SchemaManagerFunctionalTestCase.php 17.7 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Functional\Schema;

5 6
use Doctrine\DBAL\Types\Type,
    Doctrine\DBAL\Schema\AbstractSchemaManager;
7

romanb's avatar
romanb committed
8 9 10
require_once __DIR__ . '/../../../TestInit.php';

class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
11
{
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    /**
     * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
     */
    protected $_sm;

    protected function setUp()
    {
        parent::setUp();

        $class = get_class($this);
        $e = explode('\\', $class);
        $testClass = end($e);
        $dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));

        if ($this->_conn->getDatabasePlatform()->getName() !== $dbms)
        {
            $this->markTestSkipped('The ' . $testClass .' requires the use of ' . $dbms);
        }

31 32
        #$this->_conn->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);

33 34 35
        $this->_sm = $this->_conn->getSchemaManager();
    }

36 37 38 39 40 41
    public function testListSequences()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
            $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
        }

42 43
        $sequence = new \Doctrine\DBAL\Schema\Sequence('list_sequences_test_seq', 20, 10);
        $this->_sm->createSequence($sequence);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        
        $sequences = $this->_sm->listSequences();
        
        $this->assertType('array', $sequences, 'listSequences() should return an array.');

        $foundSequence = null;
        foreach($sequences AS $sequence) {
            $this->assertType('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.');
            if(strtolower($sequence->getName()) == 'list_sequences_test_seq') {
                $foundSequence = $sequence;
            }
        }

        $this->assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found.");
        $this->assertEquals(20, $foundSequence->getAllocationSize(), "Allocation Size is expected to be 20.");
        $this->assertEquals(10, $foundSequence->getInitialValue(), "Initial Value is expected to be 10.");
    }

62 63
    public function testListDatabases()
    {
64 65 66 67
        if (!$this->_sm->getDatabasePlatform()->supportsCreateDropDatabase()) {
            $this->markTestSkipped('Cannot drop Database client side with this Driver.');
        }

68 69 70 71 72
        $this->_sm->dropAndCreateDatabase('test_create_database');
        $databases = $this->_sm->listDatabases();

        $databases = \array_map('strtolower', $databases);
        
73
        $this->assertEquals(true, \in_array('test_create_database', $databases));
74 75 76 77 78 79 80
    }

    public function testListTables()
    {
        $this->createTestTable('list_tables_test');
        $tables = $this->_sm->listTables();

81
        $this->assertType('array', $tables);
82
        $this->assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'.");
83 84 85 86

        $foundTable = false;
        foreach ($tables AS $table) {
            $this->assertType('Doctrine\DBAL\Schema\Table', $table);
87
            if (strtolower($table->getName()) == 'list_tables_test') {
88 89 90 91 92 93 94
                $foundTable = true;

                $this->assertTrue($table->hasColumn('id'));
                $this->assertTrue($table->hasColumn('test'));
                $this->assertTrue($table->hasColumn('foreign_key_test'));
            }
        }
95 96

        $this->assertTrue( $foundTable , "The 'list_tables_test' table has to be found.");
97 98 99 100
    }

    public function testListTableColumns()
    {
101
        $table = new \Doctrine\DBAL\Schema\Table('list_table_columns');
102 103 104 105 106 107 108
        $table->addColumn('id', 'integer', array('notnull' => true));
        $table->addColumn('test', 'string', array('length' => 255, 'notnull' => false));
        $table->addColumn('foo', 'text', array('notnull' => true));
        $table->addColumn('bar', 'decimal', array('precision' => 10, 'scale' => 4, 'notnull' => false));
        $table->addColumn('baz1', 'datetime');
        $table->addColumn('baz2', 'time');
        $table->addColumn('baz3', 'date');
109 110

        $this->_sm->dropAndCreateTable($table);
111 112 113 114

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

        $this->assertArrayHasKey('id', $columns);
115
        $this->assertEquals('id',   strtolower($columns['id']->getname()));
116 117 118 119 120
        $this->assertType('Doctrine\DBAL\Types\IntegerType', $columns['id']->gettype());
        $this->assertEquals(false,  $columns['id']->getunsigned());
        $this->assertEquals(true,   $columns['id']->getnotnull());
        $this->assertEquals(null,   $columns['id']->getdefault());
        $this->assertType('array',  $columns['id']->getPlatformOptions());
121 122

        $this->assertArrayHasKey('test', $columns);
123
        $this->assertEquals('test', strtolower($columns['test']->getname()));
124 125 126 127 128 129 130
        $this->assertType('Doctrine\DBAL\Types\StringType', $columns['test']->gettype());
        $this->assertEquals(255,    $columns['test']->getlength());
        $this->assertEquals(false,  $columns['test']->getfixed());
        $this->assertEquals(false,  $columns['test']->getnotnull());
        $this->assertEquals(null,   $columns['test']->getdefault());
        $this->assertType('array',  $columns['test']->getPlatformOptions());

131
        $this->assertEquals('foo',  strtolower($columns['foo']->getname()));
132 133 134 135 136 137 138
        $this->assertType('Doctrine\DBAL\Types\TextType', $columns['foo']->gettype());
        $this->assertEquals(false,  $columns['foo']->getunsigned());
        $this->assertEquals(false,  $columns['foo']->getfixed());
        $this->assertEquals(true,   $columns['foo']->getnotnull());
        $this->assertEquals(null,   $columns['foo']->getdefault());
        $this->assertType('array',  $columns['foo']->getPlatformOptions());

139
        $this->assertEquals('bar',  strtolower($columns['bar']->getname()));
140 141
        $this->assertType('Doctrine\DBAL\Types\DecimalType', $columns['bar']->gettype());
        $this->assertEquals(null,   $columns['bar']->getlength());
142 143
        $this->assertEquals(10,     $columns['bar']->getprecision());
        $this->assertEquals(4,      $columns['bar']->getscale());
144 145
        $this->assertEquals(false,  $columns['bar']->getunsigned());
        $this->assertEquals(false,  $columns['bar']->getfixed());
146
        $this->assertEquals(false,  $columns['bar']->getnotnull());
147 148 149
        $this->assertEquals(null,   $columns['bar']->getdefault());
        $this->assertType('array',  $columns['bar']->getPlatformOptions());

150
        $this->assertEquals('baz1', strtolower($columns['baz1']->getname()));
151
        $this->assertType('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
152
        $this->assertEquals(true,   $columns['baz1']->getnotnull());
153 154 155
        $this->assertEquals(null,   $columns['baz1']->getdefault());
        $this->assertType('array',  $columns['baz1']->getPlatformOptions());

156
        $this->assertEquals('baz2', strtolower($columns['baz2']->getname()));
157
        $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
158
        $this->assertEquals(true,   $columns['baz2']->getnotnull());
159 160
        $this->assertEquals(null,   $columns['baz2']->getdefault());
        $this->assertType('array',  $columns['baz2']->getPlatformOptions());
161
        
162
        $this->assertEquals('baz3', strtolower($columns['baz3']->getname()));
163
        $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
164
        $this->assertEquals(true,   $columns['baz3']->getnotnull());
165 166
        $this->assertEquals(null,   $columns['baz3']->getdefault());
        $this->assertType('array',  $columns['baz3']->getPlatformOptions());
167 168
    }

169 170
    public function testListTableIndexes()
    {
171 172 173
        $table = $this->getTestTable('list_table_indexes_test');
        $table->addUniqueIndex(array('test'), 'test_index_name');
        $table->addIndex(array('id', 'test'), 'test_composite_idx');
174

175
        $this->_sm->createTable($table);
176 177

        $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
178

179 180
        $this->assertEquals(3, count($tableIndexes));

181
        $this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
182 183 184
        $this->assertEquals(array('id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
        $this->assertTrue($tableIndexes['primary']->isUnique());
        $this->assertTrue($tableIndexes['primary']->isPrimary());
185

186 187 188 189
        $this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName());
        $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns()));
        $this->assertTrue($tableIndexes['test_index_name']->isUnique());
        $this->assertFalse($tableIndexes['test_index_name']->isPrimary());
190

191 192 193 194
        $this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName());
        $this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns()));
        $this->assertFalse($tableIndexes['test_composite_idx']->isUnique());
        $this->assertFalse($tableIndexes['test_composite_idx']->isPrimary());
195 196 197 198
    }

    public function testDropAndCreateIndex()
    {
199 200 201
        $table = $this->getTestTable('test_create_index');
        $table->addUniqueIndex(array('test'), 'test');
        $this->_sm->dropAndCreateTable($table);
202

203
        $this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
204
        $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
205 206
        $this->assertType('array', $tableIndexes);

207
        $this->assertEquals('test',        strtolower($tableIndexes['test']->getName()));
208 209 210 211 212
        $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test']->getColumns()));
        $this->assertTrue($tableIndexes['test']->isUnique());
        $this->assertFalse($tableIndexes['test']->isPrimary());
    }

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    public function testCreateTableWithForeignKeys()
    {
        if(!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('Platform does not support foreign keys.');
        }

        $tableB = $this->getTestTable('test_foreign');

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

        $tableA = $this->getTestTable('test_create_fk');
        $tableA->addForeignKeyConstraint('test_foreign', array('foreign_key_test'), array('id'));

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

        $fkConstraints = $this->_sm->listTableForeignKeys('test_create_fk');
229
        $this->assertEquals(1, count($fkConstraints), "Table 'test_create_fk1' has to have one foreign key.");
230 231

        $fkConstraint = current($fkConstraints);
232 233 234 235
        $this->assertType('\Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkConstraint);
        $this->assertEquals('test_foreign',             strtolower($fkConstraint->getForeignTableName()));
        $this->assertEquals(array('foreign_key_test'),  array_map('strtolower', $fkConstraint->getColumns()));
        $this->assertEquals(array('id'),                array_map('strtolower', $fkConstraint->getForeignColumns()));
236 237
    }

238 239 240 241 242 243 244 245 246
    public function testListForeignKeys()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('Does not support foreign key constraints.');
        }

        $this->createTestTable('test_create_fk1');
        $this->createTestTable('test_create_fk2');

247
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
248
            array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onDelete' => 'CASCADE')
249 250
        );

251
        $this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
252 253 254

        $fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');

255 256
        $this->assertEquals(1, count($fkeys), "Table 'test_create_fk1' has to have one foreign key.");
        
257
        $this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
258 259 260
        $this->assertEquals(array('foreign_key_test'),  array_map('strtolower', $fkeys[0]->getLocalColumns()));
        $this->assertEquals(array('id'),                array_map('strtolower', $fkeys[0]->getForeignColumns()));
        $this->assertEquals('test_create_fk2',          strtolower($fkeys[0]->getForeignTableName()));
261

262 263 264
        if($fkeys[0]->hasOption('onDelete')) {
            $this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
        }
265 266
    }

267 268 269 270 271
    protected function getCreateExampleViewSql()
    {
        $this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
    }

272 273 274 275 276 277 278 279 280
    public function testCreateSchema()
    {
        $this->createTestTable('test_table');

        $schema = $this->_sm->createSchema();

        $this->assertTrue($schema->hasTable('test_table'));
    }

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    public function testAlterTableScenario()
    {
        if(!$this->_sm->getDatabasePlatform()->supportsAlterTable()) {
            $this->markTestSkipped('Alter Table is not supported by this platform.');
        }

        $this->createTestTable('alter_table');
        $this->createTestTable('alter_table_foreign');

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertTrue($table->hasColumn('id'));
        $this->assertTrue($table->hasColumn('test'));
        $this->assertTrue($table->hasColumn('foreign_key_test'));
        $this->assertEquals(0, count($table->getForeignKeys()));
        $this->assertEquals(1, count($table->getIndexes()));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->addedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', Type::getType('integer'));
        $tableDiff->removedColumns['test'] = $table->getColumn('test');

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertFalse($table->hasColumn('test'));
        $this->assertTrue($table->hasColumn('foo'));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo'));

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertEquals(2, count($table->getIndexes()));
        $this->assertTrue($table->hasIndex('foo_idx'));
        $this->assertEquals(array('foo'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
        $this->assertFalse($table->getIndex('foo_idx')->isPrimary());
        $this->assertFalse($table->getIndex('foo_idx')->isUnique());

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertEquals(2, count($table->getIndexes()));
        $this->assertTrue($table->hasIndex('foo_idx'));
        $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id'));
        $tableDiff->addedForeignKeys[] = $fk;

        $this->_sm->alterTable($tableDiff);
        $table = $this->_sm->listTableDetails('alter_table');

        // dont check for index size here, some platforms automatically add indexes for foreign keys.
        $this->assertFalse($table->hasIndex('foo_idx'));

        $this->assertEquals(1, count($table->getForeignKeys()));
341 342
        $fks = $table->getForeignKeys();
        $foreignKey = current($fks);
343 344 345 346 347
        $this->assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName()));
        $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $foreignKey->getColumns()));
        $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
    }

348
    public function testCreateAndListViews()
349
    {
350
        $this->createTestTable('view_test_table');
351

352 353
        $name = "doctrine_test_view";
        $sql = "SELECT * FROM view_test_table";
354

355
        $view = new \Doctrine\DBAL\Schema\View($name, $sql);
356

357 358 359
        $this->_sm->dropAndCreateView($view);

        $views = $this->_sm->listViews();
360 361
    }

romanb's avatar
romanb committed
362
    protected function createTestTable($name = 'test_table', $data = array())
363 364 365 366 367 368
    {
        $options = array();
        if (isset($data['options'])) {
            $options = $data['options'];
        }

369 370 371 372 373 374 375 376
        $table = $this->getTestTable($name, $options);

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

    protected function getTestTable($name, $options=array())
    {
        $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), \Doctrine\DBAL\Schema\Table::ID_NONE, $options);
377
        $table->setSchemaConfig($this->_sm->createSchemaConfig());
378
        $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
379
        $table->addColumn('id', 'integer', array('notnull' => true));
380
        $table->setPrimaryKey(array('id'));
381 382
        $table->addColumn('test', 'string', array('length' => 255));
        $table->addColumn('foreign_key_test', 'integer');
383
        return $table;
384
    }
385 386 387 388 389 390 391 392 393 394 395 396

    protected function assertHasTable($tables, $tableName)
    {
        $foundTable = false;
        foreach ($tables AS $table) {
            $this->assertType('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.');
            if (strtolower($table->getName()) == 'list_tables_test_new_name') {
                $foundTable = true;
            }
        }
        $this->assertTrue($foundTable, "Could not find new table");
    }
397
}