trinet/mezzio-test

mezzio项目的测试辅助工具

This package is auto-updated.

Last update: 2024-08-30 01:17:44 UTC


README

Check Build

mezzio-test

mezzio-test 提供类和工具以帮助测试 mezzio 应用程序。其API旨在与 laminas-test 相似,以简化从Laminas MVC到Mezzio的迁移。

此包不绑定任何测试框架,因为它不执行任何断言。相反,它仅根据您的配置文件启动ContainerApplication。配置文件位置默认为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.phptesting/*testing.phptesting/*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, []);