test(Pagination): add unit tests for Pagination value object

This commit is contained in:
Jérémy Munsch 2026-08-13 11:50:16 +02:00
parent 8c0d139ad3
commit 198da8a11c
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4

View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Shared;
use Shared\Pagination;
covers(Pagination::class);
describe('Pagination Value Object', function (): void {
it('can be created from page and page size', function (): void {
// GIVEN
$page = 2;
$size = 20;
// WHEN
$pagination = Pagination::ofPage($page, $size);
// THEN
expect($pagination->from())->toBe(20)
->and($pagination->to())->toBe(39)
->and($pagination->pageSize())->toBe(20)
->and($pagination->pageNumber())->toBe(2);
});
it('can be created from offset and limit', function (): void {
// GIVEN
$offset = 20;
$limit = 10;
// WHEN
$pagination = Pagination::ofOffsetAndLimit($offset, $limit);
// THEN
expect($pagination->from())->toBe(20)
->and($pagination->to())->toBe(29)
->and($pagination->pageSize())->toBe(10)
->and($pagination->pageNumber())->toBe(3);
});
it('can be created using first page', function (): void {
// GIVEN
$pageSize = 15;
// WHEN
$pagination = Pagination::firstPage($pageSize);
// THEN
expect($pagination->from())->toBe(0)
->and($pagination->to())->toBe(14)
->and($pagination->pageSize())->toBe(15)
->and($pagination->pageNumber())->toBe(1);
});
it('throws exception for invalid page size or negative index', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => Pagination::ofPage(0, 10))
->toThrow(\InvalidArgumentException::class);
});
});