osteel / openapi-httpfoundation-testing by osteel

Validate HttpFoundation requests and responses against OpenAPI (3.0.x) definitions
712,500
80
2
Package Data
Maintainer Username: osteel
Maintainer Contact: yannick@yellowraincoat.co.uk (Yannick Chenot)
Package Create Date: 2020-10-23
Package Last Update: 2024-01-12
Home Page: https://tech.osteel.me/posts/openapi-backed-api-testing-in-php-projects-a-laravel-example
Language: PHP
License: MIT
Last Refreshed: 2024-03-28 03:15:01
Package Statistics
Total Downloads: 712,500
Monthly Downloads: 38,316
Daily Downloads: 1,659
Total Stars: 80
Total Watchers: 2
Total Forks: 13
Total Open Issues: 0

OpenAPI HttpFoundation Testing

Build Status Latest Stable Version License

Validate HttpFoundation requests and responses against OpenAPI (3.0.x) definitions.

See this post for more details and this repository for an example use in a Laravel project.

💡 While you can safely use this package for your projects, as long as version 1.0 has not been released "minor" version patches can contain breaking changes. Make sure to check the release section before you upgrade.

Why?

OpenAPI is a specification intended to describe RESTful APIs in a way that is understood by humans and machines alike.

By validating an API's requests and responses against the OpenAPI definition that describes it, we guarantee that the API is used correctly and behaves in accordance with the documentation we provide, thus making the OpenAPI definition the single source of truth.

The HttpFoundation component is developed and maintained as part of the Symfony framework. It is used to handle HTTP requests and responses in projects such as Symfony, Laravel, Drupal, and many others.

How does it work?

This package is built upon the OpenAPI PSR-7 Message Validator one, which validates PSR-7 messages against OpenAPI definitions.

It converts HttpFoundation request and response objects to PSR-7 messages using Symfony's PSR-7 Bridge and Tobias Nyholm's PSR-7 implementation, before passing them on to OpenAPI PSR-7 Message Validator.

Install

Via Composer:

$ composer require --dev osteel/openapi-httpfoundation-testing

💡 This package is mostly intended to be used as part of an API test suite.

Usage

Import the builder class:

use Osteel\OpenApi\Testing\ValidatorBuilder;

Use the builder to create a \Osteel\OpenApi\Testing\Validator object, feeding it a YAML or JSON OpenAPI definition:

$validator = ValidatorBuilder::fromYaml('my-definition.yaml')->getValidator();

// or

$validator = ValidatorBuilder::fromJson('my-definition.json')->getValidator();

💡 Instead of a file, you can also pass a YAML or JSON string directly.

You can now validate \Symfony\Component\HttpFoundation\Request and \Symfony\Component\HttpFoundation\Response objects for a given path and method:

$validator->validate($response, '/users', 'post');

💡 For convenience, objects implementing \Psr\Http\Message\ServerRequestInterface or \Psr\Http\Message\ResponseInterface are also accepted.

In the example above, we check that the response matches the OpenAPI definition for a POST request on the /users path.

Each of OpenAPI's supported HTTP methods (DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT and TRACE) also has a shortcut method that calls validate under the hood, meaning the line above could also be written this way:

$validator->post($response, '/users');

Validating a request object works exactly the same way:

$validator->post($request, '/users');

In the example above, we check that the request matches the OpenAPI definition for a POST request on the /users path.

The validate method returns true in case of success, and throws \Osteel\OpenApi\Testing\Exceptions\ValidationException exceptions in case of error.

Change log

Please see the Releases section for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Credits

People

Special thanks to Pavel Batanov for his advice on structuring the package.

Packages

License

The MIT License (MIT). Please see License File for more information.