ldc-arcadia-api/tests/Integration/Presentation/Web/WebExceptionRendererTest.php

60 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\Presentation\Web;
use Illuminate\Http\Request;
use Presentation\Web\Exceptions\WebExceptionRenderer;
use Shared\DDD\Exception\NotFoundException;
covers(WebExceptionRenderer::class);
describe('Web Exception Renderer', function (): void {
it('renders 404 view for NotFoundException on web requests', function (): void {
// GIVEN
$renderer = new WebExceptionRenderer();
$exception = new NotFoundException('Resource not found');
$request = Request::create('/some-web-page', 'GET');
// WHEN
$response = $renderer->render($exception, $request);
// THEN
expect($response)->not->toBeNull()
->and($response?->getStatusCode())->toBe(404);
});
it('returns null for API or JSON requests', function (): void {
// GIVEN
$renderer = new WebExceptionRenderer();
$exception = new NotFoundException('Resource not found');
$request = Request::create(
'/api/v1/employees',
'GET',
[],
[],
[],
['HTTP_ACCEPT' => 'application/json']
);
// WHEN
$response = $renderer->render($exception, $request);
// THEN
expect($response)->toBeNull();
});
it('returns null for unhandled exceptions', function (): void {
// GIVEN
$renderer = new WebExceptionRenderer();
$exception = new \InvalidArgumentException('Invalid argument');
$request = Request::create('/some-web-page', 'GET');
// WHEN
$response = $renderer->render($exception, $request);
// THEN
expect($response)->toBeNull();
});
});