PHPackages                             osteel/openapi-httpfoundation-testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. osteel/openapi-httpfoundation-testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

osteel/openapi-httpfoundation-testing
=====================================

Validate HttpFoundation requests and responses against OpenAPI (3+) definitions

v0.14(5mo ago)1201.9M—2.4%15[1 issues](https://github.com/osteel/openapi-httpfoundation-testing/issues)5MITPHPPHP ^8.0CI passing

Since Nov 2Pushed 5mo ago4 watchersCompare

[ Source](https://github.com/osteel/openapi-httpfoundation-testing)[ Packagist](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)[ Docs](https://github.com/osteel/openapi-httpfoundation-testing)[ RSS](/packages/osteel-openapi-httpfoundation-testing/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (21)Used By (5)

OpenAPI HttpFoundation Testing
==============================

[](#openapi-httpfoundation-testing)

[![Build Status](https://github.com/osteel/openapi-httpfoundation-testing/workflows/CI/badge.svg)](https://github.com/osteel/openapi-httpfoundation-testing/actions)[![Latest Stable Version](https://camo.githubusercontent.com/168b59e06a9715214d4f4351c81cdaea3b91049d078cb1b41fa0cdc96a32df33/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f737465656c2f6f70656e6170692d68747470666f756e646174696f6e2d74657374696e67)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)[![License](https://camo.githubusercontent.com/829cbfb78a83d18e2f92c0d558d3014db787ed9991edbc0ac26276261a714a99/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f737465656c2f6f70656e6170692d68747470666f756e646174696f6e2d74657374696e67)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)[![Downloads](https://camo.githubusercontent.com/07edc07fd74d310a0fa3fddacd67bfbc689daa3ee5fc5f4cf4f3b5c637c58c6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f737465656c2f6f70656e6170692d68747470666f756e646174696f6e2d74657374696e67)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)

Validate HttpFoundation requests and responses against OpenAPI (3+) definitions.

See [this post](https://tech.osteel.me/posts/openapi-backed-api-testing-in-php-projects-a-laravel-example "OpenAPI-backed API testing in PHP projects – a Laravel example") for more details and [this repository](https://github.com/osteel/openapi-httpfoundation-testing-laravel-example) for an example use in a Laravel project.

Important

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](../../releases) before you upgrade.

Why?
----

[](#why)

[OpenAPI](https://swagger.io/specification/) is a specification intended to describe RESTful APIs in a way that can be understood by both humans and machines.

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](https://symfony.com/doc/current/components/http_foundation.html) is developed and maintained as part of the [Symfony framework](https://symfony.com/). It is used to handle HTTP requests and responses in projects such as Symfony, Laravel, Drupal, and [many others](https://symfony.com/components/HttpFoundation).

How does it work?
-----------------

[](#how-does-it-work)

This package is built on top of [OpenAPI PSR-7 Message Validator](https://github.com/thephpleague/openapi-psr7-validator), which validates [PSR-7 messages](https://www.php-fig.org/psr/psr-7/) against OpenAPI definitions.

It converts HttpFoundation request and response objects to PSR-7 messages using Symfony's [PSR-7 Bridge](https://symfony.com/doc/current/components/psr7.html) and [Tobias Nyholm](https://github.com/Nyholm)'s [PSR-7 implementation](https://github.com/Nyholm/psr7), before passing them on to OpenAPI PSR-7 Message Validator.

Installation
------------

[](#installation)

Note

This package supports PHP 8.0 and above.

Via Composer:

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

Usage
-----

[](#usage)

Note

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

Import the builder class:

```
use Osteel\OpenApi\Testing\ValidatorBuilder;
```

Use the builder to create a [`\Osteel\OpenApi\Testing\Validator`](/src/Validator.php) object, using one of the available factory methods for YAML or JSON:

```
// From a local file:

$validator = ValidatorBuilder::fromYamlFile($yamlFile)->getValidator();
$validator = ValidatorBuilder::fromJsonFile($jsonFile)->getValidator();

// From a string:

$validator = ValidatorBuilder::fromYamlString($yamlString)->getValidator();
$validator = ValidatorBuilder::fromJsonString($jsonString)->getValidator();

// From a URL:

$validator = ValidatorBuilder::fromYamlUrl($yamlUrl)->getValidator();
$validator = ValidatorBuilder::fromJsonUrl($jsonUrl)->getValidator();

// Automatic detection (slower):

$validator = ValidatorBuilder::fromYaml($yamlFileOrStringOrUrl)->getValidator();
$validator = ValidatorBuilder::fromJson($jsonFileOrStringOrUrl)->getValidator();
```

Tip

You can also use a dependency injection container to bind the `ValidatorBuilder` class to the [`ValidatorBuilderInterface`](/src/ValidatorBuilderInterface.php) interface it implements and inject the interface instead, which would also be useful for testing and mocking.

You can now validate `\Symfony\Component\HttpFoundation\Request` and `\Symfony\Component\HttpFoundation\Response` objects for a given [path](https://swagger.io/specification/#paths-object) and method:

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

Tip

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](https://swagger.io/docs/specification/paths-and-operations/ "Paths and Operations") (`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 throw a [`\Osteel\OpenApi\Testing\Exceptions\ValidationException`](/src/Exceptions/ValidationException.php) exception in case of error.

Caching
-------

[](#caching)

This package supports caching to speed up the parsing of OpenAPI definitions. Simply pass your [PSR-6](https://www.php-fig.org/psr/psr-6/) or [PSR-16](https://www.php-fig.org/psr/psr-16/) cache object to the `setCache` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class.

Here is an example using Symfony's [Array Cache Adapter](https://symfony.com/doc/current/components/cache/adapters/array_cache_adapter.html "Array Cache Adapter"):

```
use Osteel\OpenApi\Testing\ValidatorBuilder;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

$cache = new ArrayAdapter();
$validator = ValidatorBuilder::fromYamlFile($yamlFile)->setCache($cache)->getValidator();
```

Extending the package
---------------------

[](#extending-the-package)

There are two main extension points – message adapters and cache adapters.

### Message adapters

[](#message-adapters)

The [`ValidatorBuilder`](/src/ValidatorBuilder.php) class uses the [`HttpFoundationAdapter`](/src/Adapters/HttpFoundationAdapter.php) class as its default HTTP message adapter. This class converts HttpFoundation request and response objects to their PSR-7 counterparts.

If you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the [`MessageAdapterInterface`](/src/Adapters/MessageAdapterInterface.php) interface and pass it to the `setMessageAdapter` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class:

```
$validator = ValidatorBuilder::fromYamlFile($yamlFile)
    ->setMessageAdapter($yourAdapter)
    ->getValidator();
```

### Cache adapters

[](#cache-adapters)

The [`ValidatorBuilder`](/src/ValidatorBuilder.php) class uses the [`Psr16Adapter`](/src/Cache/Psr16Adapter.php) class as its default cache adapter. This class converts PSR-16 cache objects to their PSR-6 counterparts.

If you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the [`CacheAdapterInterface`](/src/Cache/CacheAdapterInterface.php) interface and pass it to the `setCacheAdapter` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class:

```
$validator = ValidatorBuilder::fromYamlFile($yamlFile)
    ->setCacheAdapter($yourAdapter)
    ->getValidator();
```

### Other interfaces

[](#other-interfaces)

The [`ValidatorBuilder`](/src/ValidatorBuilder.php) and [`Validator`](/src/Validator.php) classes are `final` but they implement the [`ValidatorBuilderInterface`](/src/ValidatorBuilderInterface.php) and [`ValidatorInterface`](/src/ValidatorInterface.php) interfaces respectively for which you can provide your own implementations if you need to.

Change log
----------

[](#change-log)

Please see the [Releases section](../../releases) for details.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](/.github/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

**People**

- [Yannick Chenot](https://github.com/osteel)
- [Patrick Rodacker](https://github.com/lordrhodos)
- [Johnathan Michael Dell](https://github.com/johnathanmdell)
- [Paul Mitchum](https://github.com/paul-m)
- [All Contributors](../../contributors)

Special thanks to [Pavel Batanov](https://github.com/scaytrase) for his advice on structuring the package.

**Packages**

- [OpenAPI PSR-7 Message Validator](https://github.com/thephpleague/openapi-psr7-validator)
- [The PSR-7 Bridge](https://symfony.com/doc/current/components/psr7.html)
- [PSR-7 implementation](https://github.com/Nyholm/psr7)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance71

Regular maintenance activity

Popularity56

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Recently: every ~183 days

Total

20

Last Release

165d ago

PHP version history (4 changes)v0.1-alphaPHP ^7.2

v0.2PHP ^7.2|^8.0

v0.9PHP ^7.3|^8.0

v0.10PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c2b03220d22111bcaf906e6bf8ba07cdf70d78810fe86f476606e4d02fca199f?d=identicon)[osteel](/maintainers/osteel)

---

Top Contributors

[![osteel](https://avatars.githubusercontent.com/u/436467?v=4)](https://github.com/osteel "osteel (56 commits)")[![ttomdewit](https://avatars.githubusercontent.com/u/2845400?v=4)](https://github.com/ttomdewit "ttomdewit (3 commits)")[![Ilyes512](https://avatars.githubusercontent.com/u/2445415?v=4)](https://github.com/Ilyes512 "Ilyes512 (2 commits)")[![odolbeau](https://avatars.githubusercontent.com/u/680206?v=4)](https://github.com/odolbeau "odolbeau (1 commits)")[![paul-m](https://avatars.githubusercontent.com/u/360238?v=4)](https://github.com/paul-m "paul-m (1 commits)")

---

Tags

apihttphttpfoundationlaravelopenapiopenapi3phppsr-7symfonytestingvalidationhttptestingapisymfonylaravelvalidationopenapihttpfoundation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/osteel-openapi-httpfoundation-testing/health.svg)

```
[![Health](https://phpackages.com/badges/osteel-openapi-httpfoundation-testing/health.svg)](https://phpackages.com/packages/osteel-openapi-httpfoundation-testing)
```

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k48.1M236](/packages/api-platform-core)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[moonshine/moonshine

Laravel administration panel

1.3k217.1k59](/packages/moonshine-moonshine)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[kirschbaum-development/laravel-openapi-validator

Automatic OpenAPI validation for Laravel HTTP tests

581.1M5](/packages/kirschbaum-development-laravel-openapi-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
