Download the PHP package helmich/phpunit-json-assert without Composer
On this page you can find all versions of the php package helmich/phpunit-json-assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download helmich/phpunit-json-assert
More information about helmich/phpunit-json-assert
Files in helmich/phpunit-json-assert
Package phpunit-json-assert
Short Description PHPUnit assertions for JSON documents
License MIT
Informations about the package phpunit-json-assert
JSON assertions for PHPUnit
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 [email protected]
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 |
---|---|---|---|---|---|---|---|---|
v1 (branch v1 ), unsupported |
:white_check_mark: | :white_check_mark: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: |
v2 (branch v2 ) |
:no_entry_sign: | :no_entry_sign: | :white_check_mark: | :white_check_mark: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: |
v3 (branch master ) |
:no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :no_entry_sign: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
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:
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:
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:
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:
assertJsonDocumentMatchesSchema($doc, $schema)
Assert that a given JSON document matches a certain JSON schema.
Example: