trinet / mezzio-test
Testing helpers for mezzio projects
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
provides classes and tools to help testing mezzio applications.
Its API aims to be similar to laminas-test
to ease migration
from Laminas MVC to Mezzio.
The package is not bound to any testing framework as it does not do any assertions. Instead it just bootstraps
the Container
and Application
based on your config file. Config file locations default to the
mezzio-skeleton, but can be reconfigured.
Also, a TestConfigProvider
is provided for loading custom testing configuration
(custom database, custom container configuration, ...).
Usage
Instantiate the \Trinet\MezzioTest\MezzioTestEnvironment
class in your test setup:
protected function setUp(): void { parent::setUp(); $this->mezzioApp = new MezzioTestEnvironment(); }
This will build your application container, bootstrap the mezzio Application (pipeline, routes) and
registers a custom \Laminas\Stratigility\Middleware\ErrorHandler
listener, which will just re-throw
any exception. Thus, the native exception assertions can be used (eg. $this->expectException()
in PHPUnit).
Currently, the Test environment offers three possibilities to dispatch a request:
dispatch(UriInterface|string $uri, ?string $method = null, array $params = [], array $headers = []): ResponseInterface
: dispatch any URI with the given method (defaults toGET
).$params
will be used as query parameters forGET
and as parsed body forPOST
requests.dispatchRoute(string $routeName, array $routeParams = [], string $method = null, array $requestParams = [], array $headers = []): ResponseInterface
: dispatch a given named routedispatchRequest(ServerRequestInterface $request): ResponseInterface
: dispatch aServerRequestInterface
If your base directory is not at the default location, a constructor parameter can be given to MezzioTestEnvironment
.
The container and router can also be retrieved with MezzioTestEnvironment->container()
and ->router()
, respectively.
Configuration
The \Trinet\MezzioTest\TestConfigProvider
can be used to load additional config files used for testing.
It will look for *testing.php
, *testing.local.php
, testing/*testing.php
and testing/*testing.local.php
in the config directory, which defaults to config/autoload/
in your project root, but can be configured
to anything else.
To use it, call the loader when in testing mode in your config/config.php
file to get an array of providers:
$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, []);
or to use another config path:
$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, []);