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 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; // 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('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('clamps invalid constructor-level values via ofIndexes', function (int $from, int $to, int $expectedFrom, int $expectedTo): void { // GIVEN / WHEN $pagination = Pagination::ofIndexes($from, $to); // THEN expect($pagination->from())->toBe($expectedFrom) ->and($pagination->to())->toBe($expectedTo); })->with([ 'negative from is clamped to 0' => [-1, 10, 0, 10], 'to below from+1 is clamped to from+1' => [10, 5, 10, 11], 'to equal from is clamped to from+1' => [10, 10, 10, 11], 'oversized range is clamped to max' => [0, 10001, 0, 10000], ]); it('clamps invalid ofPage parameters', function (int $page, int $size, int $expectedPage, int $expectedSize): void { // GIVEN / WHEN $pagination = Pagination::ofPage($page, $size); // THEN expect($pagination->pageNumber())->toBe($expectedPage) ->and($pagination->pageSize())->toBe($expectedSize); })->with([ 'page 0 is clamped to 1' => [0, 10, 1, 10], 'negative page clamped to 1' => [-1, 10, 1, 10], 'size 0 is clamped to 1' => [1, 0, 1, 1], 'negative size clamped to 1' => [1, -5, 1, 1], 'oversized clamped to max' => [1, 20000, 1, 10000], ]); it('clamps invalid ofOffsetAndLimit parameters', function (int $offset, int $limit, int $expectedFrom, int $expectedSize): void { // GIVEN / WHEN $pagination = Pagination::ofOffsetAndLimit($offset, $limit); // THEN expect($pagination->from())->toBe($expectedFrom) ->and($pagination->pageSize())->toBe($expectedSize); })->with([ 'negative offset clamped to 0' => [-1, 10, 0, 10], 'limit 0 clamped to 1' => [0, 0, 0, 1], 'negative limit clamped to 1' => [0, -5, 0, 1], 'oversized limit clamped' => [0, 20000, 0, 10000], ]); });