diff --git a/ChangeLog-10.1.md b/ChangeLog-10.1.md index 3f34d079abb..ec2ef0a1f4e 100644 --- a/ChangeLog-10.1.md +++ b/ChangeLog-10.1.md @@ -13,6 +13,7 @@ All notable changes of the PHPUnit 10.1 release series are documented in this fi * [#5237](https://github.com/sebastianbergmann/phpunit/issues/5237): Implement `IgnoreClassForCodeCoverage`, `IgnoreMethodForCodeCoverage`, and `IgnoreFunctionForCodeCoverage` attributes * [#5293](https://github.com/sebastianbergmann/phpunit/issues/5293): Allow to limit the reporting of deprecations, notices, and warnings to specified directories * [#5294](https://github.com/sebastianbergmann/phpunit/issues/5294): Introduce `` XML configuration element to configure "your code" +* [#5300](https://github.com/sebastianbergmann/phpunit/issues/5300): `TestCase::transformException()` hook method * `TestCase::createConfiguredStub()` was added as an analogon to `TestCase::createConfiguredMock()` * The `PHPUnit\Event\TestRunner\ExecutionAborted` event is now emitted when the execution of tests is stopped due to `stopOn*` attributes on the `` XML configuration element or due to `--stop-on-*` CLI options diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index b875580d571..00898d070d9 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; + } + /** * This method is called when a test method did not execute successfully. * diff --git a/tests/end-to-end/_files/transform-exception-hook-method/phpunit.xml b/tests/end-to-end/_files/transform-exception-hook-method/phpunit.xml new file mode 100644 index 00000000000..15075a73874 --- /dev/null +++ b/tests/end-to-end/_files/transform-exception-hook-method/phpunit.xml @@ -0,0 +1,16 @@ + + + + + tests + + + + + + src + + + diff --git a/tests/end-to-end/_files/transform-exception-hook-method/src/OriginalException.php b/tests/end-to-end/_files/transform-exception-hook-method/src/OriginalException.php new file mode 100644 index 00000000000..06675cf9223 --- /dev/null +++ b/tests/end-to-end/_files/transform-exception-hook-method/src/OriginalException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\TransformExceptionHookMethod; + +use RuntimeException; + +final class OriginalException extends RuntimeException +{ +} diff --git a/tests/end-to-end/_files/transform-exception-hook-method/src/TransformedException.php b/tests/end-to-end/_files/transform-exception-hook-method/src/TransformedException.php new file mode 100644 index 00000000000..8cafbbadf2c --- /dev/null +++ b/tests/end-to-end/_files/transform-exception-hook-method/src/TransformedException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\TransformExceptionHookMethod; + +use RuntimeException; + +final class TransformedException extends RuntimeException +{ +} diff --git a/tests/end-to-end/_files/transform-exception-hook-method/src/autoload.php b/tests/end-to-end/_files/transform-exception-hook-method/src/autoload.php new file mode 100644 index 00000000000..d0605dc8508 --- /dev/null +++ b/tests/end-to-end/_files/transform-exception-hook-method/src/autoload.php @@ -0,0 +1,12 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +require __DIR__ . '/OriginalException.php'; + +require __DIR__ . '/TransformedException.php'; diff --git a/tests/end-to-end/_files/transform-exception-hook-method/tests/Test.php b/tests/end-to-end/_files/transform-exception-hook-method/tests/Test.php new file mode 100644 index 00000000000..e5c68f016ce --- /dev/null +++ b/tests/end-to-end/_files/transform-exception-hook-method/tests/Test.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\TransformExceptionHookMethod; + +use PHPUnit\Framework\TestCase; +use Throwable; + +final class Test extends TestCase +{ + public function testOne(): void + { + throw new OriginalException('original message'); + } + + protected function transformException(Throwable $t): Throwable + { + return new TransformedException('transformed message'); + } +} diff --git a/tests/end-to-end/generic/transform-exception-hook-method.phpt b/tests/end-to-end/generic/transform-exception-hook-method.phpt new file mode 100644 index 00000000000..c07ef9ba9b5 --- /dev/null +++ b/tests/end-to-end/generic/transform-exception-hook-method.phpt @@ -0,0 +1,30 @@ +--TEST-- +https://github.com/sebastianbergmann/phpunit/issues/5300 +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s +Configuration: %s + +E 1 / 1 (100%) + +Time: %s, Memory: %s + +There was 1 error: + +1) PHPUnit\TestFixture\TransformExceptionHookMethod\Test::testOne +PHPUnit\TestFixture\TransformExceptionHookMethod\TransformedException: transformed message + +%s/Test.php:24 + +ERRORS! +Tests: 1, Assertions: 0, Errors: 1.