Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add code location hints from plain PHPT exception messages #3469

Merged
merged 1 commit into from Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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