webproject-xyz / php-openapi-mock-server
A lightweight PHP OpenAPI mock server using Mezzio and PSR-15 middleware.
Package info
github.com/WebProject-xyz/php-openapi-mock-server
pkg:composer/webproject-xyz/php-openapi-mock-server
Requires
- php: ~8.3.0 || ~8.4.0 || ~8.5.0
- ext-random: *
- devizzent/cebe-php-openapi: ^1.1
- fakerphp/faker: ^1.24.1
- laminas/laminas-diactoros: ^3.8
- laminas/laminas-servicemanager: ^3.24
- league/openapi-psr7-validator: ^0.22
- mezzio/mezzio: ^3.27
- mezzio/mezzio-fastroute: ^3.14
- mezzio/mezzio-problem-details: ^1.19
- mezzio/mezzio-router: ^4.2
- psr/cache: ^3.0
- psr/container: ^1.1 || ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- symfony/cache: ^6.4 || ^7.4 || ^8.0
- webmozart/assert: ^1.12 || ^2.0
Requires (Dev)
- codeception/c3: ^2.9
- codeception/codeception: ^5.3.5
- codeception/module-asserts: ^3.3
- codeception/module-phpbrowser: ^4.0
- codeception/module-rest: ^3.4.3
- codeception/module-webdriver: ^4.0.5
- friendsofphp/php-cs-fixer: ^3.94.2
- maglnet/composer-require-checker: ^4.20
- phpbench/phpbench: ^1.5
- phpro/grumphp: ^2.19
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.1.40
- phpstan/phpstan-webmozart-assert: ^2.0.0
- rector/rector: ^2.3
- roave/security-advisories: dev-master
- slam/phpstan-laminas-framework: ^2.0
- webproject-xyz/codeception-module-ai-reporter: ^1.0.1
This package is auto-updated.
Last update: 2026-03-10 19:52:02 UTC
README
A high-performance, lightweight, zero-docker OpenAPI 3.x mock server built with Mezzio.
This project provides a standalone PHP application that serves dynamic mock data based on your OpenAPI specification. It is optimized for speed, low memory footprint, and seamless CI integration.
🚀 Features
- Blazing Fast: Sub-millisecond initialization and ~4ms request processing.
- Interactive Documentation: Integrated Swagger UI at the root (
/) for real-time spec exploration. - Stateless Service Architecture: Built with a high-performance
FakerRegistryfor efficient service management. - Type-Safe Enums: Robust implementation using PHP 8.3 Enums for
MockStrategy,HttpMethod,FakerType, andFakerContext. - PSR-15 Compliant: Built on the industrial-strength Mezzio middleware stack.
- Docker Ready: Production-ready FrankenPHP image with Hot-Reload support for development.
- Intelligent Faking: Automatically generates realistic mock data using spec
examples,defaults, and schema constraints (anyOf,oneOf,allOf). - OpenAPI 3.1 Support: Advanced support for numeric exclusive constraints and modern spec features.
- Smart Caching: Built-in PSR-6 caching and schema memoization to eliminate redundant parsing and resolution.
- Validation: Optional request and response validation to ensure your clients and mocks stay in sync.
- Problem Details: Native support for RFC 7807 error responses with detailed diagnostics.
📦 Installation
git clone https://github.com/WebProject-xyz/php-openapi-mock-server.git
cd php-openapi-mock-server
composer install
Requires PHP ^8.3 with opcache enabled for best performance.
🖥️ Usage
Start the Server
php -S localhost:8080 -t public
Docker
You can also run the mock server using Docker or Docker Compose (powered by FrankenPHP).
Using Docker Compose (with Hot-Reload)
The compose.yml is configured to mount your local code, allowing for real-time updates during development.
# Start with default spec (data/openapi.yaml) docker compose up -d # Start with a specific local spec OPENAPI_SPEC=data/my-spec.yaml docker compose up -d # Start with a remote URL OPENAPI_SPEC=https://example.com/openapi.yaml docker compose up -d
Using Docker
docker build -t openapi-mock-server .
docker run -d -p 8080:80 -e OPENAPI_SPEC=data/openapi.yaml openapi-mock-server
Advanced Configuration
You can control the server via environment variables or headers:
| Variable / Header | Description | Default |
|---|---|---|
OPENAPI_SPEC |
Path or URL to your .yaml or .json spec. |
data/openapi.yaml |
X-OpenApi-Mock-Active |
Toggle mock server activation (true/false). |
true |
X-OpenApi-Mock-StatusCode |
Force a specific response status code. | Automatic Fallback |
X-OpenApi-Mock-Example |
Force a specific named example from the spec. | default |
📊 Performance
We take performance seriously. Using PHPBench, we track the overhead of our middleware and faking logic.
Benchmark Results
Running on PHP 8.3 with OPcache & JIT enabled.
| Operation | Average Time | Memory Usage |
|---|---|---|
| Middleware Creation | ~21.0 μs | ~6.0 MB |
| Full Mock Request | ~4.20 ms | ~6.0 MB |
Note: Metrics include full Request Validation + Schema Resolution + Data Generation + Response Validation.
To run benchmarks locally:
composer bench
🛠️ Development & Testing
We maintain high code quality standards:
- Unit Tests: Full coverage of fakers, validators, and factories.
- Acceptance Tests: End-to-end verification using the built-in PHP server.
- Static Analysis: PHPStan Level 8 (Strict).
- Refactoring: Rector PHP 8.3 ruleset.
Key Commands
composer test # Run all tests (Acceptance + Unit) composer test:coverage # Generate coverage report (82%+) composer stan # Run static analysis composer rector # Run refactoring dry-run composer cs:fix # Standardize code style
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'feat: add some amazing feature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📜 License
Distributed under the MIT License. See LICENSE for more information.