framework: PHPUnit warning: Test code or tested code did not remove its own exception handlers

Laravel Version

Any that uses PHPUnit ^10

PHP Version

Any

Database Driver & Version

No response

Description

Seems that, since PHPUnit 10.5.4, running any kind of test suite that boots the Laravel Framework displays the following warning multiple times: Test code or tested code did not remove its own exception handlers.

Steps To Reproduce

laravel new laravel
cd laravel
./vendor/bin/phpunit

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 27 (5 by maintainers)

Most upvoted comments

I never had this issue in 10.x, but I just upgraded from latest 10.x to latest 11.x and 11.0.4 and I’m seeing this on every single test in my project

Putting the below in (what would be) my Tests/TestCase worked in the package I am working on and encountered this in:

public function tearDown(): void
{
    parent::tearDown();

    restore_error_handler();
    restore_exception_handler();
}

We updated to L11 last week and today Dependabot bumped PHPUnit to v11 and these errors starting to appear for all tests:

  • Test code or tested code removed error handlers other than its own
  • Test code or tested code removed exception handlers other than its own

As of now, we can’t find any working solution. This issue points to Symfony thread which is still open and redirects back to #49622. Does anyone have a working solution? @phily245 suggestion didn’t do the trick 😦

seems like its back on latest phpunit version

I can confirm this has reappeared:

  • L11 : 11.2.0
  • PHPUnit : 11.1.1

Looks like it’s been re-introduced.

For those still struggling with this. Adding HandleExceptions::flushState(); seems to do the trick.

In tests/TestsCase.php

Override the createApplication() method and add HandleExceptions::flushState(); before returning $app

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require Application::inferBasePath().'/bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        HandleExceptions::flushState(); // <--- Add this line

        return $app;
    }

It seems like this is back in 11.

Not really. I still see a lot of those warnings even after removing the code above.

I’ve figured it out:

The issue was stemming by the fact that I had a custom PhpUnit bootstrap class I was using, and in it I was doing a createApplication() (i.e. $app->make(Kernel::class)->bootstrap()) to be able to call some artisan commands before/after running the tests.

I simply had to make sure I ran HandleExceptions::flushState(); at the end of each method where I was doing createApplication() in that boostrap class.

Probably what was happening was that if I manually create an application outside of tests, it would add extra error/exception handlers that neither Laravel nor PhpUnit would know about, and then PhpUnit would complain about it in the end.

I’m confused. What am I supposed to do?

I tried the HandleExceptions::flushState(); bit and the tearDown bit as well both together and separately as well, and neither does anything.

There’s nothing in the Laravel docs about what to do here, nor is there anything in the PhpUnit docs. I’m stuck and am not able to upgrade to Laravel 12 this way.

Following the advice above, I added

    public function tearDown(): void
    {
        parent::tearDown();

        restore_error_handler();
        restore_exception_handler();
    }

but I started getting an error:

Test code or tested code removed error handlers other than its own

This code helped me:

    public function tearDown(): void
    {
        parent::tearDown();

        restore_exception_handler();
    }

I’m using

"laravel/framework": "^12.16.0",
"phpunit/phpunit": "^11.5.15",
"nunomaduro/collision": "^8.8.0",

and keep getting the “Test code or tested code removed error handlers other than its own” error for each test. I tried adding this code to createApplication() HandleExceptions ::flushState(); but it doesn’t work unfortunately.

I’m getting the same issue. Here is some more information in case it helps someone:

phpunit.xml

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         bootstrap="tests/bootstrap.php"
         colors="true">

bootstrap.php

// Create application
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
HandleExceptions::flushState();

The HandleExceptions::flushState() (taken from a comment above) seems to do the trick.

ISSUE VERSION PHPUnit 12.0.5

Using the suggested


    public function tearDown(): void
    {
        parent::tearDown();

        restore_error_handler();
        restore_exception_handler();
    }

I now get the opposite error

Test code or tested code removed error handlers other than its own

We have been digging into this issue at work with a new project. My boss found that removing newRelic in the docker container solved resolved the “Test code or tested code removed…” errors. He also mentioned there appears to be a memory leak in PHPUnit 11.3, though that has not yet been confirmed. I can confirm on my instance, removing newRelic from the docker container did resolve issues for me. Tests execute as expected now.