Skip to content

Commit

Permalink
Add code location hints from plain PHPT exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Jan 11, 2019
1 parent 410c860 commit 60e7d53
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Framework/Assert.php
Expand Up @@ -3002,6 +3002,7 @@ public static function markTestSkipped(string $message = ''): void
{
if ($hint = self::detectLocationHint($message)) {
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
\array_unshift($trace, $hint);

throw new SyntheticSkippedError($hint['message'], 0, $hint['file'], $hint['line'], $trace);
}
Expand Down
46 changes: 41 additions & 5 deletions src/Runner/PhptTestCase.php
Expand Up @@ -178,9 +178,15 @@ public function run(TestResult $result = null): TestResult
if ($xfail !== false) {
$failure = new IncompleteTestError($xfail, 0, $e);
} else {
if ($e instanceof ExpectationFailedException && $e->getComparisonFailure()) {
if ($e instanceof ExpectationFailedException) {
/** @var ExpectationFailedException $e */
$hint = $this->getLocationHintFromDiff($e->getComparisonFailure()->getDiff(), $sections);
if ($e->getComparisonFailure()) {
$diff = $e->getComparisonFailure()->getDiff();
} else {
$diff = $e->getMessage();
}

$hint = $this->getLocationHintFromDiff($diff, $sections);
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
\array_unshift($trace, $hint);
$failure = new PHPTAssertionFailedError($e->getMessage(), 0, $trace[0]['file'], $trace[0]['line'], $trace);
Expand Down Expand Up @@ -608,14 +614,44 @@ private function stringifyIni(array $ini): array
private function getLocationHintFromDiff(string $message, array $sections): array
{
$needle = '';
$previousLine = '';
$block = 'message';

if (\preg_match("/--- Expected[^']+^-'([^']*)'/m", $message, $matches)) {
$needle = $matches[1];
}
foreach (explode(\PHP_EOL, $message) as $line) {
$line = \trim($line);
if ($block === 'message' && $line === '--- Expected') {
$block = 'expected';
}
if ($block === 'expected' && $line === '@@ @@') {
$block = 'diff';
}

if ($block === 'diff') {
if (substr($line, 0, 1) === '+') {
$needle = $this->getCleanDiffLine($previousLine);
break;
} elseif (substr($line, 0, 1) === '-') {
$needle = $this->getCleanDiffLine($line);
break;
}
}

if (!empty($line)) {
$previousLine = $line;
}
}
return $this->getLocationHint($needle, $sections);
}

private function getCleanDiffLine(string $line): string
{
if (\preg_match('/^[\-+]([\'\"]?)(.*)\1$/', $line, $matches)) {
$line = $matches[2];
}

return $line;
}

private function getLocationHint(string $needle, array $sections, ?string $sectionName = null): array
{
$needle = \trim($needle);
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/phpt/expect-location-hint.phpt
@@ -1,5 +1,5 @@
--TEST--
PHPT skip condition results in correct code location hint
PHPT EXPECT comparison returns correct code location hint
--FILE--
<?php
$arguments = [
Expand Down

0 comments on commit 60e7d53

Please sign in to comment.