https://github.com/estahn/phpunit-json-assertions
JSON assertions for PHPUnit (including JSON Schema)
https://github.com/estahn/phpunit-json-assertions
json-schema phpunit phpunit-assertions
Last synced: 3 months ago
JSON representation
JSON assertions for PHPUnit (including JSON Schema)
- Host: GitHub
- URL: https://github.com/estahn/phpunit-json-assertions
- Owner: estahn
- License: mit
- Created: 2016-03-05T01:50:39.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-03-01T12:31:27.000Z (about 1 year ago)
- Last Synced: 2024-05-02T00:59:35.401Z (12 months ago)
- Topics: json-schema, phpunit, phpunit-assertions
- Language: PHP
- Size: 55.7 KB
- Stars: 36
- Watchers: 5
- Forks: 10
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# phpunit-json-assertions
[](https://packagist.org/packages/estahn/phpunit-json-assertions)
[](https://packagist.org/packages/estahn/phpunit-json-assertions)
[](https://github.com/estahn/phpunit-json-assertions/actions/workflows/build.yml)
[](https://styleci.io/repos/53177096)
[](http://isitmaintained.com/project/estahn/phpunit-json-assertions "Average time to resolve an issue")
[](http://isitmaintained.com/project/estahn/phpunit-json-assertions "Percentage of issues still open")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](http://json-schema.org/)
* Access JSON data through expressions (e.g. `foo.bar[3]`)
* See more details [here](http://jmespath.org/examples.html)## Install
$ composer require estahn/phpunit-json-assertions --dev
or in your `composer.json`:
```json
{
"require-dev": {
"estahn/phpunit-json-assertions": "@stable"
}
}
```## Asserts
| Assert | Description | Available in |
| ----------------------------- | ---------------------------------------------------------------------------- | ------------ |
| [assertJsonMatchesSchema](https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema) | Asserts that json content is valid according to the provided schema file | All |
| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string | All |
| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All |
| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All |
| assertJsonResponse | Asserts that a response is successful and of type json | Symfony |## Usage
You can either use the `trait` or `class` version.
### Trait
```php
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
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
addSchema('', 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:
```php
use EnricoStahn\JsonAssert\Assert as JsonAssert;// ...
$content = $response->getContent();
$json = json_decode($content);
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
```AFTER:
```php
use EnricoStahn\JsonAssert\Extension\Symfony as JsonAssert;// ...
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);
```## Tests
To run the test suite, you need [composer](http://getcomposer.org).
$ composer install
$ bin/phpunit## Badge Mania
[](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/build-status/master)
[](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/?branch=master)
[](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/?branch=master)
[](https://www.codacy.com/app/estahn/phpunit-json-assertions)
[](https://www.codacy.com/app/estahn/phpunit-json-assertions)## Alternatives
* https://github.com/martin-helmich/phpunit-json-assert - Doesn't support JSON Schema and uses [JSONPath](https://github.com/FlowCommunications/JSONPath) instead of [jmespath.php](https://github.com/jmespath/jmespath.php)## License
The phpunit-json-assertions library is licensed under the [MIT](LICENSE).
## Stargazers over time
[](https://starchart.cc/estahn/phpunit-json-assertions)