Skip to content

Commit

Permalink
Print stack trace for deprecations when execution is stopped at a par…
Browse files Browse the repository at this point in the history
…ticular deprecation for #5954
  • Loading branch information
sebastianbergmann committed Nov 7, 2024
1 parent 5d6dc50 commit 60d34d3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Runner/TestResult/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void
$event->line(),
$event->message(),
$event->test(),
$event->stackTrace(),
);

return;
Expand Down
28 changes: 25 additions & 3 deletions src/Runner/TestResult/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,32 @@ final class Issue
*/
private array $triggeringTests;

/**
* @var ?non-empty-string
*/
private ?string $stackTrace;

/**
* @param non-empty-string $file
* @param positive-int $line
* @param non-empty-string $description
*/
public static function from(string $file, int $line, string $description, Test $triggeringTest): self
public static function from(string $file, int $line, string $description, Test $triggeringTest, ?string $stackTrace = null): self
{
return new self($file, $line, $description, $triggeringTest);
return new self($file, $line, $description, $triggeringTest, $stackTrace);
}

/**
* @param non-empty-string $file
* @param positive-int $line
* @param non-empty-string $description
*/
private function __construct(string $file, int $line, string $description, Test $triggeringTest)
private function __construct(string $file, int $line, string $description, Test $triggeringTest, ?string $stackTrace)
{
$this->file = $file;
$this->line = $line;
$this->description = $description;
$this->stackTrace = $stackTrace;

$this->triggeringTests = [
$triggeringTest->id() => [
Expand Down Expand Up @@ -112,4 +118,20 @@ public function triggeringTests(): array
{
return $this->triggeringTests;
}

/**
* @phpstan-assert-if-true !null $this->stackTrace
*/
public function hasStackTrace(): bool
{
return $this->stackTrace !== null;
}

/**
* @return ?non-empty-string
*/
public function stackTrace(): ?string
{
return $this->stackTrace;
}
}
7 changes: 6 additions & 1 deletion src/TextUI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,12 @@ public function run(array $argv): int
$result = TestResultFacade::result();

if (!$extensionReplacesResultOutput && !$configuration->debug()) {
OutputFacade::printResult($result, $testDoxResult, $duration);
OutputFacade::printResult(
$result,
$testDoxResult,
$duration,
$configuration->hasSpecificDeprecationToStopOn(),
);
}

CodeCoverage::instance()->generateReports($printer, $configuration);
Expand Down
14 changes: 10 additions & 4 deletions src/TextUI/Output/Default/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(Printer $printer, bool $displayPhpunitErrors, bool $
$this->displayDefectsInReverseOrder = $displayDefectsInReverseOrder;
}

public function print(TestResult $result): void
public function print(TestResult $result, bool $stackTraceForDeprecations = false): void
{
if ($this->displayPhpunitErrors) {
$this->printPhpunitErrors($result);
Expand Down Expand Up @@ -142,7 +142,7 @@ public function print(TestResult $result): void

if ($this->displayDetailsOnTestsThatTriggerDeprecations) {
$this->printIssueList('PHP deprecation', $result->phpDeprecations());
$this->printIssueList('deprecation', $result->deprecations());
$this->printIssueList('deprecation', $result->deprecations(), $stackTraceForDeprecations);
}
}

Expand Down Expand Up @@ -353,7 +353,7 @@ private function printSkippedTests(TestResult $result): void
* @param non-empty-string $type
* @param list<Issue> $issues
*/
private function printIssueList(string $type, array $issues): void
private function printIssueList(string $type, array $issues, bool $stackTrace = false): void
{
if (empty($issues)) {
return;
Expand Down Expand Up @@ -389,7 +389,13 @@ private function printIssueList(string $type, array $issues): void
$issue->line(),
);

$body = trim($issue->description()) . PHP_EOL . PHP_EOL . 'Triggered by:';
$body = trim($issue->description()) . PHP_EOL . PHP_EOL;

if ($stackTrace && $issue->hasStackTrace()) {
$body .= trim($issue->stackTrace()) . PHP_EOL . PHP_EOL;
}

$body .= 'Triggered by:';

$triggeringTests = $issue->triggeringTests();

Expand Down
4 changes: 2 additions & 2 deletions src/TextUI/Output/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function init(Configuration $configuration, bool $extensionReplace
/**
* @param ?array<string, TestResultCollection> $testDoxResult
*/
public static function printResult(TestResult $result, ?array $testDoxResult, Duration $duration): void
public static function printResult(TestResult $result, ?array $testDoxResult, Duration $duration, bool $stackTraceForDeprecations): void
{
assert(self::$printer !== null);

Expand All @@ -96,7 +96,7 @@ public static function printResult(TestResult $result, ?array $testDoxResult, Du
}

if (self::$defaultResultPrinter !== null) {
self::$defaultResultPrinter->print($result);
self::$defaultResultPrinter->print($result, $stackTraceForDeprecations);
}

if (self::$summaryPrinter !== null) {
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/TextUI/Output/Default/ResultPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static function provider(): array
),
],

'successful test that triggers deprecation' => [
'successful test that triggers deprecation (do not display stack trace)' => [
__DIR__ . '/expectations/successful_test_with_deprecation.txt',
self::createTestResult(
deprecations: [
Expand All @@ -193,6 +193,22 @@ public static function provider(): array
),
],

'successful test that triggers deprecation (display stack trace)' => [
__DIR__ . '/expectations/successful_test_with_deprecation_with_stack_trace.txt',
self::createTestResult(
deprecations: [
Issue::from(
'Foo.php',
1,
'message',
self::testMethod(),
'/path/to/file.php:1234',
),
],
),
true,
],

'successful test that triggers PHP deprecation' => [
__DIR__ . '/expectations/successful_test_with_php_deprecation.txt',
self::createTestResult(
Expand Down Expand Up @@ -361,7 +377,7 @@ public static function provider(): array
}

#[DataProvider('provider')]
public function testPrintsExpectedOutputForTestResultObject(string $expectationFile, TestResult $result): void
public function testPrintsExpectedOutputForTestResultObject(string $expectationFile, TestResult $result, bool $stackTraceForDeprecations = false): void
{
$printer = $this->printer();

Expand All @@ -382,7 +398,7 @@ public function testPrintsExpectedOutputForTestResultObject(string $expectationF
false,
);

$resultPrinter->print($result);
$resultPrinter->print($result, $stackTraceForDeprecations);

$summaryPrinter = new SummaryPrinter(
$printer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 test triggered 1 deprecation:

1) Foo.php:1
message

/path/to/file.php:1234

Triggered by:

* FooTest::testBar
FooTest.php:1

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 1.

0 comments on commit 60d34d3

Please sign in to comment.