PHPackages                             zrnik/phpunit-exceptions - 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. zrnik/phpunit-exceptions

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

zrnik/phpunit-exceptions
========================

Trait for easier exception testing in PHPUnit.

v0.1.0(2y ago)02.4k↑721.9%3MITPHPPHP ^8

Since Apr 19Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Zrnik/PHPUnit-Exceptions)[ Packagist](https://packagist.org/packages/zrnik/phpunit-exceptions)[ RSS](/packages/zrnik-phpunit-exceptions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (3)

PHPUnit Exceptions
==================

[](#phpunit-exceptions)

Trait for easier exception testing in [PHPUnit](https://github.com/sebastianbergmann/phpunit).

Requirements
------------

[](#requirements)

```
{
    "php": "^8",
    "phpunit/phpunit": "^9|^10|^11"
}

```

Usage:
------

[](#usage)

Add a trait `use Zrnik\PHPUnit\Exceptions;` to your test case. Then the testing assertions are available.

```
use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    use \Zrnik\PHPUnit\Exceptions; // add this trait to your TestCase

    public function textExample(): void
    {
        $exampleObject = new ExampleObject();

        $this->assertExceptionThrown(
            NotInRangeException::class, // Expected Exception Type

            // Closure running the code we expect to get an exception from.
            function () use ($exampleObject) {
                $exampleObject->assertRange(0);
            }
        );

        $this->assertNoExceptionThrown(
            function () use ($exampleObject) {
                $exampleObject->assertRange(1);
                $exampleObject->assertRange(10);
            }
        );

        $this->assertExceptionThrown(
            NotInRangeException::class,
            function () use ($exampleObject) {
                $exampleObject->assertRange(11);
            }
        );
    }
}
```

Why
---

[](#why)

I had problem with default `expectException`. The problem was creating unnecessary amount of methods or using try/catch blocks to check for exceptions. All exceptions are available in the [./tests/AssertExceptionTest.php](./tests/AssertExceptionTest.php) file.

**Note:** *Maybe, im just bad at testing. It's **totally** possible...*

### Example 1.: Using too many methods...

[](#example-1-using-too-many-methods)

```
use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    public function test_ExpectException_First(): void
    {
        $exampleObject = new ExampleObject();
        $this->expectException(NotInRangeException::class);
        $exampleObject->assertRange(0);
        //The execution ends here, the method will not continue,
        // after first exception thrown, so I need to create
        // method for every exception tested...
    }

    public function test_ExpectException_Second(): void
    {
        $exampleObject = new ExampleObject();
        $this->expectException(NotInRangeException::class);
        $exampleObject->assertRange(11);
    }

    public function test_OK_Values(): void
    {
        $exampleObject = new ExampleObject();

        $exampleObject->assertRange(1);
        $exampleObject->assertRange(10);

        $this->addToAssertionCount(2); // Yey! Not thrown!
    }
}
```

### Example 2.: Using try/catch block...

[](#example-2-using-trycatch-block)

```
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    public function test_TryCatch(): void
    {
        $exampleObject = new ExampleObject();

        try {
            $exampleObject->assertRange(0);
            // I don't want to write so long error text everytime I am checking for exceptions!
            throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
        } catch (NotInRangeException $ex) {
            $this->addToAssertionCount(1); // Yey! Thrown!
        }

        $exampleObject->assertRange(1);
        $exampleObject->assertRange(10);
        $this->addToAssertionCount(2); // Yey! Not thrown!

        try {
            $exampleObject->assertRange(11);
            throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
        } catch (NotInRangeException $ex) {
            $this->addToAssertionCount(1); // Yey! Thrown!
        }

    }
}
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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 ~211 days

Recently: every ~226 days

Total

6

Last Release

790d ago

PHP version history (2 changes)v0.0.1PHP &gt;=7.4|&gt;=8.0

v0.1.0PHP ^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/2ef2b2321754ec75f086e19c9d2ad58a349217a4fe0a2b8944299173081167dd?d=identicon)[zrnik](/maintainers/zrnik)

---

Top Contributors

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

---

Tags

phpphpunit

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zrnik-phpunit-exceptions/health.svg)

```
[![Health](https://phpackages.com/badges/zrnik-phpunit-exceptions/health.svg)](https://phpackages.com/packages/zrnik-phpunit-exceptions)
```

###  Alternatives

[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.0k](/packages/orchestra-testbench)[timacdonald/log-fake

A drop in fake logger for testing with the Laravel framework.

4235.9M54](/packages/timacdonald-log-fake)[jasonmccreary/laravel-test-assertions

A set of helpful assertions when testing Laravel applications.

3513.9M31](/packages/jasonmccreary-laravel-test-assertions)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[typo3/testing-framework

The TYPO3 testing framework provides base classes for unit, functional and acceptance testing.

675.0M772](/packages/typo3-testing-framework)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

76460.0k15](/packages/robiningelbrecht-phpunit-pretty-print)

PHPackages © 2026

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