FixtureController.php 4.37 KB
Newer Older
Mark committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console\controllers;

use Yii;
use yii\console\Controller;
12
use yii\console\Exception;
Qiang Xue committed
13
use yii\test\DbTestTrait;
Mark committed
14 15 16 17 18

/**
 * This command manages fixtures load to the database tables.
 * You can specify different options of this command to point fixture manager
 * to the specific tables of the different database connections.
Qiang Xue committed
19
 *
Mark committed
20
 * To use this command simply configure your console.php config like this:
Qiang Xue committed
21
 *
Mark committed
22
 * ~~~
Qiang Xue committed
23 24 25 26 27 28 29 30 31 32
 * 'db' => [
 *     'class' => 'yii\db\Connection',
 *     'dsn' => 'mysql:host=localhost;dbname={your_database}',
 *     'username' => '{your_db_user}',
 *     'password' => '',
 *     'charset' => 'utf8',
 * ],
 * 'fixture' => [
 *     'class' => 'yii\test\DbFixtureManager',
 * ],
Mark committed
33
 * ~~~
Qiang Xue committed
34
 *
Mark committed
35
 * ~~~
Qiang Xue committed
36 37 38
 * #load fixtures under $fixturePath to the "users" table
 * yii fixture/apply users
 *
Mark committed
39
 * #also a short version of this command (generate action is default)
Qiang Xue committed
40 41 42
 * yii fixture users
 *
 * #load fixtures under $fixturePath to the "users" table to the different connection
Qiang Xue committed
43
 * yii fixture/apply users --db=someOtherDbConneciton
Qiang Xue committed
44 45
 *
 * #load fixtures under different $fixturePath to the "users" table.
Qiang Xue committed
46
 * yii fixture/apply users --fixturePath=@app/some/other/path/to/fixtures
Mark committed
47
 * ~~~
Qiang Xue committed
48
 *
Mark committed
49 50 51 52 53 54
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
class FixtureController extends Controller
{

Qiang Xue committed
55
	use DbTestTrait;
Mark committed
56 57 58 59 60 61 62 63 64 65

	/**
	 * @var string controller default action ID.
	 */
	public $defaultAction = 'apply';

	/**
	 * Alias to the path, where all fixtures are stored.
	 * @var string
	 */
Qiang Xue committed
66
	public $fixturePath = '@tests/unit/fixtures';
Mark committed
67 68 69

	/**
	 * Id of the database connection component of the application.
Qiang Xue committed
70
	 * @var string
Mark committed
71 72 73 74 75 76 77 78 79 80
	 */
	public $db = 'db';

	/**
	 * Returns the names of the global options for this command.
	 * @return array the names of the global options for this command.
	 */
	public function globalOptions()
	{
		return array_merge(parent::globalOptions(), [
Qiang Xue committed
81
			'db', 'fixturePath'
Mark committed
82 83 84 85 86 87
		]);
	}

	/**
	 * This method is invoked right before an action is to be executed (after all possible filters.)
	 * It checks that fixtures path and database connection are available.
Qiang Xue committed
88
	 * @param \yii\base\Action $action
Mark committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	 * @return boolean
	 */
	public function beforeAction($action)
	{
		if (parent::beforeAction($action)) {
			$this->checkRequirements();
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Apply given fixture to the table. Fixture name can be the same as the table name or
	 * you can specify table name as a second parameter.
	 * @param string $fixture
	 */
106
	public function actionApply(array $fixture)
Mark committed
107
	{
108 109 110 111 112 113
		if ($this->getFixtureManager() == null) {
			throw new Exception(
				'Fixture manager is not configured properly. '
				. 'Please refer to official documentation for this purposes.');
		}

Qiang Xue committed
114 115
		$this->getFixtureManager()->basePath = $this->fixturePath;
		$this->getFixtureManager()->db = $this->db;
116 117
		$this->loadFixtures($fixture);
		$this->notifySuccess($fixture);
Mark committed
118 119 120 121 122 123 124 125
	}

	/**
	 * Truncate given table and clear all fixtures from it.
	 * @param string $table
	 */
	public function actionClear($table)
	{
Qiang Xue committed
126
		$this->getDbConnection()->createCommand()->truncateTable($table)->execute();
Mark committed
127 128 129 130 131
		echo "Table \"{$table}\" was successfully cleared. \n";
	}

	/**
	 * Checks if the database and fixtures path are available.
132
	 * @throws Exception
Mark committed
133 134 135
	 */
	public function checkRequirements()
	{
Qiang Xue committed
136
		$path = Yii::getAlias($this->fixturePath, false);
Mark committed
137 138

		if (!is_dir($path) || !is_writable($path)) {
139
			throw new Exception("The fixtures path \"{$this->fixturePath}\" not exist or is not writable");
Mark committed
140 141 142 143 144 145
		}

	}

	/**
	 * Returns database connection component
Qiang Xue committed
146
	 * @return \yii\db\Connection
147
	 * @throws Exception if [[db]] is invalid.
Mark committed
148 149 150 151 152 153
	 */
	public function getDbConnection()
	{
		$db = Yii::$app->getComponent($this->db);

		if ($db == null) {
154
			throw new Exception("There is no database connection component with id \"{$this->db}\".");
Mark committed
155 156 157 158 159
		}

		return $db;
	}

160 161 162 163 164 165 166 167 168 169 170 171 172 173
	/**
	 * 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);
		}
	}

Mark committed
174
}