Skip to content

TestCase::transformException() hook method #5300

@sebastianbergmann

Description

@sebastianbergmann

WIP: Implement TestCase::transformException() hook method (main branch)

diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index b875580d5..00898d070 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -671,7 +671,7 @@ final public function runBare(): void null ); } else { - $e = $_e; + $e = $this->transformException($_e); $this->status = TestStatus::error($e->getMessage()); @@ -1465,6 +1465,11 @@ protected function getObjectForTrait(string $traitName, array $arguments = [], s ); } + protected function transformException(Throwable $t): Throwable + { + return $t; + }

Test.php

<?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class OriginalException extends RuntimeException { } final class ModifiedException extends RuntimeException { } final class Test extends TestCase { public function testOne(): void { throw new OriginalException('original message'); } protected function transformException(Throwable $t): Throwable { return new ModifiedException('modified message'); } }
PHPUnit 10.1-dev by Sebastian Bergmann and contributors. Runtime: PHP 8.2.4 E 1 / 1 (100%) Time: 00:00.019, Memory: 4.00 MB There was 1 error: 1) Test::testOne ModifiedException: modified message /home/sb/Test.php:21 ERRORS! Tests: 1, Assertions: 0, Errors: 1. 

Originally posted by @sebastianbergmann in #5254 (comment)

Metadata

Metadata

Labels

type/enhancementA new idea that should be implemented

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions