2017 © Pedro Peláez
 

library phpunit-json-assert

PHPUnit assertions for JSON documents

image

helmich/phpunit-json-assert

PHPUnit assertions for JSON documents

  • Friday, February 9, 2018
  • by helmich
  • Repository
  • 1 Watchers
  • 39 Stars
  • 72,858 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 11 Forks
  • 1 Open issues
  • 11 Versions
  • 21 % Grown

The README.md

JSON assertions for PHPUnit

Unit tests Code Climate Test Coverage Issue Count, (*1)

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., (*2)

Martin Helmich kontakt@martin-helmich.de
This library is MIT-licensed., (*3)

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:, (*4)

"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., (*5)

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:, (*6)

<?php
use Helmich\JsonAssert\JsonAssertions;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
  use JsonAssertions;

  public function testJsonDocumentIsValid()
  {
    $jsonDocument = [
      'id'          => 1000,
      'username'    => 'mhelmich',
      'given_name'  => 'Martin',
      'family_name' => 'Helmich',
      'age'         => 27,
      'phones' => [
        'mobile' => 111,
        'home'   => 222,
      ],
      'hobbies'     => [
        'Heavy Metal',
        'Science Fiction',
        'Open Source Software',
      ]
    ];

    $this->assertJsonValueEquals($jsonDocument, '$.username', 'mhelmich');
    $this->assertJsonValueEquals($jsonDocument, '$.phones.mobile', 111);
    $this->assertJsonValueEquals($jsonDocument, '$.hobbies.0', 'Heavy Metal');
    $this->assertJsonValueEquals($jsonDocument, '$.hobbies[*]', 'Open Source Software');
  }
}

Most assertions take a $jsonPath argument which may contain any kind of expression supported by the JSONPath library., (*7)

Alternatively, you can use the functional interface by including the file src/Functions.php into your test cases:, (*8)

<?php
use Helmich\JsonAssert\JsonAssertions;
use PHPUnit\Framework\TestCase;

require_once('path/to/Functions.php');

class MyTestCase extends TestCase
{
  use JsonAssertions;

  public function testJsonDocumentIsValid()
  {
    $jsonDocument = [
      'id'          => 1000,
      'username'    => 'mhelmich',
      'given_name'  => 'Martin',
      'family_name' => 'Helmich',
      'age'         => 27,
      'hobbies'     => [
        "Heavy Metal",
        "Science Fiction",
        "Open Source Software"
      ]
    ];

    assertThat($jsonDocument, containsJsonValue('$.username', 'mhelmich'));
    assertThat($jsonDocument, matchesJsonConstraints([
        '$.given_name' => 'Martin',
        '$.age'        => greaterThanOrEqual(18),
        '$.hobbies'    => callback(function($a) { return count($a) > 2; })
    ]));
  }
}

Assertion reference

assertJsonValueEquals($doc, $jsonPath, $expected)

Asserts that the JSON value found in $doc at JSON path $jsonPath is equal to $expected., (*9)

assertJsonValueMatches($doc, $jsonPath, PHPUnit_Framework_Constraint $constraint)

Asserts that the JSON value found in $doc at JSON path $jsonPath matches the constraint $constraint., (*10)

Example:, (*11)

$this->assertJsonValueMatches(
  $jsonDocument,
  '$.age',
  PHPUnit_Framework_Assert::greaterThanOrEqual(18)
);
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., (*12)

Example:, (*13)

$this->assertJsonDocumentMatches($jsonDocument, [
    '$.username' => 'mhelmich',
    '$.age'      => PHPUnit_Framework_Assert::greaterThanOrEqual(18)
]);
assertJsonDocumentMatchesSchema($doc, $schema)

Assert that a given JSON document matches a certain JSON schema., (*14)

Example:, (*15)

$this->assertJsonDocumentMatchesSchema($jsonDocument, [
    'type'       => 'object',
    'required'   => ['username', 'age'],
    'properties' => [
        'username' => ['type' => 'string', 'minLength' => 3],
        'age'      => ['type' => 'number']
    ]
]);

The Versions

09/02 2018

dev-master

9999999-dev

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

07/09 2017

v2.0.2

2.0.2.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

06/06 2017

v1.x-dev

1.9999999.9999999.9999999-dev

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

06/06 2017

v1.1.4

1.1.4.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

06/06 2017

v2.0.1

2.0.1.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2017

v2.0.0

2.0.0.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2017

v1.1.3

1.1.3.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

15/11 2016

v1.1.2

1.1.2.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

15/11 2016

v1.1.1

1.1.1.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

15/04 2016

v1.1.0

1.1.0.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires

 

The Development Requires

13/12 2015

v1.0.0

1.0.0.0

PHPUnit assertions for JSON documents

  Sources   Download

MIT

The Requires