Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add default parameter to throw_if / throw_unless #35888

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down