PHPackages                             alezhu/phpunit-array-contains-asserts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. alezhu/phpunit-array-contains-asserts

ActiveLibrary[Testing &amp; Quality](/categories/testing)

alezhu/phpunit-array-contains-asserts
=====================================

Provides PHPUnit assertions to test array contains data or structure

10.0.5(1y ago)03.2k↓50%1MITPHPPHP ~8.1CI passing

Since Sep 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/alezhu/phpunit-array-contains-asserts)[ Packagist](https://packagist.org/packages/alezhu/phpunit-array-contains-asserts)[ RSS](/packages/alezhu-phpunit-array-contains-asserts/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (18)Used By (1)

PHPUnit arrayContains asserts
=============================

[](#phpunit-arraycontains-asserts)

[![PHPUnit 10 latest version](https://camo.githubusercontent.com/7dd6dd114bebda56749331c0740036d0573a0f0dd35ac3019e59842931604d0e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d31302e2a266c6162656c3d504850556e69742532303130)](https://camo.githubusercontent.com/7dd6dd114bebda56749331c0740036d0573a0f0dd35ac3019e59842931604d0e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d31302e2a266c6162656c3d504850556e69742532303130)[![PHPUnit 10 build Status](https://camo.githubusercontent.com/6862bb9113d696cbe4915bea4b9168e458303fa316968212ba2dc4b275709442/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f7068702e796d6c3f6c6162656c3d504850556e69742532303130)](https://camo.githubusercontent.com/6862bb9113d696cbe4915bea4b9168e458303fa316968212ba2dc4b275709442/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f7068702e796d6c3f6c6162656c3d504850556e69742532303130)[![PHPUnit 9 latest version](https://camo.githubusercontent.com/8310bd16bb9d3f6266ade225696c14d6f260b5d119a72fc9925e4a27c36489f9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d392e2a266c6162656c3d504850556e697425323039)](https://camo.githubusercontent.com/8310bd16bb9d3f6266ade225696c14d6f260b5d119a72fc9925e4a27c36489f9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d392e2a266c6162656c3d504850556e697425323039)[![PHPUnit 9 build Status ](https://camo.githubusercontent.com/f365308c2e6172c3357da4d749487d475c0a134d033d6f512d5333ef2ec8c0ac/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f706870756e69745f392e796d6c3f6c6162656c3d504850556e697425323039)](https://camo.githubusercontent.com/f365308c2e6172c3357da4d749487d475c0a134d033d6f512d5333ef2ec8c0ac/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f706870756e69745f392e796d6c3f6c6162656c3d504850556e697425323039)[![PHPUnit 8 latest version](https://camo.githubusercontent.com/ecd6d46943932cf038aec7ca30f5574e70005f0db129cf1c622fd1d1bed3ac7c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d382e2a266c6162656c3d504850556e697425323038)](https://camo.githubusercontent.com/ecd6d46943932cf038aec7ca30f5574e70005f0db129cf1c622fd1d1bed3ac7c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274733f66696c7465723d382e2a266c6162656c3d504850556e697425323038)[![PHPUnit 8 build Status ](https://camo.githubusercontent.com/c5d776ba0655597f07bdbe932e53d47313ef86a3135d3b937be1eb7b2d124011/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f706870756e69745f382e796d6c3f6c6162656c3d504850556e697425323038)](https://camo.githubusercontent.com/c5d776ba0655597f07bdbe932e53d47313ef86a3135d3b937be1eb7b2d124011/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f706870756e69745f382e796d6c3f6c6162656c3d504850556e697425323038)[![Total Downloads](https://camo.githubusercontent.com/81db42ccaa39037a7696026e49ef312d642a0f347117fa446fb15cb39f34868f/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f646f776e6c6f616473)](https://camo.githubusercontent.com/81db42ccaa39037a7696026e49ef312d642a0f347117fa446fb15cb39f34868f/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f646f776e6c6f616473)[![License](https://camo.githubusercontent.com/53cc508984a1f4796d5a0dfb9f8a8e3fb6bf94885c607fca536dac4e70889222/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f6c6963656e7365)](https://camo.githubusercontent.com/53cc508984a1f4796d5a0dfb9f8a8e3fb6bf94885c607fca536dac4e70889222/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f6c6963656e7365)[![PHP Version Require](https://camo.githubusercontent.com/e77700036db329b49bf61ed56f479ba8f0bf0a3218c66bf7a807244efe27896c/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f726571756972652f706870)](https://camo.githubusercontent.com/e77700036db329b49bf61ed56f479ba8f0bf0a3218c66bf7a807244efe27896c/687474703a2f2f706f7365722e707567782e6f72672f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d617373657274732f726571756972652f706870)[![GitHub code size in bytes](https://camo.githubusercontent.com/bcc067c097d69980a78e702658607ff22ef3fd5943eae234da4deadeac984cfd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d61737365727473)](https://camo.githubusercontent.com/bcc067c097d69980a78e702658607ff22ef3fd5943eae234da4deadeac984cfd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f616c657a68752f706870756e69742d61727261792d636f6e7461696e732d61737365727473)

Provides PHPUnit assertions to test when array-like data contains expected data with expected structure.

This PHPUnit extension was written for PHPUnit 10, but also has branches for PHPUnit 8 and PHPUnit 9 . If it doesn't work properly, please don't hesitate to open a [new Issue on GitHub](https://github.com/alezhu/phpunit-array-contains-asserts/issues), or, even better, create a Pull Request with a proposed fix .

**Table of contents:**

1. [Install](#install)
2. [Usage](#usage)
    1. [Constraint `ArrayContains`](#constraint-ArrayContains)
    2. [Constraint `ArrayContainsOnly`](#constraint-ArrayContainsOnly)

Install
-------

[](#install)

`PHPUnit arrayContains asserts` is available on [Packagist.org](https://packagist.org/packages/alezhu/phpunit-array-contains-asserts) and can be installed using [Composer](https://getcomposer.org/):

```
composer require --dev alezhu/phpunit-array-contains-asserts
```

Usage
-----

[](#usage)

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

- By using the static [class `Alezhu\PHPUnitArrayContainsAsserts\Assert`](https://github.com/alezhu/phpunit-array-contains-asserts/blob/main/src/Assert.php)
- By using the [trait `Alezhu\PHPUnitArrayContainsAsserts\ArrayContainsTrait`](https://github.com/alezhu/phpunit-array-contains-asserts/blob/main/src/ArrayContainsTrait.php)in your test case
- By creating new [constraint instances](https://github.com/alezhu/phpunit-array-contains-asserts/tree/main/src/Constraint) ( `Alezhu\PHPUnitArrayContainsAsserts\Constraint\…`)

All options do the same, the only difference is that the static class and trait both throw [class `Alezhu\PHPUnitArrayContainsAsserts\Exception\InvalidArgumentTypeException`](https://github.com/alezhu/phpunit-array-contains-asserts/blob/main/src/Exception/InvalidArgumentTypeException.php) ( or `PHPUnit\Framework\InvalidArgumentException` for PHPUnit 9 and 8) exceptions for invalid parameters. Creating new constraint instances is useful for advanced assertions, e.g. together with `PHPUnit\Framework\Constraint\LogicalAnd`.

### Constraint `ArrayContains`

[](#constraint-arraycontains)

The [`ArrayContains` constraint](https://github.com/alezhu/phpunit-array-contains-asserts/blob/main/src/Constraint/ArrayContains.php)asserts that an array contains all expected values (for non-associative arrays) or all expected keys with expected values (for associative arrays).

Expected values can be set directly or via another PHPUnit constraints (`PHPUnit\Framework\Constraint\...`).

Expected and actual data can be array or iterator or inherit ArrayObject or implements ArrayAccess+Countable interfaces.

Expected and actual data must have same associative kind.

**Usage:**

```
use Alezhu\PHPUnitArrayContainsAsserts\Assert;
use PHPUnit\Framework\Constraint\IsType;

//Passed
Assert::assertArrayContains(
    [
        "foo" => new isType(IsType::TYPE_STRING),
        "baz" => 1
    ],
    [
        "foo" => "value",
        "bar" => true,
        "baz" => 1
    ]
);
//Not Passed
Assert::assertArrayContains(
    [
        "foo" => new isType(IsType::TYPE_STRING),
        "baz" => 1
    ],
    [
        "foo" => "bar",
    ]
);
```

### Constraint `ArrayContainsOnly`

[](#constraint-arraycontainsonly)

The [`ArrayContainsOnly` constraint](https://github.com/alezhu/phpunit-array-contains-asserts/blob/main/src/Constraint/ArrayContainsOnly.php)asserts that an array contains **only** all expected values (for non-associative arrays) or ***only*** all expected keys with expected values (for associative arrays).

Expected values can be set directly or via another PHPUnit constraints (`PHPUnit\Framework\Constraint\...`).

Expected and actual data can be array or iterator or inherit ArrayObject or implements ArrayAccess+Countable interfaces.

Expected and actual data must have same associative kind.

**Usage:**

```
use Alezhu\PHPUnitArrayContainsAsserts\Assert;
use PHPUnit\Framework\Constraint\IsType;

//Passed
Assert::assertArrayContainsOnly(
    [
        "foo" => new isType(IsType::TYPE_STRING),
        "baz" => 1
    ],
    [
        "foo" => "value",
        "baz" => 1
    ]
);
//Not Passed
Assert::assertArrayContainsOnly(
    [
        "foo" => new isType(IsType::TYPE_STRING),
        "baz" => 1
    ],
    [
        "foo" => "bar",
        "bar" => true,
        "baz" => 1
    ]
);
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance43

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~39 days

Recently: every ~135 days

Total

15

Last Release

440d ago

Major Versions

8.0.2 → 9.0.02023-09-06

9.0.0 → 10.0.02023-09-06

8.0.4 → 10.0.32023-09-08

9.0.2 → 10.0.42023-09-08

8.0.5 → 10.0.52025-01-30

PHP version history (5 changes)8.0.0PHP &gt;=7.2

8.0.2PHP ~7.2.0

9.0.0PHP ~7.3.0

10.0.0PHP ~8.1

9.0.3PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/89b34307446e726849ef428d22f106c38c89bc7949e4ae50c15ba4d4ff270798?d=identicon)[alezhu](/maintainers/alezhu)

---

Top Contributors

[![alezhu](https://avatars.githubusercontent.com/u/1639172?v=4)](https://github.com/alezhu "alezhu (34 commits)")

---

Tags

phpphpunitphpunit-extensionphpunit-assertions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alezhu-phpunit-array-contains-asserts/health.svg)

```
[![Health](https://phpackages.com/badges/alezhu-phpunit-array-contains-asserts/health.svg)](https://phpackages.com/packages/alezhu-phpunit-array-contains-asserts)
```

###  Alternatives

[phrozenbyte/phpunit-array-asserts

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

1055.0k1](/packages/phrozenbyte-phpunit-array-asserts)[nunomaduro/mock-final-classes

Allows mocking of final methods and classes in PHP.

113854.3k23](/packages/nunomaduro-mock-final-classes)[hot/phpunit-runner

The lib allows to watch phpunit tests

3066.9k4](/packages/hot-phpunit-runner)[derptest/phpmachinist

Testing object factory for PHP

3636.9k1](/packages/derptest-phpmachinist)[robiningelbrecht/phpunit-coverage-tools

PHPUnit coverage tools

1783.0k34](/packages/robiningelbrecht-phpunit-coverage-tools)[cable8mm/water-melon

Water Melon is simple melon.com api sdk for php

182.7k](/packages/cable8mm-water-melon)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
