diff --git a/composer.json b/composer.json index bff7b523..c96b7379 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": "^8.1.0", "brianium/paratest": "^7.1.2", - "nunomaduro/collision": "^7.3.3", + "nunomaduro/collision": "^v7.x-dev", "nunomaduro/termwind": "^1.15.1", "pestphp/pest-plugin": "^2.0.1", "pestphp/pest-plugin-arch": "^2.0.2", diff --git a/overrides/Event/Value/ThrowableBuilder.php b/overrides/Event/Value/ThrowableBuilder.php new file mode 100644 index 00000000..681fcfd6 --- /dev/null +++ b/overrides/Event/Value/ThrowableBuilder.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Event\Code; + +use NunoMaduro\Collision\Contracts\RenderableOnCollisionEditor; +use PHPUnit\Event\NoPreviousThrowableException; +use PHPUnit\Framework\Exception; +use PHPUnit\Util\Filter; +use PHPUnit\Util\ThrowableToStringMapper; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class ThrowableBuilder +{ + /** + * @throws Exception + * @throws NoPreviousThrowableException + */ + public static function from(\Throwable $t): Throwable + { + $previous = $t->getPrevious(); + + if ($previous !== null) { + $previous = self::from($previous); + } + + $trace = Filter::getFilteredStacktrace($t); + + if ($t instanceof RenderableOnCollisionEditor && $frame = $t->toCollisionEditor()) { + $file = $frame->getFile(); + $line = $frame->getLine(); + + $trace = "$file:$line\n$trace"; + } + + return new Throwable( + $t::class, + $t->getMessage(), + ThrowableToStringMapper::map($t), + $trace, + $previous + ); + } +} diff --git a/src/Bootstrappers/BootOverrides.php b/src/Bootstrappers/BootOverrides.php index e1489d7c..0fc1aee6 100644 --- a/src/Bootstrappers/BootOverrides.php +++ b/src/Bootstrappers/BootOverrides.php @@ -24,6 +24,7 @@ final class BootOverrides implements Bootstrapper 'TextUI/Command/WarmCodeCoverageCacheCommand.php', 'TextUI/Output/Default/ProgressPrinter/TestSkippedSubscriber.php', 'TextUI/TestSuiteFilterProcessor.php', + 'Event/Value/ThrowableBuilder.php', ]; /** diff --git a/tests/Unit/Overrides/ThrowableBuilder.php b/tests/Unit/Overrides/ThrowableBuilder.php new file mode 100644 index 00000000..08a69680 --- /dev/null +++ b/tests/Unit/Overrides/ThrowableBuilder.php @@ -0,0 +1,28 @@ + __DIR__.'/../../Pest.php', + 'line' => 5, + ]); + } + }; + + expect(ThrowableBuilder::from($exception)) + ->stackTrace()->toStartWith('/data/projects/open-source/pest/tests/Unit/Overrides/../../Pest.php:5') + ->and(ThrowableBuilder::from(new Exception('test'))) + ->stackTrace()->toStartWith('/data/projects/open-source/pest/tests/Unit/Overrides/ThrowableBuilder.php:26'); +});