Skip to content

Commit 25ae457

Browse files
committed
Add tests
1 parent f0119d5 commit 25ae457

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator;
4+
use Illuminate\Support\Collection;
5+
6+
test('it returns the correct items', function () {
7+
$items = new Collection(['Item 1', 'Item 2', 'Item 3']);
8+
9+
$paginator = new LengthAwarePaginator($items, total: 3, perPage: 3, currentPage: 1);
10+
11+
expect($paginator->items())->toBe($items);
12+
});
13+
14+
test('it calculates the total correctly', function () {
15+
$paginator = new LengthAwarePaginator(new Collection, total: 25, perPage: 5, currentPage: 1);
16+
17+
expect($paginator->total())->toBe(25);
18+
});
19+
20+
test('it calculates the per page correctly', function () {
21+
$paginator = new LengthAwarePaginator(new Collection, total: 30, perPage: 5, currentPage: 1);
22+
23+
expect($paginator->perPage())->toBe(5);
24+
});
25+
26+
test('it can determine the current page', function () {
27+
$paginator = new LengthAwarePaginator(new Collection, total: 30, perPage: 5, currentPage: 2);
28+
29+
expect($paginator->currentPage())->toBe(2);
30+
});
31+
32+
test('it calculates the last page', function () {
33+
$paginator = new LengthAwarePaginator(new Collection, total: 30, perPage: 5, currentPage: 1);
34+
35+
expect($paginator->lastPage())->toBe(6);
36+
});
37+
38+
test('it can tell if there are enough items for multiple pages', function () {
39+
$paginator = new LengthAwarePaginator(new Collection, total: 6, perPage: 5, currentPage: 1);
40+
41+
expect($paginator->hasPages())->toBeTrue();
42+
});
43+
44+
test('it can detect if there are more pages', function () {
45+
$paginator = new LengthAwarePaginator(new Collection, total: 10, perPage: 5, currentPage: 1);
46+
47+
expect($paginator->hasMorePages())->toBeTrue();
48+
});
49+
50+
test('it provides a correct next page url', function () {
51+
$paginator = new LengthAwarePaginator(new Collection, total: 10, perPage: 5, currentPage: 1, path: 'http://example.com/users');
52+
53+
expect($paginator->nextPageUrl())->toBe('http://example.com/users?page=2');
54+
});
55+
56+
test('it provides a correct previous page url', function () {
57+
$paginator = new LengthAwarePaginator(new Collection, total: 10, perPage: 5, currentPage: 2, path: 'http://example.com/users');
58+
59+
expect($paginator->previousPageUrl())->toBe('http://example.com/users?page=1');
60+
});
61+
62+
test('it returns null for next page url when on last page', function () {
63+
$paginator = new LengthAwarePaginator(new Collection, total: 10, perPage: 5, currentPage: 2, path: 'http://example.com/users');
64+
65+
expect($paginator->nextPageUrl())->toBeNull();
66+
});
67+
68+
test('it returns null for previous page url when on first page', function () {
69+
$paginator = new LengthAwarePaginator(new Collection, total: 10, perPage: 5, currentPage: 1, path: 'http://example.com/users');
70+
71+
expect($paginator->previousPageUrl())->toBeNull();
72+
});
73+
74+
test('it returns an array representation', function () {
75+
$paginator = new LengthAwarePaginator(new Collection(['Item 1', 'Item 2']), total: 2, perPage: 2, currentPage: 1, path: 'http://example.com/users');
76+
77+
$array = $paginator->toArray();
78+
79+
expect($array)->toMatchArray([
80+
'path' => 'http://example.com/users',
81+
'data' => ['Item 1', 'Item 2'],
82+
'total' => 2,
83+
'per_page' => 2,
84+
'last_page' => 1,
85+
'current_page' => 1,
86+
'from' => 1,
87+
'to' => 2,
88+
'first_page_url' => 'http://example.com/users?page=1',
89+
'last_page_url' => 'http://example.com/users?page=1',
90+
'next_page_url' => null,
91+
'prev_page_url' => null,
92+
]);
93+
});
94+
95+
test('it preserves existing query parameters in the next page url', function () {
96+
$paginator = new LengthAwarePaginator(
97+
new Collection,
98+
total: 10,
99+
perPage: 5,
100+
currentPage: 1,
101+
path: 'http://example.com/users',
102+
query: ['foo' => 'bar']
103+
);
104+
105+
expect($paginator->nextPageUrl())->toBe('http://example.com/users?foo=bar&page=2');
106+
});

0 commit comments

Comments
 (0)