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

Phpunit Assert Exception Laravel Package

vladahejda/phpunit-assert-exception

A PHPUnit helper that adds an assertException() assertion for testing exceptions thrown inside callbacks. Specify the expected exception class, message, and code in one concise call, reducing boilerplate and making exception tests clearer in PHP projects.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Test-strategy standardisation. Decide whether failure-path tests in our Laravel services are written in a consistent arrange-act-assert style (assert after the call returns) versus PHPUnit's native expectException*(), which forces expectations to be declared before the code under test runs.
  • API and domain error-contract coverage. This trait is a natural fit for asserting what a service layer throws: exception class, message, code, and custom exception hierarchy — the contract our HTTP error responses and Laravel exception handler mappings depend on. That directly supports roadmap work on public API error semantics and versioned error payloads.
  • Build vs. buy vs. write-in-house. The decision is not "adopt a framework" but "adopt a ~single-file trait" versus writing an equivalent 30–50 line internal test helper. The package is a candidate to vendor or fork, which makes the buy-vs-build calculus cheap and reversible.
  • Quality-gate investment. Informs whether we spend engineering time on test ergonomics (readability, multiple assertions per thrown exception, asserting on the exception object itself) as part of a testing-modernisation or PHPUnit/Laravel upgrade workstream.
  • Use cases to scope: multiple/thrown-in-sequence exception assertions, asserting no-throw paths in one uniform API, tests for queued jobs and console commands that fail via exceptions, and static-analysis-friendly tests where try/catch + assertInstanceOf is verbose.

When to Consider This Package

Consider it when:

  • The codebase is PHPUnit-based and has a meaningful number of failure-path tests (validation, domain rules, integrations) where the act-then-assert flow reads better than expectException.
  • You need to assert more than one property of a thrown throwable (type + message + code + previous exception) — the native API makes this awkward.
  • You have exception inheritance/hierarchies and want explicit, readable assertions on which concrete class was thrown.
  • You want a dev-only, zero-runtime-dependency addition; the blast radius is limited to the test suite and can be pinned or vendored.
  • You are on a Pest-free, PHPUnit-first test suite (Pest already offers throws() ergonomics).

Look elsewhere (or write it yourself) when:

  • The team already uses Pest, or PHPUnit's newer expectException* / ExpectException object APIs cover your cases.
  • Your organisation requires actively maintained, community-backed dependencies — 18 stars and 0 dependents is a genuine maintenance-risk signal, so treat it as a snippet to vendor rather than a long-term dependency.
  • Test suites are small or exception testing is incidental; the adoption cost isn't worth the ergonomic gain.
  • PHPUnit version compatibility is unverified against your pinned major — confirm before planning the upgrade path.
  • You need mocking/stubbing of thrown exceptions or broader test-double tooling (that's a different category of package).

How to Pitch It (Stakeholders)

For executives (1 minute): This is a small, free, MIT-licensed developer-only helper for our PHP test suite. It doesn't ship to production, adds no runtime dependency, and carries no customer-facing risk. It makes tests around error and failure paths easier to read and harder to write incorrectly, which is exactly the area where bugs escape to customers. Cost is effectively zero; the main caveat is that it's a low-popularity package, so we'd vendor a copy into our repo and own it ourselves. Recommendation: a low-risk, low-cost experiment on one service, not a platform-wide mandate.

For engineering leads: A single trait that enables post-hoc assertions on thrown throwables — run the code, then assert on the exception class, message, and code. It replaces the expectException-before-the-call pattern and the verbose try/catch + assertInstanceOf alternative, and it composes with existing PHPUnit test cases. There are no runtime dependencies and the surface area is tiny, so we can vendor it, adapt it, or drop it with a one-line change. Decision needed: approve a one-sprint pilot on a single exception-heavy service, measure readability and review feedback, and revisit whether to promote it to the shared test-utilities package or replace it with ~40 lines of in-house code.

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.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package