62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Framework\ErrorHandling\Handlers;
|
|
|
|
use App\Framework\ErrorHandling\Handlers\ErrorHandlerPriority;
|
|
use App\Framework\ErrorHandling\Handlers\HttpErrorHandler;
|
|
use App\Framework\Http\Exception\HttpException;
|
|
use App\Framework\Http\Status;
|
|
|
|
describe('HttpErrorHandler', function () {
|
|
beforeEach(function () {
|
|
$this->handler = new HttpErrorHandler();
|
|
});
|
|
|
|
it('handles HttpException', function () {
|
|
$exception = new HttpException(
|
|
'Not Found',
|
|
Status::NOT_FOUND,
|
|
headers: ['X-Custom' => 'value']
|
|
);
|
|
|
|
expect($this->handler->canHandle($exception))->toBeTrue();
|
|
|
|
$result = $this->handler->handle($exception);
|
|
|
|
expect($result->handled)->toBeTrue();
|
|
expect($result->message)->toBe('Not Found');
|
|
expect($result->statusCode)->toBe(404);
|
|
expect($result->data['error_type'])->toBe('http');
|
|
expect($result->data['headers'])->toBe(['X-Custom' => 'value']);
|
|
});
|
|
|
|
it('handles HttpException with no headers', function () {
|
|
$exception = new HttpException(
|
|
'Bad Request',
|
|
Status::BAD_REQUEST
|
|
);
|
|
|
|
$result = $this->handler->handle($exception);
|
|
|
|
expect($result->handled)->toBeTrue();
|
|
expect($result->statusCode)->toBe(400);
|
|
expect($result->data['headers'])->toBe([]);
|
|
});
|
|
|
|
it('does not handle non-HttpException', function () {
|
|
$exception = new \RuntimeException('Some error');
|
|
|
|
expect($this->handler->canHandle($exception))->toBeFalse();
|
|
});
|
|
|
|
it('has NORMAL priority', function () {
|
|
expect($this->handler->getPriority())->toBe(ErrorHandlerPriority::NORMAL);
|
|
});
|
|
|
|
it('has correct name', function () {
|
|
expect($this->handler->getName())->toBe('http_error_handler');
|
|
});
|
|
});
|