Commit 7863a0ff by Qiang Xue

Merge pull request #2279 from Ragazzo/fixtures_controller_improvements

Fixtures controller improvements
parents 27738286 2ed30dfe
Database Fixtures
=================
Fixtures console flow
=====================
Fixtures are important part of testing. Their main purpose is to populate you with data that needed by testing
different cases. With this data using your tests becoming more efficient and useful.
Yii supports database fixtures via the `yii fixture` command line tool. This tool supports:
Yii supports fixtures via the `yii fixture` command line tool. This tool supports:
* Applying new fixtures to database tables;
* Clearing, database tables (with sequences);
* Loading fixtures to different storages such as: database, nosql, etc;
* Unloading fixtures in different ways (usually it is clearing storage);
* Auto-generating fixtures and populating it with random data.
Fixtures format
---------------
Fixtures are just plain php files returning array. These files are usually stored under `@tests/unit/fixtures` path, but it
can be [configured](#configure-command-globally) in other way. Example of fixture file:
Fixtures are objects with different methods and configurations, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md) on them.
Lets assume we have fixtures data to load:
```
#users.php file under fixtures path
#users.php file under fixtures data path, by default @tests\unit\fixtures\data
return [
[
......@@ -36,31 +36,35 @@ return [
],
];
```
This data will be loaded to the `users`, but before it will be loaded table `users` will be cleared: all data deleted, sequence reset.
If we are using fixture that loads data into database then these rows will be applied to `users` table. If we are using nosql fixtures, for example `mongodb`
fixture, then this data will be applied to `users` mongodb collection. In order to learn about implementing various loading strategies and more, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md).
Above fixture example was auto-generated by `yii2-faker` extension, read more about it in these [section](#auto-generating-fixtures).
Fixture classes name should not be plural.
Loading fixtures
----------------
Applying fixtures
-----------------
Fixture classes should be suffixed by `Fixture` class. By default fixtures will be searched under `tests\unit\fixtures` namespace, you can
change this behavior with config or command options.
To apply fixture to the table, run the following command:
To apply fixture, run the following command:
```
yii fixture/apply <tbl_name>
yii fixture/apply <fixture_name>
```
The required `tbl_name` parameter specifies a database table to which data will be loaded. You can load data to several tables at once.
The required `fixture_name` parameter specifies a fixture name which data will be loaded. You can load several fixtures at once.
Below are correct formats of this command:
```
// apply fixtures to the "users" table of database
yii fixture/apply users
// apply `users` fixture
yii fixture/apply User
// same as above, because default action of "fixture" command is "apply"
yii fixture users
yii fixture User
// apply several fixtures to several tables. Note that there should not be any whitespace between ",", it should be one string.
yii fixture users,users_profiles
// apply several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture User,UserProfile
// apply all fixtures
yii fixture/apply all
......@@ -68,29 +72,31 @@ yii fixture/apply all
// same as above
yii fixture all
// apply fixtures to the table users, but fixtures will be taken from different path.
yii fixture users --fixturePath='@app/my/custom/path/to/fixtures'
// apply fixtures, but for other database connection.
yii fixtures User --db='customDbConnectionId'
// apply fixtures to the table users, but for other database connection.
yii fixtures users --db='customDbConnectionId'
// apply fixtures, but search them in different namespace. By default namespace is: tests\unit\fixtures.
yii fixtures User --namespace='alias\my\custom\namespace'
```
Clearing tables
---------------
Unloading fixtures
------------------
To clear table, run the following command:
To unload fixture, run the following command:
```
// clear given table: delete all data and reset sequence.
yii fixture/clear users
// unload Users fixture, by default it will clear fixture storage (for example "users" table, or "users" collection if this is mongodb fixture).
yii fixture/clear User
// clear several tables. Note that there should not be any whitespace between ",", it should be one string.
yii fixture/clear users,users_profile
// Unload several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture/clear User,UserProfile
// clear all tables of current connection in current schema
// unload all fixtures
yii fixture/clear all
```
Same command options like: `db`, `namespace` also can be applied to this command.
Configure Command Globally
--------------------------
While command line options allow us to configure the migration command
......@@ -101,8 +107,8 @@ different migration path as follows:
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\FixtureController',
'fixturePath' => '@app/my/custom/path/to/fixtures',
'db' => 'customDbConnectionId',
'namespace' => 'myalias\some\custom\namespace',
],
]
```
......
......@@ -175,6 +175,47 @@ This means you only need to work with `@app/tests/fixtures/initdb.php` if you wa
before each test. You may otherwise simply focus on developing each individual test case and the corresponding fixtures.
Fixtures hierarchy convention
-----------------------------
Usually you will have one fixture class per needed fixture and will be only switching data files for fixture classes.
When you have simple project that does not have much database testing and fixtures, you can put all fixtures data files under `data` folder, as it is done by default.
But when your project is not very simple you should not be greedy when using data files and organize them according these rule:
- data file should follow same hierarchy that is used for your project classes namespace.
Lets see example:
```php
#under folder tests\unit\fixtures
data\
components\
some_fixture_data_file1.php
some_fixture_data_file2.php
...
some_fixture_data_fileN.php
models\
db\
some_fixture_data_file1.php
some_fixture_data_file2.php
...
some_fixture_data_fileN.php
forms\
some_fixture_data_file1.php
some_fixture_data_file2.php
...
some_fixture_data_fileN.php
#and so on
```
In this way you will avoid fixture data collision between tests and use them as you need.
> **Note** In the example above fixture files are named only for example purposes, in real life you should name them according what fixture type you are using.
It can be table name, or mongodb collection name if you are using mongodb fixture. In order to know how to specify and name data files for your fixtures read above on this article.
Same rule can be applied to organize fixtures classes in your project, so similar hierarchy will be build under `fixtures` directory, avoiding usage of `data` directory, that is reserved for data files.
Summary
-------
......
......@@ -53,12 +53,12 @@ class ActiveFixture extends BaseActiveFixture
/**
* Loads the fixture data.
* The default implementation will first reset the DB table and then populate it with the data
* returned by [[getData()]].
* Data will be batch inserted into the given collection.
*/
public function load()
{
$this->resetCollection();
parent::load();
$data = $this->getData();
$this->getCollection()->batchInsert($data);
foreach ($data as $alias => $row) {
......@@ -66,6 +66,17 @@ class ActiveFixture extends BaseActiveFixture
}
}
/**
* Unloads the fixture.
*
* The default implementation will clean up the colection by calling [[resetCollection()]].
*/
public function unload()
{
$this->resetCollection();
parent::unload();
}
protected function getCollection()
{
return $this->db->getCollection($this->getCollectionName());
......
......@@ -12,9 +12,11 @@ use yii\console\Controller;
use yii\console\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Console;
use yii\test\FixtureTrait;
use yii\helpers\Inflector;
/**
* This command manages fixtures load to the database tables.
* This command manages loading and unloading fixtures.
* You can specify different options of this command to point fixture manager
* to the specific tables of the different database connections.
*
......@@ -28,23 +30,20 @@ use yii\helpers\Console;
* 'password' => '',
* 'charset' => 'utf8',
* ],
* 'fixture' => [
* 'class' => 'yii\test\DbFixtureManager',
* ],
* ~~~
*
* ~~~
* #load fixtures under $fixturePath to the "users" table
* yii fixture/apply users
* #load fixtures under $fixturePath from UsersFixture class with default namespace "tests\unit\fixtures"
* yii fixture/apply User
*
* #also a short version of this command (generate action is default)
* yii fixture users
* yii fixture User
*
* #load fixtures under $fixturePath to the "users" table to the different connection
* yii fixture/apply users --db=someOtherDbConneciton
* #load fixtures under $fixturePath with the different database connection
* yii fixture/apply User --db=someOtherDbConneciton
*
* #load fixtures under different $fixturePath to the "users" table.
* yii fixture/apply users --fixturePath=@app/some/other/path/to/fixtures
* #load fixtures under different $fixturePath.
* yii fixture/apply User --namespace=alias\my\custom\namespace\goes\here
* ~~~
*
* @author Mark Jebri <mark.github@yandex.ru>
......@@ -52,7 +51,8 @@ use yii\helpers\Console;
*/
class FixtureController extends Controller
{
use DbTestTrait;
use FixtureTrait;
/**
* type of fixture apply to database
......@@ -64,16 +64,14 @@ class FixtureController extends Controller
*/
public $defaultAction = 'apply';
/**
* Alias to the path, where all fixtures are stored.
* @var string
*/
public $fixturePath = '@tests/unit/fixtures';
/**
* Id of the database connection component of the application.
* @var string
* @var string id of the database connection component of the application.
*/
public $db = 'db';
/**
* @var string default namespace to search fixtures in
*/
public $namespace = 'tests\unit\fixtures';
/**
* Returns the names of the global options for this command.
......@@ -82,27 +80,11 @@ class FixtureController extends Controller
public function globalOptions()
{
return array_merge(parent::globalOptions(), [
'db', 'fixturePath'
'db','namespace'
]);
}
/**
* 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.
* @param \yii\base\Action $action
* @return boolean
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
$this->checkRequirements();
return true;
} else {
return false;
}
}
/**
* Apply given fixture to the table. You can load several fixtures specifying
* their names separated with commas, like: tbl_user,tbl_profile. Be sure there is no
* whitespace between tables names.
......@@ -111,10 +93,6 @@ class FixtureController extends Controller
*/
public function actionApply(array $fixtures, array $except = [])
{
if ($this->getFixtureManager() === null) {
throw new Exception('Fixture manager is not configured properly. Please refer to official documentation for this purposes.');
}
$foundFixtures = $this->findFixtures($fixtures);
if (!$this->needToApplyAll($fixtures[0])) {
......@@ -127,7 +105,7 @@ class FixtureController extends Controller
if (!$foundFixtures) {
throw new Exception("No files were found by name: \"" . implode(', ', $fixtures) . "\".\n"
. "Check that fixtures with these name exists, under fixtures path: \n\"" . Yii::getAlias($this->fixturePath) . "\"."
. "Check that files with these name exists, under fixtures path: \n\"" . Yii::getAlias($this->getFixturePath()) . "\"."
);
}
......@@ -135,17 +113,19 @@ class FixtureController extends Controller
return;
}
$fixtures = array_diff($foundFixtures, $except);
$fixtures = $this->getFixturesConfig(array_diff($foundFixtures, $except));
$this->getFixtureManager()->basePath = $this->fixturePath;
$this->getFixtureManager()->db = $this->db;
if (!$fixtures) {
throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"'.'');
}
$transaction = Yii::$app->db->beginTransaction();
try {
$this->loadFixtures($foundFixtures);
$this->getDbConnection()->createCommand()->checkIntegrity(false)->execute();
$this->loadFixtures($fixtures);
$this->getDbConnection()->createCommand()->checkIntegrity(true)->execute();
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollback();
$this->stdout("Exception occured, transaction rollback. Tables will be in same state.\n", Console::BG_RED);
......@@ -155,32 +135,49 @@ class FixtureController extends Controller
}
/**
* Truncate given table and clear all fixtures from it. You can clear several tables specifying
* Unloads given fixtures. You can clear environment and unload multiple fixtures by specifying
* their names separated with commas, like: tbl_user,tbl_profile. Be sure there is no
* whitespace between tables names.
* @param array|string $tables
* @param array|string $fixtures
* @param array|string $except
*/
public function actionClear(array $tables, array $except = ['tbl_migration'])
public function actionClear(array $fixtures, array $except = [])
{
if ($this->needToApplyAll($tables[0])) {
$tables = $this->getDbConnection()->schema->getTableNames();
$foundFixtures = $this->findFixtures($fixtures);
if (!$this->needToApplyAll($fixtures[0])) {
$notFoundFixtures = array_diff($fixtures, $foundFixtures);
if ($notFoundFixtures) {
$this->notifyNotFound($notFoundFixtures);
}
}
if (!$foundFixtures) {
throw new Exception("No files were found by name: \"" . implode(', ', $fixtures) . "\".\n"
. "Check that fixtures with these name exists, under fixtures path: \n\"" . Yii::getAlias($this->getFixturePath()) . "\"."
);
}
if (!$this->confirmClear($tables, $except)) {
if (!$this->confirmClear($foundFixtures, $except)) {
return;
}
$tables = array_diff($tables, $except);
$fixtures = $this->getFixturesConfig(array_diff($foundFixtures, $except));
if (!$fixtures) {
throw new Exception('No fixtures were found in namespace: ' . $this->namespace . '".');
}
$transaction = Yii::$app->db->beginTransaction();
try {
$this->getDbConnection()->createCommand()->checkIntegrity(false)->execute();
foreach($tables as $table) {
$this->getDbConnection()->createCommand()->delete($table)->execute();
$this->getDbConnection()->createCommand()->resetSequence($table)->execute();
$this->stdout(" Table \"{$table}\" was successfully cleared. \n", Console::FG_GREEN);
foreach($fixtures as $fixtureConfig) {
$fixture = Yii::createObject($fixtureConfig);
$fixture->unload();
$this->stdout("\tFixture \"{$fixture::className()}\" was successfully unloaded. \n", Console::FG_GREEN);
}
$this->getDbConnection()->createCommand()->checkIntegrity(true)->execute();
......@@ -194,23 +191,9 @@ class FixtureController extends Controller
}
/**
* Checks if the database and fixtures path are available.
* @throws Exception
*/
public function checkRequirements()
{
$path = Yii::getAlias($this->fixturePath, false);
if (!is_dir($path) || !is_writable($path)) {
throw new Exception("The fixtures path \"{$this->fixturePath}\" not exist or is not writable.");
}
}
/**
* Returns database connection component
* @return \yii\db\Connection
* @throws Exception if [[db]] is invalid.
* @throws yii\console\Exception if [[db]] is invalid.
*/
public function getDbConnection()
{
......@@ -229,8 +212,8 @@ class FixtureController extends Controller
*/
private function notifySuccess($fixtures)
{
$this->stdout("Fixtures were successfully loaded from path:\n", Console::FG_YELLOW);
$this->stdout(Yii::getAlias($this->fixturePath) . "\n\n", Console::FG_GREEN);
$this->stdout("Fixtures were successfully loaded from namespace:\n", Console::FG_YELLOW);
$this->stdout("\t\"" . Yii::getAlias($this->namespace) . "\"\n\n", Console::FG_GREEN);
$this->outputList($fixtures);
}
......@@ -241,7 +224,8 @@ class FixtureController extends Controller
private function notifyNotFound($fixtures)
{
$this->stdout("Some fixtures were not found under path:\n", Console::BG_RED);
$this->stdout(Yii::getAlias($this->fixturePath) . "\n\n", Console::FG_GREEN);
$this->stdout("\t" . Yii::getAlias($this->getFixturePath()) . "\n\n", Console::FG_GREEN);
$this->stdout("Check that they have correct namespace \"{$this->namespace}\" \n", Console::BG_RED);
$this->outputList($fixtures);
$this->stdout("\n");
}
......@@ -254,8 +238,10 @@ class FixtureController extends Controller
*/
private function confirmApply($fixtures, $except)
{
$this->stdout("Fixtures will be loaded from path: \n", Console::FG_YELLOW);
$this->stdout(Yii::getAlias($this->fixturePath) . "\n\n", Console::FG_GREEN);
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW);
$this->stdout("\t" . $this->namespace . "\n\n", Console::FG_GREEN);
$this->stdout("Fixtures below will be loaded:\n\n", Console::FG_YELLOW);
$this->outputList($fixtures);
if (count($except)) {
......@@ -263,26 +249,29 @@ class FixtureController extends Controller
$this->outputList($except);
}
return $this->confirm("\nLoad to database above fixtures?");
return $this->confirm("\nLoad above fixtures?");
}
/**
* Prompts user with confirmation for tables that should be cleared.
* @param array $tables
* Prompts user with confirmation for fixtures that should be unloaded.
* @param array $fixtures
* @param array $except
* @return boolean
*/
private function confirmClear($tables, $except)
private function confirmClear($fixtures, $except)
{
$this->stdout("Tables below will be cleared:\n\n", Console::FG_YELLOW);
$this->outputList($tables);
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW);
$this->stdout("\t" . $this->namespace . "\n\n", Console::FG_GREEN);
$this->stdout("Fixtures below will be unloaded:\n\n", Console::FG_YELLOW);
$this->outputList($fixtures);
if (count($except)) {
$this->stdout("\nTables that will NOT be cleared:\n\n", Console::FG_YELLOW);
$this->stdout("\nFixtures that will NOT be unloaded:\n\n", Console::FG_YELLOW);
$this->outputList($except);
}
return $this->confirm("\nClear tables?");
return $this->confirm("\nUnload fixtures?");
}
/**
......@@ -292,7 +281,7 @@ class FixtureController extends Controller
private function outputList($data)
{
foreach($data as $index => $item) {
$this->stdout(" " . ($index + 1) . ". {$item}\n", Console::FG_GREEN);
$this->stdout("\t" . ($index + 1) . ". {$item}\n", Console::FG_GREEN);
}
}
......@@ -312,13 +301,13 @@ class FixtureController extends Controller
*/
private function findFixtures(array $fixtures)
{
$fixturesPath = Yii::getAlias($this->fixturePath);
$fixturesPath = Yii::getAlias($this->getFixturePath());
$filesToSearch = ['*.php'];
$filesToSearch = ['*Fixture.php'];
if (!$this->needToApplyAll($fixtures[0])) {
$filesToSearch = [];
foreach ($fixtures as $fileName) {
$filesToSearch[] = $fileName . '.php';
$filesToSearch[] = $fileName . 'Fixture.php';
}
}
......@@ -326,10 +315,42 @@ class FixtureController extends Controller
$foundFixtures = [];
foreach ($files as $fixture) {
$foundFixtures[] = basename($fixture , '.php');
$foundFixtures[] = basename($fixture , 'Fixture.php');
}
return $foundFixtures;
}
/**
* Returns valid fixtures config that can be used to load them.
* @param array $fixtures fixtures to configure
* @return array
*/
private function getFixturesConfig($fixtures)
{
$config = [];
foreach($fixtures as $fixture) {
$fullClassName = $this->namespace . '\\' . $fixture . 'Fixture';
if (class_exists($fullClassName)) {
$config[Inflector::camel2id($fixture, '_')] = [
'class' => $fullClassName,
];
}
}
return $config;
}
/**
* Returns fixture path that determined on fixtures namespace.
* @return string fixture path
*/
private function getFixturePath()
{
return Yii::getAlias('@' . str_replace('\\', '/', $this->namespace));
}
}
......@@ -65,7 +65,6 @@ class ActiveFixture extends BaseActiveFixture
/**
* Loads the fixture.
*
* The default implementation will first clean up the table by calling [[resetTable()]].
* It will then populate the table with the data returned by [[getData()]].
*
* If you override this method, you should consider calling the parent implementation
......@@ -73,7 +72,7 @@ class ActiveFixture extends BaseActiveFixture
*/
public function load()
{
$this->resetTable();
parent::load();
$table = $this->getTableSchema();
......@@ -92,6 +91,17 @@ class ActiveFixture extends BaseActiveFixture
}
/**
* Unloads the fixture.
*
* The default implementation will clean up the table by calling [[resetTable()]].
*/
public function unload()
{
$this->resetTable();
parent::unload();
}
/**
* Returns the fixture data.
*
* The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
......
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