MicroUnit

← Back to Home

Assertions

MicroUnit provides several assertion classes.
Each assertion method is listed below with its signature, a concise explanation, and usage examples.
Most assertion methods can be called either via an assertion chain (e.g., AssertSingle::begin($value)->equals(...)) or statically (e.g., Assert::equals($value, $expected)).
Note: Static calls are not available for AssertMock methods due to additional setup required for mock tracking.


Table of Contents


Static Assert Methods

Most assertion methods can be called statically via the Assert class. These static methods get used under the hood by AssertSingle, AssertArray and AssertNumeric (Fluent API).

List of Available Static Methods

Note:
Static assert methods are not available for AssertMock because mock assertions require additional context and tracking.

Fluent Assertions

No matter which fluent assertion class you are using to start the fluent assertion process you call AssertClass::begin($value) (e.g AssertSingle::begin('something')).

AssertSingle

Defines different assertion methods for single values (bool, string, int, float, etc.).

equals(mixed $expected): self

Asserts that the value is equal to $expected (==).

Example:

AssertSingle::begin($value)->equals(42);

notEquals(mixed $unexpected): self

Asserts that the value is not equal to $unexpected (==).

Example:

AssertSingle::begin($value)->notEquals(0);

exact(mixed $expected): self

Asserts that the value is exactly equal to $expected (===).

Example:

AssertSingle::begin($value)->exact('foo');

notExact(mixed $unexpected): self

Asserts that the value is not exactly equal to $unexpected (===).

Example:

AssertSingle::begin($value)->notExact(false);

instanceOf(string $expectedInstance): self

Asserts that the value is an instance of the given class/interface.

Example:

AssertSingle::begin($object)->instanceOf(DateTime::class);

isTrue(): self

Asserts that the value is true.

Example:

AssertSingle::begin($flag)->isTrue();

isFalse(): self

Asserts that the value is false.

Example:

AssertSingle::begin($flag)->isFalse();

isNull(): self

Asserts that the value is null.

Example:

AssertSingle::begin($value)->isNull();

notNull(): self

Asserts that the value is not null.

Example:

AssertSingle::begin($value)->notNull();

AssertNumeric

Defines assertion methods for numeric values.

exact(int|float $expected): self

Asserts that the value is exactly equal to $expected (===).

Example:

AssertNumeric::begin($value)->exact(42);

notExact(int|float $unexpected): self

Asserts that the value is not exactly equal to $unexpected (===).

Example:

AssertNumeric::begin($value)->notExact(36);

isGreaterThan(int|float $min): self

Asserts that the value is greater than $min.

Example:

AssertNumeric::begin($number)->isGreaterThan(10);

isLessThan(int|float $max): self

Asserts that the value is less than $max.

Example:

AssertNumeric::begin($number)->isLessThan(100);

isBetween(int|float $min, int|float $max, bool $inclusive = true)

Asserts that the value is between $min and $max.

Example:

AssertNumeric::begin($number)->isBetween(1, 10);

AssertArray

Defines assertion methods for arrays.

equals(array $expected): self

Asserts that the array is equal to $expected (==).

Example:

AssertArray::begin($array)->equals(['a', 'b']);

notEquals(array $unexpected): self

Asserts that the array is not equal to $unexpected (==).

Example:

AssertArray::begin($array)->notEquals([]);

exact(array $expected): self

Asserts that the array is exactly equal to $expected (===).

Example:

AssertArray::begin($array)->exact(['a' => 1, 'b' => 2]);

notExact(array $unexpected): self

Asserts that the array is not exactly equal to $unexpected (===).

Example:

AssertArray::begin($array)->notExact(['a' => 2]);

empty(): self

Asserts that the array is empty.

Example:

AssertArray::begin($array)->empty();

notEmpty(): self

Asserts that the array is not empty.

Example:

AssertArray::begin($array)->notEmpty();

contains(mixed $element, bool $shouldUseStrict = true): self

Asserts that the array contains the given element. If $shouldUseStrict is true the type is checked as well.

Example:

AssertArray::begin($array)->contains('foo');

countEquals(int $count): self

Asserts that the array has exactly $count elements.

Example:

AssertArray::begin($array)->countEquals(3);

hasKey(mixed $key): self

Asserts that the array has the specified key.

Example:

AssertArray::begin($array)->hasKey('id');

notHasKey(mixed $key): self

Asserts that the array does not have the specified key.

Example:

AssertArray::begin($array)->notHasKey('password');

keysEqual(array $expectedKeys): self

Asserts that the array’s keys are equal to $expectedKeys.

Example:

AssertArray::begin($array)->keysEqual(['id', 'name']);

containsOnly(array $allowedValues): self

Asserts that the array contains only the specified values.

Example:

AssertArray::begin($array)->containsOnly([1, 2, 3]);

AssertMock

Defines assertion methods for mock objects.

Note: Static calls are not available for AssertMock methods. Use AssertMock::begin($mock) to initiate the assertion via fluent api.


isCalledTimes(string $method, int $count): self

Asserts that the specified method was called exactly $count times.

Example:

AssertMock::begin($mock)->isCalledTimes('foo', 2);

isCalledOnce(string $method): self

Asserts that the specified method was called exactly once.

Example:

AssertMock::begin($mock)->isCalledOnce('foo');

isNotCalled(string $method): self

Asserts that the specified method was never called.

Example:

AssertMock::begin($mock)->isNotCalled('foo');

isCalledAtLeast(string $method, int $minCallCount): self

Asserts that the specified method was called at least $minCallCount times.

Example:

AssertMock::begin($mock)->isCalledAtLeast('foo', 3);

isCalledMoreThan(string $method, int $minCallCount): self

Asserts that the specified method was called more than $minCallCount times.

Example:

AssertMock::begin($mock)->isCalledMoreThan('foo', 2);

isCalledAtMost(string $method, int $maxCallCount): self

Asserts that the specified method was called at most $maxCallCount times.

Example:

AssertMock::begin($mock)->isCalledAtMost('foo', 5);

isCalledLessThan(string $method, int $maxCallCount): self

Asserts that the specified method was called less than $maxCallCount times.

Example:

AssertMock::begin($mock)->isCalledLessThan('foo', 3);

isCalledWith(string $method, array $args, bool $showActualMethodCallsOnError = true): self

Asserts that the method was called at least once with the given arguments.

If $showActualMethodCallsOnError is set to false the actual method calls along with their arguments are not going to be printed in the erorr message. This saves a bit of time when executing the test.

Example:

AssertMock::begin($mock)->isCalledWith('bar', [1, 2]);

isCalledWithOnSpecificCall(string $method, array $args, int $onCall): self

Asserts that the method was called with the given arguments on a specific call number (1-based index).

Example:

AssertMock::begin($mock)->isCalledWithOnSpecificCall('foo', ['arg'], 2);

isOnlyCalledWith(string $method, array $expectedArgs, bool $showActualMethodCallsOnError = true): self Not Yet Released

This method will be available in the next release.

Asserts that the method was only called with the given arguments.

If $showActualMethodCallsOnError is set to false the actual method calls along with their arguments are not going to be printed in the erorr message. This saves a bit of time when executing the test.

Example:

AssertMock::begin($mock)->isOnlyCalledWith('bar', [1, 2]);

isOnlyCalledWithMatchingArgs(string $method, callable $matcher, bool $showActualMethodCallsOnError = true): self Not Yet Released

This method will be available in the next release.

Asserts that the method was only called with arguments where $matcher returns true.

If $showActualMethodCallsOnError is set to false the actual method calls along with their arguments are not going to be printed in the erorr message. This saves a bit of time when executing the test.

Example:

AssertMock::begin($mock)->isOnlyCalledWithMatchingArgs('bar', function (array $args): bool {
                    $arg1 = $args[0];
                    $arg2 = $args[1];

                    if ($arg1 !== 'foo') {
                        return false;
                    }

                    if ($arg2 !== 15 || $arg2 !== 7) {
                        return false;
                    }

                    return true;
                });

isCalledWithMatchingOnSpecificCall(string $method, callable $matcher, int $onCall): self Not Yet Released

This method will be available in the next release.

Asserts that the method was called with arguments where $matcher returns true on a specifc call.

Example:

AssertMock::begin($mock)->isOnlyCalledWithMatchingArgs('bar', function (array $args): bool {
                    $arg1 = $args[0];
                    $arg2 = $args[1];

                    if ($arg1 !== 'foo') {
                        return false;
                    }

                    return true;
                }, 2) // -> Only relevant when method is called the second time !

isCalledOn(string $method, int $callNumber): self Not Yet Released

This method will be available in the next release.

Checks that the method was called at a specific point across all method calls made on the mock.

AssertMock::begin($mock)->isCalledOn('bar', 2) // 'bar' has to be the second method that get's called on the mock

checkMethod(string $method, callable $assertMethod): self

Provides a fluent way to group method assertions using a callback that receives an AssertMockMethod instance. See AssertMockMethod for details.

Example:

AssertMock::begin($mock)->checkMethod('foo', function ($assert) {
    $assert->isCalledOnce()->isCalledWith(['x']);
});

AssertMockMethod

Helper class for scoped assertions on a specific mock method, used via $assertMock->checkMethod(). See checkMethod description for details.


isCalledTimes(int $expectedCallCount): self

Asserts that the method was called exactly $expectedCallCount times.


isCalledOnce(): self

Asserts that the method was called exactly once.


isNotCalled(): self

Asserts that the method was never called.


isCalledAtLeast(int $minCallCount): self

Asserts that the method was called at least $minCallCount times.


isCalledMoreThan(int $minCallCount): self

Asserts that the method was called more than $minCallCount times.


isCalledAtMost(int $maxCallCount): self

Asserts that the method was called at most $maxCallCount times.


isCalledLessThan(int $maxCallCount): self

Asserts that the method was called less than $maxCallCount times.


isCalledWith(array $expectedArgs, bool $showActualMethodCallsOnError = true): self

Asserts that the method was called at least once with the given arguments.

if $showActualMethodCallsOnError is set to false the actual method calls and their arguments are not going to be printed in the error message. This saves a bit of time when executing the test.


isCalledWithOnSpecificCall(array $expectedArgs, int $onCall): self

Asserts that the method was called with the given arguments on a specific call number.

isOnlyCalledWith(array $expectedArgs, bool $showActualMethodCallsOnError = true): self Not Yet Released

This method will be available in the next release.

Asserts that the method was only called with the given arguments.

If $showActualMethodCallsOnError is set to false the actual method calls along with their arguments are not going to be printed in the erorr message. This saves a bit of time when executing the test.

isOnlyCalledWithMatchingArgs(callable $matcher, bool $showActualMethodCallsOnError = true): self Not Yet Released

This method will be available in the next release.

Asserts that the method was only called with arguments where $matcher returns true.

If $showActualMethodCallsOnError is set to false the actual method calls along with their arguments are not going to be printed in the erorr message. This saves a bit of time when executing the test.

isCalledWithMatchingOnSpecificCall(callable $matcher, int $onCall): self Not Yet Released

This method will be available in the next release.

Asserts that the method was called with arguments where $matcher returns true on a specifc call.

isCalledOn(string $method, int $callNumber): self Not Yet Released

This method will be available in the next release.

Checks that the method was called at a specific point across all method calls made on the mock.


⬆ Back to Top📘 Home