From 1a7baad3382ceb6f184bde8936b6f41730217a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Sat, 7 Aug 2021 08:22:16 +0300 Subject: [PATCH 1/5] add throwsIf exception --- src/PendingObjects/TestCall.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index be839bff..f8ada58d 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -78,6 +78,26 @@ final class TestCall return $this; } + /** + * Asserts that the test throws the given `$exceptionClass` when called if the given condition is true. + * + * @param Closure|bool|int $condition + */ + public function throwsIf($condition, string $exception, string $exceptionMessage = null): TestCall + { + $condition = is_callable($condition) + ? $condition + : Closure::fromCallable(function () use ($condition): bool { + return (bool) $condition; + }); + + if ($condition() === true) { + return $this->throws($exception, $exceptionMessage); + } + + return $this; + } + /** * Runs the current test multiple times with * each item of the given `iterable`. From 8e32b88fc8f251674a86032913a70a24a3cdac02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Sat, 7 Aug 2021 08:22:26 +0300 Subject: [PATCH 2/5] add tests --- tests/Features/Exceptions.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index 37eaaeb9..9970c2a9 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -17,3 +17,23 @@ it('catch exceptions and messages', function () { it('can just define the message', function () { throw new Exception('Something bad happened'); })->throws('Something bad happened'); + +it('not catch exceptions if given condition is false', function () { + $this->assertTrue(true); +})->throwsIf(false, Exception::class); + +it('catch exceptions if given condition is true', function () { + throw new Exception('Something bad happened'); +})->throwsIf(function () { return true; }, Exception::class); + +it('catch exceptions and messages if given condition is true', function () { + throw new Exception('Something bad happened'); +})->throwsIf(true, Exception::class, 'Something bad happened'); + +it('can just define the message if given condition is true', function () { + throw new Exception('Something bad happened'); +})->throwsIf(true, 'Something bad happened'); + +it('can just define the message if given condition is 1', function () { + throw new Exception('Something bad happened'); +})->throwsIf(1, 'Something bad happened'); From 1d4c1a5359915e8e26ff3e597d506d0fa3eb2357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Sat, 7 Aug 2021 08:22:31 +0300 Subject: [PATCH 3/5] update snapshots --- tests/.snapshots/success.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index a6f69129..90293a29 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -107,6 +107,11 @@ ✓ it catch exceptions ✓ it catch exceptions and messages ✓ it can just define the message + ✓ it not catch exceptions if given condition is false + ✓ it catch exceptions if given condition is true + ✓ it catch exceptions and messages if given condition is true + ✓ it can just define the message if given condition is true + ✓ it can just define the message if given condition is 1 PASS Tests\Features\Expect\HigherOrder\methods ✓ it can access methods @@ -647,5 +652,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 419 passed + Tests: 4 incompleted, 9 skipped, 424 passed \ No newline at end of file From 3c3e6b160bbfd9f040e4b601fbae369588c7e8e7 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 24 Sep 2021 21:10:02 +0100 Subject: [PATCH 4/5] refactor: expectation when --- src/Expectation.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 7a1e6057..d39d0904 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -178,15 +178,16 @@ final class Expectation } /** - * It skips the tests in the callback if the condition is not truthy. + * Apply the callback if the given "condition" is truthy. * - * @param Closure|bool|string $condition + * @param (callable(): bool)|bool $condition + * @param callable(Expectation): mixed $callback */ public function when($condition, callable $callback): Expectation { $condition = is_callable($condition) ? $condition - : function () use ($condition) { + : static function () use ($condition): mixed { return $condition; }; From 4daf7ee4ab4004e51d385a0ccc1d33efe6db0027 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 24 Sep 2021 21:15:31 +0100 Subject: [PATCH 5/5] refactor: throwsIf method --- src/PendingObjects/TestCall.php | 10 +++++----- tests/.snapshots/success.txt | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index f8ada58d..287ca7a9 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -81,17 +81,17 @@ final class TestCall /** * Asserts that the test throws the given `$exceptionClass` when called if the given condition is true. * - * @param Closure|bool|int $condition + * @param (callable(): bool)|bool $condition */ public function throwsIf($condition, string $exception, string $exceptionMessage = null): TestCall { $condition = is_callable($condition) ? $condition - : Closure::fromCallable(function () use ($condition): bool { - return (bool) $condition; - }); + : static function () use ($condition): mixed { + return $condition; + }; - if ($condition() === true) { + if ($condition()) { return $this->throws($exception, $exceptionMessage); } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index cac11672..40ab0a49 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -695,5 +695,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 454 passed + Tests: 4 incompleted, 9 skipped, 459 passed \ No newline at end of file