Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestCase::transformException() hook method #5300

Closed
sebastianbergmann opened this issue Apr 3, 2023 · 0 comments
Closed

TestCase::transformException() hook method #5300

sebastianbergmann opened this issue Apr 3, 2023 · 0 comments
Assignees
Labels
type/enhancement A new idea that should be implemented
Milestone

Comments

@sebastianbergmann
Copy link
Owner

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)

@sebastianbergmann sebastianbergmann added the type/enhancement A new idea that should be implemented label Apr 3, 2023
@sebastianbergmann sebastianbergmann added this to the PHPUnit 10.1 milestone Apr 3, 2023
@sebastianbergmann sebastianbergmann self-assigned this Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement A new idea that should be implemented
Projects
None yet
Development

No branches or pull requests

1 participant