From 6ed962fe724b021ab0ed0bd32bbdfda3f0aa371c Mon Sep 17 00:00:00 2001 From: Sjors Ottjes Date: Thu, 14 Jan 2021 14:09:01 +0100 Subject: [PATCH] add default parameter to throw_if/throw_unless --- src/Illuminate/Support/helpers.php | 4 ++-- tests/Support/SupportHelpersTest.php | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index f44371bb469e..6a35438699d5 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -277,7 +277,7 @@ function tap($value, $callback = null) * * @throws \Throwable */ - function throw_if($condition, $exception, ...$parameters) + function throw_if($condition, $exception = 'RuntimeException', ...$parameters) { if ($condition) { throw (is_string($exception) ? new $exception(...$parameters) : $exception); @@ -298,7 +298,7 @@ function throw_if($condition, $exception, ...$parameters) * * @throws \Throwable */ - function throw_unless($condition, $exception, ...$parameters) + function throw_unless($condition, $exception = 'RuntimeException', ...$parameters) { if (! $condition) { throw (is_string($exception) ? new $exception(...$parameters) : $exception); diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index bb3e5ead59ce..32faabe2fcde 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Env; use Illuminate\Support\Optional; +use LogicException; use Mockery as m; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -362,10 +363,31 @@ public function testTap() } public function testThrow() + { + $this->expectException(LogicException::class); + + throw_if(true, new LogicException); + } + + public function testThrowDefaultException() + { + $this->expectException(RuntimeException::class); + + throw_if(true); + } + + public function testThrowUnless() + { + $this->expectException(LogicException::class); + + throw_unless(false, new LogicException); + } + + public function testThrowUnlessDefaultException() { $this->expectException(RuntimeException::class); - throw_if(true, new RuntimeException); + throw_unless(false); } public function testThrowReturnIfNotThrown()