PHPackages                             cschindl/php-openapi-mock-middleware - 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. [API Development](/categories/api)
4. /
5. cschindl/php-openapi-mock-middleware

ActiveLibrary[API Development](/categories/api)

cschindl/php-openapi-mock-middleware
====================================

PSR-15 Middleware that simulates the API responses using an OpenAPI schema.

0.1.0(3y ago)19.0k↓34.6%MITPHPPHP ^8.0 || ^8.1 || ^8.2

Since Feb 5Pushed 3y ago1 watchersCompare

[ Source](https://github.com/cschindl/php-openapi-mock-middleware)[ Packagist](https://packagist.org/packages/cschindl/php-openapi-mock-middleware)[ GitHub Sponsors](https://github.com/cschindl)[ RSS](/packages/cschindl-php-openapi-mock-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (16)Versions (2)Used By (0)

php-openapi-mock-middleware
===========================

[](#php-openapi-mock-middleware)

[![Tests](https://github.com/cschindl/php-openapi-mock-middleware/workflows/Tests/badge.svg)](https://github.com/cschindl/php-openapi-mock-middleware/workflows/Tests/badge.svg)[![PHPStan](https://camo.githubusercontent.com/d300bb2a4c15228ad2fa238fe49f1aa67ea3fb95332a246ab4303af0d3a0178e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c2532304d61782d627269676874677265656e2e7376673f7374796c653d666c6174266c6f676f3d706870)](https://phpstan.org)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

PSR-15 Middleware that simulates the API responses using an OpenAPI schema.

Define requests/responses using the [OpenAPI schema](https://www.openapis.org) and this data is immediately available, so development/testing against this API can begin even though the functionality has not yet been implemented.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- [PSR-17](https://github.com/php-fig/http-factory) HTTP factories implementation
- [PSR-15](https://github.com/php-fig/http-server-middleware) HTTP server middleware dispatcher
- [PSR-6](https://github.com/php-fig/cache) Caching interface implementation (optional)

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

[](#installation)

You can install the package via composer:

```
composer require cschindl/php-openapi-mock-middleware
```

Example usage
-------------

[](#example-usage)

To see how to use and extend `OpenApiMockMiddleware`, have a look at our [example project](https://github.com/cschindl/php-openapi-mock-server).

Usage
-----

[](#usage)

First you need to create an instance of `OpenApiMockMiddleware` with your schema that you want to fake data from. You can use `createFromYamlFile`, `createFromJsonFile`, `createFromYaml` or `createFromJson` to create an instance of `OpenApiMockMiddleware`.

```
use Cschindl\OpenApiMockMiddleware\OpenApiMockMiddlewareConfig;
use Cschindl\OpenApiMockMiddleware\OpenApiMockMiddlewareFactory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/** @var ContainerInterface $container */
$container = require _DIR__ . '/config/container.php';

/** @var ResponseFactoryInterface $responseFactory */
$responseFactory = $container->get(ResponseFactoryInterface::class);

/** @var StreamFactoryInterface $responseFactory */
$streamFactory = $container->get(StreamFactoryInterface::class);

/** @var CacheItemPoolInterface|null $cache */
$cache = $container->get(CacheItemPoolInterface::class);

$pathToOpenApiFile = _DIR__ . '/data/openapi.yaml';
$config = new OpenApiMockMiddlewareConfig();

$openApiMockMiddleware = OpenApiMockMiddlewareFactory::createFromYamlFile(
    $pathToOpenApiFile,
    $config,
    $responseFactory,
    $streamFactory,
    $cache
);
```

After that, register the middleware.

```
use Cschindl\OpenApiMockMiddleware\OpenApiMockMiddleware;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

$app = new MiddlewareRunner();
$app->add($openApiMockMiddleware);

// To enable the middleware, add this header to your requests
// If this header is not present in the request, the middleware will skip to the next handler
$prepareOpenApiMiddleware = function (
    ServerRequestInterface $request,
    RequestHandlerInterface $handler
) {
    return $handler->handle(
        $request->withAddedHeader(
            OpenApiMockMiddleware::HEADER_OPENAPI_MOCK_ACTIVE,
            'true'
        )
    );
);
// Make sure that this middleware is called before $openApiMockMiddleware
$app->add($prepareOpenApiMiddleware);

$app->run($request, $response);
```

Options
-------

[](#options)

There are some options you can use to modify some behaviour.

```
$settings = [
    'validateRequest' => true,
    'validateResponse' => true,
    'faker' => [
        'minItems' => 1,
        'maxItems' => 10,
        'alwaysFakeOptionals' => false,
        'strategy' => Options::STRATEGY_STATIC,
    ],
];

// @see https://github.com/canvural/php-openapi-faker#options
$fakerOptions = (new Options())
    ->setMinItems($settings['faker']['minItems'])
    ->setMaxItems($settings['faker']['maxItems'])
    ->setAlwaysFakeOptionals($settings['faker']['alwaysFakeOptionals'])
    ->setStrategy($settings['faker']['strategy']);

$config = new OpenApiMockMiddlewareConfig(
    $settings['validateRequest'],
    $settings['validateResponse'],
    $fakerOptions
);
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

Credits
-------

[](#credits)

People:

- [Carsten Schindler](https://github.com/cschindl)
- [All Contributors](../../contributors)

Resources:

- [canvural/php-openapi-faker](https://github.com/canvural/php-openapi-faker)
- [cebe/php-openapi](https://github.com/cebe/php-openapi)
- [league/openapi-psr7-validator](https://github.com/thephpleague/openapi-psr7-validator)

License
-------

[](#license)

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

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

1198d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/98ffeaa066043b3759063e5b9adb51e991de59b48726a59320634a53c214776a?d=identicon)[cschindl](/maintainers/cschindl)

---

Top Contributors

[![cschindl](https://avatars.githubusercontent.com/u/28109094?v=4)](https://github.com/cschindl "cschindl (38 commits)")

---

Tags

middlewareopenapiphppsr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/cschindl-php-openapi-mock-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/cschindl-php-openapi-mock-middleware/health.svg)](https://phpackages.com/packages/cschindl-php-openapi-mock-middleware)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[saloonphp/saloon

Build beautiful API integrations and SDKs with Saloon

2.4k9.6M468](/packages/saloonphp-saloon)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[swisnl/json-api-client

A PHP package for mapping remote JSON:API resources to Eloquent like models and collections.

211473.2k12](/packages/swisnl-json-api-client)

PHPackages © 2026

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