{"record":{"id":"fee0025515b3149b","repo":"sebastianbergmann/phpunit","slug":"failed-asserting-that-exception-with-message-match","errorCode":null,"errorMessage":"Failed asserting that exception with message matching \"%s\" is thrown","messagePattern":"Failed asserting that exception with message matching \"(.+?)\" is thrown","errorType":"exception","errorClass":"AssertionFailedError","httpStatus":null,"severity":"error","filePath":"src/Framework/TestCase/ExceptionExpectation.php","lineNumber":163,"sourceCode":"        if ($this->expectedException !== null) {\n            Assert::assertThat(\n                null,\n                new ExceptionConstraint($this->expectedException),\n            );\n        } elseif ($this->expectedMessageConstraint !== null) {\n            $test->addToAssertionCount(1);\n\n            throw new AssertionFailedError(\n                sprintf(\n                    'Failed asserting that exception with message %s \"%s\" is thrown',\n                    $this->expectedMessageConstraint instanceof ExceptionMessageIs ? 'is' : 'containing',\n                    $this->expectedMessage,\n                ),\n            );\n        } elseif ($this->expectedMessageRegularExpression !== null) {\n            $test->addToAssertionCount(1);\n\n            throw new AssertionFailedError(\n                sprintf(\n                    'Failed asserting that exception with message matching \"%s\" is thrown',\n                    $this->expectedMessageRegularExpression,\n                ),\n            );\n        } elseif ($this->expectedCode !== null) {\n            $test->addToAssertionCount(1);\n\n            throw new AssertionFailedError(\n                sprintf(\n                    'Failed asserting that exception with code \"%s\" is thrown',\n                    $this->expectedCode,\n                ),\n            );\n        }\n    }\n}\n","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/TestCase/ExceptionExpectation.php#L145-L181","documentation":"Thrown by ExceptionExpectation::assertWasRaised() when expectExceptionMessageMatches() set a regular expression expectation but no exception was raised during the test. Since assertWasRaised() only runs when the test body completed without throwing, there is no message to match and PHPUnit reports the failed regex expectation.","triggerScenarios":"Calling $this->expectExceptionMessageMatches('/.../') while the code under test completes without throwing any exception.","commonSituations":"Behavior changed so the previous error no longer occurs; the test exercises the happy path by mistake; the failing branch is behind a condition the test does not satisfy.","solutions":["Make the test drive the code into the branch that throws","Update or remove the expectation if the code intentionally no longer throws"],"exampleFix":"// before\n$this->expectExceptionMessageMatches('/status code \\d+/');\n$client->request('GET', '/health'); // returns 200, no exception\n\n// after\n$this->expectExceptionMessageMatches('/status code \\d+/');\n$client->request('GET', '/missing');","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Drive the test into the branch that throws before relying on expectExceptionMessageMatches()","Re-check regex expectations when error message wording changes between versions"],"tags":["phpunit","exception-assertion","regex","expected-exception"],"backgroundTag":"expected-exception-regex-not-thrown","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}