From ccfb7638d07b9500ee2e6b35630fc5c8437f8321 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 4 Jan 2014 01:16:05 +0400 Subject: [PATCH 1/3] Added to the basic app, fixed config, added to core commands. --- apps/basic/config/console.php | 14 +++++++++++ apps/basic/tests/unit/fixtures/.gitkeep | 0 .../tests/unit/templates/fixtures/.gitkeep | 0 framework/yii/console/Application.php | 1 + .../console/controllers/FixtureController.php | 25 +++++++++++++++++-- 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 apps/basic/tests/unit/fixtures/.gitkeep create mode 100644 apps/basic/tests/unit/templates/fixtures/.gitkeep diff --git a/apps/basic/config/console.php b/apps/basic/config/console.php index 6f3f9a8b5c9..55630fc078f 100644 --- a/apps/basic/config/console.php +++ b/apps/basic/config/console.php @@ -1,4 +1,7 @@ 'basic-console', @@ -19,6 +22,17 @@ ], ], ], + 'db' => [ + 'class' => 'yii\db\Connection', + 'dsn' => 'mysql:host=localhost;dbname=yii2basic', + 'username' => 'root', + 'password' => '', + 'charset' => 'utf8', + ], + 'fixture' => [ + 'class' => 'yii\test\DbFixtureManager', + 'basePath' => '@tests/unit/fixtures', + ], ], 'params' => $params, ]; diff --git a/apps/basic/tests/unit/fixtures/.gitkeep b/apps/basic/tests/unit/fixtures/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/apps/basic/tests/unit/templates/fixtures/.gitkeep b/apps/basic/tests/unit/templates/fixtures/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/framework/yii/console/Application.php b/framework/yii/console/Application.php index 67013048aa0..6117752a57a 100644 --- a/framework/yii/console/Application.php +++ b/framework/yii/console/Application.php @@ -146,6 +146,7 @@ public function coreCommands() 'migrate' => 'yii\console\controllers\MigrateController', 'cache' => 'yii\console\controllers\CacheController', 'asset' => 'yii\console\controllers\AssetController', + 'fixture' => 'yii\console\controllers\FixtureController', ]; } diff --git a/framework/yii/console/controllers/FixtureController.php b/framework/yii/console/controllers/FixtureController.php index 84ab7d40c63..cb78a47aa75 100644 --- a/framework/yii/console/controllers/FixtureController.php +++ b/framework/yii/console/controllers/FixtureController.php @@ -103,11 +103,18 @@ public function beforeAction($action) * you can specify table name as a second parameter. * @param string $fixture */ - public function actionApply($fixture) + public function actionApply(array $fixture) { + if ($this->getFixtureManager() == null) { + throw new Exception( + 'Fixture manager is not configured properly. ' + . 'Please refer to official documentation for this purposes.'); + } + $this->getFixtureManager()->basePath = $this->fixturePath; $this->getFixtureManager()->db = $this->db; - $this->loadFixtures([$fixture]); + $this->loadFixtures($fixture); + $this->notifySuccess($fixture); } /** @@ -150,4 +157,18 @@ public function getDbConnection() return $db; } + /** + * Notifies user that fixtures were successfully loaded. + * @param array $fixtures + */ + private function notifySuccess($fixtures) + { + $this->stdout("Fixtures were successfully loaded from path: \n", Console::FG_YELLOW); + $this->stdout(realpath(Yii::getAlias($this->fixturePath)) . "\n\n", Console::FG_GREEN); + + foreach($fixtures as $index => $fixture) { + $this->stdout($index +1 . ". " . $fixture . "\n", Console::FG_GREEN); + } + } + } From f4397f1b8a059016bf5acf1e54720541a94fd05f Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 4 Jan 2014 01:48:56 +0400 Subject: [PATCH 2/3] fixture controller improved --- .../console/controllers/FixtureController.php | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/framework/yii/console/controllers/FixtureController.php b/framework/yii/console/controllers/FixtureController.php index cb78a47aa75..8575d2a7494 100644 --- a/framework/yii/console/controllers/FixtureController.php +++ b/framework/yii/console/controllers/FixtureController.php @@ -11,6 +11,7 @@ use yii\console\Controller; use yii\console\Exception; use yii\test\DbTestTrait; +use yii\helpers\Console; /** * This command manages fixtures load to the database tables. @@ -103,7 +104,7 @@ public function beforeAction($action) * you can specify table name as a second parameter. * @param string $fixture */ - public function actionApply(array $fixture) + public function actionApply(array $fixtures) { if ($this->getFixtureManager() == null) { throw new Exception( @@ -111,20 +112,30 @@ public function actionApply(array $fixture) . 'Please refer to official documentation for this purposes.'); } + if (!$this->confirmApply($fixtures)) { + return; + } + $this->getFixtureManager()->basePath = $this->fixturePath; $this->getFixtureManager()->db = $this->db; - $this->loadFixtures($fixture); - $this->notifySuccess($fixture); + $this->loadFixtures($fixtures); + $this->notifySuccess($fixtures); } /** * Truncate given table and clear all fixtures from it. - * @param string $table + * @param string $tables */ - public function actionClear($table) + public function actionClear(array $tables) { - $this->getDbConnection()->createCommand()->truncateTable($table)->execute(); - echo "Table \"{$table}\" was successfully cleared. \n"; + if (!$this->confirmClear($tables)) { + return; + } + + foreach($tables as $table) { + $this->getDbConnection()->createCommand()->truncateTable($table)->execute(); + $this->stdout("Table \"{$table}\" was successfully cleared. \n", Console::FG_GREEN); + } } /** @@ -165,9 +176,42 @@ private function notifySuccess($fixtures) { $this->stdout("Fixtures were successfully loaded from path: \n", Console::FG_YELLOW); $this->stdout(realpath(Yii::getAlias($this->fixturePath)) . "\n\n", Console::FG_GREEN); + $this->outputList($fixtures); + } - foreach($fixtures as $index => $fixture) { - $this->stdout($index +1 . ". " . $fixture . "\n", Console::FG_GREEN); + /** + * Prompts user with confirmation if fixtures should be loaded. + * @param array $fixtures + * @return boolean + */ + private function confirmApply($fixtures) + { + $this->stdout("Fixtures will be loaded from path: \n", Console::FG_YELLOW); + $this->stdout(realpath(Yii::getAlias($this->fixturePath)) . "\n\n", Console::FG_GREEN); + $this->outputList($fixtures); + return $this->confirm('Load to database above fixtures?'); + } + + /** + * Prompts user with confirmation for tables that should be cleared. + * @param array $tables + * @return boolean + */ + private function confirmClear($tables) + { + $this->stdout("Tables below will be cleared: \n\n", Console::FG_YELLOW); + $this->outputList($tables); + return $this->confirm('Clear tables?'); + } + + /** + * Outputs data to the console as a list. + * @param array $data + */ + private function outputList($data) + { + foreach($data as $index => $item) { + $this->stdout($index +1 . ". " . $item . "\n", Console::FG_GREEN); } } From 00ab5ef2d0eda5ceb3fe6913e7a23299cb234a24 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 4 Jan 2014 02:18:02 +0400 Subject: [PATCH 3/3] alias fixed --- apps/basic/config/console.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/basic/config/console.php b/apps/basic/config/console.php index 55630fc078f..4eaf8ff269f 100644 --- a/apps/basic/config/console.php +++ b/apps/basic/config/console.php @@ -1,6 +1,6 @@