PHPackages                             automattic/patchwork-redefine-exit - 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. automattic/patchwork-redefine-exit

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

automattic/patchwork-redefine-exit
==================================

Use antecedent/patchwork to redefine `exit` and `die` for more robust PHPUnit testing.

v2.0.9(6mo ago)24.3k↓82.2%12GPL-2.0-or-laterPHPPHP &gt;=7.2CI failing

Since May 22Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/Automattic/patchwork-redefine-exit)[ Packagist](https://packagist.org/packages/automattic/patchwork-redefine-exit)[ RSS](/packages/automattic-patchwork-redefine-exit/feed)WikiDiscussions trunk Synced 1mo ago

READMEChangelogDependencies (4)Versions (15)Used By (2)

automattic/patchwork-redefine-exit
==================================

[](#automatticpatchwork-redefine-exit)

This package uses [antecedent/patchwork](https://github.com/antecedent/patchwork) to redefine `exit` and `die` for more robust PHPUnit testing.

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

[](#installation)

Require using `composer require --dev automattic/patchwork-redefine-exit`.

Configuration
-------------

[](#configuration)

You'll need to configure Patchwork to allow redefining of `exit()` and `die()` by setting `redefinable-internals` in your `patchwork.json`. At minimum that would look like this:

```
{
	"redefinable-internals": [ "exit", "die" ]
}
```

(you can place `patchwork.json` in the same directory as your PHPUnit boostrap file).

You'll also need to configure PHPUnit to load Patchwork and this package, which is normally done by including something like this in your PHPUnit boostrap file.

```
// Require Patchwork.
require_once __DIR__ . '/../vendor/antecedent/patchwork/Patchwork.php';

// Enable Automattic/patchwork-redefine-exit.
\Automattic\RedefineExit::setup();
```

### Use with other testing frameworks

[](#use-with-other-testing-frameworks)

For other testing frameworks, you'll need to use a bootstrap file or similar mechanism to load Patchwork before any code under test is loaded, and call `Automattic\RedefineExit::setup()` before any tests are run.

If the testing framework itself calls `exit` or `die` in a manner that gets caught incorrectly (e.g. to terminate with an appropriate exit code at the end of the test run), you may subclass `Automattic\RedefineExit` and implement the `ignoreExitCall()` method to identify and ignore such calls.

Usage
-----

[](#usage)

If everything is set up correctly, calls to `exit` or `die` outside of PHPUnit itself will throw an instance of `Automattic\RedefineExit\ExitException` instead of exiting. If not caught or expected (using PHPUnit's `expectException()` and related methods), this will cause the test to gracefully fail with a message about the uncaught exception.

The message on the exception will describe how `exit` or `die` was called, and the code will match the code the process would have exited with.

If you catch the exception in your test, you can determine whether it was `exit` or `die` by checking `$ex->getFunction()`, and you can access the actual argument passed with `$ex->getArgument()`.

Caveats
-------

[](#caveats)

In addition to the [limitations of Patchwork itself](https://antecedent.github.io/patchwork/limitations/) around redefining builtin functions, note that Patchwork's `restoreAll()` function should be avoided as it would also restore the exit handlers. To avoid the problems that causes, we normally set up our `exit` and `die` redefinitions with "expiration" handlers that will kill the process (with a helpful message) if `Patchwork\restoreAll()` is called. If you want to restore all other handlers except ours, a replacement function `Automattic\RedefineExit::restoreAll()` is provided. Or, if you'd rather avoid the expiration handlers entirely, call `Automattic\RedefineExit::setupDangerously()` instead of `::setup()`. In this case you may want to call `::setupDangerously()` again every time you call `Patchwork\restoreAll()`.

Security
--------

[](#security)

Need to report a security vulnerability? Go to  or directly to our security bug bounty site .

License
-------

[](#license)

patchwork-redefine-exit is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance80

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~60 days

Total

13

Last Release

180d ago

Major Versions

v1.0.2 → v2.0.02024-11-18

PHP version history (2 changes)v1.0.0PHP &gt;=7.0

v2.0.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c5869ecbb8e0eac7e8b8e0f3cf7bdd8d5fcdc4abc10a72281872c53f8639d44?d=identicon)[automattic](/maintainers/automattic)

![](https://www.gravatar.com/avatar/d48ad8a33091a43b53f295c6a6f25a03339f4e89032b74259d85e14e5317cd2b?d=identicon)[anomie](/maintainers/anomie)

---

Top Contributors

[![anomiex](https://avatars.githubusercontent.com/u/1030580?v=4)](https://github.com/anomiex "anomiex (28 commits)")[![tbradsha](https://avatars.githubusercontent.com/u/32492176?v=4)](https://github.com/tbradsha "tbradsha (22 commits)")[![gmjuhasz](https://avatars.githubusercontent.com/u/36671565?v=4)](https://github.com/gmjuhasz "gmjuhasz (2 commits)")[![manzoorwanijk](https://avatars.githubusercontent.com/u/18226415?v=4)](https://github.com/manzoorwanijk "manzoorwanijk (2 commits)")[![haqadn](https://avatars.githubusercontent.com/u/3737780?v=4)](https://github.com/haqadn "haqadn (1 commits)")[![Initsogar](https://avatars.githubusercontent.com/u/1488641?v=4)](https://github.com/Initsogar "Initsogar (1 commits)")[![robertsreberski](https://avatars.githubusercontent.com/u/8419292?v=4)](https://github.com/robertsreberski "robertsreberski (1 commits)")[![sergeymitr](https://avatars.githubusercontent.com/u/1341249?v=4)](https://github.com/sergeymitr "sergeymitr (1 commits)")[![adamwoodnz](https://avatars.githubusercontent.com/u/1017872?v=4)](https://github.com/adamwoodnz "adamwoodnz (1 commits)")[![zinigor](https://avatars.githubusercontent.com/u/374293?v=4)](https://github.com/zinigor "zinigor (1 commits)")[![allilevine](https://avatars.githubusercontent.com/u/1689238?v=4)](https://github.com/allilevine "allilevine (1 commits)")[![CGastrell](https://avatars.githubusercontent.com/u/157240?v=4)](https://github.com/CGastrell "CGastrell (1 commits)")[![chihsuan](https://avatars.githubusercontent.com/u/4344253?v=4)](https://github.com/chihsuan "chihsuan (1 commits)")

---

Tags

testingphpunitredefinitionpatchworkexitdie

### Embed Badge

![Health badge](/badges/automattic-patchwork-redefine-exit/health.svg)

```
[![Health](https://phpackages.com/badges/automattic-patchwork-redefine-exit/health.svg)](https://phpackages.com/packages/automattic-patchwork-redefine-exit)
```

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k910.7M134.3k](/packages/phpunit-phpunit)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33412.5M348](/packages/brain-monkey)[codedungeon/phpunit-result-printer

PHPUnit Pretty Result Printer

1.2k8.8M396](/packages/codedungeon-phpunit-result-printer)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69417.9M511](/packages/spatie-phpunit-snapshot-assertions)[dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

56426.3M456](/packages/dg-bypass-finals)

PHPackages © 2026

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