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

- Added tests for creating Pagination from explicit indexes.
- Updated tests for invalid constructor inputs and parameters.
This commit is contained in:
Jérémy Munsch 2026-08-13 13:07:16 +02:00
parent 289140c630
commit aaa39004bc
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4

View file

@ -8,7 +8,7 @@ use Shared\Pagination;
covers(Pagination::class);
describe('Pagination Value Object', function (): void {
describe('Pagination Value Object Unit Tests', function (): void {
it('can be created from page and page size', function (): void {
// GIVEN
$page = 2;
@ -39,7 +39,21 @@ describe('Pagination Value Object', function (): void {
->and($pagination->pageNumber())->toBe(3);
});
it('can be created using first page', function (): void {
it('can be created from explicit indexes using ofIndexes', function (): void {
// GIVEN
$from = 5;
$to = 14;
// WHEN
$pagination = Pagination::ofIndexes($from, $to);
// THEN
expect($pagination->from())->toBe(5)
->and($pagination->to())->toBe(14)
->and($pagination->pageSize())->toBe(10);
});
it('can be created using first page helper', function (): void {
// GIVEN
$pageSize = 15;
@ -53,9 +67,47 @@ describe('Pagination Value Object', function (): void {
->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);
it('formats pagination as string representation via __toString', function (): void {
// GIVEN
$pagination = Pagination::ofIndexes(0, 9);
// WHEN
$stringRepresentation = (string) $pagination;
// THEN
expect($stringRepresentation)->toBe('Pagination{from=0, to=9}');
});
it('throws InvalidArgumentException for invalid constructor inputs', function (int $from, int $to, string $expectedMessage): void {
// GIVEN / WHEN / THEN
expect(fn () => new Pagination($from, $to))
->toThrow(\InvalidArgumentException::class, $expectedMessage);
})->with([
[-1, 10, 'From index must be non-negative'],
[0, -1, 'To index must be non-negative'],
[10, 5, 'To index must be greater than from index'],
[10, 10, 'To index must be greater than from index'],
[0, 10001, 'Page size must be less than or equal to 10000'],
]);
it('throws InvalidArgumentException for invalid ofPage parameters', function (int $page, int $size, string $expectedMessage): void {
// GIVEN / WHEN / THEN
expect(fn () => Pagination::ofPage($page, $size))
->toThrow(\InvalidArgumentException::class, $expectedMessage);
})->with([
[0, 10, 'Page number must be positive and non-zero'],
[-1, 10, 'Page number must be positive and non-zero'],
[1, 0, 'Page size must be positive and non-zero'],
[1, -5, 'Page size must be positive and non-zero'],
]);
it('throws InvalidArgumentException for invalid ofOffsetAndLimit parameters', function (int $offset, int $limit, string $expectedMessage): void {
// GIVEN / WHEN / THEN
expect(fn () => Pagination::ofOffsetAndLimit($offset, $limit))
->toThrow(\InvalidArgumentException::class, $expectedMessage);
})->with([
[-1, 10, 'Offset must be non-negative'],
[0, 0, 'Limit must be positive and non-zero'],
[0, -5, 'Limit must be positive and non-zero'],
]);
});