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 ...@@ -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. 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. By default, fixture classes look for the corresponding data files under the `data` folder which is a sub-folder
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. of the folder containing the fixture class files. You can follow this convention when working with simple projects.
But when your project is not very simple you should not be greedy when using data files and organize them according these rule: 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. ```
# under folder tests\unit\fixtures
Lets see example:
```php
#under folder tests\unit\fixtures
data\ data\
components\ components\
some_fixture_data_file1.php fixture_data_file1.php
some_fixture_data_file2.php fixture_data_file2.php
... ...
some_fixture_data_fileN.php fixture_data_fileN.php
models\ models\
db\ fixture_data_file1.php
some_fixture_data_file1.php fixture_data_file2.php
some_fixture_data_file2.php ...
... fixture_data_fileN.php
some_fixture_data_fileN.php # and so on
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. 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. The similar hierarchy can be used to organize fixture class files. Instead of using `data` as the root directory, you may
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. 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 Summary
------- -------
...@@ -227,5 +223,5 @@ of running unit tests related with DB: ...@@ -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; - Load fixtures: clean up the relevant DB tables and populate them with fixture data;
- Perform the actual test; - Perform the actual test;
- Unload fixtures. - Unload fixtures.
3. Repeat 2 until all tests finish. 3. Repeat Step 2 until all tests finish.
...@@ -9,8 +9,8 @@ namespace yii\faker; ...@@ -9,8 +9,8 @@ namespace yii\faker;
use Yii; use Yii;
use yii\console\Exception; use yii\console\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Console; use yii\helpers\Console;
use yii\helpers\FileHelper;
/** /**
* This command manage fixtures creations based on given template. * This command manage fixtures creations based on given template.
......
...@@ -10,10 +10,10 @@ namespace yii\console\controllers; ...@@ -10,10 +10,10 @@ namespace yii\console\controllers;
use Yii; use Yii;
use yii\console\Controller; use yii\console\Controller;
use yii\console\Exception; use yii\console\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Console; use yii\helpers\Console;
use yii\test\FixtureTrait; use yii\helpers\FileHelper;
use yii\helpers\Inflector; use yii\helpers\Inflector;
use yii\test\FixtureTrait;
/** /**
* This command manages loading and unloading fixtures. * This command manages loading and unloading fixtures.
...@@ -40,7 +40,7 @@ use yii\helpers\Inflector; ...@@ -40,7 +40,7 @@ use yii\helpers\Inflector;
* yii fixture User * yii fixture User
* *
* #load fixtures under $fixturePath with the different database connection * #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. * #load fixtures under different $fixturePath.
* yii fixture/apply User --namespace=alias\my\custom\namespace\goes\here * yii fixture/apply User --namespace=alias\my\custom\namespace\goes\here
...@@ -51,7 +51,7 @@ use yii\helpers\Inflector; ...@@ -51,7 +51,7 @@ use yii\helpers\Inflector;
*/ */
class FixtureController extends Controller class FixtureController extends Controller
{ {
use FixtureTrait; use FixtureTrait;
/** /**
...@@ -80,7 +80,7 @@ class FixtureController extends Controller ...@@ -80,7 +80,7 @@ class FixtureController extends Controller
public function globalOptions() public function globalOptions()
{ {
return array_merge(parent::globalOptions(), [ return array_merge(parent::globalOptions(), [
'db','namespace' 'db', 'namespace'
]); ]);
} }
...@@ -116,7 +116,7 @@ class FixtureController extends Controller ...@@ -116,7 +116,7 @@ class FixtureController extends Controller
$fixtures = $this->getFixturesConfig(array_diff($foundFixtures, $except)); $fixtures = $this->getFixturesConfig(array_diff($foundFixtures, $except));
if (!$fixtures) { 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(); $transaction = Yii::$app->db->beginTransaction();
...@@ -174,7 +174,7 @@ class FixtureController extends Controller ...@@ -174,7 +174,7 @@ class FixtureController extends Controller
try { try {
$this->getDbConnection()->createCommand()->checkIntegrity(false)->execute(); $this->getDbConnection()->createCommand()->checkIntegrity(false)->execute();
foreach($fixtures as $fixtureConfig) { foreach ($fixtures as $fixtureConfig) {
$fixture = Yii::createObject($fixtureConfig); $fixture = Yii::createObject($fixtureConfig);
$fixture->unload(); $fixture->unload();
$this->stdout("\tFixture \"{$fixture::className()}\" was successfully unloaded. \n", Console::FG_GREEN); $this->stdout("\tFixture \"{$fixture::className()}\" was successfully unloaded. \n", Console::FG_GREEN);
...@@ -185,7 +185,7 @@ class FixtureController extends Controller ...@@ -185,7 +185,7 @@ class FixtureController extends Controller
} catch (\Exception $e) { } catch (\Exception $e) {
$transaction->rollback(); $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; throw $e;
} }
} }
...@@ -193,7 +193,7 @@ class FixtureController extends Controller ...@@ -193,7 +193,7 @@ class FixtureController extends Controller
/** /**
* Returns database connection component * Returns database connection component
* @return \yii\db\Connection * @return \yii\db\Connection
* @throws yii\console\Exception if [[db]] is invalid. * @throws \yii\console\Exception if [[db]] is invalid.
*/ */
public function getDbConnection() public function getDbConnection()
{ {
...@@ -280,7 +280,7 @@ class FixtureController extends Controller ...@@ -280,7 +280,7 @@ class FixtureController extends Controller
*/ */
private function outputList($data) private function outputList($data)
{ {
foreach($data as $index => $item) { foreach ($data as $index => $item) {
$this->stdout("\t" . ($index + 1) . ". {$item}\n", Console::FG_GREEN); $this->stdout("\t" . ($index + 1) . ". {$item}\n", Console::FG_GREEN);
} }
} }
...@@ -315,7 +315,7 @@ class FixtureController extends Controller ...@@ -315,7 +315,7 @@ class FixtureController extends Controller
$foundFixtures = []; $foundFixtures = [];
foreach ($files as $fixture) { foreach ($files as $fixture) {
$foundFixtures[] = basename($fixture , 'Fixture.php'); $foundFixtures[] = basename($fixture, 'Fixture.php');
} }
return $foundFixtures; return $foundFixtures;
...@@ -330,7 +330,7 @@ class FixtureController extends Controller ...@@ -330,7 +330,7 @@ class FixtureController extends Controller
{ {
$config = []; $config = [];
foreach($fixtures as $fixture) { foreach ($fixtures as $fixture) {
$fullClassName = $this->namespace . '\\' . $fixture . '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