diff --git a/extensions/mongodb/console/controllers/MigrateController.php b/extensions/mongodb/console/controllers/MigrateController.php index e5590c5..b6b5b9a 100644 --- a/extensions/mongodb/console/controllers/MigrateController.php +++ b/extensions/mongodb/console/controllers/MigrateController.php @@ -136,14 +136,14 @@ class MigrateController extends BaseMigrateController return $history; } + private $baseMigrationEnsured = false; + /** * Ensures migration history contains at least base migration entry. */ protected function ensureBaseMigrationHistory() { - static $ensured = false; - - if (!$ensured) { + if (!$this->baseMigrationEnsured) { $query = new Query; $row = $query->select(['version']) ->from($this->migrationCollection) @@ -153,7 +153,7 @@ class MigrateController extends BaseMigrateController if (empty($row)) { $this->addMigrationHistory(self::BASE_MIGRATION); } - $ensured = true; + $this->baseMigrationEnsured = true; } } diff --git a/tests/unit/extensions/mongodb/console/controllers/MigrateControllerTest.php b/tests/unit/extensions/mongodb/console/controllers/MigrateControllerTest.php new file mode 100644 index 0000000..62d4a9b --- /dev/null +++ b/tests/unit/extensions/mongodb/console/controllers/MigrateControllerTest.php @@ -0,0 +1,54 @@ +<?php + +namespace yiiunit\extensions\mongodb\console\controllers; + +use yii\mongodb\Exception; +use yii\mongodb\Migration; +use yii\mongodb\Query; +use Yii; +use yiiunit\extensions\mongodb\MongoDbTestCase; +use yiiunit\framework\console\controllers\MigrateControllerTestTrait; +use yii\mongodb\console\controllers\MigrateController; + +/** + * Unit test for [[\yii\mongodb\console\controllers\MigrateController]]. + * @see MigrateController + * + * @group mongodb + * @group console + */ +class MigrateControllerTest extends MongoDbTestCase +{ + use MigrateControllerTestTrait; + + public function setUp() + { + $this->migrateControllerClass = MigrateController::className(); + $this->migrationBaseClass = Migration::className(); + + parent::setUp(); + + $this->setUpMigrationPath(); + Yii::$app->setComponents(['mongodb' => $this->getConnection()]); + } + + public function tearDown() + { + parent::tearDown(); + try { + $this->getConnection()->getCollection('migration')->drop(); + } catch (Exception $e) { + // shutdown exception + } + $this->tearDownMigrationPath(); + } + + /** + * @return array applied migration entries + */ + protected function getMigrationHistory() + { + $query = new Query(); + return $query->from('migration')->all(); + } +} \ No newline at end of file