Weave Code
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport