bovigo/assert
Functional-style assertions for PHP unit tests. Use assertThat(value, predicate[, description]) for readable checks with helpful failure messages. Integrates with PHPUnit via AssertionFailure extending AssertionFailedError. Requires PHP 8.3+.
Start by installing the package as a development dependency:
composer require --dev "bovigo/assert": "^8.1"
đź’ˇ Requirement: Requires PHP 8.3+.
Begin with simple assertions using the functional API. Import core functions in your test files:
use function bovigo\assert\assertThat;
use function bovigo\assert\predicate\equals;
use function bovigo\assert\predicate\isInstanceOf;
assertThat($result, equals(42));
assertThat($object, isInstanceOf(SomeClass::class));
The assertThat() function is your primary entry point—combine it with predicate functions (e.g., equals(), isEmpty(), matches()) to validate values. Failures produce clear, PHPUnit-integrated error messages.
and(), or(), not()) for complex validations:
assertThat($value, isString()->and(matches('/^[A-Z]/')));
isString(), isArray(), isObject()) instead of generic isOfType() to avoid typos and improve readability.assertThat($config, isNotEmpty(), 'Config must not be empty after bootstrapping');
AssertionFailure extending \PHPUnit\Framework\AssertionFailedError—no extra setup needed. Works seamlessly with phpunit/phpunit and tools like phpunit-gtk.contains(), hasKey(), isExistingFile(), etc.:
assertThat($json, isString()->and(matches('/^{"status":"ok"}/')));
assertThat($outputDir, isExistingDirectory());
contains() is case-sensitive for strings. Use equalsIgnoreCase() (if available) or convert case explicitly—note: no equalsIgnoreCase() predicate exists in v8.1.isOfSize() uses iterator_count() on a clone of Traversable—safe for iterators that move internal pointers, but ensure your data source supports cloning.isEmpty() follows PHP’s empty() logic and Countable::count() === 0. Beware: 0, '0', and false are considered empty—use isFalse() for explicit boolean checks.->withDelta() when comparing floats:
assertThat($result, equals(3.14)->withDelta(0.01));
assert() (PHP’s built-in) alongside assertThat()—use use function bovigo\assert\assertThat as ah; or qualify explicitly to prevent conflicts.2025-11-30 release date (likely a typo—verify actual version), check changelog for current features. Prioritize predicates like matchesFormat() for structured string validation (e.g., logging, dates).How can I help you explore Laravel packages today?