cschindl / php-openapi-mock-middleware
PSR-15 Middleware that simulates the API responses using an OpenAPI schema.
Fund package maintenance!
cschindl
Installs: 7 705
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/cschindl/php-openapi-mock-middleware
Requires
- php: ^8.0 || ^8.1 || ^8.2
- canvural/php-openapi-faker: ^2.1
- psr/cache: ^1.0 || ^2.0 || ^3.0
- psr/http-factory: ^1.0.1
- psr/http-message: ^1.0.1
- psr/http-server-middleware: ^1.0.1
Requires (Dev)
- doctrine/coding-standard: ^9.0.2
- ergebnis/composer-normalize: ^2.29
- jangregor/phpstan-prophecy: ^1.0
- nyholm/psr7: ^1.5.1
- phpspec/prophecy: ^1.16
- phpspec/prophecy-phpunit: ^2.0.1
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.9.14
- phpstan/phpstan-phpunit: ^1.3.3
- phpunit/phpunit: ^9.5.28
- squizlabs/php_codesniffer: ^3.7.1
This package is auto-updated.
Last update: 2025-10-07 15:50:45 UTC
README
PSR-15 Middleware that simulates the API responses using an OpenAPI schema.
Define requests/responses using the OpenAPI schema and this data is immediately available, so development/testing against this API can begin even though the functionality has not yet been implemented.
Requirements
- PHP >= 8.0
- PSR-17 HTTP factories implementation
- PSR-15 HTTP server middleware dispatcher
- PSR-6 Caching interface implementation (optional)
Installation
You can install the package via composer:
composer require cschindl/php-openapi-mock-middleware
Example usage
To see how to use and extend OpenApiMockMiddleware, have a look at our example project.
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
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
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
People:
Resources:
License
The MIT License (MIT). Please see License File for more information.