Commit cafdebb5 by Qiang Xue

CS fix. doc fix.

parent 7863a0ff
......@@ -175,46 +175,42 @@ 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
-----------------------------
Organizing Fixture Classes and Data Files
-----------------------------------------
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:
By default, fixture classes look for the corresponding data files under the `data` folder which is a sub-folder
of the folder containing the fixture class files. You can follow this convention when working with simple projects.
For big projects, chances are that you often need to switch different data files for the same fixture class for
different tests. We thus recommend that you organize the data files in a hierarchical way that is similar to
your class namespaces. For example,
- data file should follow same hierarchy that is used for your project classes namespace.
Lets see example:
```
# under folder tests\unit\fixtures
```php
#under folder tests\unit\fixtures
data\
components\
some_fixture_data_file1.php
some_fixture_data_file2.php
fixture_data_file1.php
fixture_data_file2.php
...
some_fixture_data_fileN.php
fixture_data_fileN.php
models\
db\
some_fixture_data_file1.php
some_fixture_data_file2.php
fixture_data_file1.php
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
fixture_data_fileN.php
# and so on
```
In this way you will avoid fixture data collision between tests and use them as you need.
In this way you will avoid collision of fixture data files between tests and use them as you need.
> Note: In the example above fixture files are named only for example purpose. In real life you should name them
> according to which fixture class your fixture classes are extending from. For example, if you are extending
> from [[\yii\test\ActiveFixture]] for DB fixtures, you should use DB table names as the fixture data file names;
> If you are extending for [[\yii\mongodb\ActiveFixture]] for MongoDB fixtures, you should use collection names as the file names.
> **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.
The similar hierarchy can be used to organize fixture class files. Instead of using `data` as the root directory, you may
want to use `fixtures` as the root directory to avoid conflict with the data files.
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
-------
......@@ -227,5 +223,5 @@ of running unit tests related with DB:
- Load fixtures: clean up the relevant DB tables and populate them with fixture data;
- Perform the actual test;
- Unload fixtures.
3. Repeat 2 until all tests finish.
3. Repeat Step 2 until all tests finish.
......@@ -9,8 +9,8 @@ namespace yii\faker;
use Yii;
use yii\console\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Console;
use yii\helpers\FileHelper;
/**
* This command manage fixtures creations based on given template.
......
......@@ -10,10 +10,10 @@ namespace yii\console\controllers;
use Yii;
use yii\console\Controller;
use yii\console\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Console;
use yii\test\FixtureTrait;
use yii\helpers\FileHelper;
use yii\helpers\Inflector;
use yii\test\FixtureTrait;
/**
* This command manages loading and unloading fixtures.
......@@ -40,7 +40,7 @@ use yii\helpers\Inflector;
* yii fixture User
*
* #load fixtures under $fixturePath with the different database connection
* yii fixture/apply User --db=someOtherDbConneciton
* yii fixture/apply User --db=someOtherDbConnection
*
* #load fixtures under different $fixturePath.
* yii fixture/apply User --namespace=alias\my\custom\namespace\goes\here
......@@ -80,7 +80,7 @@ class FixtureController extends Controller
public function globalOptions()
{
return array_merge(parent::globalOptions(), [
'db','namespace'
'db', 'namespace'
]);
}
......@@ -116,7 +116,7 @@ class FixtureController extends Controller
$fixtures = $this->getFixturesConfig(array_diff($foundFixtures, $except));
if (!$fixtures) {
throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"'.'');
throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"' . '');
}
$transaction = Yii::$app->db->beginTransaction();
......@@ -174,7 +174,7 @@ class FixtureController extends Controller
try {
$this->getDbConnection()->createCommand()->checkIntegrity(false)->execute();
foreach($fixtures as $fixtureConfig) {
foreach ($fixtures as $fixtureConfig) {
$fixture = Yii::createObject($fixtureConfig);
$fixture->unload();
$this->stdout("\tFixture \"{$fixture::className()}\" was successfully unloaded. \n", Console::FG_GREEN);
......@@ -185,7 +185,7 @@ class FixtureController extends Controller
} catch (\Exception $e) {
$transaction->rollback();
$this->stdout("Exception occured, transaction rollback. Tables will be in same state.\n", Console::BG_RED);
$this->stdout("Exception occurred, transaction rollback. Tables will be in same state.\n", Console::BG_RED);
throw $e;
}
}
......@@ -193,7 +193,7 @@ class FixtureController extends Controller
/**
* Returns database connection component
* @return \yii\db\Connection
* @throws yii\console\Exception if [[db]] is invalid.
* @throws \yii\console\Exception if [[db]] is invalid.
*/
public function getDbConnection()
{
......@@ -280,7 +280,7 @@ class FixtureController extends Controller
*/
private function outputList($data)
{
foreach($data as $index => $item) {
foreach ($data as $index => $item) {
$this->stdout("\t" . ($index + 1) . ". {$item}\n", Console::FG_GREEN);
}
}
......@@ -315,7 +315,7 @@ class FixtureController extends Controller
$foundFixtures = [];
foreach ($files as $fixture) {
$foundFixtures[] = basename($fixture , 'Fixture.php');
$foundFixtures[] = basename($fixture, 'Fixture.php');
}
return $foundFixtures;
......@@ -330,7 +330,7 @@ class FixtureController extends Controller
{
$config = [];
foreach($fixtures as $fixture) {
foreach ($fixtures as $fixture) {
$fullClassName = $this->namespace . '\\' . $fixture . 'Fixture';
......
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