Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.4 KiB

11 years ago
Managing Fixtures
=================
// todo: this tutorial may be merged into test-fixture.md
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.
11 years ago
Yii supports fixtures via the `yii fixture` command line tool. This tool supports:
11 years ago
* Loading fixtures to different storage such as: RDBMS, NoSQL, etc;
11 years ago
* Unloading fixtures in different ways (usually it is clearing storage);
* Auto-generating fixtures and populating it with random data.
Fixtures format
---------------
11 years ago
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:
```
11 years ago
#users.php file under fixtures data path, by default @tests\unit\fixtures\data
return [
[
'name' => 'Chase',
'login' => 'lmayert',
'email' => 'strosin.vernice@jerde.com',
'auth_key' => 'K3nF70it7tzNsHddEiq0BZ0i-OU8S3xV',
'password' => '$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/iK0r3jRuwQEs2ldRu.a2',
],
[
'name' => 'Celestine',
'login' => 'napoleon69',
'email' => 'aileen.barton@heaneyschumm.com',
'auth_key' => 'dZlXsVnIDgIzFgX4EduAqkEPuphhOh9q',
'password' => '$2y$13$kkgpvJ8lnjKo8RuoR30ay.RjDf15bMcHIF7Vz1zz/6viYG5xJExU6',
],
];
```
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).
11 years ago
Fixture classes name should not be plural.
11 years ago
Loading fixtures
----------------
Fixture classes should be suffixed by `Fixture` class. By default fixtures will be searched under `tests\unit\fixtures` namespace, you can
11 years ago
change this behavior with config or command options.
11 years ago
To apply fixture, run the following command:
```
11 years ago
yii fixture/apply <fixture_name>
```
11 years ago
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:
```
11 years ago
// apply `users` fixture
11 years ago
yii fixture/apply User
// same as above, because default action of "fixture" command is "apply"
11 years ago
yii fixture User
11 years ago
// apply several fixtures. Note that there should not be any whitespace between ",", it should be one string.
11 years ago
yii fixture User,UserProfile
// apply all fixtures
yii fixture/apply all
// same as above
yii fixture all
11 years ago
// apply fixtures, but for other database connection.
11 years ago
yii fixtures User --db='customDbConnectionId'
11 years ago
// apply fixtures, but search them in different namespace. By default namespace is: tests\unit\fixtures.
11 years ago
yii fixtures User --namespace='alias\my\custom\namespace'
```
11 years ago
Unloading fixtures
------------------
11 years ago
To unload fixture, run the following command:
```
11 years ago
// unload Users fixture, by default it will clear fixture storage (for example "users" table, or "users" collection if this is mongodb fixture).
11 years ago
yii fixture/clear User
11 years ago
// Unload several fixtures. Note that there should not be any whitespace between ",", it should be one string.
11 years ago
yii fixture/clear User,UserProfile
11 years ago
// unload all fixtures
yii fixture/clear all
```
11 years ago
Same command options like: `db`, `namespace` also can be applied to this command.
11 years ago
Configure Command Globally
--------------------------
While command line options allow us to configure the migration command
on-the-fly, sometimes we may want to configure the command once for all. For example you can configure
different migration path as follows:
```
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'db' => 'customDbConnectionId',
11 years ago
'namespace' => 'myalias\some\custom\namespace',
],
]
```
Auto-generating fixtures
------------------------
Yii also can auto-generate fixtures for you based on some template. You can generate your fixtures with different data on different languages and formats.
These feature is done by [Faker](https://github.com/fzaninotto/Faker) library and `yii2-faker` extension.
11 years ago
See extension [guide](https://github.com/yiisoft/yii2/tree/master/extensions/faker) for more docs.