mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Update test-fixtures.md
This commit is contained in:
@ -121,15 +121,19 @@ class UserProfileFixture extends ActiveFixture
|
||||
```php
|
||||
namespace app\tests\unit\models;
|
||||
|
||||
use yii\codeception\DbTestCase;
|
||||
|
||||
use app\tests\fixtures\UserProfileFixture;
|
||||
|
||||
class UserProfileTest extends DbTestCase
|
||||
{
|
||||
public function fixtures()
|
||||
class UserProfileTest extends \Codeception\Test\Unit
|
||||
{
|
||||
public function _fixtures()
|
||||
{
|
||||
return [
|
||||
'profiles' => UserProfileFixture::className(),
|
||||
'profiles' => [
|
||||
'class' => UserProfileFixture::className(),
|
||||
// fixture data located in tests/_data/user.php
|
||||
'dataFile' => codecept_data_dir() . 'user.php'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -140,59 +144,38 @@ class UserProfileTest extends DbTestCase
|
||||
在测试用例的每个测试方法运行前 `fixtures()` 方法列表返回的 Fixture 会被自动的加载,
|
||||
并在结束后自动的卸载。同样,如前面所述,当一个 Fixture 被加载之前,
|
||||
所有它依赖的 Fixture 也会被自动的加载。在上面的例子中,因为 `UserProfileFixture`
|
||||
依赖于 `UserFixtrue`,当运行测试类中的任意测试方法时,
|
||||
两个 Fixture,`UserFixture` 和 `UserProfileFixture` 会被依序加载。
|
||||
依赖于 `UserFixtrue`,当运行测试类中的任意测试方法时,两个 Fixture,`UserFixture` 和 `UserProfileFixture` 会被依序加载。
|
||||
|
||||
当我们通过 `fixtures()` 方法指定需要加载的 Fixture 时,我们既可以使用一个类名,
|
||||
也可以使用一个配置数组。配置数组可以让你自定义加载的 fixture 的属性名。
|
||||
也可以使用一个配置数组。
|
||||
配置数组可以让你自定义加载的 fixture 的属性名。
|
||||
|
||||
你同样可以给一个 Fixture 指定一个别名(alias),在上面的例子中,`UserProfileFixture` 的别名为 `profiles` 。
|
||||
在测试方法中,你可以通过别名来访问一个 Fixture 对象。比如,
|
||||
`$this->profiles` 将会返回 `UserProfileFixture` 对象。
|
||||
|
||||
```php
|
||||
$profile = $I->grabFixture('profiles', 'user1');
|
||||
```
|
||||
|
||||
将会返回 `UserProfileFixture` 对象。
|
||||
|
||||
因为 `UserProfileFixture` 从 `ActiveFixture` 处继承,
|
||||
在后面,你可以通过如下的语法形式来访问 Fixture 提供的数据:
|
||||
|
||||
```php
|
||||
// returns the data row aliased as 'user1'
|
||||
$row = $this->profiles['user1'];
|
||||
// returns the UserProfile model corresponding to the data row aliased as 'user1'
|
||||
$profile = $this->profiles('user1');
|
||||
// traverse every data row in the fixture
|
||||
foreach ($this->profiles as $row) ...
|
||||
$profile = $I->grabFixture('profiles', 'user1');
|
||||
// traverse data in the fixture
|
||||
foreach ($I->grabFixture('profiles') as $profile) ...
|
||||
```
|
||||
|
||||
> 注: `$this->profiles` 的类型仍为 `UserProfileFixture`,
|
||||
> 以上的例子是通过 PHP 魔术方法来实现的。
|
||||
|
||||
|
||||
定义和使用全局 Fixtures
|
||||
----------------------
|
||||
|
||||
以上示例的 Fixture 主要用于单个的测试用例,
|
||||
在许多情况下,你需要使用一些全局的 Fixture 来让所有或者大量的测试用例使用。
|
||||
[[yii\test\InitDbFixture]] 就是这样的一个例子,它主要做两个事情:
|
||||
|
||||
* 它通过执行在 `@app/tests/fixtures/initdb.php` 里的脚本来做一些通用的初始化任务;
|
||||
* 在加载其他的 DB Fixture 之前禁用数据库完整性校验,同时在其他的 DB Fixture 卸载后启用数据库完整性校验。
|
||||
|
||||
全局的 Fixture 和非全局的用法类似,唯一的区别是你在 [[yii\codeception\TestCase::globalFixtures()]] 中声明它,
|
||||
而非 `fixtures()` 方法。当一个测试用例加载 Fixture 时,
|
||||
它首先加载全局的 Fixture,然后才是非全局的。
|
||||
|
||||
[[yii\codeception\DbTestCase]] 默认已经在其 `globalFixtures()` 方法中声明了 `InitDbFixture`,
|
||||
这意味着如果你想要在每个测试之前执行一些初始化工作,你只需要调整 `@app/tests/fixtures/initdb.php` 中的代码即可。
|
||||
你只需要简单的集中精力中开发单个的测试用例和相关的 Fixture。
|
||||
|
||||
|
||||
组织 Fixture 类和相关的数据文件
|
||||
-----------------------------------------
|
||||
|
||||
默认情况下,Fixture 类会在其所在的目录下面的 `data` 子目录寻找相关的数据文件。
|
||||
在一些简单的项目中,你可以遵循此范例。对于一些大项目,
|
||||
您可能经常为同一个 Fixture 类的不同测试而切换不同的数据文件。
|
||||
在这种情况下,我们推荐你按照一种类似于命名空间
|
||||
的方式有层次地组织你的数据文件,比如:
|
||||
在这种情况下,我们推荐你按照一种类似于命名空间的方式有层次地组织你的数据文件,比如:
|
||||
|
||||
```
|
||||
# under folder tests\unit\fixtures
|
||||
@ -214,56 +197,27 @@ data\
|
||||
这样,你就可以避免在测试用例之间产生冲突,并根据你的需要使用它们。
|
||||
|
||||
> Note: 在以上的例子中,Fixture 文件只用于示例目的。在真实的环境下,你需要根据你的 Fixture 类继承的基类来决定它们的命名。
|
||||
比如,如果你从 [[yii\test\ActiveFixture]] 继承了一个 DB Fixture,
|
||||
你需要用数据库表名字作为 Fixture 的数据文件名;如果你从 [[yii\mongodb\ActiveFixture]] 继承了一个 MongoDB Fixture,
|
||||
你需要使用 collection 名作为文件名。
|
||||
> 比如,如果你从 [[yii\test\ActiveFixture]] 继承了一个 DB Fixture,
|
||||
> 你需要用数据库表名字作为 Fixture 的数据文件名;如果你从 [[yii\mongodb\ActiveFixture]] 继承了一个 MongoDB Fixture,
|
||||
> 你需要使用 collection 名作为文件名。
|
||||
|
||||
组织 Fixuture 类名的方式同样可以使用前述的层次组织法,但是,为了避免跟数据文件产生冲突,
|
||||
你需要用 `fixtures` 作为根目录而非 `data`。
|
||||
|
||||
## 使用 `yii fixture` 来管理 fixtures
|
||||
|
||||
总结
|
||||
-------
|
||||
Yii supports fixtures via the `yii fixture` command line tool. This tool supports:
|
||||
|
||||
> Note: 本节内容正在开发中。
|
||||
* Loading fixtures to different storage such as: RDBMS, NoSQL, etc;
|
||||
* Unloading fixtures in different ways (usually it is clearing storage);
|
||||
* Auto-generating fixtures and populating it with random data.
|
||||
|
||||
在上面,我们描述了如何定义和使用 Fixture,在下面,我们将总结出一个
|
||||
标准地运行与 DB 有关的单元测试的规范工作流程:
|
||||
### Fixtures 数据格式
|
||||
|
||||
1. 使用 `yii migrate` 工具来让你的测试数据库更新到最新的版本;
|
||||
2. 运行一个测试:
|
||||
- 加载 Fixture:清空所有的相关表结构,并用 Fixture 数据填充
|
||||
- 执行真实的测试用例
|
||||
- 卸载 Fixture
|
||||
3. 重复步骤2直到所有的测试结束。
|
||||
|
||||
|
||||
**以下部分即将被清除**
|
||||
|
||||
管理 Fixture
|
||||
=================
|
||||
|
||||
>注: 本章节正在开发中
|
||||
>
|
||||
> todo: 以下部分将会与 test-fixtures.md 合并。
|
||||
|
||||
Fixture 是测试中很很重要的一个部分,它们的主要目的是为你提供不同的测试所需要的数据。
|
||||
因为这些数据,你的测试将会更高效和有用。
|
||||
|
||||
Yii 通过 `yii fixture` 命令行工具来支持 Fixture,这个工具支持:
|
||||
|
||||
* 把 Fixture 加载到不同的存储工具中,比如:RDBMS,NoSQL 等;
|
||||
* 以不同的方式卸载 Fixture(通常是清空存储);
|
||||
* 自动的生成 Fixutre 并用随机数据填充。
|
||||
|
||||
Fixtures 格式
|
||||
---------------
|
||||
|
||||
Fixtures 是不同方法和配置的对象, 官方引用[documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md) 。
|
||||
让我们假设有 Fixtures 数据加载:
|
||||
让我们假设我们有要加载的 fixtures 数据:
|
||||
|
||||
```
|
||||
#Fixtures 数据目录的 users.php 文件,默认 @tests\unit\fixtures\data
|
||||
#users.php file under fixtures data path, by default @tests\unit\fixtures\data
|
||||
|
||||
return [
|
||||
[
|
||||
@ -282,19 +236,20 @@ return [
|
||||
],
|
||||
];
|
||||
```
|
||||
如果我们使用 Fixture 将数据加载到数据库,那么这些行将被应用于 `users` 表。 如果我们使用 nosql Fixtures ,例如 `mongodb`
|
||||
fixture,那么这些数据将应用于 `users` mongodb 集合。 为了了解更多实现各种加载策略,访问官网 [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md)。
|
||||
上面的 Fixture 案例是由 `yii2-faker` 扩展自动生成的, 在这里了解更多 [section](#auto-generating-fixtures)。
|
||||
Fixture 类名不应该是复数形式。
|
||||
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-fixtures.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.
|
||||
|
||||
加载 Fixtures
|
||||
----------------
|
||||
### 加载 Fixtures
|
||||
|
||||
Fixture 类应该以 `Fixture` 类作为后缀。默认的 Fixtures 能在 `tests\unit\fixtures` 命名空间下被搜索到,
|
||||
你可以通过配置和命名行选项来更改这个行为,你可以通过加载或者卸载指定它名字前面的`-`来排除一些 Fixtures,像 `-User`。
|
||||
你可以通过配置和命名行选项来更改这个行为,你可以通过加载或者卸载指定它名字前面的 `-` 来排除一些 Fixtures,像 `-User`。
|
||||
|
||||
运行如下命令去加载 Fixture:
|
||||
|
||||
> Note: Prior to loading data unload sequence is executed. Usually that results in cleaning up all the existing data inserted by previous fixture executions.
|
||||
|
||||
```
|
||||
yii fixture/load <fixture_name>
|
||||
```
|
||||
@ -330,8 +285,7 @@ yii fixture User --namespace='alias\my\custom\namespace'
|
||||
yii fixture User --globalFixtures='some\name\space\Custom'
|
||||
```
|
||||
|
||||
卸载 Fixtures
|
||||
------------------
|
||||
### 卸载 Fixtures
|
||||
|
||||
运行如下命名去卸载 Fixtures:
|
||||
|
||||
@ -350,10 +304,10 @@ yii fixture/unload "*" -DoNotUnloadThisOne
|
||||
|
||||
```
|
||||
|
||||
同样的命名选项: `namespace`, `globalFixtures` 也可以应用于该命令。
|
||||
同样的命名选项:`namespace`,`globalFixtures` 也可以应用于该命令。
|
||||
|
||||
### 全局命令配置
|
||||
|
||||
全局命令配置
|
||||
--------------------------
|
||||
当命令行选项允许我们配置迁移命令,
|
||||
有时我们只想配置一次。例如,
|
||||
你可以按照如下配置迁移目录:
|
||||
@ -371,9 +325,20 @@ yii fixture/unload "*" -DoNotUnloadThisOne
|
||||
]
|
||||
```
|
||||
|
||||
自动生成 fixtures
|
||||
------------------------
|
||||
### 自动生成 fixtures
|
||||
|
||||
Yii 还可以为你自动生成一些基于一些模板的 Fixtures。 你能够以不同语言格式用不同的数据生成你的 Fixtures。
|
||||
这些特征由 [Faker](https://github.com/fzaninotto/Faker) 库和 `yii2-faker` 扩展完成。
|
||||
关注 [guide](https://github.com/yiisoft/yii2-faker) 扩展获取更多的文档。
|
||||
|
||||
## 总结
|
||||
|
||||
在上面,我们描述了如何定义和使用 Fixture,在下面,我们将总结出一个
|
||||
标准地运行与 DB 有关的单元测试的规范工作流程:
|
||||
|
||||
1. 使用 `yii migrate` 工具来让你的测试数据库更新到最新的版本;
|
||||
2. 运行一个测试:
|
||||
- 加载 Fixture:清空所有的相关表结构,并用 Fixture 数据填充
|
||||
- 执行真实的测试用例
|
||||
- 卸载 Fixture
|
||||
3. 重复步骤2直到所有的测试结束。
|
||||
|
||||
Reference in New Issue
Block a user