PHPackages                             matthiasnoback/behat-expect-exception - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. matthiasnoback/behat-expect-exception

ActiveLibrary[Testing &amp; Quality](/categories/testing)

matthiasnoback/behat-expect-exception
=====================================

v1.0.0(7mo ago)2389.0k↓19.3%MITPHPPHP ^8.4CI passing

Since Oct 4Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/matthiasnoback/behat-expect-exception)[ Packagist](https://packagist.org/packages/matthiasnoback/behat-expect-exception)[ RSS](/packages/matthiasnoback-behat-expect-exception/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (6)Used By (0)

Installation
------------

[](#installation)

```
composer require --dev matthiasnoback/behat-expect-exception
```

Purpose
-------

[](#purpose)

This library lets you run code in one step definition that is expected to thrown an exception, then in another step definition allows you to verify that the correct exception was caught. Just like with PHPUnit you can compare the type of the caught exception to the expected type, and you can check if the actual exception message contains a given string.

Usage example
-------------

[](#usage-example)

```
use Behat\Behat\Context\Context;
use BehatExpectException\ExpectException;

final class FeatureContext implements Context
{
    // Use this trait in your feature context:
    use ExpectException;

    /**
     * @When I try to make a reservation for :numberOfSeats seats
     */
    public function iTryToMakeAReservation(int $numberOfSeats): void
    {
        /*
         * Catch an exception using $this->shouldFail().
         * If the code in the callable doesn't throw an exception, shouldFail()
         * itself will throw an ExpectedAnException exception.
         */

        $this->shouldFail(
            function () use ($numberOfSeats) {
                // This will throw a CouldNotMakeReservation exception:
                $this->reservationService()->makeReservation($numberOfSeats);
            }
        );
    }

    /**
     * @Then I should see an error message saying: :message
     */
    public function confirmCaughtExceptionMatchesExpectedTypeAndMessage(string $message): void
    {
        $this->assertCaughtExceptionMatches(
            CouldNotMakeReservation::class,
            $message
        );
    }

    /**
     * @When I make a reservation for :numberOfSeats seats
     */
    public function iMakeAReservation(int $numberOfSeats): void
    {
        /*
         * Catch a possible exception using $this->mayFail().
         * If the code in the callable doesn't throw an exception,
         * then it's not a problem. mayFail() doesn't throw an
         * ExpectedAnException exception itself in that case.
         * You can still use assertCaughtExceptionMatches(), but
         * it will throw an ExpectedAnException if no exception was
         * caught.
         */

        $this->mayFail(
            function () use ($numberOfSeats) {
                // This might throw a CouldNotMakeReservation exception:
                $this->reservationService()->makeReservation($numberOfSeats);
            }
        );
    }
}
```

Maintenance
-----------

[](#maintenance)

- Run `composer install` to install project dependencies (requires PHP 8.4 and Composer globally installed)
- Run `composer fix` to fix coding style
- Run `composer test` to run the tests

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance62

Regular maintenance activity

Popularity39

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~726 days

Total

4

Last Release

238d ago

Major Versions

v0.3.0 → v1.0.02025-09-22

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1193078?v=4)[Matthias Noback](/maintainers/matthiasnoback)[@matthiasnoback](https://github.com/matthiasnoback)

---

Top Contributors

[![matthiasnoback](https://avatars.githubusercontent.com/u/1193078?v=4)](https://github.com/matthiasnoback "matthiasnoback (10 commits)")

###  Code Quality

TestsBehat

Static AnalysisRector

Code StyleECS

### Embed Badge

![Health badge](/badges/matthiasnoback-behat-expect-exception/health.svg)

```
[![Health](https://phpackages.com/badges/matthiasnoback-behat-expect-exception/health.svg)](https://phpackages.com/packages/matthiasnoback-behat-expect-exception)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
