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
31 changes: 0 additions & 31 deletions src/Phaseolies/Http/Support/InteractsWithInsightErrorTracking.php

This file was deleted.

19 changes: 11 additions & 8 deletions src/Phaseolies/Http/Support/RequestAbortion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class RequestAbortion
{
use InteractsWithInsightErrorTracking;

/**
* Abort the request with a specific HTTP status code and optional message.
*
Expand All @@ -26,8 +24,6 @@ public function abort(int $code, string $message = '', array $headers = []): voi
$shouldJsonResponse = $request->isAjax() || $request->isApiRequest();
$httpException = HttpException::fromStatusCode($code, $message, null, $headers);

$this->recordInsightException($request, $httpException);

$customPath =
base_path(
'resources'
Expand All @@ -54,7 +50,7 @@ public function abort(int $code, string $message = '', array $headers = []): voi

if ($viewPath) {
throw (new HttpResponseException($message, $code, $httpException))
->setResponse($this->buildErrorViewResponse($viewPath, $code, $headers));
->setResponse($this->buildErrorViewResponse($viewPath, $code, $message, $headers, $httpException));
}
}

Expand Down Expand Up @@ -92,10 +88,18 @@ public function abortIf($condition, int $code, string $message = '', array $head
*
* @param string $viewPath
* @param int $statusCode
* @param string $message
* @param array<string, string> $headers
* @param mixed $original
* @return \Phaseolies\Http\Response
*/
protected function buildErrorViewResponse(string $viewPath, int $statusCode, array $headers = []): Response
protected function buildErrorViewResponse(
string $viewPath,
int $statusCode,
string $message = '',
array $headers = [],
mixed $original = null
): Response
{
if (ob_get_level() > 0) {
ob_get_clean();
Expand All @@ -106,7 +110,6 @@ protected function buildErrorViewResponse(string $viewPath, int $statusCode, arr
$content = ob_get_clean() ?: '';

return response($content, $statusCode, $headers)
->setOriginal($content)
->setStatusCode($statusCode);
->setOriginal($original ?? $content);
}
}
98 changes: 38 additions & 60 deletions tests/Requests/RequestAbortionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
use Mockery;
use Tests\Support\MockContainer;

class TestableRequestAbortion extends RequestAbortion
{
public function publicBuildErrorViewResponse(
string $viewPath,
int $statusCode,
string $message = '',
array $headers = [],
mixed $original = null
) {
return $this->buildErrorViewResponse($viewPath, $statusCode, $message, $headers, $original);
}
}

class RequestAbortionTest extends TestCase
{
protected RequestAbortion $requestAbortion;
Expand All @@ -39,7 +52,7 @@ protected function setUp(): void
];
PHP);
$this->container->bind('translator', fn() => new Translator(new FileLoader($this->translationPath), 'en'));
$this->requestAbortion = new RequestAbortion();
$this->requestAbortion = new TestableRequestAbortion();
}

protected function tearDown(): void
Expand Down Expand Up @@ -122,6 +135,30 @@ public function testAbortThrowsHttpExceptionForNonAjaxNonApiRequests()
}
}

public function testBuildErrorViewResponseMakesMessageAvailableToView(): void
{
$viewPath = sys_get_temp_dir() . '/phaseolies_request_abort_error_' . uniqid() . '.php';
file_put_contents($viewPath, <<<'PHP'
<?php echo $message === 'Route [/home] not found' ? 'Not Found' : $message; ?>
PHP);

$exception = HttpException::fromStatusCode(404, 'Route [/home] not found');
ob_start();
$response = $this->requestAbortion->publicBuildErrorViewResponse(
$viewPath,
404,
'Route [/home] not found',
[],
$exception
);

$this->assertSame(404, $response->getStatusCode());
$this->assertSame('Not Found', trim($response->getBody() ?? ''));
$this->assertSame($exception, $response->getOriginal());

@unlink($viewPath);
}

public function testAbortThrowsHttpExceptionWithHeaders()
{
$mockRequest = Mockery::mock(Request::class);
Expand Down Expand Up @@ -224,63 +261,4 @@ public function testAbortWithEmptyHeaders()
throw $e;
}
}

public function testRecordInsightExceptionDelegatesToInsightRecorder(): void
{
$sink = new stdClass();
$sink->captured = [];

$mockRequest = Mockery::mock(Request::class);
$this->container->instance('Doppar\\Insight\\Support\\ErrorHistoryRecorder', new class($sink) {
public function __construct(private readonly object $sink)
{
}

public function record($exception, $request): void
{
$this->sink->captured = [$exception, $request];
}
});

$exception = HttpException::fromStatusCode(404, 'Route missing');
$method = new \ReflectionMethod($this->requestAbortion, 'recordInsightException');
$method->invoke($this->requestAbortion, $mockRequest, $exception);

$this->assertSame($exception, $sink->captured[0]);
$this->assertSame($mockRequest, $sink->captured[1]);
$this->assertSame(404, $exception->getStatusCode());
}

public function testAbortRecordsInsightForAjaxAbort(): void
{
$sink = new stdClass();
$sink->captured = [];

$mockRequest = Mockery::mock(Request::class);
$mockRequest->shouldReceive('isAjax')->andReturn(true);
$mockRequest->shouldReceive('isApiRequest')->andReturn(false);
$this->container->instance('request', $mockRequest);
$this->container->instance('Doppar\\Insight\\Support\\ErrorHistoryRecorder', new class($sink) {
public function __construct(private readonly object $sink)
{
}

public function record($exception, $request): void
{
$this->sink->captured = [$exception, $request];
}
});

try {
$this->requestAbortion->abort(404, 'Ajax missing');
$this->fail('Expected HttpResponseException was not thrown.');
} catch (HttpResponseException $exception) {
$this->assertInstanceOf(HttpException::class, $sink->captured[0]);
$this->assertSame(404, $sink->captured[0]->getStatusCode());
$this->assertSame($mockRequest, $sink->captured[1]);
$this->assertSame(404, $exception->getStatusCode());
$this->assertTrue($exception->hasResponse());
$this->assertSame(404, $exception->getResponse()?->getStatusCode());
}
}
}
Loading