PHPackages                             codeception/domain-assert - 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. codeception/domain-assert

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

codeception/domain-assert
=========================

Custom assertions for PHPUnit and Codeception

1.0.1(6y ago)22143.3k↓26.1%22MITPHPCI failing

Since Nov 12Pushed 2y agoCompare

[ Source](https://github.com/Codeception/DomainAssert)[ Packagist](https://packagist.org/packages/codeception/domain-assert)[ Docs](http://codeception.com/)[ RSS](/packages/codeception-domain-assert/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (5)Used By (2)

Domain Assertions
=================

[](#domain-assertions)

Assertion library for [PHPUnit](https://phpunit.de/) or [Codeception](http://codeception.com/) powered by [Symfony Expression Language](https://symfony.com/doc/current/components/expression_language.html).

Pitch
-----

[](#pitch)

This tiny library helps you to create domain-specific assertions in tests:

Instead of:

```
$this->assertTrue($user->isValid(), 'user is valid');
```

use

```
$this->assertUserIsValid($user);
```

### Why?

[](#why)

See how test fails in first example:

```
user is valid
Failed asserting that false is true.
```

And how test fails in second example:

```
Failed asserting that `user.isValid()`.
[user]: User Object &000000005689696e000000004066036e (
    'role' => 'guest'
)
```

What makes more sense to you? In second example you get the business logic behind the assertion as well as values passed into it. That's why if you have business logic in your project `domain-assert` is your choice.

How To Use It
-------------

[](#how-to-use-it)

Install this package:

```
composer require codeception/domain-assert --dev
```

Create a **trait** with a custom assertion. *We recommend using traits as you can reuse them accross different test cases.*

```
use Codeception\DomainRule;

trait CustomAssertion
{
    public function assertValidUser(User $user)
    {
        $this->assertThat(
            ['user' => $user],
            new DomainRule('user and user.isValid()')
        );
    }
}
```

*In this example we check that `$user` exists and `$user->isValid()` return true;*

That's all! Now inject this trait to TestCases and use it.

```
class UserTest extends \PHPUnit\Framework\TestCase
{
    use CustomAssertion;
}
```

Defining Business Rules
-----------------------

[](#defining-business-rules)

This library uses [Expression Language](https://symfony.com/doc/current/components/expression_language.html) to define domain rules for assertions.

Let's define a rule to check if we have enough products in the stock:

```
stock and product.getStock() == stock and product.getAmount() > amount

```

We have 3 items here: `product`, `stock`, and `amount` which is a number of items we need. Let's create assertion according to this rule:

```
public function assertEnoughProductsInStock(Stock stock, Product product, amount)
{
    $this->assertThat([
            'product' => $product,
            'stock' => $stock,
            'amount' => $amount
        ],
        new DomainRule('stock and product.getStock() == stock and product.getAmount() > amount')
    );
}
```

Now it can be used inside your tests:

```
$product = new Product('iPhone');
$stock->addProduct($product);
$stock->addProduct($product);
$stock->addProduct($product);
$this->assertEnoughProductsInStock($stock, $product, 2);
```

Advanced Concepts
-----------------

[](#advanced-concepts)

- Instead of `$this->assertThat` you can call static version of this method: `PHPUnit\Framework\Assert::assertThat`
- `Codeception\DomainRule` extends `PHPUnit\Framework\Constraint`.
- `Codeception\DomainRule` uses `Symfony\Component\ExpressionLanguage\ExpressionLanguage`
- [Expression Language can be extended](https://symfony.com/doc/current/components/expression_language/extending.html) by calling `$domainRule->getLanguage()`
- `assertThat` can receive first parameter as scalar value. In this case it will be treated as `expected` inside an expression:

```
public function assertIsGreaterThanMinimal()
{
    $this->assertThat(
        $minimalPrice,
        new DomainRule('expected > 1000')
    );
}
```

License
-------

[](#license)

Open-source software licensed under the [MIT](https://github.com/Codeception/DomainAssert/blob/master/LICENSE) License. © Codeception PHP Testing Framework

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~433 days

Total

3

Last Release

2244d ago

Major Versions

0.1.0 → 1.0.02019-08-16

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/97648?v=4)[davert](/maintainers/davert)[@davert](https://github.com/davert)

---

Top Contributors

[![Naktibalda](https://avatars.githubusercontent.com/u/395992?v=4)](https://github.com/Naktibalda "Naktibalda (6 commits)")[![DavertMik](https://avatars.githubusercontent.com/u/220264?v=4)](https://github.com/DavertMik "DavertMik (4 commits)")[![tuffz](https://avatars.githubusercontent.com/u/2077427?v=4)](https://github.com/tuffz "tuffz (3 commits)")

---

Tags

unit testingBDDTDDassertions

### Embed Badge

![Health badge](/badges/codeception-domain-assert/health.svg)

```
[![Health](https://phpackages.com/badges/codeception-domain-assert/health.svg)](https://phpackages.com/packages/codeception-domain-assert)
```

###  Alternatives

[codeception/codeception

All-in-one PHP Testing Framework

4.9k86.2M2.9k](/packages/codeception-codeception)[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)[orchestra/testbench-core

Testing Helper for Laravel Development

27043.7M310](/packages/orchestra-testbench-core)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

36918.1M98](/packages/php-mock-php-mock)

PHPackages © 2026

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