ldc-arcadia-api/tests/Unit/Infra/Http/Helpers/RequestHelperTest.php

116 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Infra\Http\Helpers;
use Illuminate\Http\Request as HttpRequest;
use Infra\Http\Helpers\RequestHelper;
use Shared\Pagination;
use Uri\Rfc3986\Uri;
covers(RequestHelper::class);
describe('RequestHelper Unit Tests', function (): void {
it('prevents direct instantiation but covers constructor', function (): void {
// GIVEN
$reflection = new \ReflectionClass(RequestHelper::class);
// WHEN
$constructor = $reflection->getConstructor();
$constructor?->invoke($reflection->newInstanceWithoutConstructor());
// THEN
expect(fn () => $reflection->newInstance())
->toThrow(\ReflectionException::class);
});
it('creates offset and limit pagination when offset or limit query param is present', function (): void {
// GIVEN
$request = HttpRequest::create('/api/v1/employees?offset=10&limit=50', 'GET');
// WHEN
$pagination = RequestHelper::pagination($request, 25);
// THEN
expect($pagination)->toBeInstanceOf(Pagination::class)
->and($pagination->from())->toBe(10)
->and($pagination->to())->toBe(59);
});
it('creates page and page size pagination using pageSize query param', function (): void {
// GIVEN
$request = HttpRequest::create('/api/v1/employees?page=2&pageSize=15', 'GET');
// WHEN
$pagination = RequestHelper::pagination($request, 25);
// THEN
expect($pagination)->toBeInstanceOf(Pagination::class)
->and($pagination->from())->toBe(15)
->and($pagination->to())->toBe(29);
});
it('creates page and page size pagination using per_page query param fallback', function (): void {
// GIVEN
$request = HttpRequest::create('/api/v1/employees?page=3&per_page=20', 'GET');
// WHEN
$pagination = RequestHelper::pagination($request, 25);
// THEN
expect($pagination)->toBeInstanceOf(Pagination::class)
->and($pagination->from())->toBe(40)
->and($pagination->to())->toBe(59);
});
it('creates page and page size pagination using pagination query param fallback', function (): void {
// GIVEN
$request = HttpRequest::create('/api/v1/employees?page=1&pagination=30', 'GET');
// WHEN
$pagination = RequestHelper::pagination($request, 25);
// THEN
expect($pagination)->toBeInstanceOf(Pagination::class)
->and($pagination->from())->toBe(0)
->and($pagination->to())->toBe(29);
});
it('uses default page size when no pagination parameters are supplied', function (): void {
// GIVEN
$request = HttpRequest::create('/api/v1/employees', 'GET');
// WHEN
$pagination = RequestHelper::pagination($request, 25);
// THEN
expect($pagination)->toBeInstanceOf(Pagination::class)
->and($pagination->from())->toBe(0)
->and($pagination->to())->toBe(24);
});
it('converts request URL to native RFC 3986 Uri instance', function (): void {
// GIVEN
$request = HttpRequest::create('http://localhost/api/v1/employees?foo=bar', 'GET');
// WHEN
$uri = RequestHelper::toUri($request);
// THEN
expect($uri)->toBeInstanceOf(Uri::class)
->and($uri->toString())->toBe('http://localhost/api/v1/employees');
});
it('converts full request URL with query parameters to native RFC 3986 Uri instance', function (): void {
// GIVEN
$request = HttpRequest::create('http://localhost/api/v1/employees?page=2&pageSize=10', 'GET');
// WHEN
$uri = RequestHelper::toFullUri($request);
// THEN
expect($uri)->toBeInstanceOf(Uri::class)
->and($uri->toString())->toBe('http://localhost/api/v1/employees?page=2&pageSize=10');
});
});