mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-18 17:21:26 +08:00
docs fixed
This commit is contained in:
@ -4,20 +4,20 @@ Database Fixtures
|
||||
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 new fixtures to different storages like: 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,34 @@ 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 to database then this 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. You can implement different loading strategies and other needed things, for that 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).
|
||||
|
||||
Applying fixtures
|
||||
-----------------
|
||||
Loading fixtures
|
||||
----------------
|
||||
|
||||
To apply fixture to the table, run the following command:
|
||||
Fixtures 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, 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 Users
|
||||
|
||||
// same as above, because default action of "fixture" command is "apply"
|
||||
yii fixture users
|
||||
yii fixture Users
|
||||
|
||||
// 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 Users,UsersProfiles
|
||||
|
||||
// apply all fixtures
|
||||
yii fixture/apply all
|
||||
@ -68,29 +71,37 @@ 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 Users fixture, but fixture will be taken from different path.
|
||||
yii fixture Users --fixturePath='@app/my/custom/path/to/fixtures'
|
||||
|
||||
// apply fixtures to the table users, but for other database connection.
|
||||
yii fixtures users --db='customDbConnectionId'
|
||||
// apply fixtures, 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 Users --namespace='my/custom/namespace'
|
||||
|
||||
// apply fixtures without namespace under fixturesPath
|
||||
yii fixtures all --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 Users
|
||||
|
||||
// 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 Users,UsersProfiles
|
||||
|
||||
// clear all tables of current connection in current schema
|
||||
// unload all fixtures
|
||||
yii fixture/clear all
|
||||
```
|
||||
|
||||
Same command options like: `fixturesPath`, `db`, `namespace` also can be applied to this command.
|
||||
|
||||
Configure Command Globally
|
||||
--------------------------
|
||||
While command line options allow us to configure the migration command
|
||||
|
Reference in New Issue
Block a user