This package can validate requests made in application tests, based of Symfony's WebTestCase, against OpenAPI
specifications. This is done by converting HttpFoundation objects using the
PSR-7 Bridge and passing them to the
OpenAPI PSR-7 Message Validator.
composer require --dev gertjuhh/symfony-openapi-validator
- Add the
OpenApiValidatortrait to yourWebTestCase - Create a client by calling
self::createClient()- Alternatively use your own custom logic creating an instance of
KernelBrowser
- Alternatively use your own custom logic creating an instance of
- Execute the request you wish to validate using the client
- Call
self::assertOpenApiSchema(<schema>, <client>);schema: path to corresponding OpenAPI yaml schemaclient: the client used to make the request
- Or optionally use the
self::assertResponseAgainstOpenApiSchema(<schema>, <client>);to only validate the response- The
operationAddresscan be passed as a third argument for this function but by default it will retrieve the operation from theclient.
- The
The underlying library can use a PSR-6 cache. This provides a significant speedup when running multiple tests against a single schema, since it can be parsed once and reused.
In order to activate this cache, you can pass a PSR-6 cache instance to the static property
\Gertjuhh\SymfonyOpenapiValidator\StaticOpenApiValidatorCache::$validatorCache. For example:
<?php
use Gertjuhh\SymfonyOpenapiValidator\StaticOpenApiValidatorCache;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
StaticOpenApiValidatorCache::$validatorCache = new ArrayAdapter(storeSerialized: false);Setting storeSerialized to false on the ArrayAdapter instance is recommended as it lowers memory usage by storing the actual objects;
otherwise, Symfony will store a serialized representation of the OpenAPI schema and deserialize it on every test run.
This snippet can be embedded in a bootstrap script for PHPUnit.
<?php
declare(strict_types=1);
namespace App\ApplicationTests;
use Gertjuhh\SymfonyOpenapiValidator\OpenApiValidator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class HelloWorldTest extends WebTestCase
{
use OpenApiValidator;
public function testHelloWorldReturnsSuccessfulResponse(): void
{
$client = self::createClient();
$client->xmlHttpRequest('GET', '/hello-world');
self::assertResponseIsSuccessful();
self::assertOpenApiSchema('public/openapi.yaml', $client);
}
}