2026-08-13 11:50:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Shared;
|
|
|
|
|
|
|
|
|
|
use Shared\Pagination;
|
|
|
|
|
|
|
|
|
|
covers(Pagination::class);
|
|
|
|
|
|
2026-08-13 13:07:16 +02:00
|
|
|
describe('Pagination Value Object Unit Tests', function (): void {
|
2026-08-13 11:50:16 +02:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-13 13:07:16 +02:00
|
|
|
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 {
|
2026-08-13 11:50:16 +02:00
|
|
|
// 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);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-13 13:07:16 +02:00
|
|
|
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}');
|
2026-08-13 11:50:16 +02:00
|
|
|
});
|
2026-08-13 13:07:16 +02:00
|
|
|
|
|
|
|
|
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'],
|
|
|
|
|
]);
|
2026-08-13 11:50:16 +02:00
|
|
|
});
|