Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Phaseolies/Error/WebErrorRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ public function renderProduction(Throwable $exception): Response
$content = ob_get_clean() ?: '';

return response($content, $statusCode)
->setOriginal($content)
->setStatusCode($statusCode);
->setOriginal($exception);
}

return response($message, $statusCode)
->setOriginal($message)
->setStatusCode($statusCode);
->setOriginal($exception);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/Phaseolies/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ function view($view, array $data = [], array $headers = []): Response
$instance = app(Controller::class);
$content = $instance->render($view, $data, true);

return response($content, 200, $headers)->setOriginal($content);
return response($content, 200, $headers)->setOriginal([
'view' => $view,
'data' => $data,
]);
}
}

Expand All @@ -205,11 +208,13 @@ function view($view, array $data = [], array $headers = []): Response
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
$redirect = new RedirectResponse();

if (is_null($to)) {
return app('redirect');
return $redirect;
}

return app('redirect')->to($to, $status, $headers, $secure);
return $redirect->to($to, $status, $headers, $secure);
}
}

Expand All @@ -224,7 +229,7 @@ function redirect($to = null, $status = 302, $headers = [], $secure = null)
*/
function back($status = 302, $headers = [], $fallback = false)
{
return app('redirect')->back($status, $headers, $fallback);
return redirect()->back($status, $headers, $fallback);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Phaseolies/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function setException($exception): static
* Set the HTTP status code for the response.
*
* @param int $statusCode The HTTP status code.
* @return int The updated status code.
* @return static
*/
public function setStatusCode(int $statusCode, ?string $text = null): static
{
Expand Down
1 change: 1 addition & 0 deletions src/Phaseolies/Http/Response/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function setTargetUrl(string $url): static
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
}

$this->setOriginal($url);
$this->setBody(
sprintf('<!DOCTYPE html>
<html>
Expand Down
2 changes: 2 additions & 0 deletions tests/Error/JsonErrorRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function testRenderReturnsJsonResponseInstance(): void

$payload = json_decode($response->getBody(), true);

$this->assertSame($payload, $response->getOriginal());
$this->assertSame('Boom', $payload['message']);
$this->assertIsArray($payload['errors']);
$this->assertArrayHasKey('trace', $payload['errors']);
Expand All @@ -96,6 +97,7 @@ public function testRenderUsesValidationEnvelopeForKnownStatuses(): void

$payload = json_decode($response->getBody(), true);

$this->assertSame($payload, $response->getOriginal());
$this->assertSame($errors, $payload['errors']);
$this->assertIsString($payload['message']);
$this->assertNotSame('', $payload['message']);
Expand Down
5 changes: 4 additions & 1 deletion tests/Error/WebErrorRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ protected function setUp(): void
public function testRenderProductionReturnsResponseForGenericException(): void
{
$renderer = new WebErrorRenderer();
$exception = new RuntimeException('Boom');

$response = $renderer->renderProduction(new RuntimeException('Boom'));
$response = $renderer->renderProduction($exception);

$this->assertInstanceOf(Response::class, $response);
$this->assertSame(500, $response->getStatusCode());
$this->assertSame('Something went wrong', $response->getBody());
$this->assertSame($exception, $response->getOriginal());
}

public function testRenderProductionPreservesHttpExceptionStatus(): void
Expand All @@ -41,5 +43,6 @@ public function testRenderProductionPreservesHttpExceptionStatus(): void

$this->assertSame(404, $response->getStatusCode());
$this->assertSame('Not Found', $response->getBody());
$this->assertSame($exception, $response->getOriginal());
}
}
18 changes: 17 additions & 1 deletion tests/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function set($key, $value)
}
};
}

public function except(...$keys): array
{
return [];
}
}

class MockMessageBag
Expand Down Expand Up @@ -125,6 +130,8 @@ protected function setUp(): void
MockRouter::$namedRoutes = [];

$this->setupGlobalMocks();
$container->instance('session', $this->session);
$container->instance('request', $this->request);

$this->replaceMessageBag();
}
Expand Down Expand Up @@ -198,6 +205,7 @@ public function testSetTargetUrlWithValidUrl()
$result = $this->redirect->setTargetUrl($url);

$this->assertSame($this->redirect, $result);
$this->assertSame($url, $this->redirect->getOriginal());
$this->assertEquals($url, $this->redirect->headers->get('Location'));
$this->assertEquals('text/html; charset=utf-8', $this->redirect->headers->get('Content-Type'));
$this->assertStringContainsString('Redirecting to', $this->redirect->getBody());
Expand All @@ -222,6 +230,7 @@ public function testToMethod()

$this->assertSame($this->redirect, $result);
$this->assertEquals($statusCode, $this->redirect->getStatusCode());
$this->assertSame($url, $this->redirect->getOriginal());
$this->assertEquals($url, $this->redirect->headers->get('Location'));
$this->assertEquals('value', $this->redirect->headers->get('X-Custom'));
}
Expand Down Expand Up @@ -259,6 +268,8 @@ public function testBackMethodWithReferer()

$this->assertSame($this->redirect, $result);
$this->assertEquals(301, $this->redirect->getStatusCode());
$this->assertSame($referer, $this->redirect->getOriginal());
$this->assertEquals($referer, $this->redirect->headers->get('Location'));
$this->assertEquals('value', $this->redirect->headers->get('X-Test'));
}

Expand All @@ -268,6 +279,7 @@ public function testBackMethodWithoutReferer()

$result = $this->redirect->back();

$this->assertSame('/', $this->redirect->getOriginal());
$this->assertEquals('/', $this->redirect->headers->get('Location'));
}

Expand All @@ -278,14 +290,17 @@ public function testIntendedMethodWithStoredUrl()

$result = $this->redirect->intended('/default');

$this->assertEquals($intendedUrl, $this->session->get('url.intended'));
$this->assertSame($intendedUrl, $this->redirect->getOriginal());
$this->assertEquals($intendedUrl, $this->redirect->headers->get('Location'));
$this->assertNull($this->session->get('url.intended'));
}

public function testIntendedMethodWithoutStoredUrl()
{
$defaultUrl = '/dashboard';
$result = $this->redirect->intended($defaultUrl, 301);

$this->assertSame($defaultUrl, $this->redirect->getOriginal());
$this->assertEquals($defaultUrl, $this->redirect->headers->get('Location'));
$this->assertEquals(301, $this->redirect->getStatusCode());
}
Expand All @@ -297,6 +312,7 @@ public function testAwayMethod()
$result = $this->redirect->away($externalUrl, 301);

$this->assertSame($this->redirect, $result);
$this->assertSame($externalUrl, $this->redirect->getOriginal());
$this->assertEquals($externalUrl, $this->redirect->headers->get('Location'));
$this->assertEquals(301, $this->redirect->getStatusCode());
}
Expand Down
Loading
Loading