Skip to content

Commit 4faa937

Browse files
committed
Refactor test
1 parent 353f61e commit 4faa937

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

tests/DbTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,12 @@ protected function runDownMigrations(string $db = 'mysql', int $number = 2): voi
184184
$this->assertSame($downExitCode, 0);
185185
$this->assertSame($downOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' reverted.');
186186
}
187+
188+
protected function dropFkIfExists(string $table, string $fk): void
189+
{
190+
$tableSchema = Yii::$app->db->schema->getTableSchema($table);
191+
if ($tableSchema && array_key_exists($fk, $tableSchema->foreignKeys)) {
192+
Yii::$app->db->createCommand()->dropForeignKey($fk, $table)->execute();
193+
}
194+
}
187195
}

tests/unit/IssueFixTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,29 +344,17 @@ private function createTablesForCreateMigrationForDropTable132()
344344

345345
private function deleteTablesForCreateMigrationForDropTable132()
346346
{
347-
$tableSchema = Yii::$app->db->schema->getTableSchema('{{%pristines}}');
348-
if ($tableSchema && array_key_exists('name', $tableSchema->foreignKeys)) {
349-
Yii::$app->db->createCommand()->dropForeignKey('name', '{{%pristines}}')->execute();
350-
}
351-
347+
$this->dropFkIfExists('{{%pristines}}', 'name');
352348
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
353349

354-
$tableSchema = Yii::$app->db->schema->getTableSchema('{{%fruits}}');
355-
if ($tableSchema && array_key_exists('name2', $tableSchema->foreignKeys)) {
356-
Yii::$app->db->createCommand()->dropForeignKey('name2', '{{%fruits}}')->execute();
357-
}
358-
350+
$this->dropFkIfExists('{{%fruits}}', 'name2');
359351
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
360352

361353
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%upks}}')->execute();
362354
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%bigpks}}')->execute();
363355
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%ubigpks}}')->execute();
364356

365-
$tableSchema = Yii::$app->db->schema->getTableSchema('{{%the_mango_table_name}}');
366-
if ($tableSchema && array_key_exists('animal_fruit_fk', $tableSchema->foreignKeys)) {
367-
Yii::$app->db->createCommand()->dropForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}')->execute();
368-
}
369-
357+
$this->dropFkIfExists('{{%the_mango_table_name}}', 'animal_fruit_fk');
370358
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_mango_table_name}}')->execute();
371359
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_animal_table_name}}')->execute();
372360
}

0 commit comments

Comments
 (0)