diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index 1162a30167..66a1510d81 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -314,7 +314,10 @@ SQL; public function testCreateTable() { $db = $this->getConnection(); - $db->createCommand('DROP TABLE IF EXISTS {{testCreateTable}};')->execute(); + + if($db->getSchema()->getTableSchema('testCreateTable') !== null){ + $db->createCommand()->dropTable('testCreateTable')->execute(); + } $db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); $db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute(); @@ -331,7 +334,10 @@ SQL; } $db = $this->getConnection(); - $db->createCommand("DROP TABLE IF EXISTS {{testAlterTable}};")->execute(); + + if($db->getSchema()->getTableSchema('testAlterTable') !== null){ + $db->createCommand()->dropTable('testAlterTable')->execute(); + } $db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); $db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute(); @@ -347,6 +353,47 @@ SQL; } + public function testDropTable() + { + $db = $this->getConnection(); + + $tableName = 'type'; + $this->assertNotNull($db->getSchema()->getTableSchema($tableName)); + $db->createCommand()->dropTable($tableName)->execute(); + $this->assertNull($db->getSchema()->getTableSchema($tableName)); + } + + public function testTruncateTable() + { + $db = $this->getConnection(); + + $rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll(); + $this->assertEquals(2, count($rows)); + $db->createCommand()->truncateTable('animal')->execute(); + $rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll(); + $this->assertEquals(0, count($rows)); + } + + public function testRenameTable() + { + $db = $this->getConnection(); + + $fromTableName = 'type'; + $toTableName = 'new_type'; + + if($db->getSchema()->getTableSchema($toTableName) !== null){ + $db->createCommand()->dropTable($toTableName)->execute(); + } + + $this->assertNotNull($db->getSchema()->getTableSchema($fromTableName)); + $this->assertNull($db->getSchema()->getTableSchema($toTableName)); + + $db->createCommand()->renameTable($fromTableName, $toTableName)->execute(); + + $this->assertNull($db->getSchema()->getTableSchema($fromTableName, true)); + $this->assertNotNull($db->getSchema()->getTableSchema($toTableName, true)); + } + /* public function testUpdate() { @@ -356,18 +403,6 @@ SQL; { } - public function testRenameTable() - { - } - - public function testDropTable() - { - } - - public function testTruncateTable() - { - } - public function testAddColumn() { } diff --git a/tests/framework/db/mssql/MssqlCommandTest.php b/tests/framework/db/mssql/MssqlCommandTest.php index 8d6f42c236..28059de7b3 100644 --- a/tests/framework/db/mssql/MssqlCommandTest.php +++ b/tests/framework/db/mssql/MssqlCommandTest.php @@ -82,49 +82,4 @@ class MssqlCommandTest extends CommandTest $command->bindValue(':name', 'user5'); $this->assertEquals('user5@example.com', $command->queryScalar()); } - - public function testCreateTable() - { - $db = $this->getConnection(); - // on the first run the 'testCreateTable' table does not exist - try { - $db->createCommand()->dropTable('testCreateTable')->execute(); - } catch (\Exception $ex) { - // 'testCreateTable' table does not exist - } - - $db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); - $db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute(); - $records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testCreateTable}};')->queryAll(); - $this->assertEquals([ - ['id' => 1, 'bar' => 1], - ], $records); - } - - public function testAlterTable() - { - if ($this->driverName === 'sqlite'){ - $this->markTestSkipped('Sqlite does not support alterTable'); - } - - $db = $this->getConnection(); - // on the first run the 'testAlterTable' table does not exist - try { - $db->createCommand()->dropTable('testAlterTable')->execute(); - } catch (\Exception $ex) { - // 'testAlterTable' table does not exist - } - - $db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); - $db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute(); - - $db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute(); - - $db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute(); - $records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}};')->queryAll(); - $this->assertEquals([ - ['id' => 1, 'bar' => 1], - ['id' => 2, 'bar' => 'hello'], - ], $records); - } } diff --git a/tests/framework/db/oci/OracleCommandTest.php b/tests/framework/db/oci/OracleCommandTest.php index 66b7280a23..435b536e65 100644 --- a/tests/framework/db/oci/OracleCommandTest.php +++ b/tests/framework/db/oci/OracleCommandTest.php @@ -30,49 +30,4 @@ class OracleCommandTest extends CommandTest $command->execute(); $this->assertEquals(3, $db->getSchema()->getLastInsertID('profile_SEQ')); } - - public function testCreateTable() - { - $db = $this->getConnection(); - // on the first run the 'testCreateTable' table does not exist - try { - $db->createCommand()->dropTable('testCreateTable')->execute(); - } catch (\Exception $ex) { - // 'testCreateTable' table does not exist - } - - $db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); - $db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute(); - $records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testCreateTable}};')->queryAll(); - $this->assertEquals([ - ['id' => 1, 'bar' => 1], - ], $records); - } - - public function testAlterTable() - { - if ($this->driverName === 'sqlite'){ - $this->markTestSkipped('Sqlite does not support alterTable'); - } - - $db = $this->getConnection(); - // on the first run the 'testAlterTable' table does not exist - try { - $db->createCommand()->dropTable('testAlterTable')->execute(); - } catch (\Exception $ex) { - // 'testAlterTable' table does not exist - } - - $db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute(); - $db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute(); - - $db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute(); - - $db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute(); - $records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}};')->queryAll(); - $this->assertEquals([ - ['id' => 1, 'bar' => 1], - ['id' => 2, 'bar' => 'hello'], - ], $records); - } }