@@ -258,46 +258,11 @@ public static function dumpException(\Throwable $e): string
258258 }
259259
260260 if ($ e instanceof AssertException) {
261- $ expected = $ e ->expected ;
262- $ actual = $ e ->actual ;
263-
264- if (is_object ($ expected ) || is_array ($ expected ) || (is_string ($ expected ) && strlen ($ expected ) > self ::$ maxLength )
265- || is_object ($ actual ) || is_array ($ actual ) || (is_string ($ actual ) && strlen ($ actual ) > self ::$ maxLength )
266- ) {
267- $ args = isset ($ _SERVER ['argv ' ][1 ])
268- ? '.[ ' . implode (' ' , preg_replace (['#^-*(.{1,20}).*#i ' , '#[^=a-z0-9. -]+#i ' ], ['$1 ' , '- ' ], array_slice ($ _SERVER ['argv ' ], 1 ))) . '] '
269- : '' ;
270- $ stored [] = self ::saveOutput ($ testFile , $ expected , $ args . '.expected ' );
271- $ stored [] = self ::saveOutput ($ testFile , $ actual , $ args . '.actual ' );
261+ $ stored = [];
262+ $ message = self ::storeAssertDiff ($ e , $ testFile , $ stored );
263+ if (count ($ stored ) !== 2 ) {
264+ unset($ stored );
272265 }
273-
274- if ((is_string ($ actual ) && is_string ($ expected ))) {
275- for ($ i = 0 ; $ i < strlen ($ actual ) && isset ($ expected [$ i ]) && $ actual [$ i ] === $ expected [$ i ]; $ i ++);
276- for (; $ i && $ i < strlen ($ actual ) && $ actual [$ i - 1 ] >= "\x80" && $ actual [$ i ] >= "\x80" && $ actual [$ i ] < "\xC0" ; $ i --);
277- $ i = max (0 , min (
278- $ i - (int ) (self ::$ maxLength / 3 ), // try to display 1/3 of shorter string
279- max (strlen ($ actual ), strlen ($ expected )) - self ::$ maxLength + 3 // 3 = length of ...
280- ));
281- if ($ i ) {
282- $ expected = substr_replace ($ expected , '... ' , 0 , $ i );
283- $ actual = substr_replace ($ actual , '... ' , 0 , $ i );
284- }
285- }
286-
287- $ message = 'Failed: ' . $ e ->origMessage ;
288- if (((is_string ($ actual ) && is_string ($ expected )) || (is_array ($ actual ) && is_array ($ expected )))
289- && preg_match ('#^(.*)(%\d)(.*)(%\d.*)\z#s ' , $ message , $ m )
290- ) {
291- if (($ delta = strlen ($ m [1 ]) - strlen ($ m [3 ])) >= 3 ) {
292- $ message = "$ m [1 ]$ m [2 ]\n" . str_repeat (' ' , $ delta - 3 ) . "... $ m [3 ]$ m [4 ]" ;
293- } else {
294- $ message = "$ m [1 ]$ m [2 ]$ m [3 ]\n" . str_repeat (' ' , strlen ($ m [1 ]) - 4 ) . "... $ m [4 ]" ;
295- }
296- }
297- $ message = strtr ($ message , [
298- '%1 ' => self ::color ('yellow ' ) . self ::toLine ($ actual ) . self ::color ('white ' ),
299- '%2 ' => self ::color ('yellow ' ) . self ::toLine ($ expected ) . self ::color ('white ' ),
300- ]);
301266 } else {
302267 $ message = ($ e instanceof \ErrorException ? Helpers::errorTypeToString ($ e ->getSeverity ()) : get_class ($ e ))
303268 . ': ' . preg_replace ('#[\x00-\x09\x0B-\x1F]+# ' , ' ' , $ e ->getMessage ());
@@ -336,6 +301,58 @@ public static function dumpException(\Throwable $e): string
336301 return $ s ;
337302 }
338303
304+ /**
305+ * @param AssertException $assertException
306+ * @param string $testFile
307+ * @param string[] $storedFile
308+ *
309+ * @return string
310+ */
311+ public static function storeAssertDiff (AssertException $ assertException , $ testFile , array &$ storedFile )
312+ {
313+ $ expected = $ assertException ->expected ;
314+ $ actual = $ assertException ->actual ;
315+
316+ if (is_object ($ expected ) || is_array ($ expected ) || (is_string ($ expected ) && strlen ($ expected ) > self ::$ maxLength )
317+ || is_object ($ actual ) || is_array ($ actual ) || (is_string ($ actual ) && strlen ($ actual ) > self ::$ maxLength )
318+ ) {
319+ $ args = isset ($ _SERVER ['argv ' ][1 ])
320+ ? '.[ ' . implode (' ' , preg_replace (['#^-*(.{1,20}).*#i ' , '#[^=a-z0-9. -]+#i ' ], ['$1 ' , '- ' ], array_slice ($ _SERVER ['argv ' ], 1 ))) . '] '
321+ : '' ;
322+ $ storedFile [] = self ::saveOutput ($ testFile , $ expected , $ args . '.expected ' );
323+ $ storedFile [] = self ::saveOutput ($ testFile , $ actual , $ args . '.actual ' );
324+ }
325+
326+ if ((is_string ($ actual ) && is_string ($ expected ))) {
327+ for ($ i = 0 ; $ i < strlen ($ actual ) && isset ($ expected [$ i ]) && $ actual [$ i ] === $ expected [$ i ]; $ i ++) ;
328+ for (; $ i && $ i < strlen ($ actual ) && $ actual [$ i - 1 ] >= "\x80" && $ actual [$ i ] >= "\x80" && $ actual [$ i ] < "\xC0" ; $ i --) ;
329+ $ i = max (0 , min (
330+ $ i - (int )(self ::$ maxLength / 3 ), // try to display 1/3 of shorter string
331+ max (strlen ($ actual ), strlen ($ expected )) - self ::$ maxLength + 3 // 3 = length of ...
332+ ));
333+ if ($ i ) {
334+ $ expected = substr_replace ($ expected , '... ' , 0 , $ i );
335+ $ actual = substr_replace ($ actual , '... ' , 0 , $ i );
336+ }
337+ }
338+
339+ $ message = 'Failed: ' . $ assertException ->origMessage ;
340+ if (((is_string ($ actual ) && is_string ($ expected )) || (is_array ($ actual ) && is_array ($ expected )))
341+ && preg_match ('#^(.*)(%\d)(.*)(%\d.*)\z#s ' , $ message , $ m )
342+ ) {
343+ if (($ delta = strlen ($ m [1 ]) - strlen ($ m [3 ])) >= 3 ) {
344+ $ message = "$ m [1 ]$ m [2 ]\n" . str_repeat (' ' , $ delta - 3 ) . "... $ m [3 ]$ m [4 ]" ;
345+ }
346+ else {
347+ $ message = "$ m [1 ]$ m [2 ]$ m [3 ]\n" . str_repeat (' ' , strlen ($ m [1 ]) - 4 ) . "... $ m [4 ]" ;
348+ }
349+ }
350+
351+ return strtr ($ message , [
352+ '%1 ' => self ::color ('yellow ' ) . self ::toLine ($ actual ) . self ::color ('white ' ),
353+ '%2 ' => self ::color ('yellow ' ) . self ::toLine ($ expected ) . self ::color ('white ' ),
354+ ]);
355+ }
339356
340357 /**
341358 * Dumps data to folder 'output'.
0 commit comments