test(WebExceptionRenderer): add tests for rendering exceptions
Add tests to verify the behavior of WebExceptionRenderer for NotFoundException and unhandled exceptions.
This commit is contained in:
parent
52d121085a
commit
985fcedb04
2 changed files with 64 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Integration\Presentation\Web;
|
||||
|
||||
use Domain\Exceptions\NotFoundException;
|
||||
use Illuminate\Http\Request;
|
||||
use Presentation\Web\Exceptions\WebExceptionRenderer;
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
|
@ -29,6 +29,10 @@ pest()
|
|||
->use(RefreshDatabase::class)
|
||||
->in('Integration/WithApp');
|
||||
|
||||
pest()
|
||||
->extend(LaravelTestCase::class)
|
||||
->in('Integration/Presentation');
|
||||
|
||||
pest()
|
||||
->extend(BasePHPUnitTestCase::class)
|
||||
->in('Integration/Standalones');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue