From fe77e58077b6e8583afe5cdd2b9d621e265dfe30 Mon Sep 17 00:00:00 2001 From: Bizley Date: Sun, 29 Jan 2017 13:53:32 +0100 Subject: [PATCH] installation tests --- tests/unit/InstallationTest.php | 228 ++++++++++++++++++++++++++++++-- 1 file changed, 215 insertions(+), 13 deletions(-) diff --git a/tests/unit/InstallationTest.php b/tests/unit/InstallationTest.php index 74c8e19..53e4e6a 100644 --- a/tests/unit/InstallationTest.php +++ b/tests/unit/InstallationTest.php @@ -15,30 +15,113 @@ class InstallationTest extends Unit public $tester; public $steps = [ - 'createTest' => [ + 'createTableTest' => [ [ - 'table' => 'log', + 'table' => 'test', 'call' => 'createTable', 'data' => [ 'schema' => [ - 'id' => Schema::TYPE_BIGPK, + 'id' => Schema::TYPE_PK, 'level' => Schema::TYPE_INTEGER, 'category' => Schema::TYPE_STRING, - 'log_time' => Schema::TYPE_DOUBLE, - 'ip' => Schema::TYPE_STRING . '(20)', 'message' => Schema::TYPE_TEXT, - 'model' => Schema::TYPE_INTEGER, - 'user' => Schema::TYPE_INTEGER, + 'key_f' => Schema::TYPE_INTEGER, ], ], ] - ] + ], + 'addColumnTest' => [ + [ + 'table' => 'test', + 'call' => 'addColumn', + 'data' => [ + 'col' => 'added', + 'type' => Schema::TYPE_INTEGER, + ], + ] + ], + 'dropColumnTest' => [ + [ + 'table' => 'test', + 'call' => 'dropColumn', + 'data' => [ + 'col' => 'added', + ], + ] + ], + 'addIndexTest' => [ + [ + 'table' => 'test', + 'call' => 'addIndex', + 'data' => [ + 'name' => 'sort', + 'cols' => ['level'], + ], + ] + ], + 'dropIndexTest' => [ + [ + 'table' => 'test', + 'call' => 'dropIndex', + 'data' => [ + 'name' => 'sort', + ], + ] + ], + 'addForeignTest' => [ + [ + 'table' => 'test', + 'call' => 'addForeign', + 'data' => [ + 'key' => 'key_f', + 'ref' => 'test_foreign', + 'col' => 'id', + 'delete' => 'CASCADE', + 'update' => 'CASCADE', + ], + ] + ], + 'dropForeignTest' => [ + [ + 'table' => 'test', + 'call' => 'dropForeign', + 'data' => [ + 'name' => 'key_f', + ], + ] + ], + 'alterColumnTest' => [ + [ + 'table' => 'test', + 'call' => 'alterColumn', + 'data' => [ + 'col' => 'message', + 'type' => Schema::TYPE_STRING, + ], + ] + ], + 'renameColumnTest' => [ + [ + 'table' => 'test', + 'call' => 'renameColumn', + 'data' => [ + 'col' => 'message', + 'name' => 'messageRenamed', + ], + ] + ], ]; + protected function _before() + { + parent::_before(); + \Yii::$app->session->remove(Installation::SESSION_KEY); + } + public static function tearDownAfterClass() { $db = (new Installation)->db; - $tables = ['log']; + $tables = ['test', 'test_foreign']; foreach ($tables as $table) { if ($db->schema->getTableSchema("{{%podium_$table}}", true) !== null) { $db->createCommand()->dropTable("{{%podium_$table}}")->execute(); @@ -49,22 +132,141 @@ public static function tearDownAfterClass() public function testCreateTable() { - \Yii::$app->session->remove(Installation::SESSION_KEY); - $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['createTest']]); + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['createTableTest']]); $result = $install->nextStep(); expect($result['drop'])->equals(false); expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table podium_test has been created'); + expect($result['table'])->equals('podium_test'); + expect_that($install->db->schema->getTableSchema("{{%podium_test}}", true)); } + /** + * @depends testCreateTable + */ + public function testAddColumn() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['addColumnTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table column added has been added'); + expect($result['table'])->equals('podium_test'); + expect($install->db->schema->getTableSchema("{{%podium_test}}", true)->columns)->hasKey('added'); + } + + /** + * @depends testAddColumn + */ + public function testDropColumn() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['dropColumnTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_WARNING); + expect($result['result'])->equals('Table column added has been dropped'); + expect($result['table'])->equals('podium_test'); + expect($install->db->schema->getTableSchema("{{%podium_test}}", true)->columns)->hasntKey('added'); + } + + /** + * @depends testCreateTable + */ + public function testAddIndex() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['addIndexTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table index idx-podium_test-sort has been added'); + expect($result['table'])->equals('podium_test'); + } + + /** + * @depends testAddIndex + */ + public function testDropIndex() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['dropIndexTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_WARNING); + expect($result['result'])->equals('Table index idx-podium_test-sort has been dropped'); + expect($result['table'])->equals('podium_test'); + } + + /** + * @depends testCreateTable + */ + public function testAddForeign() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['addForeignTest']]); + $install->db->createCommand()->createTable('{{%podium_test_foreign}}', [ + 'id' => Schema::TYPE_PK, + 'level' => Schema::TYPE_INTEGER + ], $install->tableOptions)->execute(); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table foreign key fk-podium_test-key_f has been added'); + expect($result['table'])->equals('podium_test'); + } + + /** + * @depends testAddForeign + */ + public function testDropForeign() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['dropForeignTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_WARNING); + expect($result['result'])->equals('Table foreign key fk-podium_test-key_f has been dropped'); + expect($result['table'])->equals('podium_test'); + $install->db->createCommand()->dropTable("{{%podium_test_foreign}}")->execute(); + } + + /** + * @depends testCreateTable + */ + public function testAlterColumn() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['alterColumnTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table column message has been updated'); + expect($result['table'])->equals('podium_test'); + } + + /** + * @depends testCreateTable + */ + public function testRenameColumn() + { + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['renameColumnTest']]); + $result = $install->nextStep(); + expect($result['drop'])->equals(false); + expect($result['type'])->equals(Installation::TYPE_SUCCESS); + expect($result['result'])->equals('Table column message has been renamed to messageRenamed'); + expect($result['table'])->equals('podium_test'); + expect($install->db->schema->getTableSchema("{{%podium_test}}", true)->columns)->hasntKey('message'); + expect($install->db->schema->getTableSchema("{{%podium_test}}", true)->columns)->hasKey('messageRenamed'); + } + + /** * @depends testCreateTable */ public function testDropTable() { - \Yii::$app->session->remove(Installation::SESSION_KEY); - $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['createTest']]); + $install = Stub::construct(Installation::className(), [], ['_steps' => $this->steps['createTableTest']]); $result = $install->nextDrop(); expect_that($result['drop']); expect($result['type'])->equals(Installation::TYPE_WARNING); + expect($result['result'])->equals('Table podium_test has been dropped'); + expect($result['table'])->equals('podium_test'); + expect_not($install->db->schema->getTableSchema("{{%podium_test}}", true)); } } +