sebastianbergmann/phpunit · error · ExpectationFailedException

Expected deprecation with message matching regular expressio

Error message

Expected deprecation with message matching regular expression "%s" was not triggered

What it means

Thrown after the test body when expectDeprecationMatch() was used but no collected user deprecation matches the given regular expression. verifyDeprecationExpectations() runs preg_match() of your pattern against every recorded deprecation and fails when none match (invalid regexes never match because of the @ suppression).

Source

Thrown at src/Framework/TestCase.php:1523

                throw new ExpectationFailedException(
                    sprintf(
                        'Expected deprecation with message "%s" was not triggered',
                        $deprecationExpectation,
                    ),
                );
            }
        }

        foreach ($this->expectedUserDeprecationMessageRegularExpression as $deprecationExpectation) {
            $this->numberOfAssertionsPerformed++;

            $expectedDeprecationTriggered = array_any(
                DeprecationCollector::deprecations(),
                static fn (string $deprecation) => @preg_match($deprecationExpectation, $deprecation) > 0,
            );

            if (!$expectedDeprecationTriggered) {
                throw new ExpectationFailedException(
                    sprintf(
                        'Expected deprecation with message matching regular expression "%s" was not triggered',
                        $deprecationExpectation,
                    ),
                );
            }
        }
    }

    /**
     * @throws Throwable
     */
    private function verifyMockObjects(): void
    {
        $allowsMockObjectsWithoutExpectations = $this->allowsMockObjectsWithoutExpectations();
        $isPhpunitTestSuite                   = str_starts_with($this::class, 'PHPUnit\\');
        $requireSealedMockObjects             = ConfigurationRegistry::get()->requireSealedMockObjects();

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Test the regex against the actual deprecation message (e.g. in a scratch script) and fix the pattern
  2. Make the pattern more permissive (drop exact anchors, escape regex metacharacters that appear literally in the message)
  3. Confirm the deprecated code path runs in this test and in the same process
  4. Use expectDeprecation() with the literal message when no variable parts exist

Example fix

// before
$this->expectDeprecationMatch('/^Using Config/'); // message is 'Config v2 is deprecated'

// after
$this->expectDeprecationMatch('/Config v\d+ is deprecated/');
Defensive patterns

Strategy: validation

Validate before calling

$pattern = '/Config v\d+ is deprecated/';
$actual = 'Config v2 is deprecated';
self::assertSame(1, preg_match($pattern, $actual), 'regex must match the real message');

Prevention

When it happens

Trigger: Calling $this->expectDeprecationMatch('/pattern/') where no triggered E_USER_DEPRECATED message matches the pattern; also when the pattern itself is invalid PCRE and silently matches nothing.

Common situations: Over-anchored or mistyped regexes; delimiters forgotten; deprecation wording changed between library versions; the deprecated path not executed.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/8045e94682800ac8. Report an issue: GitHub.