Passed
Push — html ( d230bc...4d98c2 )
by Peter
03:13
created

PaginationTest::testGetGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid\Pagination;
6
7
use AbterPhp\Framework\Form\Element\Select;
8
use PHPUnit\Framework\TestCase;
9
10
class PaginationTest extends TestCase
11
{
12
    public function getTestToStringDataProvider(): array
13
    {
14
        return [
15
            [1, 10, 8, 5, ['>1<']],
16
            [1, 10, 12, 5, ['>1<', '>2<']],
17
            [2, 10, 38, 5, ['>1<', '>2<', '>3<', '>4<']],
18
            [3, 10, 38, 3, ['>3<', '>4<']],
19
            [14, 20, 942, 5, ['>12<', '>13<', '>14<', '>15<', '>16<']],
20
            [200, 20, 5000, 9, ['>196<', '>197<', '>200<', '>203<', '>204<']],
21
        ];
22
    }
23
24
    /**
25
     * @dataProvider getTestToStringDataProvider
26
     *
27
     * @param int $page
28
     * @param int $pageSize
29
     * @param int $totalCount
30
     * @param int $numberCount
31
     * @param string[] $expectedResult
32
     */
33
    public function testToString(
34
        int $page,
35
        int $pageSize,
36
        int $totalCount,
37
        int $numberCount,
38
        array $expectedResult
39
    ): void {
40
        $params = ['page' => (string)$page];
41
42
        $sut = new Pagination($params, '', $numberCount, $pageSize, [$pageSize]);
43
44
        $sut->setTotalCount($totalCount);
45
46
        $actualResult = (string)$sut;
47
48
        foreach ($expectedResult as $number) {
49
            $this->assertStringContainsString("$number", $actualResult);
50
        }
51
52
        $this->assertSame($actualResult, (string)$sut);
53
    }
54
55
    public function testConstructFailureOnEvenNumberCount(): void
56
    {
57
        $this->expectException(\InvalidArgumentException::class);
58
59
        $params = ['page' => '1'];
60
61
        new Pagination($params, '', 4, 10, [10]);
62
    }
63
64
    public function testConstructFailureOnInvalidPageSize(): void
65
    {
66
        $this->expectException(\InvalidArgumentException::class);
67
68
        $params = ['page' => '1'];
69
70
        new Pagination($params, '', 5, 8, [10]);
71
    }
72
73
    public function testConstructFailureOnInvalidPageSizeParam(): void
74
    {
75
        $this->expectException(\InvalidArgumentException::class);
76
77
        $params = ['page' => '1', 'page-size' => -8];
78
79
        new Pagination($params, '', 4, 10, [10]);
80
    }
81
82
    public function testSetTotalCountFailureOnNegativeTotalCount(): void
83
    {
84
        $this->expectException(\InvalidArgumentException::class);
85
86
        $params = ['page' => '1'];
87
88
        $sut = new Pagination($params, '', 5, 10, [10]);
89
90
        $sut->setTotalCount(-1);
91
    }
92
93
    public function testSetTotalCountFailureOutOfRangeTotalCount(): void
94
    {
95
        $this->expectException(\InvalidArgumentException::class);
96
97
        $params = ['page' => '2'];
98
99
        $sut = new Pagination($params, '', 5, 10, [10]);
100
101
        $sut->setTotalCount(0);
102
    }
103
104
    public function testGetNodes(): void
105
    {
106
        $sut = new Pagination([], '', 5, 10, [10]);
107
108
        $actualResult = $sut->getNodes();
109
110
        $this->assertSame([], $actualResult);
111
    }
112
113
    public function testGetExtendedNodes(): void
114
    {
115
        $sut = new Pagination([], '', 5, 10, [10]);
116
117
        $actualResult = $sut->getExtendedNodes();
118
119
        $this->assertCount(2, $actualResult);
120
        $this->assertInstanceOf(Numbers::class, $actualResult[0]);
121
        $this->assertInstanceOf(Select::class, $actualResult[1]);
122
    }
123
124
    public function testTemplateIsChangeable(): void
125
    {
126
        $sut = new Pagination([], '', 5, 10, [10]);
127
128
        $sut->setTemplate('<foo>%1$s</foo><bar>%2$s%3$s</bar>');
129
130
        $this->assertMatchesRegularExpression('/\<foo\>.*\<\/foo\>\<bar\>.*\<\/bar\>/', (string)$sut);
131
    }
132
133
    public function testSetSortedUrlCanSetsUrlOnNumbersBeforeTotalCountIsSet(): void
134
    {
135
        $url = '/foo?';
136
137
        $sut = new Pagination([], '', 5, 10, [10]);
138
139
        $sut->setSortedUrl($url);
140
141
        $sut->setTotalCount(100);
142
143
        $actualResult = (string)$sut;
144
145
        $this->assertStringContainsString($url, $actualResult);
146
    }
147
148
    public function testSetSortedUrlCanNotSetsUrlOnNumbersAfterTotalCountIsSet(): void
149
    {
150
        $url = '/foo?';
151
152
        $sut = new Pagination([], '', 5, 10, [10]);
153
154
        $sut->setTotalCount(100);
155
156
        $sut->setSortedUrl($url);
157
158
        $actualResult = (string)$sut;
159
160
        $this->assertStringNotContainsString($url, $actualResult);
161
    }
162
163
    public function testGetGroup()
164
    {
165
        $expectedResult = 10;
166
167
        $sut = new Pagination([], '', 5, 10, [10]);
168
169
        $pageSize = $sut->getPageSize();
170
171
        $this->assertSame($expectedResult, $pageSize);
172
    }
173
}
174