diff --git a/tests/data/console/migrate_create/add_columns_fk.php b/tests/data/console/migrate_create/add_columns_fk.php new file mode 100644 index 00000000000..7da3034fb50 --- /dev/null +++ b/tests/data/console/migrate_create/add_columns_fk.php @@ -0,0 +1,128 @@ +addColumn('test', 'user_id', \$this->integer()); + \$this->addColumn('test', 'product_id', \$this->integer()->unsigned()->notNull()); + \$this->addColumn('test', 'order_id', \$this->integer()->notNull()); + \$this->addColumn('test', 'created_at', \$this->dateTime()->notNull()); + + // creates index for column `user_id` + \$this->createIndex( + 'idx-test-user_id', + 'test', + 'user_id' + ); + + // add foreign key for table `user` + \$this->addForeignKey( + 'fk-test-user_id', + 'test', + 'user_id', + 'user', + 'id', + 'CASCADE' + ); + + // creates index for column `product_id` + \$this->createIndex( + 'idx-test-product_id', + 'test', + 'product_id' + ); + + // add foreign key for table `product` + \$this->addForeignKey( + 'fk-test-product_id', + 'test', + 'product_id', + 'product', + 'id', + 'CASCADE' + ); + + // creates index for column `order_id` + \$this->createIndex( + 'idx-test-order_id', + 'test', + 'order_id' + ); + + // add foreign key for table `user_order` + \$this->addForeignKey( + 'fk-test-order_id', + 'test', + 'order_id', + 'user_order', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `user` + \$this->dropForeignKey( + 'fk-test-user_id', + 'test' + ); + + // drops index for column `user_id` + \$this->dropIndex( + 'idx-test-user_id', + 'test' + ); + + // drops foreign key for table `product` + \$this->dropForeignKey( + 'fk-test-product_id', + 'test' + ); + + // drops index for column `product_id` + \$this->dropIndex( + 'idx-test-product_id', + 'test' + ); + + // drops foreign key for table `user_order` + \$this->dropForeignKey( + 'fk-test-order_id', + 'test' + ); + + // drops index for column `order_id` + \$this->dropIndex( + 'idx-test-order_id', + 'test' + ); + + \$this->dropColumn('test', 'user_id'); + \$this->dropColumn('test', 'product_id'); + \$this->dropColumn('test', 'order_id'); + \$this->dropColumn('test', 'created_at'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/add_columns_prefix.php b/tests/data/console/migrate_create/add_columns_prefix.php new file mode 100644 index 00000000000..c9fb959be76 --- /dev/null +++ b/tests/data/console/migrate_create/add_columns_prefix.php @@ -0,0 +1,128 @@ +addColumn('{{%test}}', 'user_id', \$this->integer()); + \$this->addColumn('{{%test}}', 'product_id', \$this->integer()->unsigned()->notNull()); + \$this->addColumn('{{%test}}', 'order_id', \$this->integer()->notNull()); + \$this->addColumn('{{%test}}', 'created_at', \$this->dateTime()->notNull()); + + // creates index for column `user_id` + \$this->createIndex( + '{{%idx-test-user_id}}', + '{{%test}}', + 'user_id' + ); + + // add foreign key for table `{{%user}}` + \$this->addForeignKey( + '{{%fk-test-user_id}}', + '{{%test}}', + 'user_id', + '{{%user}}', + 'id', + 'CASCADE' + ); + + // creates index for column `product_id` + \$this->createIndex( + '{{%idx-test-product_id}}', + '{{%test}}', + 'product_id' + ); + + // add foreign key for table `{{%product}}` + \$this->addForeignKey( + '{{%fk-test-product_id}}', + '{{%test}}', + 'product_id', + '{{%product}}', + 'id', + 'CASCADE' + ); + + // creates index for column `order_id` + \$this->createIndex( + '{{%idx-test-order_id}}', + '{{%test}}', + 'order_id' + ); + + // add foreign key for table `{{%user_order}}` + \$this->addForeignKey( + '{{%fk-test-order_id}}', + '{{%test}}', + 'order_id', + '{{%user_order}}', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `{{%user}}` + \$this->dropForeignKey( + '{{%fk-test-user_id}}', + '{{%test}}' + ); + + // drops index for column `user_id` + \$this->dropIndex( + '{{%idx-test-user_id}}', + '{{%test}}' + ); + + // drops foreign key for table `{{%product}}` + \$this->dropForeignKey( + '{{%fk-test-product_id}}', + '{{%test}}' + ); + + // drops index for column `product_id` + \$this->dropIndex( + '{{%idx-test-product_id}}', + '{{%test}}' + ); + + // drops foreign key for table `{{%user_order}}` + \$this->dropForeignKey( + '{{%fk-test-order_id}}', + '{{%test}}' + ); + + // drops index for column `order_id` + \$this->dropIndex( + '{{%idx-test-order_id}}', + '{{%test}}' + ); + + \$this->dropColumn('{{%test}}', 'user_id'); + \$this->dropColumn('{{%test}}', 'product_id'); + \$this->dropColumn('{{%test}}', 'order_id'); + \$this->dropColumn('{{%test}}', 'created_at'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/add_columns_test.php b/tests/data/console/migrate_create/add_columns_test.php new file mode 100644 index 00000000000..147e10d8dd1 --- /dev/null +++ b/tests/data/console/migrate_create/add_columns_test.php @@ -0,0 +1,36 @@ +addColumn('test', 'title', \$this->string(10)->notNull()); + \$this->addColumn('test', 'body', \$this->text()->notNull()); + \$this->addColumn('test', 'price', \$this->money(11,2)->notNull()); + \$this->addColumn('test', 'created_at', \$this->dateTime()); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->dropColumn('test', 'title'); + \$this->dropColumn('test', 'body'); + \$this->dropColumn('test', 'price'); + \$this->dropColumn('test', 'created_at'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_fields.php b/tests/data/console/migrate_create/create_fields.php new file mode 100644 index 00000000000..b18a5dded40 --- /dev/null +++ b/tests/data/console/migrate_create/create_fields.php @@ -0,0 +1,35 @@ +createTable('test', [ + 'id' => \$this->primaryKey(), + 'title' => \$this->string(10)->notNull()->unique()->defaultValue("test"), + 'body' => \$this->text()->notNull(), + 'price' => \$this->money(11,2)->notNull(), + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->dropTable('test'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_foreign_key.php b/tests/data/console/migrate_create/create_foreign_key.php new file mode 100644 index 00000000000..6f5b40c5cda --- /dev/null +++ b/tests/data/console/migrate_create/create_foreign_key.php @@ -0,0 +1,128 @@ +createTable('test', [ + 'id' => \$this->primaryKey(), + 'user_id' => \$this->integer(), + 'product_id' => \$this->integer()->unsigned()->notNull(), + 'order_id' => \$this->integer()->notNull(), + 'created_at' => \$this->dateTime()->notNull(), + ]); + + // creates index for column `user_id` + \$this->createIndex( + 'idx-test-user_id', + 'test', + 'user_id' + ); + + // add foreign key for table `user` + \$this->addForeignKey( + 'fk-test-user_id', + 'test', + 'user_id', + 'user', + 'id', + 'CASCADE' + ); + + // creates index for column `product_id` + \$this->createIndex( + 'idx-test-product_id', + 'test', + 'product_id' + ); + + // add foreign key for table `product` + \$this->addForeignKey( + 'fk-test-product_id', + 'test', + 'product_id', + 'product', + 'id', + 'CASCADE' + ); + + // creates index for column `order_id` + \$this->createIndex( + 'idx-test-order_id', + 'test', + 'order_id' + ); + + // add foreign key for table `user_order` + \$this->addForeignKey( + 'fk-test-order_id', + 'test', + 'order_id', + 'user_order', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `user` + \$this->dropForeignKey( + 'fk-test-user_id', + 'test' + ); + + // drops index for column `user_id` + \$this->dropIndex( + 'idx-test-user_id', + 'test' + ); + + // drops foreign key for table `product` + \$this->dropForeignKey( + 'fk-test-product_id', + 'test' + ); + + // drops index for column `product_id` + \$this->dropIndex( + 'idx-test-product_id', + 'test' + ); + + // drops foreign key for table `user_order` + \$this->dropForeignKey( + 'fk-test-order_id', + 'test' + ); + + // drops index for column `order_id` + \$this->dropIndex( + 'idx-test-order_id', + 'test' + ); + + \$this->dropTable('test'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_id_pk.php b/tests/data/console/migrate_create/create_id_pk.php new file mode 100644 index 00000000000..5173bd649f4 --- /dev/null +++ b/tests/data/console/migrate_create/create_id_pk.php @@ -0,0 +1,35 @@ +createTable('test', [ + 'id' => \$this->primaryKey(), + 'address' => \$this->string(), + 'address2' => \$this->string(), + 'email' => \$this->string(), + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->dropTable('test'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_prefix.php b/tests/data/console/migrate_create/create_prefix.php new file mode 100644 index 00000000000..04dc5757aba --- /dev/null +++ b/tests/data/console/migrate_create/create_prefix.php @@ -0,0 +1,128 @@ +createTable('{{%test}}', [ + 'id' => \$this->primaryKey(), + 'user_id' => \$this->integer(), + 'product_id' => \$this->integer()->unsigned()->notNull(), + 'order_id' => \$this->integer()->notNull(), + 'created_at' => \$this->dateTime()->notNull(), + ]); + + // creates index for column `user_id` + \$this->createIndex( + '{{%idx-test-user_id}}', + '{{%test}}', + 'user_id' + ); + + // add foreign key for table `{{%user}}` + \$this->addForeignKey( + '{{%fk-test-user_id}}', + '{{%test}}', + 'user_id', + '{{%user}}', + 'id', + 'CASCADE' + ); + + // creates index for column `product_id` + \$this->createIndex( + '{{%idx-test-product_id}}', + '{{%test}}', + 'product_id' + ); + + // add foreign key for table `{{%product}}` + \$this->addForeignKey( + '{{%fk-test-product_id}}', + '{{%test}}', + 'product_id', + '{{%product}}', + 'id', + 'CASCADE' + ); + + // creates index for column `order_id` + \$this->createIndex( + '{{%idx-test-order_id}}', + '{{%test}}', + 'order_id' + ); + + // add foreign key for table `{{%user_order}}` + \$this->addForeignKey( + '{{%fk-test-order_id}}', + '{{%test}}', + 'order_id', + '{{%user_order}}', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `{{%user}}` + \$this->dropForeignKey( + '{{%fk-test-user_id}}', + '{{%test}}' + ); + + // drops index for column `user_id` + \$this->dropIndex( + '{{%idx-test-user_id}}', + '{{%test}}' + ); + + // drops foreign key for table `{{%product}}` + \$this->dropForeignKey( + '{{%fk-test-product_id}}', + '{{%test}}' + ); + + // drops index for column `product_id` + \$this->dropIndex( + '{{%idx-test-product_id}}', + '{{%test}}' + ); + + // drops foreign key for table `{{%user_order}}` + \$this->dropForeignKey( + '{{%fk-test-order_id}}', + '{{%test}}' + ); + + // drops index for column `order_id` + \$this->dropIndex( + '{{%idx-test-order_id}}', + '{{%test}}' + ); + + \$this->dropTable('{{%test}}'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_test.php b/tests/data/console/migrate_create/create_test.php new file mode 100644 index 00000000000..42d445e5ef8 --- /dev/null +++ b/tests/data/console/migrate_create/create_test.php @@ -0,0 +1,32 @@ +createTable('test', [ + 'id' => \$this->primaryKey(), + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->dropTable('test'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/create_title_pk.php b/tests/data/console/migrate_create/create_title_pk.php new file mode 100644 index 00000000000..740c163b373 --- /dev/null +++ b/tests/data/console/migrate_create/create_title_pk.php @@ -0,0 +1,34 @@ +createTable('test', [ + 'title' => \$this->primaryKey(), + 'body' => \$this->text()->notNull(), + 'price' => \$this->money(11,2), + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->dropTable('test'); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/default.php b/tests/data/console/migrate_create/default.php new file mode 100644 index 00000000000..cdef5dcfa71 --- /dev/null +++ b/tests/data/console/migrate_create/default.php @@ -0,0 +1,34 @@ +dropColumn('test', 'title'); + \$this->dropColumn('test', 'body'); + \$this->dropColumn('test', 'price'); + \$this->dropColumn('test', 'created_at'); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->addColumn('test', 'title', \$this->string(10)->notNull()); + \$this->addColumn('test', 'body', \$this->text()->notNull()); + \$this->addColumn('test', 'price', \$this->money(11,2)->notNull()); + \$this->addColumn('test', 'created_at', \$this->dateTime()); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/drop_fields.php b/tests/data/console/migrate_create/drop_fields.php new file mode 100644 index 00000000000..5e2fb108fc7 --- /dev/null +++ b/tests/data/console/migrate_create/drop_fields.php @@ -0,0 +1,34 @@ +dropTable('test'); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->createTable('test', [ + 'id' => \$this->primaryKey(), + 'body' => \$this->text()->notNull(), + 'price' => \$this->money(11,2), + ]); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/drop_test.php b/tests/data/console/migrate_create/drop_test.php new file mode 100644 index 00000000000..4f4b1661c6a --- /dev/null +++ b/tests/data/console/migrate_create/drop_test.php @@ -0,0 +1,32 @@ +dropTable('test'); + } + + /** + * @inheritdoc + */ + public function down() + { + \$this->createTable('test', [ + 'id' => \$this->primaryKey(), + ]); + } +} + +CODE; diff --git a/tests/data/console/migrate_create/junction_test.php b/tests/data/console/migrate_create/junction_test.php new file mode 100644 index 00000000000..7831a0cccc1 --- /dev/null +++ b/tests/data/console/migrate_create/junction_test.php @@ -0,0 +1,97 @@ +createTable('post_tag', [ + 'post_id' => \$this->integer(), + 'tag_id' => \$this->integer(), + 'PRIMARY KEY(post_id, tag_id)', + ]); + + // creates index for column `post_id` + \$this->createIndex( + 'idx-post_tag-post_id', + 'post_tag', + 'post_id' + ); + + // add foreign key for table `post` + \$this->addForeignKey( + 'fk-post_tag-post_id', + 'post_tag', + 'post_id', + 'post', + 'id', + 'CASCADE' + ); + + // creates index for column `tag_id` + \$this->createIndex( + 'idx-post_tag-tag_id', + 'post_tag', + 'tag_id' + ); + + // add foreign key for table `tag` + \$this->addForeignKey( + 'fk-post_tag-tag_id', + 'post_tag', + 'tag_id', + 'tag', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `post` + \$this->dropForeignKey( + 'fk-post_tag-post_id', + 'post_tag' + ); + + // drops index for column `post_id` + \$this->dropIndex( + 'idx-post_tag-post_id', + 'post_tag' + ); + + // drops foreign key for table `tag` + \$this->dropForeignKey( + 'fk-post_tag-tag_id', + 'post_tag' + ); + + // drops index for column `tag_id` + \$this->dropIndex( + 'idx-post_tag-tag_id', + 'post_tag' + ); + + \$this->dropTable('post_tag'); + } +} + +CODE; diff --git a/tests/framework/console/controllers/MigrateControllerTestTrait.php b/tests/framework/console/controllers/MigrateControllerTestTrait.php index 652d85690e2..cce9b6ff376 100644 --- a/tests/framework/console/controllers/MigrateControllerTestTrait.php +++ b/tests/framework/console/controllers/MigrateControllerTestTrait.php @@ -42,6 +42,27 @@ public function tearDownMigrationPath() FileHelper::removeDirectory($this->migrationPath); } + public function assertFileContent($expectedFile, $class) + { + $this->assertEqualsWithoutLE( + include Yii::getAlias( + "@yiiunit/data/console/migrate_create/$expectedFile.php" + ), + $this->parseNameClassMigration($class) + ); + } + + public function assertCommandCreatedFile( + $expectedFile, + $migrationName, + $params = [] + ) { + $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; + $params[0] = $migrationName; + $this->runMigrateControllerAction('create', $params); + $this->assertFileContent($expectedFile, $class); + } + /** * @return array applied migration entries */ @@ -168,1048 +189,98 @@ public function testCreate() public function testGenerateDefaultMigration() { - $migrationName = 'DefaultTest'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [$migrationName]); - $file = $this->parseNameClassMigration($class); - - $newLine = '\n'; - $code = <<assertEqualsWithoutLE($code, $file); + $this->assertCommandCreatedFile('default', 'DefaultTest'); } public function testGenerateCreateMigration() { $migrationName = 'create_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, - 'fields' => 'title:string(10):notNull:unique:defaultValue("test"),body:text:notNull,price:money(11,2):notNull' - ]); - $file = $this->parseNameClassMigration($class); - - $code = <<assertCommandCreatedFile('create_test', $migrationName); -/** - * Handles the creation for table `test`. - */ -class {$class} extends Migration -{ - /** - * @inheritdoc - */ - public function up() - { - \$this->createTable('test', [ - 'id' => \$this->primaryKey(), - 'title' => \$this->string(10)->notNull()->unique()->defaultValue("test"), - 'body' => \$this->text()->notNull(), - 'price' => \$this->money(11,2)->notNull(), + $this->assertCommandCreatedFile('create_fields', $migrationName, [ + 'fields' => 'title:string(10):notNull:unique:defaultValue("test"), + body:text:notNull, + price:money(11,2):notNull' ]); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->dropTable('test'); - } -} -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('create_title_pk', $migrationName, [ 'fields' => 'title:primaryKey,body:text:notNull,price:money(11,2)', ]); - $file = $this->parseNameClassMigration($class); - $code = <<createTable('test', [ - 'title' => \$this->primaryKey(), - 'body' => \$this->text()->notNull(), - 'price' => \$this->money(11,2), + $this->assertCommandCreatedFile('create_id_pk', $migrationName, [ + 'fields' => 'id:primaryKey, + address:string, + address2:string, + email:string', ]); - } - /** - * @inheritdoc - */ - public function down() - { - \$this->dropTable('test'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, - ]); - $file = $this->parseNameClassMigration($class); - $code = <<createTable('test', [ - 'id' => \$this->primaryKey(), - ]); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->dropTable('test'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, - 'fields' => 'id:primaryKey,address:string,address2:string,email:string', - ]); - $file = $this->parseNameClassMigration($class); - $code = <<createTable('test', [ - 'id' => \$this->primaryKey(), - 'address' => \$this->string(), - 'address2' => \$this->string(), - 'email' => \$this->string(), - ]); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->dropTable('test'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('create_foreign_key', $migrationName, [ 'fields' => 'user_id:integer:foreignKey, product_id:foreignKey:integer:unsigned:notNull, order_id:integer:foreignKey(user_order):notNull, created_at:dateTime:notNull', ]); - $file = $this->parseNameClassMigration($class); - $code = <<createTable('test', [ - 'id' => \$this->primaryKey(), - 'user_id' => \$this->integer(), - 'product_id' => \$this->integer()->unsigned()->notNull(), - 'order_id' => \$this->integer()->notNull(), - 'created_at' => \$this->dateTime()->notNull(), - ]); - // creates index for column `user_id` - \$this->createIndex( - 'idx-test-user_id', - 'test', - 'user_id' - ); - - // add foreign key for table `user` - \$this->addForeignKey( - 'fk-test-user_id', - 'test', - 'user_id', - 'user', - 'id', - 'CASCADE' - ); - - // creates index for column `product_id` - \$this->createIndex( - 'idx-test-product_id', - 'test', - 'product_id' - ); - - // add foreign key for table `product` - \$this->addForeignKey( - 'fk-test-product_id', - 'test', - 'product_id', - 'product', - 'id', - 'CASCADE' - ); - - // creates index for column `order_id` - \$this->createIndex( - 'idx-test-order_id', - 'test', - 'order_id' - ); - - // add foreign key for table `user_order` - \$this->addForeignKey( - 'fk-test-order_id', - 'test', - 'order_id', - 'user_order', - 'id', - 'CASCADE' - ); - } - - /** - * @inheritdoc - */ - public function down() - { - // drops foreign key for table `user` - \$this->dropForeignKey( - 'fk-test-user_id', - 'test' - ); - - // drops index for column `user_id` - \$this->dropIndex( - 'idx-test-user_id', - 'test' - ); - - // drops foreign key for table `product` - \$this->dropForeignKey( - 'fk-test-product_id', - 'test' - ); - - // drops index for column `product_id` - \$this->dropIndex( - 'idx-test-product_id', - 'test' - ); - - // drops foreign key for table `user_order` - \$this->dropForeignKey( - 'fk-test-order_id', - 'test' - ); - - // drops index for column `order_id` - \$this->dropIndex( - 'idx-test-order_id', - 'test' - ); - - \$this->dropTable('test'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('create_prefix', $migrationName, [ 'useTablePrefix' => true, 'fields' => 'user_id:integer:foreignKey, product_id:foreignKey:integer:unsigned:notNull, order_id:integer:foreignKey(user_order):notNull, created_at:dateTime:notNull', ]); - $file = $this->parseNameClassMigration($class); - $code = <<createTable('{{%test}}', [ - 'id' => \$this->primaryKey(), - 'user_id' => \$this->integer(), - 'product_id' => \$this->integer()->unsigned()->notNull(), - 'order_id' => \$this->integer()->notNull(), - 'created_at' => \$this->dateTime()->notNull(), - ]); - - // creates index for column `user_id` - \$this->createIndex( - '{{%idx-test-user_id}}', - '{{%test}}', - 'user_id' - ); - - // add foreign key for table `{{%user}}` - \$this->addForeignKey( - '{{%fk-test-user_id}}', - '{{%test}}', - 'user_id', - '{{%user}}', - 'id', - 'CASCADE' - ); - - // creates index for column `product_id` - \$this->createIndex( - '{{%idx-test-product_id}}', - '{{%test}}', - 'product_id' - ); - - // add foreign key for table `{{%product}}` - \$this->addForeignKey( - '{{%fk-test-product_id}}', - '{{%test}}', - 'product_id', - '{{%product}}', - 'id', - 'CASCADE' - ); - - // creates index for column `order_id` - \$this->createIndex( - '{{%idx-test-order_id}}', - '{{%test}}', - 'order_id' - ); - - // add foreign key for table `{{%user_order}}` - \$this->addForeignKey( - '{{%fk-test-order_id}}', - '{{%test}}', - 'order_id', - '{{%user_order}}', - 'id', - 'CASCADE' - ); - } - - /** - * @inheritdoc - */ - public function down() - { - // drops foreign key for table `{{%user}}` - \$this->dropForeignKey( - '{{%fk-test-user_id}}', - '{{%test}}' - ); - - // drops index for column `user_id` - \$this->dropIndex( - '{{%idx-test-user_id}}', - '{{%test}}' - ); - - // drops foreign key for table `{{%product}}` - \$this->dropForeignKey( - '{{%fk-test-product_id}}', - '{{%test}}' - ); - - // drops index for column `product_id` - \$this->dropIndex( - '{{%idx-test-product_id}}', - '{{%test}}' - ); - - // drops foreign key for table `{{%user_order}}` - \$this->dropForeignKey( - '{{%fk-test-order_id}}', - '{{%test}}' - ); - - // drops index for column `order_id` - \$this->dropIndex( - '{{%idx-test-order_id}}', - '{{%test}}' - ); - - \$this->dropTable('{{%test}}'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); } public function testGenerateDropMigration() { $migrationName = 'drop_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName - ]); - $file = $this->parseNameClassMigration($class); - - $code = <<assertCommandCreatedFile('drop_test', $migrationName); -/** - * Handles the dropping for table `test`. - */ -class {$class} extends Migration -{ - /** - * @inheritdoc - */ - public function up() - { - \$this->dropTable('test'); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->createTable('test', [ - 'id' => \$this->primaryKey(), - ]); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('drop_fields', $migrationName, [ 'fields' => 'body:text:notNull,price:money(11,2)' ]); - $file = $this->parseNameClassMigration($class); - $code = <<dropTable('test'); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->createTable('test', [ - 'id' => \$this->primaryKey(), - 'body' => \$this->text()->notNull(), - 'price' => \$this->money(11,2), - ]); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); } public function testGenerateAddColumnMigration() { $migrationName = 'add_columns_to_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('add_columns_test', $migrationName, [ 'fields' => 'title:string(10):notNull, body:text:notNull, price:money(11,2):notNull, created_at:dateTime' ]); - $file = $this->parseNameClassMigration($class); - - $code = <<addColumn('test', 'title', \$this->string(10)->notNull()); - \$this->addColumn('test', 'body', \$this->text()->notNull()); - \$this->addColumn('test', 'price', \$this->money(11,2)->notNull()); - \$this->addColumn('test', 'created_at', \$this->dateTime()); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->dropColumn('test', 'title'); - \$this->dropColumn('test', 'body'); - \$this->dropColumn('test', 'price'); - \$this->dropColumn('test', 'created_at'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - $migrationName = 'add_columns_to_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('add_columns_fk', $migrationName, [ 'fields' => 'user_id:integer:foreignKey, product_id:foreignKey:integer:unsigned:notNull, order_id:integer:foreignKey(user_order):notNull, created_at:dateTime:notNull', ]); - $file = $this->parseNameClassMigration($class); - $code = <<addColumn('test', 'user_id', \$this->integer()); - \$this->addColumn('test', 'product_id', \$this->integer()->unsigned()->notNull()); - \$this->addColumn('test', 'order_id', \$this->integer()->notNull()); - \$this->addColumn('test', 'created_at', \$this->dateTime()->notNull()); - - // creates index for column `user_id` - \$this->createIndex( - 'idx-test-user_id', - 'test', - 'user_id' - ); - - // add foreign key for table `user` - \$this->addForeignKey( - 'fk-test-user_id', - 'test', - 'user_id', - 'user', - 'id', - 'CASCADE' - ); - - // creates index for column `product_id` - \$this->createIndex( - 'idx-test-product_id', - 'test', - 'product_id' - ); - - // add foreign key for table `product` - \$this->addForeignKey( - 'fk-test-product_id', - 'test', - 'product_id', - 'product', - 'id', - 'CASCADE' - ); - - // creates index for column `order_id` - \$this->createIndex( - 'idx-test-order_id', - 'test', - 'order_id' - ); - - // add foreign key for table `user_order` - \$this->addForeignKey( - 'fk-test-order_id', - 'test', - 'order_id', - 'user_order', - 'id', - 'CASCADE' - ); - } - - /** - * @inheritdoc - */ - public function down() - { - // drops foreign key for table `user` - \$this->dropForeignKey( - 'fk-test-user_id', - 'test' - ); - - // drops index for column `user_id` - \$this->dropIndex( - 'idx-test-user_id', - 'test' - ); - - // drops foreign key for table `product` - \$this->dropForeignKey( - 'fk-test-product_id', - 'test' - ); - - // drops index for column `product_id` - \$this->dropIndex( - 'idx-test-product_id', - 'test' - ); - - // drops foreign key for table `user_order` - \$this->dropForeignKey( - 'fk-test-order_id', - 'test' - ); - - // drops index for column `order_id` - \$this->dropIndex( - 'idx-test-order_id', - 'test' - ); - - \$this->dropColumn('test', 'user_id'); - \$this->dropColumn('test', 'product_id'); - \$this->dropColumn('test', 'order_id'); - \$this->dropColumn('test', 'created_at'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); - - $migrationName = 'add_columns_to_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, + $this->assertCommandCreatedFile('add_columns_prefix', $migrationName, [ 'useTablePrefix' => true, 'fields' => 'user_id:integer:foreignKey, product_id:foreignKey:integer:unsigned:notNull, order_id:integer:foreignKey(user_order):notNull, created_at:dateTime:notNull', ]); - $file = $this->parseNameClassMigration($class); - - $code = <<addColumn('{{%test}}', 'user_id', \$this->integer()); - \$this->addColumn('{{%test}}', 'product_id', \$this->integer()->unsigned()->notNull()); - \$this->addColumn('{{%test}}', 'order_id', \$this->integer()->notNull()); - \$this->addColumn('{{%test}}', 'created_at', \$this->dateTime()->notNull()); - - // creates index for column `user_id` - \$this->createIndex( - '{{%idx-test-user_id}}', - '{{%test}}', - 'user_id' - ); - - // add foreign key for table `{{%user}}` - \$this->addForeignKey( - '{{%fk-test-user_id}}', - '{{%test}}', - 'user_id', - '{{%user}}', - 'id', - 'CASCADE' - ); - - // creates index for column `product_id` - \$this->createIndex( - '{{%idx-test-product_id}}', - '{{%test}}', - 'product_id' - ); - - // add foreign key for table `{{%product}}` - \$this->addForeignKey( - '{{%fk-test-product_id}}', - '{{%test}}', - 'product_id', - '{{%product}}', - 'id', - 'CASCADE' - ); - - // creates index for column `order_id` - \$this->createIndex( - '{{%idx-test-order_id}}', - '{{%test}}', - 'order_id' - ); - - // add foreign key for table `{{%user_order}}` - \$this->addForeignKey( - '{{%fk-test-order_id}}', - '{{%test}}', - 'order_id', - '{{%user_order}}', - 'id', - 'CASCADE' - ); - } - - /** - * @inheritdoc - */ - public function down() - { - // drops foreign key for table `{{%user}}` - \$this->dropForeignKey( - '{{%fk-test-user_id}}', - '{{%test}}' - ); - - // drops index for column `user_id` - \$this->dropIndex( - '{{%idx-test-user_id}}', - '{{%test}}' - ); - - // drops foreign key for table `{{%product}}` - \$this->dropForeignKey( - '{{%fk-test-product_id}}', - '{{%test}}' - ); - - // drops index for column `product_id` - \$this->dropIndex( - '{{%idx-test-product_id}}', - '{{%test}}' - ); - - // drops foreign key for table `{{%user_order}}` - \$this->dropForeignKey( - '{{%fk-test-order_id}}', - '{{%test}}' - ); - - // drops index for column `order_id` - \$this->dropIndex( - '{{%idx-test-order_id}}', - '{{%test}}' - ); - - \$this->dropColumn('{{%test}}', 'user_id'); - \$this->dropColumn('{{%test}}', 'product_id'); - \$this->dropColumn('{{%test}}', 'order_id'); - \$this->dropColumn('{{%test}}', 'created_at'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); } public function testGenerateDropColumnMigration() { $migrationName = 'drop_columns_from_test'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, - 'fields' => 'title:string(10):notNull,body:text:notNull,price:money(11,2):notNull,created_at:dateTime' + $this->assertCommandCreatedFile('drop_columns_test', $migrationName, [ + 'fields' => 'title:string(10):notNull,body:text:notNull, + price:money(11,2):notNull, + created_at:dateTime' ]); - $file = $this->parseNameClassMigration($class); - - $code = <<dropColumn('test', 'title'); - \$this->dropColumn('test', 'body'); - \$this->dropColumn('test', 'price'); - \$this->dropColumn('test', 'created_at'); - } - - /** - * @inheritdoc - */ - public function down() - { - \$this->addColumn('test', 'title', \$this->string(10)->notNull()); - \$this->addColumn('test', 'body', \$this->text()->notNull()); - \$this->addColumn('test', 'price', \$this->money(11,2)->notNull()); - \$this->addColumn('test', 'created_at', \$this->dateTime()); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); } public function testGenerateCreateJunctionMigration() { $migrationName = 'create_junction_post_and_tag'; - $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; - $this->runMigrateControllerAction('create', [ - $migrationName, - ]); - $file = $this->parseNameClassMigration($class); - - $code = <<createTable('post_tag', [ - 'post_id' => \$this->integer(), - 'tag_id' => \$this->integer(), - 'PRIMARY KEY(post_id, tag_id)', - ]); - - // creates index for column `post_id` - \$this->createIndex( - 'idx-post_tag-post_id', - 'post_tag', - 'post_id' - ); - - // add foreign key for table `post` - \$this->addForeignKey( - 'fk-post_tag-post_id', - 'post_tag', - 'post_id', - 'post', - 'id', - 'CASCADE' - ); - - // creates index for column `tag_id` - \$this->createIndex( - 'idx-post_tag-tag_id', - 'post_tag', - 'tag_id' - ); - - // add foreign key for table `tag` - \$this->addForeignKey( - 'fk-post_tag-tag_id', - 'post_tag', - 'tag_id', - 'tag', - 'id', - 'CASCADE' - ); - } - - /** - * @inheritdoc - */ - public function down() - { - // drops foreign key for table `post` - \$this->dropForeignKey( - 'fk-post_tag-post_id', - 'post_tag' - ); - - // drops index for column `post_id` - \$this->dropIndex( - 'idx-post_tag-post_id', - 'post_tag' - ); - - // drops foreign key for table `tag` - \$this->dropForeignKey( - 'fk-post_tag-tag_id', - 'post_tag' - ); - - // drops index for column `tag_id` - \$this->dropIndex( - 'idx-post_tag-tag_id', - 'post_tag' - ); - - \$this->dropTable('post_tag'); - } -} - -CODE; - $this->assertEqualsWithoutLE($code, $file); + $this->assertCommandCreatedFile('junction_test', $migrationName); } public function testUp()