diff --git a/ChangeLog-11.5.md b/ChangeLog-11.5.md index 70eb16eb20d..c6b700e61ec 100644 --- a/ChangeLog-11.5.md +++ b/ChangeLog-11.5.md @@ -4,6 +4,10 @@ All notable changes of the PHPUnit 11.5 release series are documented in this fi ## [11.5.3] - 2025-MM-DD +### Added + +* `Test\AfterLastTestMethodErrored`, `Test\AfterTestMethodErrored`, `Test\BeforeTestMethodErrored`, `Test\PostConditionErrored`, and `Test\PreConditionErrored` events + ### Fixed * [#6095](https://github.com/sebastianbergmann/phpunit/issues/6095): Expectation is not counted correctly when a doubled method is called more often than is expected diff --git a/src/Event/Emitter/DispatchingEmitter.php b/src/Event/Emitter/DispatchingEmitter.php index 235cadf5e66..b400912b8fe 100644 --- a/src/Event/Emitter/DispatchingEmitter.php +++ b/src/Event/Emitter/DispatchingEmitter.php @@ -412,6 +412,24 @@ public function testBeforeTestMethodCalled(string $testClassName, ClassMethod $c * @throws InvalidArgumentException * @throws UnknownEventTypeException */ + public function testBeforeTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void + { + $this->dispatcher->dispatch( + new Test\BeforeTestMethodErrored( + $this->telemetryInfo(), + $testClassName, + $calledMethod, + $throwable, + ), + ); + } + + /** + * @psalm-param class-string $testClassName + * + * @throws InvalidArgumentException + * @throws UnknownEventTypeException + */ public function testBeforeTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void { $this->dispatcher->dispatch( @@ -446,6 +464,24 @@ public function testPreConditionCalled(string $testClassName, ClassMethod $calle * @throws InvalidArgumentException * @throws UnknownEventTypeException */ + public function testPreConditionErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void + { + $this->dispatcher->dispatch( + new Test\PreConditionErrored( + $this->telemetryInfo(), + $testClassName, + $calledMethod, + $throwable, + ), + ); + } + + /** + * @psalm-param class-string $testClassName + * + * @throws InvalidArgumentException + * @throws UnknownEventTypeException + */ public function testPreConditionFinished(string $testClassName, ClassMethod ...$calledMethods): void { $this->dispatcher->dispatch( @@ -1016,6 +1052,24 @@ public function testPostConditionCalled(string $testClassName, ClassMethod $call * @throws InvalidArgumentException * @throws UnknownEventTypeException */ + public function testPostConditionErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void + { + $this->dispatcher->dispatch( + new Test\PostConditionErrored( + $this->telemetryInfo(), + $testClassName, + $calledMethod, + $throwable, + ), + ); + } + + /** + * @psalm-param class-string $testClassName + * + * @throws InvalidArgumentException + * @throws UnknownEventTypeException + */ public function testPostConditionFinished(string $testClassName, ClassMethod ...$calledMethods): void { $this->dispatcher->dispatch( @@ -1050,6 +1104,24 @@ public function testAfterTestMethodCalled(string $testClassName, ClassMethod $ca * @throws InvalidArgumentException * @throws UnknownEventTypeException */ + public function testAfterTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void + { + $this->dispatcher->dispatch( + new Test\AfterTestMethodErrored( + $this->telemetryInfo(), + $testClassName, + $calledMethod, + $throwable, + ), + ); + } + + /** + * @psalm-param class-string $testClassName + * + * @throws InvalidArgumentException + * @throws UnknownEventTypeException + */ public function testAfterTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void { $this->dispatcher->dispatch( @@ -1084,6 +1156,24 @@ public function testAfterLastTestMethodCalled(string $testClassName, ClassMethod * @throws InvalidArgumentException * @throws UnknownEventTypeException */ + public function testAfterLastTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void + { + $this->dispatcher->dispatch( + new Test\AfterLastTestMethodErrored( + $this->telemetryInfo(), + $testClassName, + $calledMethod, + $throwable, + ), + ); + } + + /** + * @psalm-param class-string $testClassName + * + * @throws InvalidArgumentException + * @throws UnknownEventTypeException + */ public function testAfterLastTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void { $this->dispatcher->dispatch( diff --git a/src/Event/Emitter/Emitter.php b/src/Event/Emitter/Emitter.php index 7b29da2e5cb..741d0218a99 100644 --- a/src/Event/Emitter/Emitter.php +++ b/src/Event/Emitter/Emitter.php @@ -88,6 +88,11 @@ public function testBeforeTestMethodCalled(string $testClassName, ClassMethod $c /** * @param class-string $testClassName */ + public function testBeforeTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void; + + /** + * @psalm-param class-string $testClassName + */ public function testBeforeTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void; /** @@ -98,6 +103,11 @@ public function testPreConditionCalled(string $testClassName, ClassMethod $calle /** * @param class-string $testClassName */ + public function testPreConditionErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void; + + /** + * @psalm-param class-string $testClassName + */ public function testPreConditionFinished(string $testClassName, ClassMethod ...$calledMethods): void; public function testPrepared(Code\Test $test): void; @@ -254,6 +264,11 @@ public function testPostConditionCalled(string $testClassName, ClassMethod $call /** * @param class-string $testClassName */ + public function testPostConditionErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void; + + /** + * @psalm-param class-string $testClassName + */ public function testPostConditionFinished(string $testClassName, ClassMethod ...$calledMethods): void; /** @@ -264,6 +279,11 @@ public function testAfterTestMethodCalled(string $testClassName, ClassMethod $ca /** * @param class-string $testClassName */ + public function testAfterTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void; + + /** + * @psalm-param class-string $testClassName + */ public function testAfterTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void; /** @@ -274,6 +294,11 @@ public function testAfterLastTestMethodCalled(string $testClassName, ClassMethod /** * @param class-string $testClassName */ + public function testAfterLastTestMethodErrored(string $testClassName, ClassMethod $calledMethod, Throwable $throwable): void; + + /** + * @psalm-param class-string $testClassName + */ public function testAfterLastTestMethodFinished(string $testClassName, ClassMethod ...$calledMethods): void; public function testSuiteFinished(TestSuite $testSuite): void; diff --git a/src/Event/Events/Test/HookMethod/AfterLastTestMethodErrored.php b/src/Event/Events/Test/HookMethod/AfterLastTestMethodErrored.php new file mode 100644 index 00000000000..874bae43495 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/AfterLastTestMethodErrored.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use const PHP_EOL; +use function sprintf; +use PHPUnit\Event\Code; +use PHPUnit\Event\Code\Throwable; +use PHPUnit\Event\Event; +use PHPUnit\Event\Telemetry; + +/** + * @psalm-immutable + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +final class AfterLastTestMethodErrored implements Event +{ + private readonly Telemetry\Info $telemetryInfo; + + /** + * @psalm-var class-string + */ + private readonly string $testClassName; + private readonly Code\ClassMethod $calledMethod; + private readonly Throwable $throwable; + + /** + * @psalm-param class-string $testClassName + */ + public function __construct(Telemetry\Info $telemetryInfo, string $testClassName, Code\ClassMethod $calledMethod, Throwable $throwable) + { + $this->telemetryInfo = $telemetryInfo; + $this->testClassName = $testClassName; + $this->calledMethod = $calledMethod; + $this->throwable = $throwable; + } + + public function telemetryInfo(): Telemetry\Info + { + return $this->telemetryInfo; + } + + /** + * @psalm-return class-string + */ + public function testClassName(): string + { + return $this->testClassName; + } + + public function calledMethod(): Code\ClassMethod + { + return $this->calledMethod; + } + + public function throwable(): Throwable + { + return $this->throwable; + } + + public function asString(): string + { + $message = $this->throwable->message(); + + if (!empty($message)) { + $message = PHP_EOL . $message; + } + + return sprintf( + 'After Last Test Method Errored (%s::%s)%s', + $this->calledMethod->className(), + $this->calledMethod->methodName(), + $message, + ); + } +} diff --git a/src/Event/Events/Test/HookMethod/AfterLastTestMethodErroredSubscriber.php b/src/Event/Events/Test/HookMethod/AfterLastTestMethodErroredSubscriber.php new file mode 100644 index 00000000000..b994fdeb2f1 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/AfterLastTestMethodErroredSubscriber.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use PHPUnit\Event\Subscriber; + +/** + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +interface AfterLastTestMethodErroredSubscriber extends Subscriber +{ + public function notify(AfterLastTestMethodErrored $event): void; +} diff --git a/src/Event/Events/Test/HookMethod/AfterTestMethodErrored.php b/src/Event/Events/Test/HookMethod/AfterTestMethodErrored.php new file mode 100644 index 00000000000..676f4c506bd --- /dev/null +++ b/src/Event/Events/Test/HookMethod/AfterTestMethodErrored.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use const PHP_EOL; +use function sprintf; +use PHPUnit\Event\Code; +use PHPUnit\Event\Code\Throwable; +use PHPUnit\Event\Event; +use PHPUnit\Event\Telemetry; + +/** + * @psalm-immutable + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +final class AfterTestMethodErrored implements Event +{ + private readonly Telemetry\Info $telemetryInfo; + + /** + * @psalm-var class-string + */ + private readonly string $testClassName; + private readonly Code\ClassMethod $calledMethod; + private readonly Throwable $throwable; + + /** + * @psalm-param class-string $testClassName + */ + public function __construct(Telemetry\Info $telemetryInfo, string $testClassName, Code\ClassMethod $calledMethod, Throwable $throwable) + { + $this->telemetryInfo = $telemetryInfo; + $this->testClassName = $testClassName; + $this->calledMethod = $calledMethod; + $this->throwable = $throwable; + } + + public function telemetryInfo(): Telemetry\Info + { + return $this->telemetryInfo; + } + + /** + * @psalm-return class-string + */ + public function testClassName(): string + { + return $this->testClassName; + } + + public function calledMethod(): Code\ClassMethod + { + return $this->calledMethod; + } + + public function throwable(): Throwable + { + return $this->throwable; + } + + public function asString(): string + { + $message = $this->throwable->message(); + + if (!empty($message)) { + $message = PHP_EOL . $message; + } + + return sprintf( + 'After Test Method Errored (%s::%s)%s', + $this->calledMethod->className(), + $this->calledMethod->methodName(), + $message, + ); + } +} diff --git a/src/Event/Events/Test/HookMethod/AfterTestMethodErroredSubscriber.php b/src/Event/Events/Test/HookMethod/AfterTestMethodErroredSubscriber.php new file mode 100644 index 00000000000..622f91625d9 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/AfterTestMethodErroredSubscriber.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use PHPUnit\Event\Subscriber; + +/** + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +interface AfterTestMethodErroredSubscriber extends Subscriber +{ + public function notify(AfterTestMethodErrored $event): void; +} diff --git a/src/Event/Events/Test/HookMethod/BeforeTestMethodErrored.php b/src/Event/Events/Test/HookMethod/BeforeTestMethodErrored.php new file mode 100644 index 00000000000..0293fb8b335 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/BeforeTestMethodErrored.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use const PHP_EOL; +use function sprintf; +use PHPUnit\Event\Code; +use PHPUnit\Event\Code\Throwable; +use PHPUnit\Event\Event; +use PHPUnit\Event\Telemetry; + +/** + * @psalm-immutable + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +final class BeforeTestMethodErrored implements Event +{ + private readonly Telemetry\Info $telemetryInfo; + + /** + * @psalm-var class-string + */ + private readonly string $testClassName; + private readonly Code\ClassMethod $calledMethod; + private readonly Throwable $throwable; + + /** + * @psalm-param class-string $testClassName + */ + public function __construct(Telemetry\Info $telemetryInfo, string $testClassName, Code\ClassMethod $calledMethod, Throwable $throwable) + { + $this->telemetryInfo = $telemetryInfo; + $this->testClassName = $testClassName; + $this->calledMethod = $calledMethod; + $this->throwable = $throwable; + } + + public function telemetryInfo(): Telemetry\Info + { + return $this->telemetryInfo; + } + + /** + * @psalm-return class-string + */ + public function testClassName(): string + { + return $this->testClassName; + } + + public function calledMethod(): Code\ClassMethod + { + return $this->calledMethod; + } + + public function throwable(): Throwable + { + return $this->throwable; + } + + public function asString(): string + { + $message = $this->throwable->message(); + + if (!empty($message)) { + $message = PHP_EOL . $message; + } + + return sprintf( + 'Before Test Method Errored (%s::%s)%s', + $this->calledMethod->className(), + $this->calledMethod->methodName(), + $message, + ); + } +} diff --git a/src/Event/Events/Test/HookMethod/BeforeTestMethodErroredSubscriber.php b/src/Event/Events/Test/HookMethod/BeforeTestMethodErroredSubscriber.php new file mode 100644 index 00000000000..e53771c4944 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/BeforeTestMethodErroredSubscriber.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use PHPUnit\Event\Subscriber; + +/** + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +interface BeforeTestMethodErroredSubscriber extends Subscriber +{ + public function notify(BeforeTestMethodErrored $event): void; +} diff --git a/src/Event/Events/Test/HookMethod/PostConditionErrored.php b/src/Event/Events/Test/HookMethod/PostConditionErrored.php new file mode 100644 index 00000000000..9aa4f6f2f03 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/PostConditionErrored.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use const PHP_EOL; +use function sprintf; +use PHPUnit\Event\Code; +use PHPUnit\Event\Code\Throwable; +use PHPUnit\Event\Event; +use PHPUnit\Event\Telemetry; + +/** + * @psalm-immutable + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +final class PostConditionErrored implements Event +{ + private readonly Telemetry\Info $telemetryInfo; + + /** + * @psalm-var class-string + */ + private readonly string $testClassName; + private readonly Code\ClassMethod $calledMethod; + private readonly Throwable $throwable; + + /** + * @psalm-param class-string $testClassName + */ + public function __construct(Telemetry\Info $telemetryInfo, string $testClassName, Code\ClassMethod $calledMethod, Throwable $throwable) + { + $this->telemetryInfo = $telemetryInfo; + $this->testClassName = $testClassName; + $this->calledMethod = $calledMethod; + $this->throwable = $throwable; + } + + public function telemetryInfo(): Telemetry\Info + { + return $this->telemetryInfo; + } + + /** + * @psalm-return class-string + */ + public function testClassName(): string + { + return $this->testClassName; + } + + public function calledMethod(): Code\ClassMethod + { + return $this->calledMethod; + } + + public function throwable(): Throwable + { + return $this->throwable; + } + + public function asString(): string + { + $message = $this->throwable->message(); + + if (!empty($message)) { + $message = PHP_EOL . $message; + } + + return sprintf( + 'Post Condition Method Errored (%s::%s)%s', + $this->calledMethod->className(), + $this->calledMethod->methodName(), + $message, + ); + } +} diff --git a/src/Event/Events/Test/HookMethod/PostConditionErroredSubscriber.php b/src/Event/Events/Test/HookMethod/PostConditionErroredSubscriber.php new file mode 100644 index 00000000000..7bd2c54ce70 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/PostConditionErroredSubscriber.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use PHPUnit\Event\Subscriber; + +/** + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +interface PostConditionErroredSubscriber extends Subscriber +{ + public function notify(PostConditionErrored $event): void; +} diff --git a/src/Event/Events/Test/HookMethod/PreConditionErrored.php b/src/Event/Events/Test/HookMethod/PreConditionErrored.php new file mode 100644 index 00000000000..7bfbc372a14 --- /dev/null +++ b/src/Event/Events/Test/HookMethod/PreConditionErrored.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use const PHP_EOL; +use function sprintf; +use PHPUnit\Event\Code; +use PHPUnit\Event\Code\Throwable; +use PHPUnit\Event\Event; +use PHPUnit\Event\Telemetry; + +/** + * @psalm-immutable + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +final class PreConditionErrored implements Event +{ + private readonly Telemetry\Info $telemetryInfo; + + /** + * @psalm-var class-string + */ + private readonly string $testClassName; + private readonly Code\ClassMethod $calledMethod; + private readonly Throwable $throwable; + + /** + * @psalm-param class-string $testClassName + */ + public function __construct(Telemetry\Info $telemetryInfo, string $testClassName, Code\ClassMethod $calledMethod, Throwable $throwable) + { + $this->telemetryInfo = $telemetryInfo; + $this->testClassName = $testClassName; + $this->calledMethod = $calledMethod; + $this->throwable = $throwable; + } + + public function telemetryInfo(): Telemetry\Info + { + return $this->telemetryInfo; + } + + /** + * @psalm-return class-string + */ + public function testClassName(): string + { + return $this->testClassName; + } + + public function calledMethod(): Code\ClassMethod + { + return $this->calledMethod; + } + + public function throwable(): Throwable + { + return $this->throwable; + } + + public function asString(): string + { + $message = $this->throwable->message(); + + if (!empty($message)) { + $message = PHP_EOL . $message; + } + + return sprintf( + 'Pre Condition Method Errored (%s::%s)%s', + $this->calledMethod->className(), + $this->calledMethod->methodName(), + $message, + ); + } +} diff --git a/src/Event/Events/Test/HookMethod/PreConditionErroredSubscriber.php b/src/Event/Events/Test/HookMethod/PreConditionErroredSubscriber.php new file mode 100644 index 00000000000..3465040bbeb --- /dev/null +++ b/src/Event/Events/Test/HookMethod/PreConditionErroredSubscriber.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use PHPUnit\Event\Subscriber; + +/** + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + */ +interface PreConditionErroredSubscriber extends Subscriber +{ + public function notify(PreConditionErrored $event): void; +} diff --git a/src/Event/Facade.php b/src/Event/Facade.php index e7f7e4d8568..7304148192c 100644 --- a/src/Event/Facade.php +++ b/src/Event/Facade.php @@ -177,13 +177,16 @@ private function registerDefaultTypes(TypeMap $typeMap): void Test\DataProviderMethodFinished::class, Test\MarkedIncomplete::class, Test\AfterLastTestMethodCalled::class, + Test\AfterLastTestMethodErrored::class, Test\AfterLastTestMethodFinished::class, Test\AfterTestMethodCalled::class, + Test\AfterTestMethodErrored::class, Test\AfterTestMethodFinished::class, Test\BeforeFirstTestMethodCalled::class, Test\BeforeFirstTestMethodErrored::class, Test\BeforeFirstTestMethodFinished::class, Test\BeforeTestMethodCalled::class, + Test\BeforeTestMethodErrored::class, Test\BeforeTestMethodFinished::class, Test\ComparatorRegistered::class, Test\ConsideredRisky::class, @@ -201,8 +204,10 @@ private function registerDefaultTypes(TypeMap $typeMap): void Test\PhpunitWarningTriggered::class, Test\PhpWarningTriggered::class, Test\PostConditionCalled::class, + Test\PostConditionErrored::class, Test\PostConditionFinished::class, Test\PreConditionCalled::class, + Test\PreConditionErrored::class, Test\PreConditionFinished::class, Test\PreparationStarted::class, Test\Prepared::class, diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 11580bd7f74..65b0da40234 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -2330,6 +2330,7 @@ private function invokeBeforeClassHookMethods(array $hookMethods, Event\Emitter $hookMethods['beforeClass'], $emitter, 'testBeforeFirstTestMethodCalled', + 'testBeforeFirstTestMethodErrored', 'testBeforeFirstTestMethodFinished', ); } @@ -2345,6 +2346,7 @@ private function invokeBeforeTestHookMethods(array $hookMethods, Event\Emitter $ $hookMethods['before'], $emitter, 'testBeforeTestMethodCalled', + 'testBeforeTestMethodErrored', 'testBeforeTestMethodFinished', ); } @@ -2360,6 +2362,7 @@ private function invokePreConditionHookMethods(array $hookMethods, Event\Emitter $hookMethods['preCondition'], $emitter, 'testPreConditionCalled', + 'testPreConditionErrored', 'testPreConditionFinished', ); } @@ -2375,6 +2378,7 @@ private function invokePostConditionHookMethods(array $hookMethods, Event\Emitte $hookMethods['postCondition'], $emitter, 'testPostConditionCalled', + 'testPostConditionErrored', 'testPostConditionFinished', ); } @@ -2390,6 +2394,7 @@ private function invokeAfterTestHookMethods(array $hookMethods, Event\Emitter $e $hookMethods['after'], $emitter, 'testAfterTestMethodCalled', + 'testAfterTestMethodErrored', 'testAfterTestMethodFinished', ); } @@ -2407,17 +2412,19 @@ private function invokeAfterClassHookMethods(array $hookMethods, Event\Emitter $ $hookMethods['afterClass'], $emitter, 'testAfterLastTestMethodCalled', + 'testAfterLastTestMethodErrored', 'testAfterLastTestMethodFinished', ); } /** * @param 'testAfterLastTestMethodCalled'|'testAfterTestMethodCalled'|'testBeforeFirstTestMethodCalled'|'testBeforeTestMethodCalled'|'testPostConditionCalled'|'testPreConditionCalled' $calledMethod + * @param 'testAfterLastTestMethodErrored'|'testAfterTestMethodErrored'|'testBeforeFirstTestMethodErrored'|'testBeforeTestMethodErrored'|'testPostConditionErrored'|'testPreConditionErrored' $erroredMethod * @param 'testAfterLastTestMethodFinished'|'testAfterTestMethodFinished'|'testBeforeFirstTestMethodFinished'|'testBeforeTestMethodFinished'|'testPostConditionFinished'|'testPreConditionFinished' $finishedMethod * * @throws Throwable */ - private function invokeHookMethods(HookMethodCollection $hookMethods, Event\Emitter $emitter, string $calledMethod, string $finishedMethod): void + private function invokeHookMethods(HookMethodCollection $hookMethods, Event\Emitter $emitter, string $calledMethod, string $erroredMethod, string $finishedMethod): void { $methodsInvoked = []; @@ -2426,16 +2433,16 @@ private function invokeHookMethods(HookMethodCollection $hookMethods, Event\Emit continue; } - try { - $this->{$methodName}(); - } catch (Throwable $t) { - } - $methodInvoked = new Event\Code\ClassMethod( static::class, $methodName, ); + try { + $this->{$methodName}(); + } catch (Throwable $t) { + } + $emitter->{$calledMethod}( static::class, $methodInvoked @@ -2444,6 +2451,12 @@ private function invokeHookMethods(HookMethodCollection $hookMethods, Event\Emit $methodsInvoked[] = $methodInvoked; if (isset($t)) { + $emitter->{$erroredMethod}( + static::class, + $methodInvoked, + Event\Code\ThrowableBuilder::from($t), + ); + break; } } diff --git a/tests/end-to-end/event/_files/ExceptionInTearDownAfterClassTest.php b/tests/end-to-end/event/_files/ExceptionInTearDownAfterClassTest.php new file mode 100644 index 00000000000..22094ddd65d --- /dev/null +++ b/tests/end-to-end/event/_files/ExceptionInTearDownAfterClassTest.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\Event; + +use Exception; +use PHPUnit\Framework\TestCase; + +final class ExceptionInTearDownAfterClassTest extends TestCase +{ + public static function tearDownAfterClass(): void + { + throw new Exception; + } + + public function testOne(): void + { + $this->assertTrue(true); + } +} diff --git a/tests/end-to-end/event/_files/ExceptionInTearDownTest.php b/tests/end-to-end/event/_files/ExceptionInTearDownTest.php new file mode 100644 index 00000000000..5b7e35311f9 --- /dev/null +++ b/tests/end-to-end/event/_files/ExceptionInTearDownTest.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\Event; + +use Exception; +use PHPUnit\Framework\TestCase; + +final class ExceptionInTearDownTest extends TestCase +{ + protected function tearDown(): void + { + throw new Exception; + } + + public function testOne(): void + { + $this->assertTrue(true); + } +} diff --git a/tests/end-to-end/event/assertion-failure-in-after-test-method.phpt b/tests/end-to-end/event/assertion-failure-in-after-test-method.phpt index 409109ecf0a..712890831a6 100644 --- a/tests/end-to-end/event/assertion-failure-in-after-test-method.phpt +++ b/tests/end-to-end/event/assertion-failure-in-after-test-method.phpt @@ -23,6 +23,8 @@ Test Preparation Started (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTe Test Prepared (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::testOne) Test Passed (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::testOne) After Test Method Called (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::afterTest) +After Test Method Errored (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::afterTest) +Failed asserting that false is true. After Test Method Finished: - PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::afterTest Test Failed (PHPUnit\TestFixture\Event\AssertionFailureInTearDownTest::testOne) diff --git a/tests/end-to-end/event/assertion-failure-in-before-test-method.phpt b/tests/end-to-end/event/assertion-failure-in-before-test-method.phpt index c4b138ed4db..0e6bc67984a 100644 --- a/tests/end-to-end/event/assertion-failure-in-before-test-method.phpt +++ b/tests/end-to-end/event/assertion-failure-in-before-test-method.phpt @@ -21,6 +21,8 @@ Test Runner Execution Started (1 test) Test Suite Started (PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest, 1 test) Test Preparation Started (PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest::testOne) Before Test Method Called (PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest::beforeTest) +Before Test Method Errored (PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest::beforeTest) +Failed asserting that false is true. Before Test Method Finished: - PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest::beforeTest Test Preparation Failed (PHPUnit\TestFixture\Event\AssertionFailureInSetUpTest::testOne) diff --git a/tests/end-to-end/event/assertion-failure-in-postcondition-method.phpt b/tests/end-to-end/event/assertion-failure-in-postcondition-method.phpt index 4034eaf1e55..c181b6f005a 100644 --- a/tests/end-to-end/event/assertion-failure-in-postcondition-method.phpt +++ b/tests/end-to-end/event/assertion-failure-in-postcondition-method.phpt @@ -22,6 +22,8 @@ Test Suite Started (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTes Test Preparation Started (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::testOne) Test Prepared (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::testOne) Post Condition Method Called (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::postCondition) +Post Condition Method Errored (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::postCondition) +Failed asserting that false is true. Post Condition Method Finished: - PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::postCondition Test Failed (PHPUnit\TestFixture\Event\AssertionFailureInPostConditionTest::testOne) diff --git a/tests/end-to-end/event/assertion-failure-in-precondition-method.phpt b/tests/end-to-end/event/assertion-failure-in-precondition-method.phpt index 5e90267b8cc..d74ebd0de59 100644 --- a/tests/end-to-end/event/assertion-failure-in-precondition-method.phpt +++ b/tests/end-to-end/event/assertion-failure-in-precondition-method.phpt @@ -21,6 +21,8 @@ Test Runner Execution Started (1 test) Test Suite Started (PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest, 1 test) Test Preparation Started (PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest::testOne) Pre Condition Method Called (PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest::preCondition) +Pre Condition Method Errored (PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest::preCondition) +Failed asserting that false is true. Pre Condition Method Finished: - PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest::preCondition Test Preparation Failed (PHPUnit\TestFixture\Event\AssertionFailureInPreConditionTest::testOne) diff --git a/tests/end-to-end/event/exception-in-setup.phpt b/tests/end-to-end/event/exception-in-setup.phpt index a5d04f0278a..c797516793e 100644 --- a/tests/end-to-end/event/exception-in-setup.phpt +++ b/tests/end-to-end/event/exception-in-setup.phpt @@ -21,6 +21,7 @@ Test Runner Execution Started (1 test) Test Suite Started (PHPUnit\TestFixture\Event\ExceptionInSetUpTest, 1 test) Test Preparation Started (PHPUnit\TestFixture\Event\ExceptionInSetUpTest::testOne) Before Test Method Called (PHPUnit\TestFixture\Event\ExceptionInSetUpTest::setUp) +Before Test Method Errored (PHPUnit\TestFixture\Event\ExceptionInSetUpTest::setUp) Before Test Method Finished: - PHPUnit\TestFixture\Event\ExceptionInSetUpTest::setUp Test Errored (PHPUnit\TestFixture\Event\ExceptionInSetUpTest::testOne) diff --git a/tests/end-to-end/event/exception-in-teardown-after-class.phpt b/tests/end-to-end/event/exception-in-teardown-after-class.phpt new file mode 100644 index 00000000000..29c502db206 --- /dev/null +++ b/tests/end-to-end/event/exception-in-teardown-after-class.phpt @@ -0,0 +1,29 @@ +--TEST-- +The right events are emitted in the right order for when an exception is raised in tearDownAfterClass() +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit Started (PHPUnit %s using %s) +Test Runner Configured +Event Facade Sealed +Test Suite Loaded (1 test) +Test Runner Started +Test Suite Sorted +Test Runner Execution Started (1 test) +Test Suite Started (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest::testOne) +Test Prepared (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest::testOne) +Test Passed (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest::testOne) +Test Finished (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\Event\ExceptionInTearDownAfterClassTest, 1 test) +Test Runner Execution Finished +Test Runner Finished +PHPUnit Finished (Shell Exit Code: 0) diff --git a/tests/end-to-end/event/exception-in-teardown.phpt b/tests/end-to-end/event/exception-in-teardown.phpt new file mode 100644 index 00000000000..69091a30a72 --- /dev/null +++ b/tests/end-to-end/event/exception-in-teardown.phpt @@ -0,0 +1,34 @@ +--TEST-- +The right events are emitted in the right order for when an exception is raised in tearDown() +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit Started (PHPUnit %s using %s) +Test Runner Configured +Event Facade Sealed +Test Suite Loaded (1 test) +Test Runner Started +Test Suite Sorted +Test Runner Execution Started (1 test) +Test Suite Started (PHPUnit\TestFixture\Event\ExceptionInTearDownTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::testOne) +Test Prepared (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::testOne) +Test Passed (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::testOne) +After Test Method Called (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::tearDown) +After Test Method Errored (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::tearDown) +After Test Method Finished: +- PHPUnit\TestFixture\Event\ExceptionInTearDownTest::tearDown +Test Errored (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::testOne) +Test Finished (PHPUnit\TestFixture\Event\ExceptionInTearDownTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\Event\ExceptionInTearDownTest, 1 test) +Test Runner Execution Finished +Test Runner Finished +PHPUnit Finished (Shell Exit Code: 2) diff --git a/tests/end-to-end/event/test-skipped-in-setup.phpt b/tests/end-to-end/event/test-skipped-in-setup.phpt index aa6bc9806f9..072887c94eb 100644 --- a/tests/end-to-end/event/test-skipped-in-setup.phpt +++ b/tests/end-to-end/event/test-skipped-in-setup.phpt @@ -21,6 +21,7 @@ Test Runner Execution Started (1 test) Test Suite Started (PHPUnit\TestFixture\Event\SkippedInSetupTest, 1 test) Test Preparation Started (PHPUnit\TestFixture\Event\SkippedInSetupTest::testOne) Before Test Method Called (PHPUnit\TestFixture\Event\SkippedInSetupTest::setUp) +Before Test Method Errored (PHPUnit\TestFixture\Event\SkippedInSetupTest::setUp) Before Test Method Finished: - PHPUnit\TestFixture\Event\SkippedInSetupTest::setUp Test Skipped (PHPUnit\TestFixture\Event\SkippedInSetupTest::testOne) diff --git a/tests/unit/Event/Emitter/DispatchingEmitterTest.php b/tests/unit/Event/Emitter/DispatchingEmitterTest.php index b61dbb790f5..4e0aef7bb5a 100644 --- a/tests/unit/Event/Emitter/DispatchingEmitterTest.php +++ b/tests/unit/Event/Emitter/DispatchingEmitterTest.php @@ -615,6 +615,54 @@ public function notify(Test\AfterTestMethodCalled $event): void $this->assertSame($calledMethod, $event->calledMethod()); } + public function testTestAfterTestMethodErroredDispatchesTestAfterTestMethodErroredEvent(): void + { + $testClassName = self::class; + $calledMethod = new Code\ClassMethod(...array_values(explode( + '::', + __METHOD__, + ))); + + $subscriber = new class extends RecordingSubscriber implements Test\AfterTestMethodErroredSubscriber + { + public function notify(Test\AfterTestMethodErrored $event): void + { + $this->record($event); + } + }; + + $dispatcher = $this->dispatcherWithRegisteredSubscriber( + Test\AfterTestMethodErroredSubscriber::class, + Test\AfterTestMethodErrored::class, + $subscriber, + ); + + $telemetrySystem = $this->telemetrySystem(); + + $emitter = new DispatchingEmitter( + $dispatcher, + $telemetrySystem, + ); + + $throwable = ThrowableBuilder::from(new Exception('error')); + + $emitter->testAfterTestMethodErrored( + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame(1, $subscriber->recordedEventCount()); + + $event = $subscriber->lastRecordedEvent(); + + $this->assertInstanceOf(Test\AfterTestMethodErrored::class, $event); + + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + public function testTestAfterLastTestMethodFinishedDispatchesTestAfterLastTestMethodFinishedEvent(): void { $testClassName = self::class; @@ -706,6 +754,54 @@ public function notify(Test\BeforeFirstTestMethodCalled $event): void $this->assertSame($calledMethod, $event->calledMethod()); } + public function testTestAfterLastTestMethodErroredDispatchesTestAfterLastTestMethodErroredEvent(): void + { + $testClassName = self::class; + $calledMethod = new Code\ClassMethod(...array_values(explode( + '::', + __METHOD__, + ))); + + $subscriber = new class extends RecordingSubscriber implements Test\AfterLastTestMethodErroredSubscriber + { + public function notify(Test\AfterLastTestMethodErrored $event): void + { + $this->record($event); + } + }; + + $dispatcher = $this->dispatcherWithRegisteredSubscriber( + Test\AfterLastTestMethodErroredSubscriber::class, + Test\AfterLastTestMethodErrored::class, + $subscriber, + ); + + $telemetrySystem = $this->telemetrySystem(); + + $emitter = new DispatchingEmitter( + $dispatcher, + $telemetrySystem, + ); + + $throwable = ThrowableBuilder::from(new Exception('error')); + + $emitter->testAfterLastTestMethodErrored( + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame(1, $subscriber->recordedEventCount()); + + $event = $subscriber->lastRecordedEvent(); + + $this->assertInstanceOf(Test\AfterLastTestMethodErrored::class, $event); + + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + public function testTestBeforeFirstTestMethodFinishedDispatchesTestBeforeFirstTestMethodFinishedEvent(): void { $testClassName = self::class; @@ -797,6 +893,54 @@ public function notify(Test\BeforeTestMethodCalled $event): void $this->assertSame($calledMethod, $event->calledMethod()); } + public function testTestBeforeTestMethodErroredDispatchesTestBeforeTestMethodErroredEvent(): void + { + $testClassName = self::class; + $calledMethod = new Code\ClassMethod(...array_values(explode( + '::', + __METHOD__, + ))); + + $subscriber = new class extends RecordingSubscriber implements Test\BeforeTestMethodErroredSubscriber + { + public function notify(Test\BeforeTestMethodErrored $event): void + { + $this->record($event); + } + }; + + $dispatcher = $this->dispatcherWithRegisteredSubscriber( + Test\BeforeTestMethodErroredSubscriber::class, + Test\BeforeTestMethodErrored::class, + $subscriber, + ); + + $telemetrySystem = $this->telemetrySystem(); + + $emitter = new DispatchingEmitter( + $dispatcher, + $telemetrySystem, + ); + + $throwable = ThrowableBuilder::from(new Exception('error')); + + $emitter->testBeforeTestMethodErrored( + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame(1, $subscriber->recordedEventCount()); + + $event = $subscriber->lastRecordedEvent(); + + $this->assertInstanceOf(Test\BeforeTestMethodErrored::class, $event); + + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + public function testTestPreConditionCalledDispatchesTestPreConditionCalledEvent(): void { $testClassName = self::class; diff --git a/tests/unit/Event/Events/Test/HookMethod/AfterLastTestMethodErroredTest.php b/tests/unit/Event/Events/Test/HookMethod/AfterLastTestMethodErroredTest.php new file mode 100644 index 00000000000..f79c58599d6 --- /dev/null +++ b/tests/unit/Event/Events/Test/HookMethod/AfterLastTestMethodErroredTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use Exception; +use PHPUnit\Event\AbstractEventTestCase; +use PHPUnit\Event\Code; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Small; + +#[CoversClass(AfterLastTestMethodErrored::class)] +#[Small] +final class AfterLastTestMethodErroredTest extends AbstractEventTestCase +{ + public function testConstructorSetsValues(): void + { + $telemetryInfo = $this->telemetryInfo(); + $testClassName = 'Test'; + $calledMethod = $this->calledMethod(); + $throwable = Code\ThrowableBuilder::from(new Exception('message')); + + $event = new AfterLastTestMethodErrored( + $telemetryInfo, + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame($telemetryInfo, $event->telemetryInfo()); + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + + public function testCanBeRepresentedAsString(): void + { + $event = new AfterLastTestMethodErrored( + $this->telemetryInfo(), + 'test class name', + $this->calledMethod(), + Code\ThrowableBuilder::from(new Exception('message')), + ); + + $this->assertStringEqualsStringIgnoringLineEndings( + <<<'EOT' +After Last Test Method Errored (HookClass::hookMethod) +message +EOT, + $event->asString(), + ); + } + + private function calledMethod(): Code\ClassMethod + { + return new Code\ClassMethod('HookClass', 'hookMethod'); + } +} diff --git a/tests/unit/Event/Events/Test/HookMethod/AfterTestMethodErroredTest.php b/tests/unit/Event/Events/Test/HookMethod/AfterTestMethodErroredTest.php new file mode 100644 index 00000000000..23e40d12285 --- /dev/null +++ b/tests/unit/Event/Events/Test/HookMethod/AfterTestMethodErroredTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use Exception; +use PHPUnit\Event\AbstractEventTestCase; +use PHPUnit\Event\Code; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Small; + +#[CoversClass(AfterTestMethodErrored::class)] +#[Small] +final class AfterTestMethodErroredTest extends AbstractEventTestCase +{ + public function testConstructorSetsValues(): void + { + $telemetryInfo = $this->telemetryInfo(); + $testClassName = 'Test'; + $calledMethod = $this->calledMethod(); + $throwable = Code\ThrowableBuilder::from(new Exception('message')); + + $event = new AfterTestMethodErrored( + $telemetryInfo, + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame($telemetryInfo, $event->telemetryInfo()); + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + + public function testCanBeRepresentedAsString(): void + { + $event = new AfterTestMethodErrored( + $this->telemetryInfo(), + 'test class name', + $this->calledMethod(), + Code\ThrowableBuilder::from(new Exception('message')), + ); + + $this->assertStringEqualsStringIgnoringLineEndings( + <<<'EOT' +After Test Method Errored (HookClass::hookMethod) +message +EOT, + $event->asString(), + ); + } + + private function calledMethod(): Code\ClassMethod + { + return new Code\ClassMethod('HookClass', 'hookMethod'); + } +} diff --git a/tests/unit/Event/Events/Test/HookMethod/BeforeTestMethodErroredTest.php b/tests/unit/Event/Events/Test/HookMethod/BeforeTestMethodErroredTest.php new file mode 100644 index 00000000000..8da6c27e5f7 --- /dev/null +++ b/tests/unit/Event/Events/Test/HookMethod/BeforeTestMethodErroredTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use Exception; +use PHPUnit\Event\AbstractEventTestCase; +use PHPUnit\Event\Code; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Small; + +#[CoversClass(BeforeTestMethodErrored::class)] +#[Small] +final class BeforeTestMethodErroredTest extends AbstractEventTestCase +{ + public function testConstructorSetsValues(): void + { + $telemetryInfo = $this->telemetryInfo(); + $testClassName = 'Test'; + $calledMethod = $this->calledMethod(); + $throwable = Code\ThrowableBuilder::from(new Exception('message')); + + $event = new BeforeTestMethodErrored( + $telemetryInfo, + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame($telemetryInfo, $event->telemetryInfo()); + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + + public function testCanBeRepresentedAsString(): void + { + $event = new BeforeTestMethodErrored( + $this->telemetryInfo(), + 'Test', + $this->calledMethod(), + Code\ThrowableBuilder::from(new Exception('message')), + ); + + $this->assertStringEqualsStringIgnoringLineEndings( + <<<'EOT' +Before Test Method Errored (HookClass::hookMethod) +message +EOT, + $event->asString(), + ); + } + + private function calledMethod(): Code\ClassMethod + { + return new Code\ClassMethod('HookClass', 'hookMethod'); + } +} diff --git a/tests/unit/Event/Events/Test/HookMethod/PostConditionErroredTest.php b/tests/unit/Event/Events/Test/HookMethod/PostConditionErroredTest.php new file mode 100644 index 00000000000..65c882186ec --- /dev/null +++ b/tests/unit/Event/Events/Test/HookMethod/PostConditionErroredTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use Exception; +use PHPUnit\Event\AbstractEventTestCase; +use PHPUnit\Event\Code; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Small; + +#[CoversClass(PostConditionErrored::class)] +#[Small] +final class PostConditionErroredTest extends AbstractEventTestCase +{ + public function testConstructorSetsValues(): void + { + $telemetryInfo = $this->telemetryInfo(); + $testClassName = 'Test'; + $calledMethod = $this->calledMethod(); + $throwable = Code\ThrowableBuilder::from(new Exception('message')); + + $event = new PostConditionErrored( + $telemetryInfo, + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame($telemetryInfo, $event->telemetryInfo()); + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + + public function testCanBeRepresentedAsString(): void + { + $event = new PostConditionErrored( + $this->telemetryInfo(), + 'test class name', + $this->calledMethod(), + Code\ThrowableBuilder::from(new Exception('message')), + ); + + $this->assertStringEqualsStringIgnoringLineEndings( + <<<'EOT' +Post Condition Method Errored (HookClass::hookMethod) +message +EOT, + $event->asString(), + ); + } + + private function calledMethod(): Code\ClassMethod + { + return new Code\ClassMethod('HookClass', 'hookMethod'); + } +} diff --git a/tests/unit/Event/Events/Test/HookMethod/PreConditionErroredTest.php b/tests/unit/Event/Events/Test/HookMethod/PreConditionErroredTest.php new file mode 100644 index 00000000000..f8aa0141874 --- /dev/null +++ b/tests/unit/Event/Events/Test/HookMethod/PreConditionErroredTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Event\Test; + +use Exception; +use PHPUnit\Event\AbstractEventTestCase; +use PHPUnit\Event\Code; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Small; + +#[CoversClass(PreConditionErrored::class)] +#[Small] +final class PreConditionErroredTest extends AbstractEventTestCase +{ + public function testConstructorSetsValues(): void + { + $telemetryInfo = $this->telemetryInfo(); + $testClassName = 'Test'; + $calledMethod = $this->calledMethod(); + $throwable = Code\ThrowableBuilder::from(new Exception('message')); + + $event = new PreConditionErrored( + $telemetryInfo, + $testClassName, + $calledMethod, + $throwable, + ); + + $this->assertSame($telemetryInfo, $event->telemetryInfo()); + $this->assertSame($testClassName, $event->testClassName()); + $this->assertSame($calledMethod, $event->calledMethod()); + $this->assertSame($throwable, $event->throwable()); + } + + public function testCanBeRepresentedAsString(): void + { + $event = new PreConditionErrored( + $this->telemetryInfo(), + 'test class name', + $this->calledMethod(), + Code\ThrowableBuilder::from(new Exception('message')), + ); + + $this->assertStringEqualsStringIgnoringLineEndings( + <<<'EOT' +Pre Condition Method Errored (HookClass::hookMethod) +message +EOT, + $event->asString(), + ); + } + + private function calledMethod(): Code\ClassMethod + { + return new Code\ClassMethod('HookClass', 'hookMethod'); + } +}