From aaa39004bc9061f042747514ad1de5e8aba6d142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 13 Aug 2026 13:07:16 +0200 Subject: [PATCH] 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. --- tests/Unit/Shared/PaginationTest.php | 64 +++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/tests/Unit/Shared/PaginationTest.php b/tests/Unit/Shared/PaginationTest.php index 272ea4c..71ebadf 100644 --- a/tests/Unit/Shared/PaginationTest.php +++ b/tests/Unit/Shared/PaginationTest.php @@ -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'], + ]); });