All Projects → martin-helmich → Phpunit Json Assert

martin-helmich / Phpunit Json Assert

Licence: mit
PHPUnit assertions for JSON documents

Projects that are alternatives of or similar to Phpunit Json Assert

wp-phpunit
WordPress core PHPUnit library. [READ ONLY] Versions for new WordPress releases are built daily.
Stars: ✭ 65 (-27.78%)
Mutual labels:  unit-testing, phpunit
PHPUnit-Polyfills
Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
Stars: ✭ 147 (+63.33%)
Mutual labels:  unit-testing, phpunit
Codeception
Full-stack testing PHP framework
Stars: ✭ 4,401 (+4790%)
Mutual labels:  unit-testing, phpunit
concise
✅ Concise is test framework for using plain English and minimal code, built on PHPUnit.
Stars: ✭ 47 (-47.78%)
Mutual labels:  unit-testing, phpunit
Tester
Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏
Stars: ✭ 281 (+212.22%)
Mutual labels:  unit-testing, phpunit
Unit Testing Tips
Unit testing tips by examples in PHP
Stars: ✭ 318 (+253.33%)
Mutual labels:  unit-testing, phpunit
Lode
A universal GUI for unit testing
Stars: ✭ 51 (-43.33%)
Mutual labels:  unit-testing, phpunit
Tcunit
An unit testing framework for Beckhoff's TwinCAT 3
Stars: ✭ 74 (-17.78%)
Mutual labels:  unit-testing
Android Mvp Architecture
MVP + Kotlin + Retrofit2 + Dagger2 + Coroutines + Anko + Kotlin-Android-Extensions + RX-java + Mockk + Espresso + Junit5
Stars: ✭ 82 (-8.89%)
Mutual labels:  unit-testing
Memote
memote – the genome-scale metabolic model test suite
Stars: ✭ 73 (-18.89%)
Mutual labels:  unit-testing
Publish Unit Test Result Action
GitHub Action to publish unit test results on GitHub
Stars: ✭ 71 (-21.11%)
Mutual labels:  unit-testing
Phpumoji
PHPUnit Emoji Result Printer
Stars: ✭ 75 (-16.67%)
Mutual labels:  phpunit
Jasmine Marbles
Marble testing helpers for RxJS and Jasmine
Stars: ✭ 85 (-5.56%)
Mutual labels:  unit-testing
Sinon
Test spies, stubs and mocks for JavaScript.
Stars: ✭ 8,828 (+9708.89%)
Mutual labels:  unit-testing
Rubberduck
Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
Stars: ✭ 1,287 (+1330%)
Mutual labels:  unit-testing
Aunit
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test. Used with AUniter or EpoxyDuino for continuous builds.
Stars: ✭ 73 (-18.89%)
Mutual labels:  unit-testing
Bitsofbytes
Code and projects from my blog posts.
Stars: ✭ 89 (-1.11%)
Mutual labels:  unit-testing
Testing Workshop
A workshop for learning how to test JavaScript applications
Stars: ✭ 1,276 (+1317.78%)
Mutual labels:  unit-testing
Mock Webserver
Simple mock web server in PHP for unit testing.
Stars: ✭ 82 (-8.89%)
Mutual labels:  phpunit
Sample Code Movies
This repository contains sample code. Its purpose being, to quickly demonstrate Android and software development in general, clean code, best practices, testing and all those other must know goodies.
Stars: ✭ 81 (-10%)
Mutual labels:  unit-testing

JSON assertions for PHPUnit

Unit tests Code Climate Test Coverage Issue Count

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
v1 (branch v1), unsupported 🚫 🚫 🚫 🚫
v2 (branch v2) 🚫 🚫 🚫 🚫
v3 (branch master) 🚫 🚫 🚫 🚫

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:

<?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,
      'hobbies'     => [
        "Heavy Metal",
        "Science Fiction",
        "Open Source Software"
      ]
    ];

    $this->assertJsonValueEquals($jsonDocument, '$.username', 'mhelmich');
    $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.

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

<?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.

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

Asserts that the JSON value found in $doc at JSON path $jsonPath matches the constraint $constraint.

Example:

$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.

Example:

$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.

Example:

$this->assertJsonDocumentMatchesSchema($jsonDocument, [
    'type'       => 'object',
    'required'   => ['username', 'age'],
    'properties' => [
        'username' => ['type' => 'string', 'minLength' => 3],
        'age'      => ['type' => 'number']
    ]
]);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].