diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a00b9..cb8dbd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Raised minimum required PHP version to 8.3.0 * Deprecated `bovigo\assert\CatchedError`, introduced `bovigo\assert\TriggeredError` instead +* Deprecated `bovigo\assert\CatchedException`, introduced `bovigo\assert\CaughtThrowable` instead * Fixed implicitly nullable type declarations * Prevented deprecation notice about used const E_STRICT diff --git a/src/main/php/CatchedException.php b/src/main/php/CatchedException.php index 3d649ab..ec0b403 100644 --- a/src/main/php/CatchedException.php +++ b/src/main/php/CatchedException.php @@ -17,6 +17,7 @@ * Allows to make assertions on a catched exception. * * @since 1.6.0 + * @deprecated will be replaced by CaughtThrowable in release 9.0.0 */ class CatchedException { diff --git a/src/main/php/CaughtThrowable.php b/src/main/php/CaughtThrowable.php new file mode 100644 index 0000000..5a97565 --- /dev/null +++ b/src/main/php/CaughtThrowable.php @@ -0,0 +1,19 @@ +runCode(); if (null === $this->exception) { @@ -83,7 +83,7 @@ public function throws(string|Throwable|null $expected = null): CatchedException assertThat($this->exception, $isExpected); } - return new CatchedException($this->exception); + return new CaughtThrowable($this->exception); } private function getType(string|Throwable $expected): string diff --git a/src/test/php/CatchedExceptionTest.php b/src/test/php/CatchedExceptionTest.php deleted file mode 100644 index dce8fc0..0000000 --- a/src/test/php/CatchedExceptionTest.php +++ /dev/null @@ -1,171 +0,0 @@ -> - */ - public static function throwables(): array - { - return ['exception' => [new Exception('failure', 2)], - 'error' => [new Error('failure', 2)] - ]; - } - - private function catchedException(Throwable $throwable): CatchedException - { - return new CatchedException($throwable); - } - - #[Test] - #[DataProvider('throwables')] - public function withMessageComparesUsingEquals(Throwable $throwable): void - { - assertThat( - $this->catchedException($throwable)->withMessage('failure'), - isInstanceOf(CatchedException::class) - ); - } - - #[Test] - #[DataProvider('throwables')] - public function withMessageFailsThrowsAssertionFailure(Throwable $throwable): void - { - expect(fn() => $this->catchedException($throwable)->withMessage('error')) - ->throws(AssertionFailure::class) - ->withMessage( - "Failed asserting that exception message 'failure' is equal to . ---- Expected -+++ Actual -@@ @@ --'error' -+'failure' -" - ); - } - - #[Test] - #[DataProvider('throwables')] - public function messageAssertsWithGivenPredicate(Throwable $throwable): void - { - assertThat( - $this->catchedException($throwable)->message(contains('fail')), - isInstanceOf(CatchedException::class) - ); - } - - /** - * @since 5.0.1 - */ - #[Test] - #[DataProvider('throwables')] - public function messageAssertsWithGivenCallable(Throwable $throwable): void - { - assertThat( - $this->catchedException($throwable)->message('is_string'), - isInstanceOf(CatchedException::class) - ); - } - - #[Test] - #[DataProvider('throwables')] - public function messageAssertsWithGivenPredicateThrowsAssertionFailureWhenPredicateFails( - Throwable $throwable - ): void { - expect(fn() => $this->catchedException($throwable)->message(contains('error'))) - ->throws(AssertionFailure::class) - ->withMessage( - "Failed asserting that exception message 'failure' contains 'error'." - ); - } - - #[Test] - #[DataProvider('throwables')] - public function withCodeComparesUsingEquals(Throwable $throwable): void - { - assertThat( - $this->catchedException($throwable)->withCode(2), - isInstanceOf(CatchedException::class) - ); - } - - #[Test] - #[DataProvider('throwables')] - public function withCodeFailsThrowsAssertionFailure(Throwable $throwable): void - { - expect(fn() => $this->catchedException($throwable)->withCode(3)) - ->throws(AssertionFailure::class) - ->withMessage( - "Failed asserting that exception code 2 is equal to 3." - ); - } - - #[Test] - #[DataProvider('throwables')] - public function withAppliesPredicateToException(Throwable $throwable): void - { - $this->catchedException($throwable)->with(isSameAs($throwable)); - } - - #[Test] - #[DataProvider('throwables')] - public function withReturnsSelfOnSuccess(Throwable $throwable): void - { - $catchedException = $this->catchedException($throwable); - assertThat( - $catchedException->with(fn() => true), - isSameAs($catchedException) - ); - } - - #[Test] - #[DataProvider('throwables')] - public function withThrowsAssertionFailureWhenPredicateFails(Throwable $throwable): void - { - expect(fn() => $this->catchedException($throwable)->with( - isNotSameAs($throwable), - 'additional info' - )) - ->throws(AssertionFailure::class) - ->withMessage( - 'Failed asserting that object of type "' . get_class($throwable) - . '" is not identical to object of type "' . get_class($throwable) - . '". -additional info' - ); - } - - #[Test] - #[DataProvider('throwables')] - public function afterExecutesGivenPredicateWithGivenValue(Throwable $throwable): void - { - $catchedException = $this->catchedException($throwable); - $catchedException->after($catchedException, isSameAs($catchedException)); - } -} diff --git a/src/test/php/CaughtThrowableTest.php b/src/test/php/CaughtThrowableTest.php new file mode 100644 index 0000000..f477a17 --- /dev/null +++ b/src/test/php/CaughtThrowableTest.php @@ -0,0 +1,166 @@ + [new CaughtThrowable($exception), $exception]; + $error = new Error('failure', 2); + yield 'error' => [new CaughtThrowable($error), $error]; + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withMessageComparesUsingEquals(CaughtThrowable $caughtThrowable): void + { + assertThat( + $caughtThrowable->withMessage('failure'), + isInstanceOf(CaughtThrowable::class) + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withMessageFailsThrowsAssertionFailure(CaughtThrowable $caughtThrowable): void + { + expect(fn() => $caughtThrowable->withMessage('error')) + ->throws(AssertionFailure::class) + ->withMessage( + "Failed asserting that exception message 'failure' is equal to . +--- Expected ++++ Actual +@@ @@ +-'error' ++'failure' +" + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function messageAssertsWithGivenPredicate(CaughtThrowable $caughtThrowable): void + { + assertThat( + $caughtThrowable->message(contains('fail')), + isInstanceOf(CaughtThrowable::class) + ); + } + + /** + * @since 5.0.1 + */ + #[Test] + #[DataProvider('provideThrowables')] + public function messageAssertsWithGivenCallable(CaughtThrowable $caughtThrowable): void + { + assertThat( + $caughtThrowable->message('is_string'), + isInstanceOf(CaughtThrowable::class) + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function messageAssertsWithGivenPredicateThrowsAssertionFailureWhenPredicateFails( + CaughtThrowable $caughtThrowable + ): void { + expect(fn() => $caughtThrowable->message(contains('error'))) + ->throws(AssertionFailure::class) + ->withMessage( + "Failed asserting that exception message 'failure' contains 'error'." + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withCodeComparesUsingEquals(CaughtThrowable $caughtThrowable): void + { + assertThat( + $caughtThrowable->withCode(2), + isInstanceOf(CaughtThrowable::class) + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withCodeFailsThrowsAssertionFailure(CaughtThrowable $caughtThrowable): void + { + expect(fn() => $caughtThrowable->withCode(3)) + ->throws(AssertionFailure::class) + ->withMessage( + "Failed asserting that exception code 2 is equal to 3." + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withAppliesPredicateToException( + CaughtThrowable $caughtThrowable, + Throwable $throwable + ): void { + $caughtThrowable->with(isSameAs($throwable)); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withReturnsSelfOnSuccess(CaughtThrowable $caughtThrowable): void + { + assertThat( + $caughtThrowable->with(fn() => true), + isSameAs($caughtThrowable) + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function withThrowsAssertionFailureWhenPredicateFails( + CaughtThrowable $caughtThrowable, + Throwable $throwable + ): void { + expect(fn() => $caughtThrowable->with( + isNotSameAs($throwable), + 'additional info' + )) + ->throws(AssertionFailure::class) + ->withMessage( + 'Failed asserting that object of type "' . get_class($throwable) + . '" is not identical to object of type "' . get_class($throwable) + . '". +additional info' + ); + } + + #[Test] + #[DataProvider('provideThrowables')] + public function afterExecutesGivenPredicateWithGivenValue(CaughtThrowable $caughtThrowable): void + { + $caughtThrowable->after($caughtThrowable, isSameAs($caughtThrowable)); + } +} diff --git a/src/test/php/ExpectationTest.php b/src/test/php/ExpectationTest.php index 2d6ef48..a1098b3 100644 --- a/src/test/php/ExpectationTest.php +++ b/src/test/php/ExpectationTest.php @@ -60,7 +60,7 @@ public function expectationReturnsCatchedExceptionWhenThrowsSucceeds( ): void { assertThat( expect(fn() => throw $throwable)->throws(), - isInstanceOf(CatchedException::class) + isInstanceOf(CaughtThrowable::class) ); }