kirschbaum-development/laravel-openapi-validator
Composer 安装命令:
composer require kirschbaum-development/laravel-openapi-validator
包简介
Automatic OpenAPI validation for Laravel HTTP tests
README 文档
README
Using an OpenAPI spec is a great way to create and share a contract to which your API adheres. This package will automatically verify both the request and response used in your integration and feature tests wherever the Laravel HTTP testing methods (->get('/uri'), etc) are used.
Behind the scenes this package connects the Laravel HTTP helpers to The PHP League's OpenAPI Validator.
Installation
You can install the package via composer:
composer require kirschbaum-development/laravel-openapi-validator
Setup
In any feature/integration test (such as those that extend the framework's Tests\TestCase base class), add the ValidatesOpenApiSpec trait:
use Kirschbaum\OpenApiValidator\ValidatesOpenApiSpec; class HttpTest extends TestCase { use ValidatesOpenApiSpec; }
In many situations, the defaults should handle configuration. If you need to customize your configuration (namely the location of the openapi.yaml or openapi.json file), publish the config with:
php artisan vendor:publish --provider="Kirschbaum\OpenApiValidator\OpenApiValidatorServiceProvider"
and configure the path to the OpenAPI spec in config/openapi_validator.php to fit your needs.
Usage
After applying the trait to your test class, anytime you interact with an HTTP test method (get, post, put, delete, postJson, call, etc), the validator will validate both the request and the response.
Skipping Validation
Especially when initially writing tests (such as in TDD), it can be helpful to turn off the request or response validation until the tests are closer to complete. You can do so as follows:
public function testEndpointInProgress() { $response = $this->withoutRequestValidation()->get('/'); // Skips request validation, still validates response // or $response = $this->withoutResponseValidation()->get('/'); // Validates the request, but skips response // or $response = $this->withoutValidation()->get('/'); // No validation }
You are free to chain these methods as shown above, or call them on their own:
public function testEndpointInProgress() { $this->withoutRequestValidation(); $response = $this->get('/'); }
Keep in mind that withoutRequestValidation(), withoutResponseValidation(), and withoutValidation() only apply to the next request/response and will reset afterwards.
Skipping Responses Based on Response Code
We assume, by default, that any 5xx status code should not be validated. You may change this by setting the protected responseCodesToSkip property on your test class, or by using the skipResponseCode method to add response codes (single, array, or a regex pattern):
use Kirschbaum\OpenApiValidator\ValidatesOpenApiSpec; class HttpTest extends TestCase { use ValidatesOpenApiSpec; protected $responseCodesToSkip = [200]; // Will validate every response EXCEPT 200 public function testNoRedirects() { $this->skipResponseCode(300); // Will skip 200 and 300 $this->skipResponseCode(301, 302); // Will skip 200, 300, 301, 302 $this->skipResponseCode('3[1-2]1'); // Will skip 200, 300, 301, 302, 311, and 321 // ... } }
Authentication/Authorization
In most tests, you're likely using Laravel's helpers such as actingAs($user) to handle auth. This package, by default, assumes you're using bearer token as an authorization header, and that this is specified in your OpenAPI spec. The validator will expect the authorization to be part of the request, even though Laravel does not send them. If you are using security other than a bearer token, you should override the getAuthenticatedRequest method and add the appropriate headers. Note that they do not need to be valid (unless your code will check them), they just need to be present to satisfy the validator.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@kirschbaumdevelopment.com instead of using the issue tracker.
Credits
Sponsorship
Development of this package is sponsored by Kirschbaum Development Group, a developer driven company focused on problem solving, team building, and community. Learn more about us or join us!
License
The MIT License (MIT). Please see License File for more information.
kirschbaum-development/laravel-openapi-validator 适用场景与选型建议
kirschbaum-development/laravel-openapi-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24M 次下载、GitHub Stars 达 58, 最近一次更新时间为 2020 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「swagger」 「openapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kirschbaum-development/laravel-openapi-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kirschbaum-development/laravel-openapi-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kirschbaum-development/laravel-openapi-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
A jQuery augmented PHP library for creating and validating HTML forms
一个接口文档生成器
A Laravel validator for delimiter-separated list of emails.
统计信息
- 总下载量: 1.24M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 58
- 点击次数: 20
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-13