helmich/phpunit-json-assert
Composer 安装命令:
composer require helmich/phpunit-json-assert
包简介
PHPUnit assertions for JSON documents
README 文档
README
This library adds several new assertions to PHPUnit that allow you to easily and concisely verify complex data structures (often, but not necessarily, JSON documents) using JSONPath expressions and JSON schemas.
Author and copyright
Martin Helmich kontakt@martin-helmich.de
This library is MIT-licensed.
Installation
$ composer require --dev helmich/phpunit-json-assert
Compatibility
There are several release branches of this library, each of these being compatible with different releases of PHPUnit and PHP. The following table should give an easy overview:
| "JSON assertion" version | PHPUnit 4 | PHPUnit 5 | PHPUnit 6 | PHPUnit 7 | PHPUnit 8 | PHPUnit 9 | PHPUnit 10 | PHPUnit 11 | PHPUnit 12 | PHPUnit 13 |
|---|---|---|---|---|---|---|---|---|---|---|
v1 (branch v1), unsupported |
✅ | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
v2 (branch v2) |
🚫 | 🚫 | ✅ | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
v3 (branch master) |
🚫 | 🚫 | 🚫 | 🚫 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
When you are using composer require and have already declared a dependency to phpunit/phpunit in your composer.json file, Composer should pick latest compatible version automatically.
Usage
Simply use the trait Helmich\JsonAssert\JsonAssertions in your test case. This
trait offers a set of new assert* functions that you can use in your test
cases:
<?php use Helmich\JsonAssert\JsonAssertions; use PHPUnit\Framework\TestCase; class MyTestCase extends TestCase { use JsonAssertions; public function testJsonDocumentIsValid() { $jsonDocument = [ 'id' => 1000, 'username' => 'mhelmich', 'given_name' => 'Martin', 'family_name' => 'Helmich', 'age' => 27, 'phones' => [ 'mobile' => 111, 'home' => 222, ], 'hobbies' => [ 'Heavy Metal', 'Science Fiction', 'Open Source Software', ] ]; $this->assertJsonValueEquals($jsonDocument, '$.username', 'mhelmich'); $this->assertJsonValueEquals($jsonDocument, '$.phones.mobile', 111); $this->assertJsonValueEquals($jsonDocument, '$.hobbies.0', 'Heavy Metal'); $this->assertJsonValueEquals($jsonDocument, '$.hobbies[*]', 'Open Source Software'); } }
Most assertions take a $jsonPath argument which may contain any kind of
expression supported by the JSONPath library.
Alternatively, you can use the functional interface by including the file
src/Functions.php into your test cases:
<?php use Helmich\JsonAssert\JsonAssertions; use PHPUnit\Framework\TestCase; require_once('path/to/Functions.php'); class MyTestCase extends TestCase { use JsonAssertions; public function testJsonDocumentIsValid() { $jsonDocument = [ 'id' => 1000, 'username' => 'mhelmich', 'given_name' => 'Martin', 'family_name' => 'Helmich', 'age' => 27, 'hobbies' => [ "Heavy Metal", "Science Fiction", "Open Source Software" ] ]; assertThat($jsonDocument, containsJsonValue('$.username', 'mhelmich')); assertThat($jsonDocument, matchesJsonConstraints([ '$.given_name' => 'Martin', '$.age' => greaterThanOrEqual(18), '$.hobbies' => callback(function($a) { return count($a) > 2; }) ])); } }
Assertion reference
assertJsonValueEquals($doc, $jsonPath, $expected)
Asserts that the JSON value found in $doc at JSON path $jsonPath is equal
to $expected.
assertJsonValueMatches($doc, $jsonPath, \PHPUnit\Framework\Constraint $constraint)
Asserts that the JSON value found in $doc at JSON path $jsonPath matches
the constraint $constraint.
Example:
$this->assertJsonValueMatches( $jsonDocument, '$.age', \PHPUnit\Framework\Assert::greaterThanOrEqual(18) );
assertJsonDocumentMatches($doc, $constraints)
Asserts that a variable number of JSON values match a constraint. $constraints
is a key-value array in which JSON path expressions are used as keys to a
constraint value.
Example:
$this->assertJsonDocumentMatches($jsonDocument, [ '$.username' => 'mhelmich', '$.age' => \PHPUnit\Framework\Assert::greaterThanOrEqual(18) ]);
assertJsonDocumentMatchesSchema($doc, $schema)
Assert that a given JSON document matches a certain JSON schema.
Example:
$this->assertJsonDocumentMatchesSchema($jsonDocument, [ 'type' => 'object', 'required' => ['username', 'age'], 'properties' => [ 'username' => ['type' => 'string', 'minLength' => 3], 'age' => ['type' => 'number'] ] ]);
helmich/phpunit-json-assert 适用场景与选型建议
helmich/phpunit-json-assert 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.44M 次下载、GitHub Stars 达 129, 最近一次更新时间为 2015 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 helmich/phpunit-json-assert 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 helmich/phpunit-json-assert 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.44M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 129
- 点击次数: 21
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-13