webparking
An OpenAPI documentation validator using your existing Laravel tests
An OpenAPI documentation validator using your existing Laravel tests.
composer require --dev webparking/laravel-openapi-validator
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Webparking\OpenAPIValidator\ValidatesHttpMessagesAgainstDocs;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use ValidatesHttpMessagesAgainstDocs;
}
The Laravel versions listed below are all currently supported:
When creating and maintaining an HTTP REST API, you want to make sure it works well and is well documented. We consider it essential to be able to check both automatically.
We combined Laravel HTTP testing with ThePHPLeague's OpenAPI PSR7 validator by overriding MakesHttpRequests#call. Before executing the request, we have it validated against the OpenAPI documentation. And before returning the response, we have that validated too. Exceptions are thrown for any problems found.
This solution has enabled us to use both OpenAPI and HTTP tests to the fullest; no compromises needed.
Simply use the ValidatesHttpMessagesAgainstDocs
in your test class. We generally do this on the project level TestCase.
The default index file for the docs is docs/index.yaml
, but you're free to override that by setting the yamlPath
property.
You may want to exclude certain URI's from validation. Set the ignoredUris
property to do this.