diff --git a/src/Runner/TestResult/Collector.php b/src/Runner/TestResult/Collector.php index 5d671eba720..acb9ae40c72 100644 --- a/src/Runner/TestResult/Collector.php +++ b/src/Runner/TestResult/Collector.php @@ -363,6 +363,7 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void $event->line(), $event->message(), $event->test(), + $event->stackTrace(), ); return; diff --git a/src/Runner/TestResult/Issue.php b/src/Runner/TestResult/Issue.php index 1ffdb09bad6..a7f213bd515 100644 --- a/src/Runner/TestResult/Issue.php +++ b/src/Runner/TestResult/Issue.php @@ -38,14 +38,19 @@ 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); } /** @@ -53,11 +58,12 @@ public static function from(string $file, int $line, string $description, Test $ * @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() => [ @@ -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; + } } diff --git a/src/TextUI/Application.php b/src/TextUI/Application.php index 2f1a9ac0b58..3b09150d891 100644 --- a/src/TextUI/Application.php +++ b/src/TextUI/Application.php @@ -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); diff --git a/src/TextUI/Output/Default/ResultPrinter.php b/src/TextUI/Output/Default/ResultPrinter.php index 1357bf59830..4c48de020d6 100644 --- a/src/TextUI/Output/Default/ResultPrinter.php +++ b/src/TextUI/Output/Default/ResultPrinter.php @@ -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); @@ -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); } } @@ -353,7 +353,7 @@ private function printSkippedTests(TestResult $result): void * @param non-empty-string $type * @param list $issues */ - private function printIssueList(string $type, array $issues): void + private function printIssueList(string $type, array $issues, bool $stackTrace = false): void { if (empty($issues)) { return; @@ -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(); diff --git a/src/TextUI/Output/Facade.php b/src/TextUI/Output/Facade.php index ab966da4eea..cfcd73acc2c 100644 --- a/src/TextUI/Output/Facade.php +++ b/src/TextUI/Output/Facade.php @@ -79,7 +79,7 @@ public static function init(Configuration $configuration, bool $extensionReplace /** * @param ?array $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); @@ -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) { diff --git a/tests/unit/TextUI/Output/Default/ResultPrinterTest.php b/tests/unit/TextUI/Output/Default/ResultPrinterTest.php index 092e7aa8f1d..096807db727 100644 --- a/tests/unit/TextUI/Output/Default/ResultPrinterTest.php +++ b/tests/unit/TextUI/Output/Default/ResultPrinterTest.php @@ -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: [ @@ -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( @@ -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(); @@ -382,7 +398,7 @@ public function testPrintsExpectedOutputForTestResultObject(string $expectationF false, ); - $resultPrinter->print($result); + $resultPrinter->print($result, $stackTraceForDeprecations); $summaryPrinter = new SummaryPrinter( $printer, diff --git a/tests/unit/TextUI/Output/Default/expectations/successful_test_with_deprecation_with_stack_trace.txt b/tests/unit/TextUI/Output/Default/expectations/successful_test_with_deprecation_with_stack_trace.txt new file mode 100644 index 00000000000..14eede871c4 --- /dev/null +++ b/tests/unit/TextUI/Output/Default/expectations/successful_test_with_deprecation_with_stack_trace.txt @@ -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.