radebatz/openapi-verifier
Composer 安装命令:
composer require radebatz/openapi-verifier
包简介
Verify JSON (api response) against OpenAPI specification.
README 文档
README
Introduction
Allows to validate a controller response from your API project against a given OpenAPI specification.
Requirements
Installation
You can use composer or simply download the release.
Composer
The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.
Once composer is installed, execute the following command in your project root to install this library:
composer require radebatz/openapi-verifier
After that all required classes should be availabe in your project to add routing support.
Usage
Manual verification
The VerifiesOpenApi trait can be used directly and customized in 3 ways in order to provide the reqired OpenApi specifications:
- Overriding the method
getOpenApiSpecificationLoader()as shown below - Populating the
$openapiSpecificationLoaderproperty. - Setting a property
$openapiSpecificationpointing to the specification file
<?php namespace Tests\Feature; use Radebatz\OpenApi\Verifier\VerifiesOpenApi; use Radebatz\OpenApi\Verifier\OpenApiSpecificationLoader; use PHPUnit\Framework\TestCase; class UsersTest extends TestCase { use VerifiesOpenApi; /** @inheritdoc */ protected function getOpenApiSpecificationLoader(): ?OpenApiSpecificationLoader { return new OpenApiSpecificationLoader(__DIR__ . '/specifications/users.yaml'); } /** @test */ public function index() { // PSR client $client = $this->client(); $response = $client->get('/users'); $this->assertEquals(200, $response->getStatusCode()); // will throw OpenApiSchemaMismatchException if verification fails $this->verifyOpenApiResponseBody('get', '/users', 200, (string) $response->getBody()); } }
Laravel adapter
The adapter will try to resolve the specification dynamically in this order:
- filename passed into
registerOpenApiVerifier() /tests/openapi.json/tests/openapi.yaml- Generate specification from scratch by scanning the
appfolder
The code expects to be in the context of a Laravel Test\TestCase.
<?php namespace Tests\Feature; use Radebatz\OpenApi\Verifier\Adapters\Laravel\OpenApiResponseVerifier; use Tests\TestCase; class UsersTest extends TestCase { use OpenApiResponseVerifier; public function setUp(): void { parent::setUp(); $this->registerOpenApiVerifier(/* $this->>createApplication() */ /* , [specification filename] */); } /** @test */ public function index() { // will `fail` if schema found and validation fails $response = $this->get('/users'); $response->assertOk(); } }
Slim adapter
The adapter will try to resolve the specification dynamically in this order:
- filename passed into
registerOpenApiVerifier() /tests/openapi.json/tests/openapi.yaml- Generate specification from scratch by scanning the
srcfolder
Simplest way is to register the verifier in the Tests\Functional\BaseTestCase.
Attention: In order to be able to resolve routes with placeholders (i.e. something like /user/{id}) it is required to register the verifier before the routing middleware.
<?php namespace Tests\Functional; use ... use Radebatz\OpenApi\Verifier\Adapters\Slim\OpenApiResponseVerifier; use PHPUnit\Framework\TestCase; class BaseTestCase extends TestCase { use OpenApiResponseVerifier; public function runApp($requestMethod, $requestUri, $requestData = null) { ... $app = new App(); // register OpenApi verifier $this->registerOpenApiVerifier($app, __DIR__ . '/../specifications/users.yaml'); $app->addRoutingMiddleware(); // ... } }
<?php namespace Tests\Functional; class UsersTest extends BaseTestCase { /** @test */ public function index() { // will `fail` if schema found and validation fails $response = $this->runApp('GET', '/users'); $this->assertEquals(200, $response->getStatusCode()); } }
License
The openapi-verifier project is released under the MIT license.
radebatz/openapi-verifier 适用场景与选型建议
radebatz/openapi-verifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 165.46k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 radebatz/openapi-verifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 radebatz/openapi-verifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 165.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-12