estahn / phpunit-json-assertions
PHPUnit 的 JSON 断言(包括 JSON Schema)
v4.0.0
2021-06-22 07:01 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- justinrainbow/json-schema: ^5.0
- mtdowling/jmespath.php: ^2.3
Requires (Dev)
- codacy/coverage: dev-master
- phpunit/phpunit: ^9
- symfony/http-foundation: ^2.8|^3.0|^5.0
- dev-master
- v4.0.0
- v3.0.0
- v2.0.1
- v2.0.0
- v1.0.0
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-renovate/configure
- dev-dependabot/github_actions/actions/setup-node-3.6.0
- dev-dependabot/github_actions/actions/cache-3.0.11
- dev-dependabot/github_actions/actions/checkout-3.1.0
- dev-dependabot/github_actions/github/codeql-action-2
This package is not auto-updated.
Last update: 2024-09-10 22:49:51 UTC
README
PHPUnit 的 JSON 断言包含 trait 和方法,用于通过各种方式验证您的 JSON 数据。
功能
- 通过 JSON Schema 验证您的 JSON 数据
- 描述您现有的数据格式
- 清晰、可读的文档
- 完整的结构验证,适用于
- 自动化测试
- 验证客户端提交的数据
- 更多信息请点击 这里
- 通过表达式访问 JSON 数据(例如
foo.bar[3]
)- 更多信息请点击 这里
安装
$ composer require estahn/phpunit-json-assertions --dev
或在您的 composer.json
{ "require-dev": { "estahn/phpunit-json-assertions": "@stable" } }
断言
用法
您可以使用 trait
或 class
版本。
trait
<?php namespace EnricoStahn\JsonAssert\Tests; use EnricoStahn\JsonAssert\Assert as JsonAssert; class MyTestCase extends \PHPUnit_Framework_TestCase { use JsonAssert; public function testJsonDocumentIsValid() { // my-schema.json // // { // "type" : "object", // "properties" : { // "foo" : { // "type" : "integer" // } // }, // "required" : [ "foo" ] // } $json = json_decode('{"foo":1}'); $this->assertJsonMatchesSchema($json, './my-schema.json'); $this->assertJsonValueEquals(1, '* | [0]', $json); } }
类
如果您不想使用 trait
,可以使用提供的类,该类继承自 \PHPUnit_Framework_TestCase
。您可以扩展您的测试用例或使用以下静态方法。
<?php namespace EnricoStahn\JsonAssert\Tests; use EnricoStahn\JsonAssert\AssertClass as JsonAssert; class MyTestCase extends \PHPUnit_Framework_TestCase { public function testJsonDocumentIsValid() { // my-schema.json // // { // "type" : "object", // "properties" : { // "foo" : { // "type" : "integer" // } // }, // "required" : [ "foo" ] // } $json = json_decode('{"foo":1}'); JsonAssert::assertJsonMatchesSchema($json, './my-schema.json'); JsonAssert::assertJsonValueEquals(1, '* | [0]', $json); } }
Schema 存储
justinrainbow/json-schema
的 schema 存储 允许注册将覆盖实际 schema 位置的 schema。
示例
{"$ref" : "https://iglu.foobar.com/myschema.json#/definitions/positiveInteger"}
解析器将从此端点获取 schema 并与 JSON 文档进行匹配。使用 schema 存储可以覆盖此行为。
$schemastorage->addSchema('https://iglu.foobar.com/myschema.json', (object)['type' => 'string']);
设置完成后,解析器将使用已存在的 schema,而无需再次下载。
<?php namespace EnricoStahn\JsonAssert\Tests; use EnricoStahn\JsonAssert\AssertClass as JsonAssert; class MyTestCase extends \PHPUnit_Framework_TestCase { public function setUp() { self::$schemaStorage = new SchemaStorage(); self::$schemaStorage->addSchema('<id>', obj); ... } public function testJsonDocumentIsValid() { // my-schema.json // // { // "type" : "object", // "properties" : { // "foo" : { // "type" : "integer" // } // }, // "required" : [ "foo" ] // } $json = json_decode('{"foo":1}'); JsonAssert::assertJsonMatchesSchema($json, './my-schema.json'); JsonAssert::assertJsonValueEquals(1, '* | [0]', $json); } }
扩展
phpunit-json-assertions
为不同的使用场景提供了扩展,以简化处理。
Symfony HttpFoundation 组件
扩展 EnricoStahn\JsonAssert\Extension\Symfony
允许传入 symfony 框架生成的实际响应对象,并负责解码部分。
BEFORE
use EnricoStahn\JsonAssert\Assert as JsonAssert; // ... $content = $response->getContent(); $json = json_decode($content); JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
AFTER
use EnricoStahn\JsonAssert\Extension\Symfony as JsonAssert; // ... JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);
测试
要运行测试套件,您需要 composer。
$ composer install
$ bin/phpunit
徽章狂热
替代方案
- https://github.com/martin-helmich/phpunit-json-assert - 不支持 JSON Schema,并使用 JSONPath 而不是 jmespath.php
许可证
phpunit-json-assertions 库采用 MIT 许可。