Skip to content

Commit 60d34d3

Browse files
Print stack trace for deprecations when execution is stopped at a particular deprecation for #5954
1 parent 5d6dc50 commit 60d34d3

File tree

7 files changed

+77
-13
lines changed

7 files changed

+77
-13
lines changed

src/Runner/TestResult/Collector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void
363363
$event->line(),
364364
$event->message(),
365365
$event->test(),
366+
$event->stackTrace(),
366367
);
367368

368369
return;

src/Runner/TestResult/Issue.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,32 @@ final class Issue
3838
*/
3939
private array $triggeringTests;
4040

41+
/**
42+
* @var ?non-empty-string
43+
*/
44+
private ?string $stackTrace;
45+
4146
/**
4247
* @param non-empty-string $file
4348
* @param positive-int $line
4449
* @param non-empty-string $description
4550
*/
46-
public static function from(string $file, int $line, string $description, Test $triggeringTest): self
51+
public static function from(string $file, int $line, string $description, Test $triggeringTest, ?string $stackTrace = null): self
4752
{
48-
return new self($file, $line, $description, $triggeringTest);
53+
return new self($file, $line, $description, $triggeringTest, $stackTrace);
4954
}
5055

5156
/**
5257
* @param non-empty-string $file
5358
* @param positive-int $line
5459
* @param non-empty-string $description
5560
*/
56-
private function __construct(string $file, int $line, string $description, Test $triggeringTest)
61+
private function __construct(string $file, int $line, string $description, Test $triggeringTest, ?string $stackTrace)
5762
{
5863
$this->file = $file;
5964
$this->line = $line;
6065
$this->description = $description;
66+
$this->stackTrace = $stackTrace;
6167

6268
$this->triggeringTests = [
6369
$triggeringTest->id() => [
@@ -112,4 +118,20 @@ public function triggeringTests(): array
112118
{
113119
return $this->triggeringTests;
114120
}
121+
122+
/**
123+
* @phpstan-assert-if-true !null $this->stackTrace
124+
*/
125+
public function hasStackTrace(): bool
126+
{
127+
return $this->stackTrace !== null;
128+
}
129+
130+
/**
131+
* @return ?non-empty-string
132+
*/
133+
public function stackTrace(): ?string
134+
{
135+
return $this->stackTrace;
136+
}
115137
}

src/TextUI/Application.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ public function run(array $argv): int
258258
$result = TestResultFacade::result();
259259

260260
if (!$extensionReplacesResultOutput && !$configuration->debug()) {
261-
OutputFacade::printResult($result, $testDoxResult, $duration);
261+
OutputFacade::printResult(
262+
$result,
263+
$testDoxResult,
264+
$duration,
265+
$configuration->hasSpecificDeprecationToStopOn(),
266+
);
262267
}
263268

264269
CodeCoverage::instance()->generateReports($printer, $configuration);

src/TextUI/Output/Default/ResultPrinter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct(Printer $printer, bool $displayPhpunitErrors, bool $
8383
$this->displayDefectsInReverseOrder = $displayDefectsInReverseOrder;
8484
}
8585

86-
public function print(TestResult $result): void
86+
public function print(TestResult $result, bool $stackTraceForDeprecations = false): void
8787
{
8888
if ($this->displayPhpunitErrors) {
8989
$this->printPhpunitErrors($result);
@@ -142,7 +142,7 @@ public function print(TestResult $result): void
142142

143143
if ($this->displayDetailsOnTestsThatTriggerDeprecations) {
144144
$this->printIssueList('PHP deprecation', $result->phpDeprecations());
145-
$this->printIssueList('deprecation', $result->deprecations());
145+
$this->printIssueList('deprecation', $result->deprecations(), $stackTraceForDeprecations);
146146
}
147147
}
148148

@@ -353,7 +353,7 @@ private function printSkippedTests(TestResult $result): void
353353
* @param non-empty-string $type
354354
* @param list<Issue> $issues
355355
*/
356-
private function printIssueList(string $type, array $issues): void
356+
private function printIssueList(string $type, array $issues, bool $stackTrace = false): void
357357
{
358358
if (empty($issues)) {
359359
return;
@@ -389,7 +389,13 @@ private function printIssueList(string $type, array $issues): void
389389
$issue->line(),
390390
);
391391

392-
$body = trim($issue->description()) . PHP_EOL . PHP_EOL . 'Triggered by:';
392+
$body = trim($issue->description()) . PHP_EOL . PHP_EOL;
393+
394+
if ($stackTrace && $issue->hasStackTrace()) {
395+
$body .= trim($issue->stackTrace()) . PHP_EOL . PHP_EOL;
396+
}
397+
398+
$body .= 'Triggered by:';
393399

394400
$triggeringTests = $issue->triggeringTests();
395401

src/TextUI/Output/Facade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function init(Configuration $configuration, bool $extensionReplace
7979
/**
8080
* @param ?array<string, TestResultCollection> $testDoxResult
8181
*/
82-
public static function printResult(TestResult $result, ?array $testDoxResult, Duration $duration): void
82+
public static function printResult(TestResult $result, ?array $testDoxResult, Duration $duration, bool $stackTraceForDeprecations): void
8383
{
8484
assert(self::$printer !== null);
8585

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

9898
if (self::$defaultResultPrinter !== null) {
99-
self::$defaultResultPrinter->print($result);
99+
self::$defaultResultPrinter->print($result, $stackTraceForDeprecations);
100100
}
101101

102102
if (self::$summaryPrinter !== null) {

tests/unit/TextUI/Output/Default/ResultPrinterTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static function provider(): array
179179
),
180180
],
181181

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

196+
'successful test that triggers deprecation (display stack trace)' => [
197+
__DIR__ . '/expectations/successful_test_with_deprecation_with_stack_trace.txt',
198+
self::createTestResult(
199+
deprecations: [
200+
Issue::from(
201+
'Foo.php',
202+
1,
203+
'message',
204+
self::testMethod(),
205+
'/path/to/file.php:1234',
206+
),
207+
],
208+
),
209+
true,
210+
],
211+
196212
'successful test that triggers PHP deprecation' => [
197213
__DIR__ . '/expectations/successful_test_with_php_deprecation.txt',
198214
self::createTestResult(
@@ -361,7 +377,7 @@ public static function provider(): array
361377
}
362378

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

@@ -382,7 +398,7 @@ public function testPrintsExpectedOutputForTestResultObject(string $expectationF
382398
false,
383399
);
384400

385-
$resultPrinter->print($result);
401+
$resultPrinter->print($result, $stackTraceForDeprecations);
386402

387403
$summaryPrinter = new SummaryPrinter(
388404
$printer,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1 test triggered 1 deprecation:
2+
3+
1) Foo.php:1
4+
message
5+
6+
/path/to/file.php:1234
7+
8+
Triggered by:
9+
10+
* FooTest::testBar
11+
FooTest.php:1
12+
13+
OK, but there were issues!
14+
Tests: 1, Assertions: 1, Deprecations: 1.

0 commit comments

Comments
 (0)