Download the PHP package phrozenbyte/phpunit-array-asserts without Composer
On this page you can find all versions of the php package phrozenbyte/phpunit-array-asserts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phrozenbyte/phpunit-array-asserts
More information about phrozenbyte/phpunit-array-asserts
Files in phrozenbyte/phpunit-array-asserts
Package phpunit-array-asserts
Short Description Provides various array-related PHPUnit assertions, primarily used for API testing.
License MIT
Informations about the package phpunit-array-asserts
PHPUnitArrayAssertions
PHPUnitArrayAssertions
is a small PHPUnit extension to improve testing of PHP arrays and array-like data. It introduces the 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 "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 :heart: by Daniel Rudolf. PHPUnitArrayAssertions
is free and open source software, released under the terms of the MIT license.
Table of contents:
- Install
- Usage
- Constraint
AssociativeArray
- Constraint
ArrayHasKeyWith
- Constraint
SequentialArray
- Constraint
ArrayHasItemWith
- Constraint
- Example
Install
PHPUnitArrayAssertions
is available on Packagist.org and can be installed using Composer:
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
:
- By using the static class
PhrozenByte\PHPUnitArrayAsserts\Assert
- By using the trait
PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait
in your test case - By creating new constraint instances (
PhrozenByte\PHPUnitArrayAsserts\Constraint\…
)
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:
Example:
Debugging:
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:
Example:
Debugging:
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 Traversable
s to be rewindable. For NoRewindIterator
instances it assumes that the iterator is still in its initial state. Generator
s 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 Iterator
s 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:
Example:
Debugging:
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 Traversable
s to be rewindable. For NoRewindIterator
instances it assumes that the iterator is still in its initial state. Generator
s 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 Iterator
s 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:
Example:
Debugging:
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.
All versions of phpunit-array-asserts with dependencies
phrozenbyte/phpunit-throwable-asserts Version ^1.0
phplucidframe/console-table Version ^1.2