Improved API for exception message expectations · Issue #6559 · sebastianbergmann/phpunit · GitHub
Skip to content

Improved API for exception message expectations #6559

Closed

Description

@sebastianbergmann

expectExceptionMessage() uses str_contains() to verify the exception message. This means that expectExceptionMessage('foo') also passes when the actual exception message is 'foobar'. There is currently no way to assert that the exception message is exactly a given string.

Introduce expectExceptionMessageIs() that uses === instead of str_contains() for exception message verification.

Additionally, introduce expectExceptionMessageIsOrContains() as an explicitly-named replacement for expectExceptionMessage() that preserves the current str_contains() behavior.

Motivated by #6425.

Activity

  1. added this to the PHPUnit 13.2 milestone on Apr 3, 2026
  2. changed the title [-]`expectExceptionMessageIs()` for exact-match exception message expectations[/-] [+]Improved API for exception message expectations[/+] on Apr 3, 2026
  3. added a commit that references this issue on Apr 3, 2026
    8987840
  4. locked as resolved and limited conversation to collaborators on May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature/assertionIssues related to assertions and expectationstype/enhancementA new idea that should be implemented

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions