sixlive/laravel-json-schema-assertions

Laravel JSON Schema assertions

v4.3.2 2024-08-15 10:36 UTC

README

Packagist Version Packagist Downloads StyleCI

Laravel 测试响应的 JSON Schema 断言。底层使用 swaggest/php-json-schema

安装

您可以通过 composer 安装此包。

> composer require sixlive/laravel-json-schema-assertions

此包使用 Laravel 的 包发现 将服务提供者注册到框架中。如果您使用的是旧版本的 Laravel 或不使用包发现,请参见下文。

提供者注册
// config/app.php

'providers' => [
    sixlive\Laravel\JsonSchemaAssertions\ServiceProvider::class,
]

配置

发布包配置文件

> php artisan vendor:publish --provider="sixlive\Laravel\JsonSchemaAssertions\ServiceProvider" --tag="config"

这是将在 config/json-schema-assertions 发布的文件内容

return [
    'schema_base_path' => base_path('schemas'),
];

使用方法

如果您正在使用外部模式引用,例如 $ref: 'bar.json,您必须通过文件路径或使用配置路径解析来引用模式。

├── app
├── bootstrap
├── config
├── database
├── public
├── resources
├── routes
├── schemas
│   ├── bar.json
│   └── foo.json
├── storage
├── tests
└── vendor
/** @test */
public function it_has_a_valid_response()
{
    $schema = [
        'type' => 'object',
        'properties' => [
           'foo' => [
                'type' => 'string',
           ],
         ],
         'required' => [
            'foo',
        ],
    ];

    $response = $this->get('/foo');

    // Schema as an array
    $response->assertJsonSchema($schema);

    // Schema from raw JSON
    $response->assertJsonSchema(json_encode($schema));

    // Schema from a file
    $response->assertJsonSchema(base_path('schemas/foo.json'));

    // Schema from config path
    $response->assertJsonSchema('foo');

    // Remote schema
    $response->assertJsonSchema('https://docs.foo.io/schemas/foo.json');
}

测试

> composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

代码风格

除了 php-cs-fixer 规则外,StyleCI 还将应用 Laravel 预设

代码风格检查

> composer styles:lint

修复

> composer styles:fix

安全

如果您发现任何与安全相关的问题,请通过电子邮件 oss@tjmiller.co 通知,而不是使用问题跟踪器。

鸣谢

许可

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