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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package phpunit-array-asserts

PHPUnitArrayAssertions

MIT license Code coverage

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:

  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:

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:

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 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:

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 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:

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

PHP Build Version
Package Version
Requires php Version ^7.2 || ^8.0
phrozenbyte/phpunit-throwable-asserts Version ^1.0
phplucidframe/console-table Version ^1.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package phrozenbyte/phpunit-array-asserts contains the following files

Loading the files please wait ....