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.


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

AssertSingle

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

equals($expected)

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

Example:

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

notEquals($unexpected)

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

Example:

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

exact($expected)

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

Example:

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

notExact($unexpected)

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

Example:

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

instanceOf($className)

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

Example:

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

isTrue()

Asserts that the value is true.

Example:

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

isFalse()

Asserts that the value is false.

Example:

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

isNull()

Asserts that the value is null.

Example:

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

notNull()

Asserts that the value is not null.

Example:

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

AssertNumeric

Defines assertion methods for numeric values.

exact($expected)

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

Example:

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

notExact($unexpected)

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

Example:

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

isGreaterThan($min)

Asserts that the value is greater than $min.

Example:

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

isLessThan($max)

Asserts that the value is less than $max.

Example:

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

isBetween($min, $max, $inclusive = true)

Asserts that the value is between $min and $max (inclusive by default).

Example:

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

AssertArray

Defines assertion methods for arrays.

equals($expected)

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

Example:

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

notEquals($unexpected)

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

Example:

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

exact($expected)

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

Example:

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

notExact($unexpected)

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

Example:

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

empty()

Asserts that the array is empty.

Example:

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

notEmpty()

Asserts that the array is not empty.

Example:

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

contains($element)

Asserts that the array contains the given element.

Example:

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

countEquals($count)

Asserts that the array has exactly $count elements.

Example:

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

hasKey($key)

Asserts that the array has the specified key.

Example:

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

notHasKey($key)

Asserts that the array does not have the specified key.

Example:

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

keysEqual($expectedKeys)

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

Example:

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

containsOnly($allowedValues)

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.

isCalledTimes($method, $count)

Asserts that the mock method was called $count times.

Example:

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

isCalledWith($method, $args)

Asserts that the mock method was called with the specified arguments.

Example:

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

⬆ Back to Top📘 Home