Skip to content

Commit 2240153

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

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

src/Framework/Dumper.php

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -268,46 +268,11 @@ 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');
271+
$stored = [];
272+
$message = self::storeAssertDiff($e, $testFile, $stored);
273+
if (count($stored) !== 2) {
274+
unset($stored);
282275
}
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-
]);
311276
} else {
312277
$message = ($e instanceof \ErrorException ? Helpers::errorTypeToString($e->getSeverity()) : get_class($e))
313278
. ': ' . preg_replace('#[\x00-\x09\x0B-\x1F]+#', ' ', $e->getMessage());
@@ -346,6 +311,58 @@ public static function dumpException($e)
346311
return $s;
347312
}
348313

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

350367
/**
351368
* Dumps data to folder 'output'.

0 commit comments

Comments
 (0)