When using assertEquals("Windows EOL", "Unix EOL") with multi-line text, assertEquals() properly displays a failure, but does not show why in the difference report.
1) Namespace\ClassName::testCreateResponse
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
Parameter: parameter value
Context: context value
URL: http://localhost/test?key=value
/path/to/test/file.php:55
The best I could come up with for this is:
diff --git a/PHPUnit/Util/Diff.php b/PHPUnit/Util/Diff.php
index f9040bf..df09fde 100644
--- a/PHPUnit/Util/Diff.php
+++ b/PHPUnit/Util/Diff.php
@@ -148,6 +148,9 @@ class PHPUnit_Util_Diff
*/
public static function diffToArray($from, $to)
{
+ preg_match_all('(\r\n|\r|\n)', $from, $fromMatches);
+ preg_match_all('(\r\n|\r|\n)', $to, $toMatches);
+
if (is_string($from)) {
$from = preg_split('(\r\n|\r|\n)', $from);
}
@@ -189,6 +192,14 @@ class PHPUnit_Util_Diff
$diff = array();
$line = 0;
+ if(isset($fromMatches[0])
+ && $toMatches[0]
+ && count($fromMatches[0]) === count($toMatches[0])
+ && $fromMatches[0] !== $toMatches[0]
+ ) {
+ $diff[] = array('#Warning: Strings contain different line endings!', 0);
+ }
+
foreach ($start as $token) {
$diff[] = array($token, 0 /* OLD */);
}
It would produce:
1) Issue503Test::testCompareDifferentLineEnding
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
#Warning: Strings contain different line endings!
foo
Is that good enough for you @kherge?
Do you like it @sebastianbergmann ? I couldn't come up with nicer reporting without rewriting more than half of the diff algo (and possibly breaking lots of things).
This now only produces the Warning if the number of lines is the same so "Foo\n" vs "Bar\r\n\r\n\r\n" still gets reported properly without that warning.
AFAICS, this detects whether different line-endings are used and reports that fact in case the diff is empty. Looks good to me, then.
Thank you @edorian!
For anyone who would have the issue in the future.
A little helper approach that will also display a debug statement showing the difference:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2221/commits/d60e963e80f45e2050085964f67c6769f347381f
even better: https://github.com/GeckoPackages/GeckoPHPUnit/pull/18
output is not pretty, but at least sth is displayed so it's possible to find where the wrong character is.
As a die-hard windows user, thank you :)
Most helpful comment
As a die-hard windows user, thank you :)