| sidebar_key | swagger-test | |||
|---|---|---|---|---|
| tags |
|
A set of tools for testing your REST calls based on the OpenApi specification using PHPUnit.
Currently, this library supports the OpenApi specifications 2.0 (formerly swagger), 3.0.x, and 3.1.x.
- Swagger 2.0 - Full support
- OpenAPI 3.0.x - Full support (3.0.0, 3.0.1, 3.0.2, 3.0.3)
- OpenAPI 3.1.x - Full support with JSON Schema 2020-12 features ✨
OpenAPI 3.1 brings full JSON Schema 2020-12 compatibility. Key features supported:
- Nullable Types: Use JSON Schema union types
["string", "null"]instead of the deprecatednullablekeyword - Webhooks: Describe and validate incoming HTTP requests your API will receive
constKeyword: Validate constant values- Conditional Schemas: Use
if/then/elsefor conditional validation - Tuple Validation: Validate arrays with specific types at specific positions using
prefixItems $refwith Sibling Keywords: References can have additional properties alongside them
For detailed documentation on OpenAPI 3.1 features, see OpenAPI 3.1 Features Guide.
Some features of the OpenAPI specification are not fully implemented:
- Callbacks (OpenAPI 3.0/3.1)
- Links (OpenAPI 3.0/3.1)
- References to external documents/objects
- Some advanced JSON Schema 2020-12 keywords (e.g.,
unevaluatedProperties,dependentSchemas)
For details on the schema classes and their specific features, see Schema Classes.
PHP Swagger Test can help you to test your REST API. You can use this tool both for Unit Tests or Functional Tests.
This tool reads an OpenAPI/Swagger specification in JSON format (not YAML) and enables you to test the request and response. You can use the tool "https://github.com/zircote/swagger-php" for creating the JSON file when you are developing your REST API.
The ApiTestCase's assertion process is based on throwing exceptions if some validation or test failed.
- Functional test cases - Testing your API with HTTP requests
- Contract test cases - Testing without HTTP using custom requesters
- Runtime parameters validator - Validating requests in production
- Mocking Requests - Testing with mocked responses
- Schema classes - Working with OpenAPI 2.0, 3.0, and 3.1 schemas
- OpenAPI 3.1 features - Webhooks, const, if/then/else, tuple validation, and more
- Using the OpenApiValidation trait - Flexible validation without extending ApiTestCase
- Advanced usage - File uploads, custom clients, authentication, and more
- Exception handling - Understanding and handling validation exceptions
- Migration guide - Upgrading from older versions
- Troubleshooting - Common issues and solutions
- Version comparison - Feature support matrix across OpenAPI versions
- ByJG Gluo — PHP REST API starter
- ByJG Gluo for Laravel — contract testing and runtime validation inside a Laravel app
- Laravel Swagger Test
composer require "byjg/swagger-test"SPEC=swagger php -S 127.0.0.1:8080 tests/rest/app.php &
SPEC=openapi php -S 127.0.0.1:8081 tests/rest/app.php &
vendor/bin/phpunitThis project uses the byjg/webrequest component. It implements the PSR-7 specification, and a HttpClient / MockClient to do the requests. Check it out to get more information.
Please raise your issue on Github issue.
flowchart TD
byjg/swagger-test --> byjg/webrequest