Skip to content

Commit 29c9186

Browse files
committed
Extract storeAssertDiff from Dumper::dumpException
1 parent ea95940 commit 29c9186

File tree

1 file changed

+57
-39
lines changed

1 file changed

+57
-39
lines changed

src/Framework/Dumper.php

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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());
@@ -337,6 +302,59 @@ public static function dumpException(\Throwable $e): string
337302
}
338303

339304

305+
/**
306+
* @param AssertException $assertException
307+
* @param string $testFile
308+
* @param string[] $storedFile
309+
*
310+
* @return string
311+
*/
312+
public static function storeAssertDiff(AssertException $assertException, $testFile, array &$storedFile)
313+
{
314+
$expected = $assertException->expected;
315+
$actual = $assertException->actual;
316+
317+
if (is_object($expected) || is_array($expected) || (is_string($expected) && strlen($expected) > self::$maxLength)
318+
|| is_object($actual) || is_array($actual) || (is_string($actual) && strlen($actual) > self::$maxLength)
319+
) {
320+
$args = isset($_SERVER['argv'][1])
321+
? '.[' . implode(' ', preg_replace(['#^-*(.{1,20}).*#i', '#[^=a-z0-9. -]+#i'], ['$1', '-'], array_slice($_SERVER['argv'], 1))) . ']'
322+
: '';
323+
$storedFile[] = self::saveOutput($testFile, $expected, $args . '.expected');
324+
$storedFile[] = self::saveOutput($testFile, $actual, $args . '.actual');
325+
}
326+
327+
if ((is_string($actual) && is_string($expected))) {
328+
for ($i = 0; $i < strlen($actual) && isset($expected[$i]) && $actual[$i] === $expected[$i]; $i++) ;
329+
for (; $i && $i < strlen($actual) && $actual[$i - 1] >= "\x80" && $actual[$i] >= "\x80" && $actual[$i] < "\xC0"; $i--) ;
330+
$i = max(0, min(
331+
$i - (int) (self::$maxLength / 3), // try to display 1/3 of shorter string
332+
max(strlen($actual), strlen($expected)) - self::$maxLength + 3 // 3 = length of ...
333+
));
334+
if ($i) {
335+
$expected = substr_replace($expected, '...', 0, $i);
336+
$actual = substr_replace($actual, '...', 0, $i);
337+
}
338+
}
339+
340+
$message = 'Failed: ' . $assertException->origMessage;
341+
if (((is_string($actual) && is_string($expected)) || (is_array($actual) && is_array($expected)))
342+
&& preg_match('#^(.*)(%\d)(.*)(%\d.*)\z#s', $message, $m)
343+
) {
344+
if (($delta = strlen($m[1]) - strlen($m[3])) >= 3) {
345+
$message = "$m[1]$m[2]\n" . str_repeat(' ', $delta - 3) . "...$m[3]$m[4]";
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+
}
356+
357+
340358
/**
341359
* Dumps data to folder 'output'.
342360
* @internal

0 commit comments

Comments
 (0)