zenstruck/assert
Lightweight PHP assertion helpers for cleaner, safer code. zenstruck/assert provides expressive, fluent assertions and value validation with helpful exception messages, improving readability in libraries and Laravel apps without bringing a full testing framework.
Start by installing the package via Composer:
composer require --dev zenstruck/assert
Then, import and use the Assert class in your tests or application code:
use function Zenstruck\Assert\assert;
assert($user->isActive())->true();
assert($user->getEmail())->email()->notBlank();
assert($items)->count(3);
The README and src/ directory contain the full API reference — begin with assert(), assertNotNull(), and Assert::that() factory methods.
assertEquals(), assertTrue() calls with chainable assertions for intent clarity:
assert($response->getStatusCode())->eq(200);
assert($response->json('data.user'))->hasKey('email')->and()->email();
assert($order->total())->greaterThan(0);
assert($product->stock())->greaterThanOrEqualTo($requestedQuantity);
assert($payload)->isArray()->hasKey('id')->and()->isInt()->greaterThan(0);
AssertionFailedError.use function Zenstruck\Assert\assert; to enable assert(...) without class resolution — avoid naming conflicts with PHP’s built-in assert().assert($value, 'User email must be verified')->email();assert($result, 'Must contain valid product', "Expected product '{$productId}' to exist");
each(), all(), and hasKey() for arrays/objects to validate complex structures.Assertion subclasses for domain-specific checks (e.g., assert()->isStripeId()).How can I help you explore Laravel packages today?