trinet / mezzio-test
mezzio项目的测试辅助工具
Requires
- php: ^7.4 || ^8.0
- fig/http-message-util: ^1.1
- laminas/laminas-config-aggregator: ^1.2
- laminas/laminas-diactoros: ^2.2
- laminas/laminas-stratigility: ^3.2
- mezzio/mezzio: ^3.2
- mezzio/mezzio-router: ^3.1
- psr/container: ^1.0 || ^2.0
- psr/http-message: ^1.0
- thecodingmachine/safe: ^1.0 || ^2.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.0
- eventjet/coding-standard: ^3.1
- infection/infection: ^0.26.0
- laminas/laminas-servicemanager: ^3.4
- maglnet/composer-require-checker: ^3.3 || ^4.0
- mezzio/mezzio-fastroute: ^3.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^1.4
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.16.1
- thecodingmachine/phpstan-safe-rule: ^1.0
- vimeo/psalm: ^4.0
README
mezzio-test
mezzio-test
提供类和工具以帮助测试 mezzio 应用程序。其API旨在与 laminas-test
相似,以简化从Laminas MVC到Mezzio的迁移。
此包不绑定任何测试框架,因为它不执行任何断言。相反,它仅根据您的配置文件启动Container
和Application
。配置文件位置默认为mezzio-skeleton,但可以进行重新配置。
此外,还提供了一个TestConfigProvider
,用于加载自定义测试配置(自定义数据库、自定义容器配置等)。
用法
在您的测试设置中实例化\Trinet\MezzioTest\MezzioTestEnvironment
类
protected function setUp(): void { parent::setUp(); $this->mezzioApp = new MezzioTestEnvironment(); }
这将构建您的应用程序容器,启动mezzio应用程序(管道、路由)并注册一个自定义的\Laminas\Stratigility\Middleware\ErrorHandler
监听器,该监听器将仅重新抛出任何异常。因此,可以使用原生异常断言(例如,PHPUnit中的$this->expectException()
)。
目前,测试环境提供了三种发送请求的可能性
dispatch(UriInterface|string $uri, ?string $method = null, array $params = [], array $headers = []): ResponseInterface
:发送具有给定方法的任何URI(默认为GET
)。$params
将用于GET
查询参数,并作为POST
请求的解析正文。dispatchRoute(string $routeName, array $routeParams = [], string $method = null, array $requestParams = [], array $headers = []): ResponseInterface
:发送给定命名的路由dispatchRequest(ServerRequestInterface $request): ResponseInterface
:发送ServerRequestInterface
如果您的基本目录不在默认位置,可以将构造函数参数提供给MezzioTestEnvironment
。
容器和路由器也可以分别通过MezzioTestEnvironment->container()
和->router()
检索。
配置
可以使用\Trinet\MezzioTest\TestConfigProvider
加载用于测试的附加配置文件。它将在配置目录中查找*testing.php
、*testing.local.php
、testing/*testing.php
和testing/*testing.local.php
,默认为项目根目录中的config/autoload/
,但可以进行配置为其他任何位置。
要使用它,请在config/config.php
文件中在测试模式下调用加载器,以获取提供者数组
$providers = [ \A\ConfigProvider::class, \B\ConfigProvider::class, // ... ]; if (getenv('APP_TESTING') !== false) { $providers = array_merge($providers, \Trinet\MezzioTest\TestConfigProvider::load()); } $aggregator = new ConfigAggregator($providers, null, []);
或使用其他配置路径
$providers = [ \A\ConfigProvider::class, \B\ConfigProvider::class, // ... ]; if (getenv('APP_TESTING') !== false) { $providers = array_merge($providers, \Trinet\MezzioTest\TestConfigProvider::load('custom/path')); } $aggregator = new ConfigAggregator($providers, null, []);