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 6ce7c0b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 48 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
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
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>

<extensions>
<bootstrap class="PHPUnit\TestFixture\Issue5219\Extension"/>
</extensions>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5219;
namespace PHPUnit\TestFixture\Event\MyExtension;

use PHPUnit\Event\Test\Prepared;
use PHPUnit\Event\Test\PreparedSubscriber;
use PHPUnit\Event\TestRunner\ExecutionFinished;
use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber;
use RuntimeException;

final class TestPreparedSubscriber implements PreparedSubscriber
final class MyExecutionFinishedSubscriber implements ExecutionFinishedSubscriber
{
public function notify(Prepared $event): void
public function notify(ExecutionFinished $event): void
{
throw new RuntimeException('message');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5219;
namespace PHPUnit\TestFixture\Event\MyExtension;

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

final class Extension implements ExtensionInterface
final class MyExtensionBootstrap implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscriber(new TestPreparedSubscriber);
$facade->registerSubscriber(new MyExecutionFinishedSubscriber);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

/*
* This file is part of PHPUnit.
*
Expand All @@ -7,8 +8,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5219;

require __DIR__ . '/Extension.php';
require __DIR__ . '/MyExtensionBootstrap.php';

require __DIR__ . '/TestPreparedSubscriber.php';
require __DIR__ . '/MyExecutionFinishedSubscriber.php';
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5219;
namespace PHPUnit\TestFixture\Event\MyExtension;

use PHPUnit\Framework\TestCase;

final class Issue5219Test extends TestCase
final class Test extends TestCase
{
public function testOne(): void
{
Expand Down
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.
22 changes: 0 additions & 22 deletions tests/end-to-end/regression/5219.phpt

This file was deleted.

6 changes: 2 additions & 4 deletions tests/unit/Event/Dispatcher/DirectDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function testDispatchesEventToKnownSubscribers(): void
$subscriber
->expects($this->once())
->method('notify')
->with($this->identicalTo($event))
->willThrowException(new RuntimeException('third-party exception'));
->with($this->identicalTo($event));

$dispatcher->registerSubscriber($subscriber);

Expand All @@ -53,8 +52,7 @@ public function testDispatchesEventToTracers(): void
$tracer
->expects($this->once())
->method('trace')
->with($this->identicalTo($event))
->willThrowException(new RuntimeException('third-party exception'));
->with($this->identicalTo($event));

$dispatcher->registerTracer($tracer);

Expand Down

0 comments on commit 6ce7c0b

Please sign in to comment.