PHPackages                             fangx/testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. fangx/testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

fangx/testing
=============

hyperf database testing support

v3.2.0(4y ago)13.5k3MITPHPPHP &gt;=7.2CI failing

Since May 27Pushed 4y ago1 watchersCompare

[ Source](https://github.com/fangx-packages/hyperf-testing)[ Packagist](https://packagist.org/packages/fangx/testing)[ RSS](/packages/fangx-testing/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (11)Versions (23)Used By (0)

hyperf-testing
--------------

[](#hyperf-testing)

Hyperf 中单元测试增强扩展包

- 支持数据库测试.
- 支持 Http Response 直接进行断言.
- 支持 Event 断言, 而不是直接执行 Event.
- 支持 Queue 断言, 而不是直接执行 Queue.
- 支持 Http 伪造响应, 而不需要真正向第三方发送请求.
- 支持 Blade 视图渲染测试.

在 hyperf 中使用数据库进行单元测试.

**推荐使用 [`fangx/sqlite-driver`](https://github.com/nfangxu/hyperf-sqlite-driver) 作为数据库驱动进行测试.**

> 为了保证测试独立, 所以每个测试完成后都会重新刷新容器. 该情况下会导致使用异步协程情况的时候, 使用 `ApplicationContext::getContainer()` 每次获取到的容器可能不一样 在异步里面应该避免直接使用 `ApplicationContext::getContainer()` 获取容器的相关内容. Hyperf 官方推荐做法是通过构造函数直接直接注入 `Container`, 而不是每次使用容器都通过 `ApplicationContext::getContainer()` 获取.

安装
--

[](#安装)

```
composer require fangx/testing --dev
```

使用
--

[](#使用)

Hyperf 2.0 使用 `2.1` 版本, Hyperf 2.1 使用 `3.*` 版本

> - `Fangx\Testing\Concerns\CommandCaller`: 提供 `command()` 方法, 在程序中执行命令.
> - `Fangx\Testing\Concerns\DatabaseMigrations`: 参照 laravel. 需要依赖 `CommandCaller` 执行迁移命令.
> - `Fangx\Testing\Concerns\RefreshDatabase`: 参照 laravel. 需要依赖 `CommandCaller` 执行迁移命令.
> - `Fangx\Testing\Concerns\DatabaseTransactions`: 参照 laravel.

**运行迁移时, 默认执行的是 `migrations/testing` 目录下的迁移文件. 可以通过设置 `getMigrationsPath` 方法来自定义迁移文件的目录**

### 使用命令创建

[](#使用命令创建)

```
php bin/hyperf.php fx:test UserTest
php bin/hyperf.php fx:test UserTest --unit
```

### 直接创建

[](#直接创建)

在项目中的测试目录下创建 `Units/UserTest.php` 填入一下内容.

```
