ensi / laravel-openapi-testing
laravel openapi testing
Installs: 44 727
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 5
Open Issues: 0
Requires
- php: ^8.1
- ensi/openapi-httpfoundation-testing: ^0.0.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.1 || ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11
- spaze/phpstan-disallowed-calls: ^2.15
README
This packages is based on ensi/openapi-httpfoundation-testing
and provides ValidatesAgainstOpenApiSpec
trait
Installation
You can install the package via composer:
composer require ensi/laravel-openapi-testing --dev
Version Compatibility
Basic usage
Let's add validation according to oas3 to our tests.
All we need is to use ValidatesAgainstOpenApiSpec;
and implement getOpenApiDocumentPath(): string
method like that:
class SomeTestCase extends AnotherTestCase { use ValidatesAgainstOpenApiSpec; protected function getOpenApiDocumentPath(): string { return public_path('api-docs/v1/index.yaml'); } }
The trait overrides $this->call
method to add the needed validation
As a result all http related helper methods ($this->get()
, $this->postJson()
and e.t.c) perform the validation too.
Both request and response is validated to match some part of the given spec. If validation fails your tests is automatically marked as failed, no need to need any manual assertions.
Turning validation off
In some cases you may want to turn validation for a specific request. Here is an example how to do it:
// Turn off validation for both request $this->skipNextOpenApiRequestValidation()->getJson(...); // Turn off validation for both response $this->skipNextOpenApiResponseValidation()->getJson(...); // Turn off validation for both request and response $this->skipNextOpenApiValidation()->getJson(...);
Mapping paths
In order to validate request against oas3 the package need to map it to one of the paths described in specification document.
We use path from Laravel's route ($request->route()->uri
) for that purpose.
If it does not fully match in your case you can explicitly set OpenApi path for the current request like that:
$this->forceOpenApiPath('/pets/{petId}')->getJson(...);
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.