PHPackages                             sdobreff/json-response-test - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. sdobreff/json-response-test

ActiveLibrary[Testing &amp; Quality](/categories/testing)

sdobreff/json-response-test
===========================

Test JSON response against provided pattern in file

1.3(5y ago)017MITPHPPHP ^7.3

Since Oct 6Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sdobreff/json-response-test)[ Packagist](https://packagist.org/packages/sdobreff/json-response-test)[ RSS](/packages/sdobreff-json-response-test/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

json-response-test
==================

[](#json-response-test)

JsonApiTest is a PHPUnit TestCase that will make your life as a PHP API developer much easier.

Thanks to [PHP-Matcher](https://github.com/coduo/php-matcher) you can, test you API JSON responses against provided schema.

Sandbox
-------

[](#sandbox)

You can use [PHP-Matcher Sandbox](https://php-matcher.norbert.tech/), to create your JSON schema.

Installation
------------

[](#installation)

Assuming you already have Composer installed globally:

```
$ composer require sdobreff/json-response-test

```

Usage
-----

[](#usage)

With this in place, any string under key message will match the pattern. More complicated expected response could look like this:

```
{
    "users":[
      {
        "firstName": "Norbert",
        "lastName": "Orzechowicz",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER"],
        "attributes": {
          "isAdmin": false,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": {
          "url": "http://avatar-image.com/avatar.png"
        }
      },
      {
        "firstName": "Michał",
        "lastName": "Dąbrowski",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER", "ROLE_ADMIN"],
        "attributes": {
          "isAdmin": true,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": null
      }
    ]
  }

```

And will match the following list of products:

```
{
    "users":[
      {
        "firstName": "@string@",
        "lastName": "@string@",
        "created": "@string@.isDateTime()",
        "roles": [
            "ROLE_USER",
            "@...@"
        ],
        "attributes": {
          "isAdmin": @boolean@,
          "@*@": "@*@"
        },
        "avatar": "@json@.match({\"url\":\"@string@.isUrl()\"})"
      }
      ,
      @...@
    ]
  }

```

Available patterns
------------------

[](#available-patterns)

```
@string@
@integer@
@number@
@double@
@boolean@
@array@
@...@ - unbounded array
@null@
@*@ || @wildcard@
expr(expression) - optional, requires symfony/expression-language: ^2.3|^3.0|^4.0|^5.0 to be present
@uuid@
@json@
@string@||@integer@ - string OR integer

```

Available pattern expanders
---------------------------

[](#available-pattern-expanders)

```
startsWith($stringBeginning, $ignoreCase = false)
endsWith($stringEnding, $ignoreCase = false)
contains($string, $ignoreCase = false)
notContains($string, $ignoreCase = false)
isDateTime()
isEmail()
isUrl()
isIp()
isEmpty()
isNotEmpty()
lowerThan($boundry)
greaterThan($boundry)
inArray($value)
hasProperty($propertyName) - example "@json@.hasProperty(\"property_name\")"
oneOf(...$expanders) - example "@string@.oneOf(contains('foo'), contains('bar'), contains('baz'))"
matchRegex($regex) - example "@string@.matchRegex('/^lorem.+/')"
optional() - work's only with ArrayMatcher, JsonMatcher and XmlMatcher
count() - work's only with ArrayMatcher - example "@array@.count(5)"
repeat($pattern, $isStrict = true) - example '@array@.repeat({"name": "foe"})' or "@array@.repeat('@string@')"
match($pattern) - example {"image":"@json@.match({\"url\":\"@string@.isUrl()\"})"}

```

Example usage
-------------

[](#example-usage)

Your test class

```
