{"record":{"id":"c6764a2f2a8c4816","repo":"sebastianbergmann/phpunit","slug":"e-user-error-was-triggered","errorCode":null,"errorMessage":"E_USER_ERROR was triggered","messagePattern":"E_USER_ERROR was triggered","errorType":"exception","errorClass":"ErrorException","httpStatus":null,"severity":"error","filePath":"src/Runner/ErrorHandler.php","lineNumber":310,"sourceCode":"                    $ignoredByBaseline,\n                    $ignoredByTest,\n                    $this->deprecationIgnoredByFilter($errorString, $errorFile, $errorLine, $trigger),\n                    $trigger,\n                    $this->stackTrace($errorFile, $errorLine),\n                );\n\n                break;\n\n            case E_USER_ERROR:\n                Event\\Facade::emitter()->testTriggeredError(\n                    $test,\n                    $errorString,\n                    $errorFile,\n                    $errorLine,\n                    $suppressed,\n                );\n\n                throw new ErrorException('E_USER_ERROR was triggered');\n\n                /**\n                 * No other error type that can be handled by a user-defined\n                 * error handler is raised by PHP 8.\n                 */\n                // @codeCoverageIgnoreStart\n            default:\n                return $handledByPreviousErrorHandler;\n                // @codeCoverageIgnoreEnd\n        }\n\n        return $handledByPreviousErrorHandler;\n    }\n\n    public function handleNonTestCaseIssue(int $errorNumber, string $errorString, string $errorFile, int $errorLine): true\n    {\n        /**\n         * A previously registered error handler may delegate an error that is being","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Runner/ErrorHandler.php#L292-L328","documentation":"PHPUnit installs its own error handler while a test runs. When code executed inside that test calls trigger_error() with E_USER_ERROR severity, the handler emits a Test\\ErrorTriggered event and then throws PHPUnit\\Runner\\ErrorException('E_USER_ERROR was triggered') so the test is marked as errored. Lower severities (notice, warning, deprecation) are only recorded; E_USER_ERROR is the only user-raised level converted into an exception, because PHP treats it as an abort-level error.","triggerScenarios":"Test code, or production code the test invokes, executes trigger_error($message, E_USER_ERROR) (E_USER_ERROR is also the default severity of trigger_error(), so a bare trigger_error('msg') in PHP 8 hits this path). Typical sources: legacy libraries that signal fatal conditions via user errors instead of exceptions, or old hand-rolled assertion/abort helpers.","commonSituations":"Writing tests around legacy PHP code that used trigger_error(E_USER_ERROR) instead of throwing; a dependency raising E_USER_ERROR deep inside a code path under test; porting old test suites whose helpers abort via user errors.","solutions":["Fix the code under test to throw an exception instead of calling trigger_error(..., E_USER_ERROR).","If the error is expected in this test, declare it: $this->expectException(\\PHPUnit\\Runner\\ErrorException::class); before the triggering call.","If the call must not abort the test, wrap it in a try/catch for \\PHPUnit\\Runner\\ErrorException inside the test and assert on state afterwards.","Downgrade the severity to E_USER_WARNING or E_USER_DEPRECATED when abort semantics are not required; PHPUnit then records the issue instead of throwing."],"exampleFix":"// before\npublic function testLegacyAbort(): void\n{\n    LegacyLogger::fail('disk full'); // calls trigger_error(..., E_USER_ERROR)\n    self::assertTrue(true);\n}\n\n// after\npublic function testLegacyAbort(): void\n{\n    $this->expectException(\\PHPUnit\\Runner\\ErrorException::class);\n    LegacyLogger::fail('disk full');\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// inside a PHPUnit test\ntry {\n    LegacyCode::run(); // may call trigger_error(..., E_USER_ERROR)\n    self::assertTrue($expectedState);\n} catch (\\PHPUnit\\Runner\\ErrorException $e) {\n    // reached only when E_USER_ERROR was raised during the call\n    self::assertSame('E_USER_ERROR was triggered', $e->getMessage());\n}","preventionTips":["Prefer exceptions over trigger_error(E_USER_ERROR) in code you own; PHPUnit converts that severity into an exception.","Pair any test of intentional E_USER_ERROR aborts with expectException(\\PHPUnit\\Runner\\ErrorException::class).","Grep for 'trigger_error(' in dependencies before writing tests around legacy components to know what to expect."],"tags":["phpunit","php","trigger-error","e-user-error","error-handling"],"backgroundTag":"trigger-error-converted-to-exception","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}