You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add assertions for ignoring whitespace differences in strings #5757
ICU 72.1 replaced an ASCII space with a Unicode Narrow-Non-Breaking-Space.
In our codebase we have some code which works with \DateTime to produce dates in various formats and we confirm that the output is correct.
With the changes in ICU 72.1 out tests break because the visually identical space is different.
It would not be correct to update the string in the test because it is dependent upon the version of ICU installed on the test system.
PHPUnit already has assertions for things like assertEqualsIgnorningCase, assertStringContainsStringIgnoringCase, and assertEqualsCanonicalizing.
I wonder whether there is some merit in creatinng an assertion for assertEqualsIgnoringWhitespace which would essentially add:
Commit 14448ef adds a family of "ignoring whitespace" assertions that will be released in PHPUnit 13.2.
A new constraint, StringEqualsStringIgnoringWhitespace, normalizes both the expected and actual strings by collapsing all consecutive whitespace (spaces, tabs, newlines, etc.) into a single space and trimming leading/trailing whitespace. It does this via trim(preg_replace('/\s+/u', ' ', $string)).
Two strings are considered equal if their normalized forms are identical.
New assertions (6 total, each with a positive and negative variant):
assertStringEqualsStringIgnoringWhitespace()
Compares two strings directly, ignoring whitespace differences.
assertStringEqualsFileIgnoringWhitespace()
Compares a string against the contents of a file, ignoring whitespace.
assertFileEqualsFileIgnoringWhitespace()
Compares the contents of two files, ignoring whitespace.
A factory method, stringEqualsStringIgnoringWhitespace(), is also exposed so the constraint can be used standalone with assertThat() for custom compositions.
This follows the existing pattern established by the "IgnoringCase" and "IgnoringLineEndings" assertion variants.
Activity
sebastianbergmann commentedon Apr 8, 2026
Commit 14448ef adds a family of "ignoring whitespace" assertions that will be released in PHPUnit 13.2.
A new constraint,
StringEqualsStringIgnoringWhitespace, normalizes both the expected and actual strings by collapsing all consecutive whitespace (spaces, tabs, newlines, etc.) into a single space and trimming leading/trailing whitespace. It does this viatrim(preg_replace('/\s+/u', ' ', $string)).Two strings are considered equal if their normalized forms are identical.
New assertions (6 total, each with a positive and negative variant):
assertStringEqualsStringIgnoringWhitespace()Compares two strings directly, ignoring whitespace differences.
assertStringEqualsFileIgnoringWhitespace()Compares a string against the contents of a file, ignoring whitespace.
assertFileEqualsFileIgnoringWhitespace()Compares the contents of two files, ignoring whitespace.
A factory method,
stringEqualsStringIgnoringWhitespace(), is also exposed so the constraint can be used standalone withassertThat()for custom compositions.This follows the existing pattern established by the "IgnoringCase" and "IgnoringLineEndings" assertion variants.
Closes sebastianbergmann#5757