Passed
Pull Request — master (#653)
by Aleksei
07:43
created

PaginatorTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 11
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Pagination;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Pagination\Paginator;
16
use Spiral\Pagination\PaginatorInterface;
17
18
class PaginatorTest extends TestCase
19
{
20
    public function testInterfaces()
21
    {
22
        $paginator = new Paginator(25);
23
24
        $this->assertInstanceOf(PaginatorInterface::class, $paginator);
25
    }
26
27
    public function testParameterTracking()
28
    {
29
        $paginator = new Paginator(25, 0, 'request:page');
30
        $this->assertSame('request:page', $paginator->getParameter());
31
    }
32
33
    public function testLimit()
34
    {
35
        $paginator = new Paginator(25);
36
37
        $this->assertSame(25, $paginator->getLimit());
38
        $newPaginator = $paginator->withLimit(50);
39
        $this->assertSame(25, $paginator->getLimit());
40
        $this->assertSame(50, $newPaginator->getLimit());
41
    }
42
43
    public function testCountsAndPages()
44
    {
45
        $paginator = new Paginator(25);
46
47
        $this->assertSame(0, $paginator->count());
48
        $this->assertSame($paginator->count(), $paginator->count());
49
        $this->assertSame($paginator->count(), count($paginator));
50
51
        $this->assertSame(1, $paginator->getPage());
52
        $this->assertSame(0, $paginator->getOffset());
53
        $this->assertSame(1, $paginator->countPages());
54
        $this->assertSame(0, $paginator->countDisplayed());
55
    }
56
57
    public function testFirstPage()
58
    {
59
        $paginator = new Paginator(25);
60
        $paginator = $paginator->withCount(100);
61
62
        $this->assertSame(1, $paginator->getPage());
63
64
        $this->assertSame(null, $paginator->previousPage());
65
        $this->assertSame(2, $paginator->nextPage());
66
67
        $this->assertSame(100, $paginator->count());
68
        $this->assertSame($paginator->count(), $paginator->count());
69
        $this->assertSame($paginator->count(), count($paginator));
70
71
        $this->assertSame(0, $paginator->getOffset());
72
        $this->assertSame(4, $paginator->countPages());
73
        $this->assertSame(25, $paginator->countDisplayed());
74
    }
75
76
77
    public function testSecondPage()
78
    {
79
        $paginator = new Paginator(25);
80
        $paginator = $paginator->withCount(110);
81
82
        $this->assertSame(110, $paginator->count());
83
        $this->assertSame($paginator->count(), $paginator->count());
84
        $this->assertSame($paginator->count(), count($paginator));
85
86
        $this->assertSame(1, $paginator->getPage());
87
88
        $paginator = $paginator->withPage(2);
89
90
        $this->assertSame(1, $paginator->previousPage());
91
        $this->assertSame(3, $paginator->nextPage());
92
93
        $this->assertSame(2, $paginator->getPage());
94
        $this->assertSame(25, $paginator->getOffset());
95
        $this->assertSame(5, $paginator->countPages());
96
        $this->assertSame(25, $paginator->countDisplayed());
97
    }
98
99
    public function testLastPage()
100
    {
101
        $paginator = new Paginator(25);
102
        $paginator = $paginator->withCount(100);
103
104
        $this->assertSame(1, $paginator->getPage());
105
106
        $this->assertSame(null, $paginator->previousPage());
107
        $this->assertSame(2, $paginator->nextPage());
108
109
        $paginator = $paginator->withPage(100);
110
111
        $this->assertSame(4, $paginator->getPage());
112
        $this->assertSame(3, $paginator->previousPage());
113
        $this->assertSame(null, $paginator->nextPage());
114
115
        $this->assertSame(100, $paginator->count());
116
        $this->assertSame($paginator->count(), $paginator->count());
117
        $this->assertSame($paginator->count(), count($paginator));
118
119
        $this->assertSame(75, $paginator->getOffset());
120
        $this->assertSame(4, $paginator->countPages());
121
        $this->assertSame(25, $paginator->countDisplayed());
122
    }
123
124
    public function testNegativePage()
125
    {
126
        $paginator = new Paginator(25);
127
        $paginator = $paginator->withCount(100);
128
        $paginator = $paginator->withPage(-1);
129
130
        $this->assertSame(1, $paginator->getPage());
131
132
        $this->assertSame(100, $paginator->count());
133
        $this->assertSame($paginator->count(), $paginator->count());
134
        $this->assertSame($paginator->count(), count($paginator));
135
    }
136
137
    public function testNegativeCount()
138
    {
139
        $paginator = new Paginator(25);
140
        $paginator = $paginator->withCount(-100);
141
142
        $paginator = $paginator->withPage(-10);
143
        $this->assertSame(1, $paginator->getPage());
144
145
        $this->assertSame(null, $paginator->previousPage());
146
        $this->assertSame(null, $paginator->nextPage());
147
148
        $this->assertSame(0, $paginator->count());
149
        $this->assertSame(0, $paginator->getOffset());
150
        $this->assertSame(1, $paginator->countPages());
151
        $this->assertSame(0, $paginator->countDisplayed());
152
    }
153
154
    public function testLastPageNumber()
155
    {
156
        $paginator = new Paginator(25);
157
        $paginator = $paginator->withCount(110);
158
159
        $this->assertSame(110, $paginator->count());
160
        $this->assertSame(1, $paginator->getPage());
161
162
        $paginator = $paginator->withPage(100);
163
164
        $this->assertSame($paginator->countPages(), $paginator->getPage());
165
        $this->assertSame(
166
            ($paginator->getPage() - 1) * $paginator->getLimit(),
167
            $paginator->getOffset()
168
        );
169
170
        $this->assertSame(5, $paginator->countPages());
171
        $this->assertSame(10, $paginator->countDisplayed());
172
    }
173
174
    public function testIsRequired()
175
    {
176
        $paginator = new Paginator(25);
177
178
        $paginator = $paginator->withCount(24);
179
        $this->assertFalse($paginator->isRequired());
180
181
        $paginator = $paginator->withCount(25);
182
        $this->assertFalse($paginator->isRequired());
183
184
        $paginator = $paginator->withCount(26);
185
        $this->assertTrue($paginator->isRequired());
186
    }
187
}
188