Skip to content

Commit e5265fc

Browse files
committed
Extract storeAssertDiff from Dumper::dumpException
1 parent 8cf24cf commit e5265fc

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/Framework/Dumper.php

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -268,46 +268,8 @@ public static function dumpException($e)
268268
}
269269

270270
if ($e instanceof AssertException) {
271-
$expected = $e->expected;
272-
$actual = $e->actual;
273-
274-
if (is_object($expected) || is_array($expected) || (is_string($expected) && strlen($expected) > self::$maxLength)
275-
|| is_object($actual) || is_array($actual) || (is_string($actual) && strlen($actual) > self::$maxLength)
276-
) {
277-
$args = isset($_SERVER['argv'][1])
278-
? '.[' . implode(' ', preg_replace(['#^-*(.{1,20}).*#i', '#[^=a-z0-9. -]+#i'], ['$1', '-'], array_slice($_SERVER['argv'], 1))) . ']'
279-
: '';
280-
$stored[] = self::saveOutput($testFile, $expected, $args . '.expected');
281-
$stored[] = self::saveOutput($testFile, $actual, $args . '.actual');
282-
}
283-
284-
if ((is_string($actual) && is_string($expected))) {
285-
for ($i = 0; $i < strlen($actual) && isset($expected[$i]) && $actual[$i] === $expected[$i]; $i++);
286-
for (; $i && $i < strlen($actual) && $actual[$i - 1] >= "\x80" && $actual[$i] >= "\x80" && $actual[$i] < "\xC0"; $i--);
287-
$i = max(0, min(
288-
$i - (int) (self::$maxLength / 3), // try to display 1/3 of shorter string
289-
max(strlen($actual), strlen($expected)) - self::$maxLength + 3 // 3 = length of ...
290-
));
291-
if ($i) {
292-
$expected = substr_replace($expected, '...', 0, $i);
293-
$actual = substr_replace($actual, '...', 0, $i);
294-
}
295-
}
296-
297-
$message = 'Failed: ' . $e->origMessage;
298-
if (((is_string($actual) && is_string($expected)) || (is_array($actual) && is_array($expected)))
299-
&& preg_match('#^(.*)(%\d)(.*)(%\d.*)\z#s', $message, $m)
300-
) {
301-
if (($delta = strlen($m[1]) - strlen($m[3])) >= 3) {
302-
$message = "$m[1]$m[2]\n" . str_repeat(' ', $delta - 3) . "...$m[3]$m[4]";
303-
} else {
304-
$message = "$m[1]$m[2]$m[3]\n" . str_repeat(' ', strlen($m[1]) - 4) . "... $m[4]";
305-
}
306-
}
307-
$message = strtr($message, [
308-
'%1' => self::color('yellow') . self::toLine($actual) . self::color('white'),
309-
'%2' => self::color('yellow') . self::toLine($expected) . self::color('white'),
310-
]);
271+
$stored = [];
272+
$message = self::storeAssertDiff($e, $testFile, $stored);
311273
} else {
312274
$message = ($e instanceof \ErrorException ? Helpers::errorTypeToString($e->getSeverity()) : get_class($e))
313275
. ': ' . preg_replace('#[\x00-\x09\x0B-\x1F]+#', ' ', $e->getMessage());
@@ -346,6 +308,58 @@ public static function dumpException($e)
346308
return $s;
347309
}
348310

311+
/**
312+
* @param AssertException $assertException
313+
* @param string $testFile
314+
* @param string[] $storedFile
315+
*
316+
* @return string
317+
*/
318+
public static function storeAssertDiff(AssertException $assertException, $testFile, array &$storedFile)
319+
{
320+
$expected = $assertException->expected;
321+
$actual = $assertException->actual;
322+
323+
if (is_object($expected) || is_array($expected) || (is_string($expected) && strlen($expected) > self::$maxLength)
324+
|| is_object($actual) || is_array($actual) || (is_string($actual) && strlen($actual) > self::$maxLength)
325+
) {
326+
$args = isset($_SERVER['argv'][1])
327+
? '.[' . implode(' ', preg_replace(['#^-*(.{1,20}).*#i', '#[^=a-z0-9. -]+#i'], ['$1', '-'], array_slice($_SERVER['argv'], 1))) . ']'
328+
: '';
329+
$storedFile[] = self::saveOutput($testFile, $expected, $args . '.expected');
330+
$storedFile[] = self::saveOutput($testFile, $actual, $args . '.actual');
331+
}
332+
333+
if ((is_string($actual) && is_string($expected))) {
334+
for ($i = 0; $i < strlen($actual) && isset($expected[$i]) && $actual[$i] === $expected[$i]; $i++) ;
335+
for (; $i && $i < strlen($actual) && $actual[$i - 1] >= "\x80" && $actual[$i] >= "\x80" && $actual[$i] < "\xC0"; $i--) ;
336+
$i = max(0, min(
337+
$i - (int)(self::$maxLength / 3), // try to display 1/3 of shorter string
338+
max(strlen($actual), strlen($expected)) - self::$maxLength + 3 // 3 = length of ...
339+
));
340+
if ($i) {
341+
$expected = substr_replace($expected, '...', 0, $i);
342+
$actual = substr_replace($actual, '...', 0, $i);
343+
}
344+
}
345+
346+
$message = 'Failed: ' . $assertException->origMessage;
347+
if (((is_string($actual) && is_string($expected)) || (is_array($actual) && is_array($expected)))
348+
&& preg_match('#^(.*)(%\d)(.*)(%\d.*)\z#s', $message, $m)
349+
) {
350+
if (($delta = strlen($m[1]) - strlen($m[3])) >= 3) {
351+
$message = "$m[1]$m[2]\n" . str_repeat(' ', $delta - 3) . "...$m[3]$m[4]";
352+
}
353+
else {
354+
$message = "$m[1]$m[2]$m[3]\n" . str_repeat(' ', strlen($m[1]) - 4) . "... $m[4]";
355+
}
356+
}
357+
358+
return strtr($message, [
359+
'%1' => self::color('yellow') . self::toLine($actual) . self::color('white'),
360+
'%2' => self::color('yellow') . self::toLine($expected) . self::color('white'),
361+
]);
362+
}
349363

350364
/**
351365
* Dumps data to folder 'output'.

0 commit comments

Comments
 (0)