From 6c3d8829cef6d74ead894bef245b0576d5e68eee Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Thu, 24 Aug 2023 09:28:47 +0700 Subject: [PATCH 1/2] feat: `throwsUnless` method --- src/PendingCalls/TestCall.php | 18 ++++++++++++++++++ tests/Features/Exceptions.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 9b1edabc..1bc29e7a 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -104,6 +104,24 @@ final class TestCall return $this; } + /** + * Asserts that the test throws the given `$exceptionClass` when called if the given condition is falsy. + * + * @param (callable(): bool)|bool $condition + */ + public function throwsUnless(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): self + { + $condition = is_callable($condition) + ? $condition + : static fn (): bool => $condition; + + if (! $condition()) { + return $this->throws($exception, $exceptionMessage, $exceptionCode); + } + + return $this; + } + /** * Runs the current test multiple times with * each item of the given `iterable`. diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index 3b45b0db..d0ce2c6c 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -59,3 +59,37 @@ it('can just define the message if given condition is 1', function () { it('can just define the code if given condition is 1', function () { throw new Exception('Something bad happened', 1); })->throwsIf(1, 1); + +it('not catch exceptions if given condition is true', function () { + $this->assertTrue(true); +})->throwsUnless(true, Exception::class); + +it('catch exceptions if given condition is falsy', function () { + throw new Exception('Something bad happened'); +})->throwsUnless(function () { + return false; +}, Exception::class); + +it('catch exceptions and messages if given condition is falsy', function () { + throw new Exception('Something bad happened'); +})->throwsUnless(false, Exception::class, 'Something bad happened'); + +it('catch exceptions, messages and code if given condition is falsy', function () { + throw new Exception('Something bad happened', 1); +})->throwsUnless(false, Exception::class, 'Something bad happened', 1); + +it('can just define the message if given condition is falsy', function () { + throw new Exception('Something bad happened'); +})->throwsUnless(false, 'Something bad happened'); + +it('can just define the code if given condition is falsy', function () { + throw new Exception('Something bad happened', 1); +})->throwsUnless(false, 1); + +it('can just define the message if given condition is 0', function () { + throw new Exception('Something bad happened'); +})->throwsUnless(0, 'Something bad happened'); + +it('can just define the code if given condition is 0', function () { + throw new Exception('Something bad happened', 1); +})->throwsUnless(0, 1); From e888f3613bfc749ea9a0e891cf35eb3b14f7545c Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Thu, 24 Aug 2023 16:40:30 +0700 Subject: [PATCH 2/2] refactor: change `falsy` to `false` --- src/PendingCalls/TestCall.php | 2 +- tests/Features/Exceptions.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 1bc29e7a..f30267c9 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -105,7 +105,7 @@ final class TestCall } /** - * Asserts that the test throws the given `$exceptionClass` when called if the given condition is falsy. + * Asserts that the test throws the given `$exceptionClass` when called if the given condition is false. * * @param (callable(): bool)|bool $condition */ diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index d0ce2c6c..a2f942f3 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -64,25 +64,25 @@ it('not catch exceptions if given condition is true', function () { $this->assertTrue(true); })->throwsUnless(true, Exception::class); -it('catch exceptions if given condition is falsy', function () { +it('catch exceptions if given condition is false', function () { throw new Exception('Something bad happened'); })->throwsUnless(function () { return false; }, Exception::class); -it('catch exceptions and messages if given condition is falsy', function () { +it('catch exceptions and messages if given condition is false', function () { throw new Exception('Something bad happened'); })->throwsUnless(false, Exception::class, 'Something bad happened'); -it('catch exceptions, messages and code if given condition is falsy', function () { +it('catch exceptions, messages and code if given condition is false', function () { throw new Exception('Something bad happened', 1); })->throwsUnless(false, Exception::class, 'Something bad happened', 1); -it('can just define the message if given condition is falsy', function () { +it('can just define the message if given condition is false', function () { throw new Exception('Something bad happened'); })->throwsUnless(false, 'Something bad happened'); -it('can just define the code if given condition is falsy', function () { +it('can just define the code if given condition is false', function () { throw new Exception('Something bad happened', 1); })->throwsUnless(false, 1);