Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 6, 2017
1 parent 8412be3 commit 74ecb34
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 46 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
Expand Down Expand Up @@ -667,6 +668,11 @@ function redirect($to = null, $status = 302, $headers = [], $secure = null)
*/
function report($exception)
{
if ($exception instanceof Throwable &&
! $exception instanceof Exception) {
$exception = new FatalThrowableError($exception);
}

app(ExceptionHandler::class)->report($exception);
}
}
Expand Down Expand Up @@ -695,6 +701,26 @@ function request($key = null, $default = null)
}
}

if (! function_exists('rescue')) {
/**
* Catch a potential exception and return a default value.
*
* @param callable $callback
* @param mixed $rescue
* @return mixed
*/
function rescue(callable $callback, $rescue)
{
try {
return $callback();
} catch (Throwable $e) {
report($e);

return value($rescue);
}
}
}

if (! function_exists('resolve')) {
/**
* Resolve a service from the container.
Expand Down
18 changes: 0 additions & 18 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,24 +742,6 @@ function preg_replace_array($pattern, array $replacements, $subject)
}
}

if (! function_exists('rescue')) {
/**
* Catch a potential exception and return a default.
*
* @param callable $rescuee
* @param mixed $rescuer
* @return mixed
*/
function rescue(callable $rescuee, $rescuer)
{
try {
return $rescuee();
} catch (Throwable $e) {
return is_callable($rescuer) ? $rescuer() : $rescuer;
}
}
}

if (! function_exists('retry')) {
/**
* Retry an operation a given number of times.
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Tests\Integration\Foundation;

use Exception;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class HelpersTest extends TestCase
{
public function test_rescue()
{
$this->assertEquals(rescue(function () {
throw new Exception;
}, 'rescued!'), 'rescued!');

$this->assertEquals(rescue(function () {
throw new Exception;
}, function () {
return 'rescued!';
}), 'rescued!');

$this->assertEquals(rescue(function () {
return 'no need to rescue';
}, 'rescued!'), 'no need to rescue');

$testClass = new class {
public function test(int $a)
{
return $a;
}
};

$this->assertEquals(rescue(function () use ($testClass) {
$testClass->test([]);
}, 'rescued!'), 'rescued!');
}
}
28 changes: 0 additions & 28 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,34 +794,6 @@ public function something()
})->present()->something());
}

public function testRescue()
{
$this->assertEquals(rescue(function () {
throw new Exception;
}, 'rescued!'), 'rescued!');

$this->assertEquals(rescue(function () {
throw new Exception;
}, function () {
return 'rescued!';
}), 'rescued!');

$this->assertEquals(rescue(function () {
return 'no need to rescue';
}, 'rescued!'), 'no need to rescue');

$testClass = new class {
public function test(int $a)
{
return $a;
}
};

$this->assertEquals(rescue(function () use ($testClass) {
$testClass->test([]);
}, 'rescued!'), 'rescued!');
}

public function testTransform()
{
$this->assertEquals(10, transform(5, function ($value) {
Expand Down

0 comments on commit 74ecb34

Please sign in to comment.