HeaderTest::testGetQueryParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid\Component;
6
7
use AbterPhp\Framework\Grid\Cell\Cell;
8
use AbterPhp\Framework\Grid\Cell\Sortable;
9
use AbterPhp\Framework\Grid\Collection\Cells;
10
use AbterPhp\Framework\Grid\Row\IRow;
11
use AbterPhp\Framework\Grid\Row\Row;
12
use PHPUnit\Framework\TestCase;
13
14
class HeaderTest extends TestCase
15
{
16
    public function testSetBaseUrlSetsBaseUrlOfAllSortables(): void
17
    {
18
        $expectedResult = '/foo';
19
20
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
21
22
        $sut->setBaseUrl($expectedResult);
23
24
        foreach ($sut as $row) {
25
            assert($row instanceof IRow);
26
27
            foreach ($row->getCells() as $cell) {
28
                if ($cell instanceof Sortable) {
29
                    $this->assertSame($expectedResult, $cell->getBaseUrl());
30
                } else {
31
                    $this->fail('$cell is not Sortable');
32
                }
33
            }
34
        }
35
    }
36
37
    public function testSetBaseUrlSkipsNonSortables(): void
38
    {
39
        $expectedResult = '/foo';
40
41
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
42
43
        $cells   = new Cells();
44
        $cells[] = new Cell('e', 'A');
45
        $cells[] = new Cell('f', 'B');
46
        $sut[]   = new Row($cells);
47
48
        $sut->setBaseUrl($expectedResult);
49
50
        foreach ($sut as $row) {
51
            assert($row instanceof IRow);
52
            foreach ($row->getCells() as $cell) {
53
                if (!($cell instanceof Sortable)) {
54
                    continue;
55
                }
56
57
                $this->assertSame($expectedResult, $cell->getBaseUrl());
58
            }
59
        }
60
    }
61
62
    public function testSetParamsSkipsNonSortables(): void
63
    {
64
        $params = [];
65
66
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
67
68
        $cells   = new Cells();
69
        $cells[] = new Cell('e', 'A');
70
        $cells[] = new Cell('f', 'B');
71
        $sut[]   = new Row($cells);
72
73
        $sut->setParams($params);
74
75
        foreach ($sut as $row) {
76
            assert($row instanceof IRow);
77
78
            foreach ($row->getCells() as $cell) {
79
                if (!($cell instanceof Sortable)) {
80
                    continue;
81
                }
82
83
                $this->assertNull($cell->getQueryParam());
84
            }
85
        }
86
    }
87
88
    /**
89
     * @return array[]
90
     */
91
    public function getSortedUrlProvider(): array
92
    {
93
        return [
94
            'no-params' => [
95
                [],
96
                '/foo?',
97
                '/foo?',
98
            ],
99
            'non-matching-params' => [
100
                ['bar' => '1'],
101
                '/foo?',
102
                '/foo?',
103
            ],
104
            'matching-param-zero-value-is-ignored' => [
105
                ['sort-A-input' => '0'],
106
                '/foo?',
107
                '/foo?',
108
            ],
109
            'one-matching-param-positive' => [
110
                ['sort-A-input' => '1'],
111
                '/foo?',
112
                '/foo?sort-A-input=1&',
113
            ],
114
            'one-matching-param-very-positive' => [
115
                ['sort-A-input' => '100'],
116
                '/foo?',
117
                '/foo?sort-A-input=100&',
118
            ],
119
            'one-matching-param-negative' => [
120
                ['sort-A-input' => '-1'],
121
                '/foo?',
122
                '/foo?sort-A-input=-1&',
123
            ],
124
            'one-matching-param-very-negative' => [
125
                ['sort-A-input' => '-100'],
126
                '/foo?',
127
                '/foo?sort-A-input=-100&',
128
            ],
129
            'matching-param-one-matching-one-ignored' => [
130
                ['sort-A-input' => '1', 'sort-B-input' => '0'],
131
                '/foo?',
132
                '/foo?sort-A-input=1&',
133
            ],
134
            'complex' => [
135
                ['sort-A-input' => '1', 'sort-B-input' => '-1'],
136
                '/foo?',
137
                '/foo?sort-A-input=1&sort-B-input=-1&',
138
            ],
139
        ]   ;
140
    }
141
142
    /**
143
     * @dataProvider getSortedUrlProvider
144
     *
145
     * @param array  $params
146
     * @param string $baseUrl
147
     * @param string $expectedResult
148
     */
149
    public function testGetSortedUrl(array $params, string $baseUrl, string $expectedResult): void
150
    {
151
        $sut = $this->createHeader(
152
            ['A' => 'A-input', 'B' => 'B-input'],
153
            ['A' => 'a_field', 'b' => 'b_field'],
154
            ['a' => 'A', 'b' => 'B']
155
        )  ;
156
157
        $sut->setParams($params);
158
159
        $actualResult = $sut->getSortedUrl($baseUrl);
160
161
        $this->assertSame($expectedResult, $actualResult);
162
    }
163
164
    public function testGetSortedUrlSkipsNonSortables(): void
165
    {
166
        $baseUrl = '/foo?';
167
        $params = [];
168
169
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
170
171
        $cells   = new Cells();
172
        $cells[] = new Cell('e', 'A');
173
        $cells[] = new Cell('f', 'B');
174
        $sut[]   = new Row($cells);
175
176
        $sut->setParams($params);
177
178
        $actualResult = $sut->getSortedUrl($baseUrl);
179
180
        $this->assertSame($baseUrl, $actualResult);
181
    }
182
183
    public function testGetSortedConditions(): void
184
    {
185
        $expectedResult = [
186
            'a_field ASC',
187
            'b_field DESC',
188
        ];
189
190
        $params = [
191
            'sort-A-input' => '1',
192
            'sort-B-input' => '-1',
193
        ];
194
195
        $sut = $this->createHeader(
196
            ['A' => 'A-input', 'B' => 'B-input'],
197
            ['A' => 'a_field', 'B' => 'b_field'],
198
            ['a' => 'A', 'b' => 'B']
199
        )  ;
200
201
        $sut->setParams($params);
202
203
        $actualResult = $sut->getSortConditions();
204
205
        $this->assertSame($expectedResult, $actualResult);
206
    }
207
208
    public function testGetSortConditionSkipsNonSortables(): void
209
    {
210
        $params = [];
211
212
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
213
214
        $cells   = new Cells();
215
        $cells[] = new Cell('e', 'A');
216
        $cells[] = new Cell('f', 'B');
217
        $sut[]   = new Row($cells);
218
219
        $sut->setParams($params);
220
221
        $actualResult = $sut->getSortConditions();
222
223
        $this->assertSame([], $actualResult);
224
    }
225
226
    /**
227
     * @param array $inputNames
228
     * @param array $fieldNames
229
     * @param array ...$rows
230
     *
231
     * @return Header
232
     */
233
    private function createHeader(array $inputNames, array $fieldNames, array ...$rows): Header
234
    {
235
        $sut = new Header();
236
237
        foreach ($rows as $row) {
238
            $cells = new Cells();
239
            foreach ($row as $content => $group) {
240
                $inputName = $inputNames[$group] ?? '';
241
                $fieldName = $fieldNames[$group] ?? '';
242
243
                $cells[] = new Sortable($content, $group, $inputName, $fieldName);
244
            }
245
            $sut[] = new Row($cells);
246
        }
247
248
        return $sut;
249
    }
250
251
    public function testGetQueryParams(): void
252
    {
253
        $sut = $this->createHeader([], [], ['a' => 'A', 'b' => 'B'], ['c' => 'A', 'd' => 'B']);
254
255
        $actualResult = $sut->getQueryParams();
256
257
        $this->assertSame([], $actualResult);
258
    }
259
}
260