Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Assert Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • In Tests: Replace verbose assertEquals(), assertTrue() calls with chainable assertions for intent clarity:
    assert($response->getStatusCode())->eq(200);
    assert($response->json('data.user'))->hasKey('email')->and()->email();
    
  • Runtime Validation: Wrap domain logic with assertions for fail-fast behavior:
    assert($order->total())->greaterThan(0);
    assert($product->stock())->greaterThanOrEqualTo($requestedQuantity);
    
  • Fluent Chaining: Combine assertions for complex conditions:
    assert($payload)->isArray()->hasKey('id')->and()->isInt()->greaterThan(0);
    
  • Integration with PHPUnit: Works out-of-the-box — no special setup required; assertions throw AssertionFailedError.

Gotchas and Tips

  • Namespace Tip: Use use function Zenstruck\Assert\assert; to enable assert(...) without class resolution — avoid naming conflicts with PHP’s built-in assert().
  • Debugging: When an assertion fails, the message includes the expected, actual, and description — always provide context via assert($value, 'User email must be verified')->email();
  • Custom Messages: Pass a third argument for dynamic context:
    assert($result, 'Must contain valid product', "Expected product '{$productId}' to exist");
    
  • Exhaustive Testing: Use collection assertions like each(), all(), and hasKey() for arrays/objects to validate complex structures.
  • Extensibility: While the package is small, you can extend with your own Assertion subclasses for domain-specific checks (e.g., assert()->isStripeId()).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table