hyperf 十二、自动化测试
文档教程:Hyperf
用co-phpunit提供测试,在composer中测试。
"scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}
测试中使用Hyperf\Testing\Client模拟请求,该类调用
Hyperf\HttpServer\Server中的request()方法获取请求数据。
#HyperfTest\HttpTestCase
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;abstract class HttpTestCase extends TestCase
{/*** @var Client*/protected $client;public function __construct($name = null, array $data = [], $dataName = ''){parent::__construct($name, $data, $dataName);$this->client = make(Client::class);}
public function __call($name, $arguments){return $this->client->{$name}(...$arguments);}
}#HyperfTest\Cases\ExampleTest
class ExampleTest extends HttpTestCase
{
}#Hyperf\Testing\Client
use Hyperf\HttpServer\Server;
class Client extends Server
{protected $baseUri = 'http://127.0.0.1/';public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http'){parent::__construct($container, $container->get(HttpDispatcher::class), $container->get(ExceptionHandlerDispatcher::class), $container->get(ResponseEmitter::class));$this->packer = $packer ?? new JsonPacker();$this->initCoreMiddleware($server);$this->initBaseUri($server);}public function get($uri, $data = [], $headers = []){$response = $this->request('GET', $uri, ['headers' => $headers,'query' => $data,]);return $this->packer->unpack((string) $response->getBody());}
}
相关方法:
Hyperf\Testing\Client::get($uri, $data = [], $headers = [])
Hyperf\Testing\Client::post($uri, $data = [], $headers = [])
Hyperf\Testing\Client::put($uri, $data = [], $headers = [])
Hyperf\Testing\Client::delete($uri, $data = [], $headers = [])
Hyperf\Testing\Client::json($uri, $data = [], $headers = [])
Hyperf\Testing\Client::file($uri, $data = [], $headers = [])
Hyperf\Testing\Client::request(string $method, string $path, array $options = [])
Hyperf\Testing\Client::initRequest(string $method, string $path, array $options = [])
Hyperf\Testing\Client::sendRequest(ServerRequestInterface $psr7Request)
具体断言函数参照phpunit。
一、编写测试
namespace HyperfTest\Cases;use HyperfTest\HttpTestCase;
/*** @internal* @coversNothing*/
class ExampleTest extends HttpTestCase
{public function testExample(){$this->assertTrue(true);//var_dump($this->get('/'));$this->assertTrue(is_array($this->get('/')));}
}
composer test -- --filter=test
因为co-phpunit脱胎于phpunit,所以filter其实也是phpunit选项。
二、测试替身
测试替身文章原文:PHPUnit 手册 – 第 9 章 测试替身
测试替身用mockery实现。
mockery:
composer地址:mockery/mockery - Packagist
文档地址:Mockery — Mockery Docs 1.0-alpha documentation
github地址:https://github.com/mockery/mockery
创建的类名必须设置名称以Test结尾,否则不会调用。
#./phpunit.xml 限制后缀内容
<testsuites><testsuite name="Tests"><directory suffix="Test.php">./test</directory></testsuite>
</testsuites># composer 命令配置
"scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}
declare (strict_types = 1);
namespace HyperfTest\Cases;use App\Api\TestApi;
use App\Service\TestingService;
use HyperfTest\HttpTestCase;
use Hyperf\Di\Container;
use Mockery;/*** @internal* @coversNothing*/
class Example2Test extends HttpTestCase
{protected function tearDown(): void{Mockery::close();}public function test2(){$container = $this->getContainer();$res = $container->get(TestingService::class)->test();$this->assertEquals(1, $res['status']);}/*** @return Container*/protected function getContainer(){$container = Mockery::mock(Container::class);$apiStub = $this->createMock(TestApi::class);$apiStub->method('test')->willReturn(['status' => 1,]);//旧版//$container->shouldReceive('get')->with(TestingService::class)->andReturn(new TestingService($apiStub));//新版$container->allows()->get(TestingService::class)->andReturn(new TestingService($apiStub));return $container;}
}