Skip to content

Commit

Permalink
add default parameter to throw_if / throw_unless (#35890)
Browse files Browse the repository at this point in the history
  • Loading branch information
SjorsO committed Jan 14, 2021
1 parent 83ec510 commit 7d56eff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
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

0 comments on commit 7d56eff

Please sign in to comment.