cschindl/php-openapi-mock-middleware

PSR-15 中间件,使用 OpenAPI 架构模拟 API 响应。

0.1.0 2023-02-05 18:36 UTC

This package is auto-updated.

Last update: 2024-09-07 13:17:20 UTC


README

Tests PHPStan License: MIT

PSR-15 中间件,使用 OpenAPI 架构模拟 API 响应。

使用 OpenAPI 架构定义请求/响应,这些数据立即可用,即使功能尚未实现,也可以开始对该 API 进行开发和测试。

要求

  • PHP >= 8.0
  • PSR-17 HTTP 工厂实现
  • PSR-15 HTTP 服务器中间件调度器
  • PSR-6 缓存接口实现(可选)

安装

您可以通过 composer 安装此包。

composer require cschindl/php-openapi-mock-middleware

示例用法

要了解如何使用和扩展 OpenApiMockMiddleware,请查看我们的 示例项目

用法

首先,您需要创建一个 OpenApiMockMiddleware 实例,该实例从您要模拟数据的架构中创建。您可以使用 createFromYamlFilecreateFromJsonFilecreateFromYamlcreateFromJson 来创建 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
);

然后,注册中间件。

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);

选项

您可以使用一些选项来修改某些行为。

$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
);

更新日志

请参阅 更新日志 了解最近更改的详细信息。

贡献

请参阅 贡献指南 了解详细信息。

致谢

人员

资源

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件