Skip to content
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
31 changes: 31 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;

if (! function_exists('abort')) {
Expand Down Expand Up @@ -905,6 +906,36 @@ function session($key = null, $default = null)
}
}

if (! function_exists('status')) {
/**
* Create a response with the given code.
*
* @param int|null $code
* @return ($code is null ? object : \Illuminate\Http\Response)
*
* @throws RuntimeException
*/
function status($code = null)
{
if (! is_null($code)) {
return response()->noContent($code);
}

return new class
{
public function __call(string $name, array $arguments)
{
$constant = 'Symfony\Component\HttpFoundation\Response::HTTP_'.Str::upper(Str::snake($name));
if (! defined($constant)) {
throw new RuntimeException('Invalid status code method: '.$name);
}

return response()->noContent(constant($constant));
}
};
}
}

if (! function_exists('storage_path')) {
/**
* Get the path to the storage folder.
Expand Down
35 changes: 35 additions & 0 deletions tests/Integration/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Orchestra\Testbench\Attributes\WithConfig;
Expand Down Expand Up @@ -144,6 +145,40 @@ public function testFakeUsesLocale()
$this->assertSame('Australian Capital Territory', fake()->state());
}

public function testStatusCodeResponsesWithIntegers()
{
$code = Arr::random([100, 200, 201, 400, 409, 422, 500, 503]);

Route::get('test-status-helper', fn () => status($code));

$response = $this->get('/test-status-helper');

$response->assertStatus($code);
$response->assertContent('');
}

public function testStatusCodeResponsesWithMethodNames()
{
$status = Arr::random([100 => 'continue', 200 => 'ok', 405 => 'methodNotAllowed', 418 => 'iAmATeapot', 503 => 'serviceUnavailable'], 1, preserveKeys: true);
$code = key($status);
$name = current($status);

Route::get('test-status-helper', fn () => status()->$name());

$response = $this->get('/test-status-helper');

$response->assertStatus($code);
$response->assertContent('');
}

public function testStatusCodeResponsesThrowsException()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid status code method: unknownName');

status()->unknownName();
}

protected function makeManifest($directory = '')
{
app()->usePublicPath(__DIR__);
Expand Down