{"id":15284039,"url":"https://github.com/estahn/phpunit-json-assertions","last_synced_at":"2025-05-08T23:47:58.070Z","repository":{"id":5822368,"uuid":"53177096","full_name":"estahn/phpunit-json-assertions","owner":"estahn","description":"JSON assertions for PHPUnit (including JSON Schema)","archived":false,"fork":false,"pushed_at":"2024-08-14T02:22:26.000Z","size":57,"stargazers_count":36,"open_issues_count":6,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-08T23:47:43.204Z","etag":null,"topics":["json-schema","phpunit","phpunit-assertions"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/estahn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["estahn"]}},"created_at":"2016-03-05T01:50:39.000Z","updated_at":"2025-03-09T23:53:25.000Z","dependencies_parsed_at":"2024-06-18T13:46:41.097Z","dependency_job_id":"55aa3f17-2615-4b3f-890e-0b32f49abc39","html_url":"https://github.com/estahn/phpunit-json-assertions","commit_stats":{"total_commits":57,"total_committers":7,"mean_commits":8.142857142857142,"dds":"0.38596491228070173","last_synced_commit":"54fb7c9e76a1f8f2511db95d904c509d9e4ffe58"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estahn%2Fphpunit-json-assertions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estahn%2Fphpunit-json-assertions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estahn%2Fphpunit-json-assertions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estahn%2Fphpunit-json-assertions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/estahn","download_url":"https://codeload.github.com/estahn/phpunit-json-assertions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166477,"owners_count":21864467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-schema","phpunit","phpunit-assertions"],"created_at":"2024-09-30T14:48:53.383Z","updated_at":"2025-05-08T23:47:58.052Z","avatar_url":"https://github.com/estahn.png","language":"PHP","readme":"# phpunit-json-assertions\n\n[![Latest Stable Version](https://poser.pugx.org/estahn/phpunit-json-assertions/version.png)](https://packagist.org/packages/estahn/phpunit-json-assertions)\n[![Total Downloads](https://poser.pugx.org/estahn/phpunit-json-assertions/d/total.png)](https://packagist.org/packages/estahn/phpunit-json-assertions)\n[![Test](https://github.com/estahn/phpunit-json-assertions/actions/workflows/build.yml/badge.svg)](https://github.com/estahn/phpunit-json-assertions/actions/workflows/build.yml)\n[![StyleCI](https://styleci.io/repos/53177096/shield)](https://styleci.io/repos/53177096)\n[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/estahn/phpunit-json-assertions.svg)](http://isitmaintained.com/project/estahn/phpunit-json-assertions \"Average time to resolve an issue\")\n[![Percentage of issues still open](http://isitmaintained.com/badge/open/estahn/phpunit-json-assertions.svg)](http://isitmaintained.com/project/estahn/phpunit-json-assertions \"Percentage of issues still open\")\n\nJSON assertions for PHPUnit includes traits/methods to help validate your JSON data through various methods.\n\n## Features\n\n* Validate your JSON data via JSON Schema\n    * describes your existing data format\n    * clear, human- and machine-readable documentation\n    * complete structural validation, useful for\n        * automated testing\n        * validating client-submitted data\n    * See more details [here](http://json-schema.org/)\n* Access JSON data through expressions (e.g. `foo.bar[3]`)\n    * See more details [here](http://jmespath.org/examples.html)\n\n## Install\n\n    $ composer require estahn/phpunit-json-assertions --dev\n\nor in your `composer.json`:\n\n```json\n{\n    \"require-dev\": {\n        \"estahn/phpunit-json-assertions\": \"@stable\"\n    }\n}\n```\n\n## Asserts\n\n| Assert                        | Description                                                                  | Available in |\n| ----------------------------- | ---------------------------------------------------------------------------- | ------------ |\n| [assertJsonMatchesSchema](https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema)       | Asserts that json content is valid according to the provided schema file     | All |\n| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string   | All |\n| assertJsonValueEquals         | Asserts if the value retrieved with the expression equals the expected value | All |\n| assertJsonValueEquals         | Asserts if the value retrieved with the expression equals the expected value | All |\n| assertJsonResponse            | Asserts that a response is successful and of type json | Symfony |\n\n## Usage\n\nYou can either use the `trait` or `class` version.\n\n### Trait\n\n```php\n\u003c?php\n\nnamespace EnricoStahn\\JsonAssert\\Tests;\n\nuse EnricoStahn\\JsonAssert\\Assert as JsonAssert;\n\nclass MyTestCase extends \\PHPUnit_Framework_TestCase\n{\n    use JsonAssert;\n\n    public function testJsonDocumentIsValid()\n    {\n        // my-schema.json\n        //\n        // {\n        //   \"type\" : \"object\",\n        //   \"properties\" : {\n        //     \"foo\" : {\n        //       \"type\" : \"integer\"\n        //     }\n        //   },\n        //   \"required\" : [ \"foo\" ]\n        // }\n\n        $json = json_decode('{\"foo\":1}');\n\n        $this-\u003eassertJsonMatchesSchema($json, './my-schema.json');\n        $this-\u003eassertJsonValueEquals(1, '* | [0]', $json);\n    }\n}\n```\n\n### Class\n\nIn case you don't want to use the `trait` you can use the provided class wich extends from `\\PHPUnit_Framework_TestCase`.\nYou can either extend your test case or use the static methods like below.\n\n```php\n\u003c?php\n\nnamespace EnricoStahn\\JsonAssert\\Tests;\n\nuse EnricoStahn\\JsonAssert\\AssertClass as JsonAssert;\n\nclass MyTestCase extends \\PHPUnit_Framework_TestCase\n{\n    public function testJsonDocumentIsValid()\n    {\n        // my-schema.json\n        //\n        // {\n        //   \"type\" : \"object\",\n        //   \"properties\" : {\n        //     \"foo\" : {\n        //       \"type\" : \"integer\"\n        //     }\n        //   },\n        //   \"required\" : [ \"foo\" ]\n        // }\n\n        $json = json_decode('{\"foo\":1}');\n\n        JsonAssert::assertJsonMatchesSchema($json, './my-schema.json');\n        JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);\n    }\n}\n```\n\n## Schema storage\n\nThe [schema storage](https://github.com/justinrainbow/json-schema/blob/master/src/JsonSchema/SchemaStorage.php) of `justinrainbow/json-schema` allows to register schemas which will effectively override the actual schema location.\n\nExample:\n```json\n{\"$ref\" : \"https://iglu.foobar.com/myschema.json#/definitions/positiveInteger\"}\n```\n\nThe 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.\n\n```php\n$schemastorage-\u003eaddSchema('https://iglu.foobar.com/myschema.json', (object)['type' =\u003e 'string']);\n```\n\nWith this in place the resolver will take the schema that is already in place without downloading it again.\n\n```php\n\u003c?php\n\nnamespace EnricoStahn\\JsonAssert\\Tests;\n\nuse EnricoStahn\\JsonAssert\\AssertClass as JsonAssert;\n\nclass MyTestCase extends \\PHPUnit_Framework_TestCase\n{\n    public function setUp()\n    {\n        self::$schemaStorage = new SchemaStorage();\n        \n        self::$schemaStorage-\u003eaddSchema('\u003cid\u003e', obj);\n        ...\n    }\n    \n    public function testJsonDocumentIsValid()\n    {\n        // my-schema.json\n        //\n        // {\n        //   \"type\" : \"object\",\n        //   \"properties\" : {\n        //     \"foo\" : {\n        //       \"type\" : \"integer\"\n        //     }\n        //   },\n        //   \"required\" : [ \"foo\" ]\n        // }\n\n        $json = json_decode('{\"foo\":1}');\n\n        JsonAssert::assertJsonMatchesSchema($json, './my-schema.json');\n        JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);\n    }\n}\n```\n\n## Extensions\n\n`phpunit-json-assertions` provides extensions for simpler handling in different use cases.\n\n### Symfony HttpFoundation Component\n\nThe extension `EnricoStahn\\JsonAssert\\Extension\\Symfony` allows to pass in the actual response object generated\nby the symfony framework and takes care of the decoding part.\n\nBEFORE:\n```php\nuse EnricoStahn\\JsonAssert\\Assert as JsonAssert;\n\n// ...\n\n$content = $response-\u003egetContent();\n$json = json_decode($content);\nJsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);\n```\n\nAFTER:\n```php\nuse EnricoStahn\\JsonAssert\\Extension\\Symfony as JsonAssert;\n\n// ...\n\nJsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);\n```\n\n## Tests\n\nTo run the test suite, you need [composer](http://getcomposer.org).\n\n    $ composer install\n    $ bin/phpunit\n\n## Badge Mania\n[![Build Status](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/badges/build.png?b=master)](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/build-status/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/estahn/phpunit-json-assertions/?branch=master)\n[![Codacy Badge](https://api.codacy.com/project/badge/grade/0bbc8fdeb4044287bbce009adc07ca39)](https://www.codacy.com/app/estahn/phpunit-json-assertions)\n[![Codacy Badge](https://api.codacy.com/project/badge/coverage/0bbc8fdeb4044287bbce009adc07ca39)](https://www.codacy.com/app/estahn/phpunit-json-assertions)\n\n## Alternatives\n* 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)\n\n## License\n\nThe phpunit-json-assertions library is licensed under the [MIT](LICENSE).\n\n\n## Stargazers over time\n\n[![Stargazers over time](https://starchart.cc/estahn/phpunit-json-assertions.svg)](https://starchart.cc/estahn/phpunit-json-assertions)\n","funding_links":["https://github.com/sponsors/estahn"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Festahn%2Fphpunit-json-assertions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Festahn%2Fphpunit-json-assertions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Festahn%2Fphpunit-json-assertions/lists"}