gertjuhh / symfony-openapi-validator
OpenAPI validator for Symfony application tests
Installs: 69 903
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- league/openapi-psr7-validator: ^0.22
- nyholm/psr7: ^1.5
- symfony/browser-kit: ^5.4 || ^6.0 || ^7.0
- symfony/framework-bundle: ^5.4 || ^6.0 || ^7.0
- symfony/psr-http-message-bridge: ^2.1 || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
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.
Installation
composer require --dev gertjuhh/symfony-openapi-validator
Usage
- Add the
OpenApiValidator
trait 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
operationAddress
can be passed as a third argument for this function but by default it will retrieve the operation from theclient
.
- The
Setting up a cache
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.
Example
<?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); } }