kent013/artisan-command-spectator-test
Composer 安装命令:
composer require kent013/artisan-command-spectator-test
包简介
An artisan command to generate HTTP test using OpenAPI and Spectator.
README 文档
README
Artisan make command to generate HTTP testcases with OpenAPI and spectator.
Disclaimer
This command generate only a scaffold. You need further implementation to test your API.
I'm using api blueprint for API specification. Generating OpenAPI json with using apib2swagger.
While I'm not writing OpenAPI directly, some data such as operation ID is not natural. Perhaps it will cause a problem on this command.
Installation
composer require --dev kent013/artisan-command-spectator-test
Generate config file
php artisan vendor:publish --tag="spectator-test"
Configuration
Default namespace
If you want to change test namespaces, please add following line in your .env file and change the values.
SPECTATORTEST_NAMESPACE=Feature
OpenAPI file path
You can set default open api path with adding following line in your .env file and change the values.
SPECTATORTEST_OPENAPI_PATH=documents/api/api.openapi3.json
This value is able to overide with openapi-path option.
Command line arguments
./artisan make:spectator-test TestCaseClassName APIMethodPath...
Where APIMethodPath goes like
/api/v1/organization/{organization_id}/projects/{project_id}
Will be generate all HTTP methods corresponds on the path. If you want to select HTTP methods to generate test, prefix the path with comma-separated http methods as following.
GET,PUT:/api/v1/organization/{organization_id}/projects/{project_id}
Also you can pass multiple APIMethodPath to command as following.
GET,PUT:/api/v1/organization/{organization_id}/projects/{project_id} GET,DELETE:/api/v1/organization/{organization_id}/projects/{project_id}
For example,
./artisan make:spectator-test Http/Api/V1/OrganizationProjectTest GET,PUT:/api/v1/organization/{organization_id}
Using tag
If you using tag to group API paths, then you can use --tags argument as following.
./artisan make:spectator-test:testcase --tags Http/Api/V1/ProjectTest projects
Command line options
--openapi-path
Path to Open API specification. You can specify Json or Yaml path or URL.
--force
Overrite class file or not;
--tags
Generate test methods matched with tags. With no --tags option, arguments will be processed as a path.
You cannot select HTTP methods with tags.
--test-name-with-path
Generate test method name from path like testApiV1OrganizationProjectsPut204.
By default command will use operationId like testProjects200
Example Test
./artisan make:spectator-test Http/Api/V1/OrganizationProjectTest GET,PUT:/api/v1/organization/{organization_id}/projects/{project_id}
<?php declare(strict_types=1); namespace Tests\Feature\Http\Api\V1; use Spectator\Spectator; use Tests\TestCase; class OrganizationProjectTest extends TestCase { public function setUp(): void { parent::setUp(); Spectator::using('api.openapi3.json'); } /** * get /api/v1/organization/{organization_id}/projects/{project_id} Get status code 200 */ public function testGet200(): void { $this->getJson('/api/v1/organization/1/projects/1') ->assertStatus(200) ->assertValidRequest() ->assertValidResponse(); } /** * get /api/v1/organization/{organization_id}/projects/{project_id} Get status code 401 */ public function testGet401(): void { $this->getJson('/api/v1/organization/1/projects/1') ->assertStatus(401) ->assertValidRequest() ->assertValidResponse(); } /** * get /api/v1/organization/{organization_id}/projects/{project_id} Get status code 403 */ public function testGet403(): void { $this->getJson('/api/v1/organization/1/projects/1') ->assertStatus(403) ->assertValidRequest() ->assertValidResponse(); } /** * put /api/v1/organization/{organization_id}/projects/{project_id} Update status code 204 */ public function testUpdate204(): void { $requestBody = [ 'name' => 'Fofi', 'description' => 'An Advanced Form Filler', ]; $this->putJson('/api/v1/organization/1/projects/1', $requestBody) ->assertStatus(204) ->assertValidRequest() ->assertValidResponse(); } /** * put /api/v1/organization/{organization_id}/projects/{project_id} Update status code 401 */ public function testUpdate401(): void { $requestBody = [ 'name' => 'Fofi', 'description' => 'An Advanced Form Filler', ]; $this->putJson('/api/v1/organization/1/projects/1', $requestBody) ->assertStatus(401) ->assertValidRequest() ->assertValidResponse(); } /** * put /api/v1/organization/{organization_id}/projects/{project_id} Update status code 403 */ public function testUpdate403(): void { $requestBody = [ 'name' => 'Fofi', 'description' => 'An Advanced Form Filler', ]; $this->putJson('/api/v1/organization/1/projects/1', $requestBody) ->assertStatus(403) ->assertValidRequest() ->assertValidResponse(); } /** * put /api/v1/organization/{organization_id}/projects/{project_id} Update status code 422 */ public function testUpdate422(): void { $requestBody = [ 'name' => 'Fofi', 'description' => 'An Advanced Form Filler', ]; $this->putJson('/api/v1/organization/1/projects/1', $requestBody) ->assertStatus(422) ->assertValidRequest() ->assertValidResponse(); } }
kent013/artisan-command-spectator-test 适用场景与选型建议
kent013/artisan-command-spectator-test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「testcase」 「artisan」 「openapi」 「spectator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kent013/artisan-command-spectator-test 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kent013/artisan-command-spectator-test 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kent013/artisan-command-spectator-test 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Some classes to help write tests for laravel 4 applications
Collection of handy Laravel artisan commands that most projects needs
Additional artisan commands for Laravel
Aplus Framework Testing Library
Enterprise-Grade Modular Architecture for Laravel Applications - A powerful Laravel package that revolutionizes application development with robust Service Repository Pattern, Dynamic Module Management, and 50+ Artisan commands for scalable, maintainable applications.
Some classes to help write tests for laravel 4 applications. Forked from zizaco/testcases-laravel
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-17