Skip to content

Commit

Permalink
Report exceptions thrown by third-party event subscribers as test run…
Browse files Browse the repository at this point in the history
…ner warnings
  • Loading branch information
sebastianbergmann committed Sep 22, 2023
1 parent 8aa37e3 commit 0a0f2e4
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog-10.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes of the PHPUnit 10.4 release series are documented in this fi
* [#5505](https://github.com/sebastianbergmann/phpunit/pull/5505): Improve the failure description of `StringContains`-based assertions when the strings are encoded differently
* [#5515](https://github.com/sebastianbergmann/phpunit/issues/5515): The `Test\AssertionSucceeded` and `Test\AssertionFailed` events are always emitted again
* [#5515](https://github.com/sebastianbergmann/phpunit/issues/5515): `--log-events-verbose-text` enables the export of non-scalar values for the `Test\AssertionSucceeded` and `Test\AssertionFailed` events
* Exceptions thrown by third-party event subscribers are now reported as test runner warnings
* The name of the top-level test suite that is created when a directory or file path is passed as an argument to the test runner is now `CLI Arguments`
* Simplified the failure description for `assertInstanceOf()` and `assertNotInstanceOf()`
* Simplified the failure description for `assertJson()`
Expand Down
15 changes: 13 additions & 2 deletions src/Event/Dispatcher/DirectDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ public function dispatch(Event $event): void
*/
public function handleThrowable(Throwable $t): void
{
if (!$this->isThrowableFromThirdPartySubscriber($t)) {
throw $t;
if ($this->isThrowableFromThirdPartySubscriber($t)) {
Facade::emitter()->testRunnerTriggeredWarning(
sprintf(
'Exception in third-party event subscriber: %s%s%s',
$t->getMessage(),
PHP_EOL,
$t->getTraceAsString(),
),
);

return;
}

throw $t;
}

private function isThrowableFromThirdPartySubscriber(Throwable $t): bool
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">
<extensions>
<bootstrap class="PHPUnit\TestFixture\Event\MyExtension\MyExtensionBootstrap">
<parameter name="message" value="the-message"/>
</bootstrap>
</extensions>

<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Event\MyExtension;

use PHPUnit\Event\TestRunner\ExecutionFinished;
use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber;
use RuntimeException;

final class MyExecutionFinishedSubscriber implements ExecutionFinishedSubscriber
{
public function notify(ExecutionFinished $event): void
{
throw new RuntimeException('message');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\Event\MyExtension;

use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

final class MyExtensionBootstrap implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscriber(new MyExecutionFinishedSubscriber);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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__ . '/MyExtensionBootstrap.php';

require __DIR__ . '/MyExecutionFinishedSubscriber.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Event\MyExtension;

use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
public function testOne(): void
{
$this->assertTrue(true);
}
}
28 changes: 28 additions & 0 deletions tests/end-to-end/extension/exception-in-extension-subscriber.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Test runner warning is triggered when an exception is triggered in an extension's event subscriber
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/_files/exception-in-extension-subscriber/phpunit.xml';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

There was 1 PHPUnit test runner warning:

1) Exception in third-party event subscriber: message
%A

WARNINGS!
Tests: 1, Assertions: 1, Warnings: 1.

0 comments on commit 0a0f2e4

Please sign in to comment.