Download the PHP package byjg/swagger-test without Composer
On this page you can find all versions of the php package byjg/swagger-test. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byjg/swagger-test
More information about byjg/swagger-test
Files in byjg/swagger-test
Package swagger-test
Short Description 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) and 3.0.
License MIT
Informations about the package swagger-test
sidebar_key: swagger-test sidebar_category: http-apis tags: [php, http, testing]
Swagger Test
Test your REST API against its OpenAPI specification with PHPUnit: every request and response is checked against the contract.
Currently, this library supports the OpenApi specifications 2.0 (formerly swagger), 3.0.x, and 3.1.x.
Why
An API test usually checks a status code and a few fields. The rest of the response, and the request the client sent, can drift from the documentation without any test failing, and the clients that rely on the documentation are the ones who find out.
Swagger Test checks every request and response against the OpenAPI specification (2.0, 3.0 and 3.1). A test only says what to call and what to expect:
- Without a web server. Contract tests send the PSR-7 request straight to your
application in the same process (
AbstractRequester), for RestServer, Laravel, Symfony or Slim. - In production too. The same validation can check live requests at runtime, from the same specification.
OpenAPI Version Support
- 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 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.
Limitations
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.
Documentation
- 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
Who is using this library?
- ByJG Gluo — PHP REST API starter
- ByJG Gluo for Laravel — contract testing and runtime validation inside a Laravel app
- Laravel Swagger Test
References
This 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.
Questions?
Please raise your issue on Github issue.