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 (attempt 2) #35890

Merged
merged 1 commit into from Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Illuminate/Support/helpers.php
Expand Up @@ -277,10 +277,14 @@ 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);
if (is_string($exception) && class_exists($exception)) {
$exception = new $exception(...$parameters);
}

throw is_string($exception) ? new RuntimeException($exception) : $exception;
}

return $condition;
Expand All @@ -298,10 +302,14 @@ 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);
if (is_string($exception) && class_exists($exception)) {
$exception = new $exception(...$parameters);
}

throw is_string($exception) ? new RuntimeException($exception) : $exception;
}

return $condition;
Expand Down
56 changes: 55 additions & 1 deletion tests/Support/SupportHelpersTest.php
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,63 @@ 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 testThrowExceptionWithMessage()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('test');

throw_if(true, 'test');
}

public function testThrowExceptionAsStringWithMessage()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('test');

throw_if(true, LogicException::class, 'test');
}

public function testThrowUnless()
{
$this->expectException(LogicException::class);

throw_unless(false, new LogicException);
}

public function testThrowUnlessDefaultException()
{
$this->expectException(RuntimeException::class);

throw_unless(false);
}

public function testThrowUnlessExceptionWithMessage()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('test');

throw_unless(false, 'test');
}

public function testThrowUnlessExceptionAsStringWithMessage()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('test');

throw_if(true, new RuntimeException);
throw_unless(false, LogicException::class, 'test');
}

public function testThrowReturnIfNotThrown()
Expand Down