k-kinzal/oas-fake-php
Composer 安装命令:
composer require --dev k-kinzal/oas-fake-php
包简介
Intercept HTTP requests, validate against OpenAPI schemas, and generate fake responses for testing
README 文档
README
OpenAPI schema-driven fake API server for PHP testing. Define your API contract once, and automatically get a working fake server that generates valid responses, validates requests, and catches schema violations in your tests.
Features
- Automatic fake response generation from OpenAPI schema definitions
- Request/response validation against the OpenAPI schema
- Custom response stubs and callbacks per operation
- Declarative server classes with handler methods mapped by
operationId - Three operating modes: FAKE, RECORD, REPLAY
- PSR-15 middleware support
Requirements
- PHP 8.0+
- ext-curl
Installation
composer require --dev k-kinzal/oas-fake-php
Usage
Basic
Define a server pointing to your OpenAPI schema and start intercepting:
use OasFake\OasFake; use OasFake\Server; final class PetStoreServer extends Server { protected static string $SCHEMA = __DIR__ . '/openapi.yaml'; } // Start intercepting HTTP requests $server = OasFake::start(PetStoreServer::class); // All HTTP requests now return responses generated from the schema $client = new GuzzleHttp\Client(['base_uri' => 'https://petstore.example.com']); $response = $client->get('/pets/1'); // Stop intercepting OasFake::stop();
Custom Responses
Override responses for specific operations using the configure callback:
use OasFake\OasFake; use OasFake\Handler; $server = OasFake::start(PetStoreServer::class, fn ($s) => $s ->withResponse('listPets', 200, [['id' => 1, 'name' => 'Buddy']]) ->withCallback('createPet', function ($request, $response) { $body = json_decode((string) $request->getBody(), true); return new \GuzzleHttp\Psr7\Response(201, [], json_encode([ 'id' => 42, 'name' => $body['name'], ])); }) ->withHandler('deletePet', Handler::status(204)) );
Declarative Server
Public methods on a Server subclass are automatically registered as handlers, matched by operationId:
use OasFake\Server; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; final class PetStoreServer extends Server { protected static string $SCHEMA = __DIR__ . '/openapi.yaml'; public function listPets(ServerRequestInterface $request, ?ResponseInterface $response): ResponseInterface { return new Response(200, ['Content-Type' => 'application/json'], json_encode([ ['id' => 1, 'name' => 'Buddy'], ])); } #[\OasFake\Route(method: 'DELETE', path: '/pets/{petId}')] public function removePet(ServerRequestInterface $request, ?ResponseInterface $response): ResponseInterface { return new Response(204); } }
Configuration
use OasFake\OasFake; use OasFake\Mode; $server = OasFake::start(PetStoreServer::class, fn ($s) => $s ->withMode(Mode::RECORD) // or ->withMode('record') ->withCassettePath('./fixtures/cassettes') ->withRequestValidation(false) ->withResponseValidation(false) ->withFakerOptions(['alwaysFakeOptionals' => true, 'minItems' => 2, 'maxItems' => 5]) ->withMiddleware(new MyMiddleware()) );
All settings can also be overridden via environment variables:
OAS_FAKE_MODE=record OAS_FAKE_VALIDATE_REQUESTS=false composer test
License
k-kinzal/oas-fake-php 适用场景与选型建议
k-kinzal/oas-fake-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「http」 「testing」 「mock」 「Guzzle」 「fake」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 k-kinzal/oas-fake-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 k-kinzal/oas-fake-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 k-kinzal/oas-fake-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
A fluent PHP CURL wrapper
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-20