Commit 08ee8b18 by Carsten Brandt

Merge pull request #1759 from Ragazzo/fixtures_controller_improvements

Added to the basic app, fixed config, added to core commands.
parents b0ff35c6 00ab5ef2
<?php <?php
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/params.php'); $params = require(__DIR__ . '/params.php');
return [ return [
'id' => 'basic-console', 'id' => 'basic-console',
...@@ -19,6 +22,17 @@ return [ ...@@ -19,6 +22,17 @@ return [
], ],
], ],
], ],
'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, 'params' => $params,
]; ];
...@@ -146,6 +146,7 @@ class Application extends \yii\base\Application ...@@ -146,6 +146,7 @@ class Application extends \yii\base\Application
'migrate' => 'yii\console\controllers\MigrateController', 'migrate' => 'yii\console\controllers\MigrateController',
'cache' => 'yii\console\controllers\CacheController', 'cache' => 'yii\console\controllers\CacheController',
'asset' => 'yii\console\controllers\AssetController', 'asset' => 'yii\console\controllers\AssetController',
'fixture' => 'yii\console\controllers\FixtureController',
]; ];
} }
......
...@@ -11,6 +11,7 @@ use Yii; ...@@ -11,6 +11,7 @@ use Yii;
use yii\console\Controller; use yii\console\Controller;
use yii\console\Exception; use yii\console\Exception;
use yii\test\DbTestTrait; use yii\test\DbTestTrait;
use yii\helpers\Console;
/** /**
* This command manages fixtures load to the database tables. * This command manages fixtures load to the database tables.
...@@ -103,21 +104,38 @@ class FixtureController extends Controller ...@@ -103,21 +104,38 @@ class FixtureController extends Controller
* you can specify table name as a second parameter. * you can specify table name as a second parameter.
* @param string $fixture * @param string $fixture
*/ */
public function actionApply($fixture) public function actionApply(array $fixtures)
{ {
if ($this->getFixtureManager() == null) {
throw new Exception(
'Fixture manager is not configured properly. '
. 'Please refer to official documentation for this purposes.');
}
if (!$this->confirmApply($fixtures)) {
return;
}
$this->getFixtureManager()->basePath = $this->fixturePath; $this->getFixtureManager()->basePath = $this->fixturePath;
$this->getFixtureManager()->db = $this->db; $this->getFixtureManager()->db = $this->db;
$this->loadFixtures([$fixture]); $this->loadFixtures($fixtures);
$this->notifySuccess($fixtures);
} }
/** /**
* Truncate given table and clear all fixtures from it. * Truncate given table and clear all fixtures from it.
* @param string $table * @param string $tables
*/ */
public function actionClear($table) public function actionClear(array $tables)
{ {
if (!$this->confirmClear($tables)) {
return;
}
foreach($tables as $table) {
$this->getDbConnection()->createCommand()->truncateTable($table)->execute(); $this->getDbConnection()->createCommand()->truncateTable($table)->execute();
echo "Table \"{$table}\" was successfully cleared. \n"; $this->stdout("Table \"{$table}\" was successfully cleared. \n", Console::FG_GREEN);
}
} }
/** /**
...@@ -150,4 +168,51 @@ class FixtureController extends Controller ...@@ -150,4 +168,51 @@ class FixtureController extends Controller
return $db; 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);
$this->outputList($fixtures);
}
/**
* 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);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment