estahn / phpunit-json-assertions
JSON assertions for PHPUnit (including JSON Schema)
Fund package maintenance!
estahn
Installs: 709 355
Dependents: 8
Suggesters: 0
Security: 0
Stars: 37
Watchers: 5
Forks: 10
Open Issues: 7
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-10-22 23:25:16 UTC
README
JSON assertions for PHPUnit includes traits/methods to help validate your JSON data through various methods.
Features
- Validate your JSON data via JSON Schema
- describes your existing data format
- clear, human- and machine-readable documentation
- complete structural validation, useful for
- automated testing
- validating client-submitted data
- See more details here
- Access JSON data through expressions (e.g.
foo.bar[3]
)- See more details here
Install
$ composer require estahn/phpunit-json-assertions --dev
or in your composer.json
:
{ "require-dev": { "estahn/phpunit-json-assertions": "@stable" } }
Asserts
Usage
You can either use the trait
or class
version.
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); } }
Class
In case you don't want to use the trait
you can use the provided class wich extends from \PHPUnit_Framework_TestCase
.
You can either extend your test case or use the static methods like below.
<?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 storage
The schema storage of justinrainbow/json-schema
allows to register schemas which will effectively override the actual schema location.
Example:
{"$ref" : "https://iglu.foobar.com/myschema.json#/definitions/positiveInteger"}
The resolver will fetch the schema from this endpoint and match the JSON document against it. Using schema storage you're able to override this behaviour.
$schemastorage->addSchema('https://iglu.foobar.com/myschema.json', (object)['type' => 'string']);
With this in place the resolver will take the schema that is already in place without downloading it again.
<?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); } }
Extensions
phpunit-json-assertions
provides extensions for simpler handling in different use cases.
Symfony HttpFoundation Component
The extension EnricoStahn\JsonAssert\Extension\Symfony
allows to pass in the actual response object generated
by the symfony framework and takes care of the decoding part.
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);
Tests
To run the test suite, you need composer.
$ composer install
$ bin/phpunit
Badge Mania
Alternatives
- https://github.com/martin-helmich/phpunit-json-assert - Doesn't support JSON Schema and uses JSONPath instead of jmespath.php
License
The phpunit-json-assertions library is licensed under the MIT.