phrozenbyte/phpunit-array-asserts

Provides various array-related PHPUnit assertions, primarily used for API testing.

v1.2.0 2021-02-27 14:54 UTC

This package is auto-updated.

Last update: 2024-04-14 01:41:34 UTC


README

MIT license Code coverage

PHPUnitArrayAssertions is a small PHPUnit extension to improve testing of PHP arrays and array-like data. It introduces the AssociativeArray, ArrayHasKeyWith, SequentialArray, and ArrayHasItemWith constraints. It is often used for API testing to assert whether an API result matches certain criteria - regarding both its structure, and the data.

This PHPUnit extension allows developers to test structure and data in single assertion, making test cases less repetitive and easier to understand. In some way it's an alternative to PHPUnit's ArraySubset constraint that was deprecated in PHPUnit 8 and removed in PHPUnit 9 - just way more powerful and less confusing. Refer to the "Usage" section and "Example" section below for more info.

You want more PHPUnit constraints? Check out PHPUnitThrowableAssertions! It introduces the assertCallableThrows() and assertCallableThrowsNot() assertions to improve testing of exceptions and PHP errors. It's more powerful and flexible than PHPUnit's core expectException() method.

Made with ❤️ by Daniel Rudolf. PHPUnitArrayAssertions is free and open source software, released under the terms of the MIT license.

Table of contents:

  1. Install
  2. Usage
    1. Constraint AssociativeArray
    2. Constraint ArrayHasKeyWith
    3. Constraint SequentialArray
    4. Constraint ArrayHasItemWith
  3. Example

Install

PHPUnitArrayAssertions is available on Packagist.org and can be installed using Composer:

composer require --dev phrozenbyte/phpunit-array-asserts

This PHPUnit extension was initially written for PHPUnit 8, but should work fine with any later PHPUnit version. If it doesn't, please don't hesitate to open a new Issue on GitHub, or, even better, create a Pull Request with a proposed fix.

Usage

There are three (basically equivalent) options to use PHPUnitArrayAssertions:

All options do the same, the only difference is that the static class and trait both throw PHPUnit\Framework\InvalidArgumentException exceptions for invalid parameters. Creating new constraint instances is useful for advanced assertions, e.g. together with PHPUnit\Framework\Constraint\LogicalAnd.

Constraint AssociativeArray

The AssociativeArray constraint asserts that a value is an associative array matching a given structure and that the array's items pass other constraints.

Any native array and ArrayAccess object is considered an associative array, no matter which keys they use. However, the array's items are applied to the matching constraint (parameter $consotraints). By default, missing items will fail the constraint (parameter $allowMissing, defaults to false). Additional items will be ignored by default (parameter $allowAdditional, defaults to true). If you want the constraint to fail when additional items exist, set this option to true, however, please note that this works for native arrays only. The expected keys and constraints to apply, as well as whether missing and/or additional items should fail the constraint, are passed in the constructor. Constraints can either be arbitrary Constraint instances (e.g. PHPUnit\Framework\Constraint\StringContains), or any static value, requiring exact matches of the values.

The ArrayAssertsTrait trait exposes two public methods for the AssociativeArray constraint: Use ArrayAssertsTrait::assertAssociativeArray() to perform an assertion, and ArrayAssertsTrait::associativeArray() to create a new instance of the AssociativeArray constraint.

Usage:

// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
ArrayAssertsTrait::assertAssociativeArray(
    array $constraints,            // an array with the expected keys and constraints to apply
    array|ArrayAccess $array,      // the associative array to check
    bool $allowMissing = false,    // whether missing items fail the constraint
    bool $allowAdditional = true,  // whether additional items fail the constraint
    string $message = ''           // additional information about the test
);

// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\AssociativeArray`
new AssociativeArray(
    array $constraints,
    bool $allowMissing = false,
    bool $allowAdditional = true
);

Example:

$data = [
    'id'      => 42,
    'name'    => 'Arthur Dent',
    'options' => [ 'has_towel' => true, 'panic' => false ],
];

// asserts that `$data` is an associative array with exactly the keys:
//     - "id" with a numeric value,
//     - "name" with the value "Arthur Dent", and
//     - "options" with another associative array with the key "panic", whose value must be a boolean
$this->assertAssociativeArray([
    'id'      => $this->isType(IsType::TYPE_INT),
    'name'    => 'Arthur Dent',
    'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ], true)
], $data);

Debugging:

$data = [
    'answer' => 21 /* half the truth */
];

$this->assertAssociativeArray([
    'answer' => 42
], $data);

// Will fail with the following message:
//
//     Failed asserting that associative array matches constraints.
//     +----------+-------+----------------------+
//     | Key      | Value | Constraint           |
//     +----------+-------+----------------------+
//     | 'answer' | 21    | Value is equal to 42 |
//     +----------+-------+----------------------+
//     [ ] Allow missing; [x] Allow additional

Constraint ArrayHasKeyWith

The ArrayHasKeyWith constraint asserts that an array has a given key and that its value passes another constraint.

Accepts both native arrays and ArrayAccess objects. The constraint (parameter $constraint) will fail if the key (parameter $key) doesn't exist in the array. The item's key, and the constraint the value must pass are passed in the constructor. The constraint can either be an arbitrary Constraint instance (e.g. PHPUnit\Framework\Constraint\StringContains), or any static value, requiring an exact match of the value.

The ArrayAssertsTrait trait exposes two public methods for the ArrayHasKeyWith constraint: Use ArrayAssertsTrait::assertArrayHasKeyWith() to perform an assertion, and ArrayAssertsTrait::arrayHasKeyWith() to create a new instance of the ArrayHasKeyWith constraint.

Usage:

// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
ArrayAssertsTrait::assertArrayHasKeyWith(
    string|int $key,              // the key of the item to check
    Constraint|mixed $constraint, // the constraint the item's value is applied to
    array|ArrayAccess $array,     // the array to check
    string $message = ''          // additional information about the test
);

// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\ArrayHasKeyWith`
new ArrayHasKeyWith(
    string|int $key,
    Constraint|mixed $constraint
);

Example:

$data = [
    'id'      => 42,
    'name'    => 'Arthur Dent',
    'options' => [ 'has_towel' => true, 'panic' => false ],
];

// asserts that $data has the item `name` with the value "Arthur Dent"
$this->assertArrayHasKeyWith('name', 'Arthur Dent', $data);

Debugging:

$data = [];

$this->assertArrayHasKeyWith('answer', 42, $data);

// Will fail with the following message:
//
//     Failed asserting that Array &0 () is an array that
//     has the key 'answer' whose value is equal to 42.

Constraint SequentialArray

The SequentialArray constraint asserts that a value is like a sequential array, has a minimum and/or maximum number of items, and that all items pass another constraint.

Sequential arrays are defined as ordered lists with incrementing numeric keys starting from zero. This is especially true for native sequential arrays like [ "foo", "bar" ]. Empty arrays are considered valid, too. Traversable objects must have sequential keys to be considered valid. The expected minimum (parameter $minItems, defaults to 0) and/or maximum (parameter $maxItems, defaults to null, meaning infinite) number of items, and the constraint to apply all items to (optional parameter $constraint), are passed in the constructor. The constraint can either be an arbitrary Constraint instance (e.g. PHPUnit\Framework\Constraint\StringContains), or any static value, requiring an exact match of the value. Requiring sequential keys can be disabled by setting parameter $ignoreKeys to true (defaults to false), causing the constraint to check just for the required number of items and whether they match the given constraint.

This constraint will fully traverse any Traversable object given. It expects Traversables to be rewindable. For NoRewindIterator instances it assumes that the iterator is still in its initial state. Generators will be fully exhausted; if the iterator has begun already, the object is considered invalid. If an Iterator is given, it will try to restore the object's pointer to its previous state. This will silently fail for NoRewindIterator instances. The behaviour for Iterators with non-unique keys is undefined.

The ArrayAssertsTrait trait exposes two public methods for the SequentialArray constraint: Use ArrayAssertsTrait::assertSequentialArray() to perform an assertion, and ArrayAssertsTrait::sequentialArray() to create a new instance of the SequentialArray constraint.

Usage:

// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
ArrayAssertsTrait::assertSequentialArray(
    array|Traversable $array,            // the sequential array to check
    int $minItems,                       // required minimum number of items
    int $maxItems = null,                // required maximum number of items (pass null for infinite)
    Constraint|mixed $constraint = null, // optional constraint to apply all items to
    bool $ignoreKeys = false,            // whether to ignore non-sequential keys
    string $message = ''                 // additional information about the test
);

// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\SequentialArray`
new SequentialArray(
    int $minItems = 0,
    int $maxItems = null,
    Constraint|mixed $constraint = null,
    bool $ignoreKeys = false
);

Example:

$data = [
    "The Hitchhiker's Guide to the Galaxy",
    "The Restaurant at the End of the Universe",
    "Life, the Universe and Everything",
    "So Long, and Thanks for All the Fish",
    "Mostly Harmless",
    "And Another Thing...",
];

// asserts that `$data` is a non-empty sequential array with non-empty items
$this->assertSequentialArray($data, 1, null, $this->logicalNot($this->isEmpty()));

Debugging:

$data = [];

$this->assertSequentialArray($data, 4, null, $this->is(IsType::TYPE_STRING));

// Will fail with the following message:
//
//     Failed asserting that Array &0 () is is a sequential array
//     with ≥ 4 items matching the constraint "is of type "string"".

Constraint ArrayHasItemWith

The ArrayHasItemWith constraint asserts that an array has a item at a given index and that its value passes another constraint.

Accepts both native arrays and Traversable objects. The constraint will fail if the array has less items than required. The index of the item to check (parameter $index), and the constraint its value must pass (parameter $constraint) are passed in the constructor. The constraint can either be an arbitrary Constraint instance (e.g. PHPUnit\Framework\Constraint\StringContains), or any static value, requiring an exact match of the value.

This constraint will fully traverse any Traversable object given. It expects Traversables to be rewindable. For NoRewindIterator instances it assumes that the iterator is still in its initial state. Generators will be fully exhausted; if the iterator has begun already, the object is considered invalid. If an Iterator is given, it will try to restore the object's pointer to its previous state. This will silently fail for NoRewindIterator instances. The behaviour for Iterators with non-unique keys is undefined.

The ArrayAssertsTrait trait exposes two public methods for the ArrayHasItemWith constraint: Use ArrayAssertsTrait::assertArrayHasItemWith() to perform an assertion, and ArrayAssertsTrait::arrayHasItemWith() to create a new instance of the ArrayHasItemWith constraint.

Usage:

// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
ArrayAssertsTrait::assertArrayHasItemWith(
    int $index,                   // the index of the item to check
    Constraint|mixed $constraint, // the constraint the item's value is applied to
    array|Traversable $array,     // the array to check
    string $message = ''          // additional information about the test
);

// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\ArrayHasItemWith`
new ArrayHasItemWith(
    int $index,
    Constraint|mixed $constraint
);

Example:

$data = [
    '1979-10-12' => "The Hitchhiker's Guide to the Galaxy",
    '1980-10-00' => "The Restaurant at the End of the Universe",
    '1982-08-00' => "Life, the Universe and Everything",
    '1984-11-09' => "So Long, and Thanks for All the Fish",
    '1992-00-00' => "Mostly Harmless",
    '2009-10-12' => "And Another Thing...",
];

// asserts that `$data` contains "Life, the Universe and Everything" as third item (i.e. at index 2)
$this->assertArrayHasItemWith(2, "Life, the Universe and Everything");

Debugging:

$data = [];

$this->assertArrayHasItemWith(2, 'Arthur Dent', $data);

// Will fail with the following message:
//
//     Failed asserting that Array &0 () is an array that
//     has a value at index 2 which is equal to 'Arthur Dent'.

Example

Here's a (more or less) real-world example of PHPUnitArrayAssertions. Check out the testWithPHPUnitArrayAsserts() method to see how a complex API response is tested. For a comparison with an implementation utilizing just PHPUnit's core features, check out the testWithoutPHPUnitArrayAsserts() method. Without PHPUnitArrayAssertions you end up having 17 lines of pretty repetitive code, with this PHPUnit extension you can test the response with 7 lines of easy to understand code.

<?php
declare(strict_types=1);

namespace YourName\YourProject\Tests;

use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\TestCase;
use PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait;

class MyTest extends TestCase
{
    use ArrayAssertsTrait;

    public function testWithPHPUnitArrayAsserts(): void
    {
        // 7 lines of easy to understand code to check the API response *with* PHPUnitArrayAsserts

        // implement your test, the result is stored in $responseData

        $responseData = [
            'users' => [
                [
                    'id'      => 42,
                    'name'    => 'Arthur Dent',
                    'options' => [ 'has_towel' => true, 'panic' => false ],
                ],
            ]
        ];

        $this->assertArrayHasKeyWith('users', $this->sequentialArray(1), $responseData);

        $this->assertAssociativeArray([
            'id'      => $this->isType(IsType::TYPE_INT),
            'name'    => 'Arthur Dent',
            'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ])
        ], $responseData['users'][0]);
    }
    
    public function testWithoutPHPUnitArrayAsserts(): void
    {
        // 17 lines of pretty repetitive code to check the API response *without* PHPUnitArrayAsserts

        // implement your test, the result is stored in $responseData

        $responseData = [
            'users' => [
                [
                    'id'      => 42,
                    'name'    => 'Arthur Dent',
                    'options' => [ 'has_towel' => true, 'panic' => false ],
                ],
            ]
        ];

        $this->assertArrayHasKey('users', $responseData);
        $this->assertIsArray($responseData['users']);
        $this->assertGreaterThanOrEqual(1, count($responseData['users'])); // won't work for Traversable

        $userData = $responseData['users'][0]; // we can't really rely on the existence of key "0" here :/

        $this->assertArrayHasKey('id', $userData);
        $this->assertIsInt($userData['id']);

        $this->assertArrayHasKey('name', $userData);
        $this->assertSame('Arthur Dent', $userData['name']);

        $this->assertArrayHasKey('options', $userData);
        $this->assertIsArray($userData['options']);

        $this->assertArrayHasKey('panic', $userData['options']);
        $this->assertIsBool($userData['options']['panic']);
    }
}