hyperf/docs/zh-hk/testing.md

511 lines
12 KiB
Markdown
Raw Normal View History

2019-12-12 16:24:04 +08:00
# 自動化測試
在 Hyperf 裏測試默認通過 `phpunit` 來實現,並在 3.1 支持了基於 phpunit 的框架 `pest` [文檔](https://pestphp.com/docs/installation)。
```shell
composer require hyperf/testing --dev
composer require pestphp/pest --dev
2019-06-16 20:58:50 +08:00
```
```json
"scripts": {
"pest": "pest --colors=always",
"test": "co-phpunit -c phpunit.xml --colors=always"
},
```
| package | version |
| --------------- | ------- |
| phpunit/phpunit | ^10.1 |
| pestphp/pest | ^2.8 |
## Bootstrap
2019-12-12 16:24:04 +08:00
Hyperf 提供了默認的 `bootstrap.php` 文件,它讓用户在運行單元測試時,掃描並加載對應的庫到內存裏。
2019-06-16 21:02:21 +08:00
```php
<?php
declare(strict_types=1);
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
2019-06-16 21:02:21 +08:00
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
2019-06-16 21:02:21 +08:00
// 默認開啓 當使用 pest --parallel 特性或其他涉及到原生並行操作時需要註釋掉
Swoole\Runtime::enableCoroutine(true);
2019-06-16 21:02:21 +08:00
require BASE_PATH . '/vendor/autoload.php';
Hyperf\Di\ClassLoader::init();
$container = require BASE_PATH . '/config/container.php';
$container->get(Hyperf\Contract\ApplicationInterface::class);
2019-06-16 21:02:21 +08:00
```
運行單元測試
```
composer test
```
## 注意事項
- `hyperf/testing` 提供了 Trait [RunTestsInCoroutine](https://github.com/hyperf/hyperf/blob/master/src/testing/src/Concerns/RunTestsInCoroutine.php) 。只需在特定的 `Test` 中 use 此類即開啓協程環境
- 當使用 pest 中的 --parallel 參數特性 時需要註釋掉 `test/bootstrap.php` 中的 `Swoole\Runtime::enableCoroutine(true)`
2019-12-12 16:24:04 +08:00
## 模擬 HTTP 請求
2022-12-27 13:37:04 +08:00
在開發接口時我們通常需要一段自動化測試腳本來保證我們提供的接口按預期在運行Hyperf 框架下提供了 `Hyperf\Testing\Client` 類,可以讓您在不啓動 Server 的情況下,模擬 HTTP 服務的請求:
```php
<?php
2019-06-16 20:58:50 +08:00
use Hyperf\Testing\Client;
$client = make(Client::class);
$result = $client->get('/');
```
2019-12-12 16:24:04 +08:00
因為 Hyperf 支持多端口配置,除了驗證默認的端口接口外,如果驗證其他端口的接口呢?
```php
<?php
2019-06-16 20:58:50 +08:00
use Hyperf\Testing\Client;
$client = make(Client::class, ['server' => 'adminHttp']);
$result = $client->json('/user/0',[
'nickname' => 'Hyperf'
]);
```
默認情況下,框架使用 `JsonPacker`,會直接解析 `Body``array`,如果您直接返回 `string`,則需要設置對應 `Packer`
```php
<?php
use Hyperf\Testing\Client;
use Hyperf\Contract\PackerInterface;
$client = make(Client::class, [
'packer' => new class() implements PackerInterface {
public function pack($data): string
{
return $data;
}
public function unpack(string $data)
{
return $data;
}
},
]);
$result = $client->json('/user/0',[
'nickname' => 'Hyperf'
]);
```
### 使用 Cookies
```php
<?php
use Hyperf\Testing\Client;
2023-12-29 09:30:38 +08:00
use Hyperf\Codec\Json;
$client = make(Client::class);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
```
## 示例
2019-12-12 16:24:04 +08:00
讓我們寫個小 DEMO 來測試一下。
```php
<?php
declare(strict_types=1);
namespace HyperfTest\Cases;
2019-06-16 20:58:50 +08:00
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ExampleTest extends TestCase
{
protected Client $client;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->client = make(Client::class);
}
public function testExample()
{
$this->assertTrue(true);
$res = $this->client->get('/');
$this->assertSame(0, $res['code']);
$this->assertSame('Hello Hyperf.', $res['data']['message']);
$this->assertSame('GET', $res['data']['method']);
$this->assertSame('Hyperf', $res['data']['user']);
$res = $this->client->get('/', ['user' => 'developer']);
$this->assertSame(0, $res['code']);
$this->assertSame('developer', $res['data']['user']);
$res = $this->client->post('/', [
'user' => 'developer',
]);
$this->assertSame('Hello Hyperf.', $res['data']['message']);
$this->assertSame('POST', $res['data']['method']);
$this->assertSame('developer', $res['data']['user']);
$res = $this->client->json('/', [
'user' => 'developer',
]);
$this->assertSame('Hello Hyperf.', $res['data']['message']);
$this->assertSame('POST', $res['data']['method']);
$this->assertSame('developer', $res['data']['user']);
$res = $this->client->file('/', ['name' => 'file', 'file' => BASE_PATH . '/README.md']);
$this->assertSame('Hello Hyperf.', $res['data']['message']);
$this->assertSame('POST', $res['data']['method']);
$this->assertSame('README.md', $res['data']['file']);
}
}
2019-06-27 12:09:46 +08:00
```
2019-12-12 16:24:04 +08:00
## 調試代碼
2019-06-27 12:09:46 +08:00
2022-12-27 13:37:04 +08:00
在 FPM 場景下,我們通常改完代碼,然後打開瀏覽器訪問對應接口,所以我們通常會需要兩個函數 `dd``dump`,但 `Hyperf` 跑在 `CLI` 模式下,就算提供了這兩個函數,也需要在 `CLI` 中重啓 `Server`,然後再到瀏覽器中調用對應接口查看結果。這樣其實並沒有簡化流程,反而更麻煩了。
2019-06-27 12:09:46 +08:00
2019-12-12 16:24:04 +08:00
接下來,我來介紹如何通過配合 `testing`,來快速調試代碼,順便完成單元測試。
2019-06-27 12:09:46 +08:00
2019-12-12 16:24:04 +08:00
假設我們在 `UserDao` 中實現了一個查詢用户信息的函數
2019-06-27 12:09:46 +08:00
```php
namespace App\Service\Dao;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\User;
class UserDao extends Dao
{
/**
* @param $id
* @param bool $throw
* @return
*/
public function first($id, $throw = true)
{
$model = User::query()->find($id);
if ($throw && empty($model)) {
throw new BusinessException(ErrorCode::USRE_NOT_EXIST);
}
return $model;
}
}
```
2019-12-12 16:24:04 +08:00
那我們編寫對應的單元測試
2019-06-27 12:09:46 +08:00
```php
namespace HyperfTest\Cases;
use HyperfTest\HttpTestCase;
use App\Service\Dao\UserDao;
/**
* @internal
* @coversNothing
*/
class UserTest extends HttpTestCase
{
public function testUserDaoFirst()
{
$model = \Hyperf\Context\ApplicationContext::getContainer()->get(UserDao::class)->first(1);
2019-06-27 12:09:46 +08:00
var_dump($model);
$this->assertSame(1, $model->id);
}
}
```
2019-12-12 16:24:04 +08:00
然後執行我們的單測
2019-06-27 12:09:46 +08:00
```
composer test -- --filter=testUserDaoFirst
2019-06-28 11:39:59 +08:00
```
2019-12-12 16:24:04 +08:00
## 測試替身
2019-12-12 16:24:04 +08:00
`Gerard Meszaros``Meszaros2007` 中介紹了測試替身的概念:
2019-12-12 16:24:04 +08:00
有時候對 `被測系統(SUT)` 進行測試是很困難的,因為它依賴於其他無法在測試環境中使用的組件。這有可能是因為這些組件不可用,它們不會返回測試所需要的結果,或者執行它們會有不良副作用。在其他情況下,我們的測試策略要求對被測系統的內部行為有更多控制或更多可見性。
2019-12-12 16:24:04 +08:00
如果在編寫測試時無法使用(或選擇不使用)實際的依賴組件(DOC),可以用測試替身來代替。測試替身不需要和真正的依賴組件有完全一樣的的行為方式;他只需要提供和真正的組件同樣的 API 即可,這樣被測系統就會以為它是真正的組件!
下面展示分別通過構造函數注入依賴、通過 `#[Inject]` 註釋注入依賴的測試替身
2019-12-12 16:24:04 +08:00
### 構造函數注入依賴的測試替身
2019-11-14 01:14:29 +08:00
```php
<?php
namespace App\Logic;
use App\Api\DemoApi;
class DemoLogic
{
private DemoApi $demoApi;
public function __construct(DemoApi $demoApi)
{
$this->demoApi = $demoApi;
}
public function test()
{
$result = $this->demoApi->test();
return $result;
}
}
```
2019-11-14 01:14:29 +08:00
```php
<?php
namespace App\Api;
class DemoApi
{
public function test()
{
return [
'status' => 1
];
}
}
```
2019-11-14 01:14:29 +08:00
```php
<?php
namespace HyperfTest\Cases;
use App\Api\DemoApi;
use App\Logic\DemoLogic;
use Hyperf\Di\Container;
use HyperfTest\HttpTestCase;
use Mockery;
class DemoLogicTest extends HttpTestCase
{
2021-07-27 11:01:21 +08:00
public function tearDown(): void
{
Mockery::close();
}
public function testIndex()
{
$res = $this->getContainer()->get(DemoLogic::class)->test();
$this->assertEquals(1, $res['status']);
}
/**
* @return Container
*/
protected function getContainer()
{
$container = Mockery::mock(Container::class);
$apiStub = $this->createMock(DemoApi::class);
$apiStub->method('test')->willReturn([
'status' => 1,
]);
$container->shouldReceive('get')->with(DemoLogic::class)->andReturn(new DemoLogic($apiStub));
return $container;
}
}
```
2019-12-12 16:24:04 +08:00
### 通過 Inject 註釋注入依賴的測試替身
2019-11-14 01:14:29 +08:00
```php
<?php
namespace App\Logic;
use App\Api\DemoApi;
use Hyperf\Di\Annotation\Inject;
class DemoLogic
{
#[Inject]
private DemoApi $demoApi;
public function test()
{
$result = $this->demoApi->test();
return $result;
}
}
```
2019-11-14 01:14:29 +08:00
```php
<?php
namespace App\Api;
class DemoApi
{
public function test()
{
return [
'status' => 1
];
}
}
```
2019-11-14 01:14:29 +08:00
```php
<?php
namespace HyperfTest\Cases;
use App\Api\DemoApi;
use App\Logic\DemoLogic;
use Hyperf\Di\Container;
use Hyperf\Context\ApplicationContext;
use HyperfTest\HttpTestCase;
use Mockery;
class DemoLogicTest extends HttpTestCase
{
2021-07-27 11:01:21 +08:00
/**
* @after
*/
public function tearDownAfterMethod()
{
Mockery::close();
}
public function testIndex()
{
$this->getContainer();
$res = $this->getContainer()->get(DemoLogic::class)->test();
$this->assertEquals(11, $res['status']);
}
/**
* @return Container
*/
protected function getContainer()
{
$container = ApplicationContext::getContainer();
$apiStub = $this->createMock(DemoApi::class);
$apiStub->method('test')->willReturn([
'status' => 11
]);
$container->define(DemoApi::class, function () use ($apiStub) {
return $apiStub;
});
return $container;
}
}
```
2021-03-22 09:48:28 +08:00
# 單元測試覆蓋率
## 使用 phpdbg 生成單元測試覆蓋率
修改 `phpunit.xml` 文件內容為如下
```xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./test/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
2022-03-07 09:34:38 +08:00
<php>
<!-- other PHP.ini or environment variables -->
<ini name="memory_limit" value="-1" />
</php>
2021-03-22 09:48:28 +08:00
<testsuites>
<testsuite name="Tests">
2022-03-07 09:34:38 +08:00
// 需要執行單測的測試案例目錄
2021-03-22 09:48:28 +08:00
<directory suffix="Test.php">./test</directory>
</testsuite>
</testsuites>
2022-03-07 09:34:38 +08:00
<coverage includeUncoveredFiles="true"
processUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false">
<include>
// 需要統計單元測試覆蓋率的文件
2021-03-22 09:48:28 +08:00
<directory suffix=".php">./app</directory>
2022-03-07 09:34:38 +08:00
</include>
<exclude>
// 生產單元測試覆蓋率時,需要忽略的文件
<directory suffix=".php">./app/excludeFile</directory>
</exclude>
<report>
<html outputDirectory="test/cover/" lowUpperBound="50" highLowerBound="90"/>
</report>
</coverage>
2021-03-22 09:48:28 +08:00
<logging>
2022-03-07 09:34:38 +08:00
<junit outputFile="test/junit.xml"/>
2021-03-22 09:48:28 +08:00
</logging>
2022-03-07 09:34:38 +08:00
</phpunit>
2021-03-22 09:48:28 +08:00
```
執行以下命令
```shell
phpdbg -dmemory_limit=1024M -qrr ./vendor/bin/co-phpunit -c phpunit.xml --colors=always
```