Skip to content

Commit

Permalink
Closes #5300
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 5, 2023
1 parent 27ef39d commit 8b3f1d5
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog-10.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<source>` 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 `<phpunit>` XML configuration element or due to `--stop-on-*` CLI options

Expand Down
7 changes: 6 additions & 1 deletion src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ final public function runBare(): void
null
);
} else {
$e = $_e;
$e = $this->transformException($_e);

$this->status = TestStatus::error($e->getMessage());

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
bootstrap="src/autoload.php">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source filterDeprecations="true" filterNotices="true" filterWarnings="true">
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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');
}
}
30 changes: 30 additions & 0 deletions tests/end-to-end/generic/transform-exception-hook-method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5300
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/../_files/transform-exception-hook-method';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->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.

0 comments on commit 8b3f1d5

Please sign in to comment.