This document provides foundational context and instructions for AI agents interacting with the php-openapi-mock-server codebase.
PHP OpenAPI Mock Server is a high-performance, lightweight, zero-Docker mock server built with the Mezzio framework. It serves mock data based on OpenAPI 3.x specifications (JSON or YAML) and is designed for rapid prototyping and CI environments.
- Type: PHP Web Application (PSR-15 Middleware Stack)
- Framework: Mezzio
- Core Libraries:
league/openapi-psr7-validator: For request and response validation against OpenAPI specs.devizzent/cebe-php-openapi: For parsing OpenAPI specifications.laminas/laminas-diactoros: PSR-7 implementation.webmozart/assert: For strict runtime type verification.
- Language: PHP 8.3+ (Strict Types enabled)
The application operates primarily through a series of PSR-15 middlewares and a stateless service registry:
ForceMockActiveMiddleware: Ensures the mock server is active by default by setting theX-OpenApi-Mock-Activeheader.OpenApiMockMiddleware: The core logic that intercepts requests, validates them, and returns faked responses. It handles all documentation routes (/,/openapi.yaml) by passing them to the standard routing stack.FakerRegistry: A central, stateless registry for all mock generation services.- Type-Safe Enums: Extensive use of PHP 8.3 Enums for
FakerType,FakerContext,HttpMethod, andRequestErrorTypeto eliminate magic strings and harden type safety. - Factories: Found in
src/Factory/and use PSR-17 interfaces (ResponseFactoryInterface,StreamFactoryInterface) for dependency injection.
Run the built-in PHP server:
php -S localhost:8080 -t publicOptimized FrankenPHP environment with Hot-Reload support:
docker compose up -dOPENAPI_SPEC: Path or URL to the OpenAPI specification file (Default:data/openapi.yaml).
composer test: Runs the full Codeception test suite (95 tests passing).composer test:coverage: Generates code coverage reports (~82% coverage).composer bench: Runs the PHPBench performance suite.composer stan: Runs PHPStan static analysis (Level 8 clean).composer rector:fix: Applies automated refactorings via Rector.composer cs:fix: Fixes coding standards via PHP-CS-Fixer.
- Standard: PSR-12 / PER standard via PHP-CS-Fixer.
- Strict Typing: All PHP files MUST start with
declare(strict_types=1);. - Modern PHP: Leverage PHP 8.3 features like Enums, readonly properties, and constructor property promotion.
- Stricteness: Maintain a zero-error baseline at PHPStan Level 8.
- Assertions: Use
Webmozart\Assert\Assertfor runtime type narrowing. This is foundational for the project's type safety.
- Framework: Codeception.
- Suites:
Acceptance,Unit,JsonAcceptance,RemoteAcceptance. - Base Class: Unit tests MUST extend
\Codeception\Test\Unit. - Reproducing Bugs: ALWAYS add a new regression test (unit or acceptance) before fixing a bug.
- PSR-6 Caching: Caches parsed OpenAPI specifications.
- Schema Memoization: Static caching in
SchemaFakeravoids redundant recursive resolution. - Metrics: Middleware creation is ~21μs, and full mock request processing averages ~4.2ms.
FakerRegistry: Manages stateless instances ofStringFaker,NumberFaker,ArrayFaker,ObjectFaker, etc.- OpenAPI 3.1 Support: Full support for numeric
exclusiveMinimumandexclusiveMaximum. - Composition: Robust handling of
anyOf,oneOf, andallOf. - Swagger UI: Automatically available at the root (
/) path.