Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
73 / 73
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10use Phexium\Domain\Pagination;
11
12pest()->group('unit');
13
14describe('Total pages', function (): void {
15    it('returns correct total pages when perPage is one', function (): void {
16        $pagination = new Pagination(totalCount: 10, page: 1, perPage: 1);
17        expect($pagination->getTotalPages())->toBe(10);
18    });
19
20    it('computes total pages with exact division', function (): void {
21        $pagination = new Pagination(totalCount: 20, page: 1, perPage: 10);
22
23        expect($pagination->getTotalPages())->toBe(2);
24    });
25
26    it('rounds up when items do not fill the last page', function (): void {
27        $pagination = new Pagination(totalCount: 21, page: 1, perPage: 10);
28
29        expect($pagination->getTotalPages())->toBe(3);
30    });
31
32    it('returns one page when total equals perPage', function (): void {
33        $pagination = new Pagination(totalCount: 10, page: 1, perPage: 10);
34
35        expect($pagination->getTotalPages())->toBe(1);
36    });
37
38    it('returns zero pages when there are no items', function (): void {
39        $pagination = new Pagination(totalCount: 0, page: 1, perPage: 10);
40
41        expect($pagination->getTotalPages())->toBe(0);
42    });
43
44    it('returns zero pages when perPage is zero', function (): void {
45        $pagination = new Pagination(totalCount: 10, page: 1, perPage: 0);
46
47        expect($pagination->getTotalPages())->toBe(0);
48    });
49});
50
51describe('Navigation', function (): void {
52    it('has next page when not on the last page', function (): void {
53        $pagination = new Pagination(totalCount: 30, page: 1, perPage: 10);
54
55        expect($pagination->hasNextPage())->toBeTrue();
56    });
57
58    it('has no next page on the last page', function (): void {
59        $pagination = new Pagination(totalCount: 30, page: 3, perPage: 10);
60
61        expect($pagination->hasNextPage())->toBeFalse();
62    });
63
64    it('has no previous page on the first page', function (): void {
65        $pagination = new Pagination(totalCount: 30, page: 1, perPage: 10);
66
67        expect($pagination->hasPreviousPage())->toBeFalse();
68    });
69
70    it('has previous page when beyond the first page', function (): void {
71        $pagination = new Pagination(totalCount: 30, page: 2, perPage: 10);
72
73        expect($pagination->hasPreviousPage())->toBeTrue();
74    });
75
76    it('has no navigation on a single page', function (): void {
77        $pagination = new Pagination(totalCount: 5, page: 1, perPage: 10);
78
79        expect($pagination->hasNextPage())->toBeFalse()
80            ->and($pagination->hasPreviousPage())->toBeFalse()
81        ;
82    });
83});
84
85describe('Offset', function (): void {
86    it('returns zero offset for the first page', function (): void {
87        $pagination = new Pagination(totalCount: 50, page: 1, perPage: 10);
88
89        expect($pagination->getOffset())->toBe(0);
90    });
91
92    it('computes offset for subsequent pages', function (): void {
93        $pagination = new Pagination(totalCount: 50, page: 3, perPage: 10);
94
95        expect($pagination->getOffset())->toBe(20);
96    });
97
98    it('returns zero offset when perPage is negative', function (): void {
99        $pagination = new Pagination(totalCount: 10, page: 2, perPage: -1);
100        expect($pagination->getOffset())->toBe(0);
101    });
102
103    it('returns zero offset when perPage is zero', function (): void {
104        $pagination = new Pagination(totalCount: 10, page: 2, perPage: 0);
105
106        expect($pagination->getOffset())->toBe(0);
107    });
108
109    it('computes offset correctly when perPage is one', function (): void {
110        $pagination = new Pagination(totalCount: 10, page: 2, perPage: 1);
111        expect($pagination->getOffset())->toBe(1);
112    });
113});