Skip to content

Allow expectOutputString() and expectOutputRegex() to be combined and repeated - #6701

Closed
sebastianbergmann wants to merge 1 commit into
mainfrom
multiple-output-expectations
Closed

sebastianbergmann wants to merge 1 commit into
mainfrom
multiple-output-expectations

Conversation

@sebastianbergmann

Copy link
Copy Markdown
Owner

Previously, the two methods for configuring an expectation on output silently replaced one another:

  • Calling expectOutputString() (or expectOutputRegex()) more than once kept only the last value
  • When both a string and a regular expression were configured, only the regular expression was ever verified

This meant a configured expectation on the output could be discarded without any indication, and a test could pass even though one of its output expectations was never checked.

Since bebab12, this discarding when expectOutputString() and expectOutputRegex() are combined or repeated in no longer silent.

The changes proposed here make output expectations additive: every call to expectOutputString() and expectOutputRegex() contributes an expectation, and the output must satisfy all of them:

  • Multiple regular expressions can be registered: the output must match each of them
  • A string expectation and one or more regular expressions can be combined: the output must be identical to the string and match every pattern
  • Registering the same string or the same pattern more than once is harmless and has no additional effect

The single case that cannot be satisfied, expecting the output to be identical to two different strings, is reported with a PHPUnit warning explaining the conflict, since no output can ever fulfil it.

This is a behavioural change: a test that previously passed only because an earlier output expectation was silently discarded may now fail, because that expectation is finally being verified. In practice such tests were already not testing what they appeared to.

@sebastianbergmann sebastianbergmann added this to the PHPUnit 13.2 milestone Jun 4, 2026
@sebastianbergmann sebastianbergmann self-assigned this Jun 4, 2026
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/assertion Issues related to assertions and expectations labels Jun 4, 2026
@sebastianbergmann
sebastianbergmann force-pushed the multiple-output-expectations branch from b970e7f to 80d86d9 Compare June 4, 2026 06:06
@codecov

codecov Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.25%. Comparing base (028c69f) to head (80d86d9).
⚠️ Report is 6 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #6701   +/-   ##
=========================================
  Coverage     97.25%   97.25%           
- Complexity     8694     8699    +5     
=========================================
  Files           868      868           
  Lines         26633    26641    +8     
=========================================
+ Hits          25901    25909    +8     
  Misses          732      732           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@sebastianbergmann
sebastianbergmann deleted the multiple-output-expectations branch June 5, 2026 08:47
sebastianbergmann added a commit to sebastianbergmann/phpunit-documentation-english that referenced this pull request Jun 5, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 8, 2026
staabm pushed a commit to staabm/phpunit that referenced this pull request Jul 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature/assertion Issues related to assertions and expectations type/enhancement A new idea that should be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant