PaginatorUtilsTraitTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 54
dl 0
loc 112
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A properlyTellsIfInLastPage() 0 3 1
A pageMessageIsProperlyFormatted() 0 3 1
A providePaginatorsToFormat() 0 11 1
A paginatorIsSerializedAsExpected() 0 4 1
A providePaginatorsInPage() 0 8 1
A providePaginatorAdapters() 0 56 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Paginator\Util;
6
7
use Laminas\Paginator\Adapter\ArrayAdapter;
8
use Laminas\Paginator\Paginator;
9
use PHPUnit\Framework\TestCase;
10
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
11
12
use function range;
13
14
class PaginatorUtilsTraitTest extends TestCase
15
{
16
    use PaginatorUtilsTrait;
17
18
    /**
19
     * @test
20
     * @dataProvider providePaginatorAdapters
21
     */
22
    public function paginatorIsSerializedAsExpected(array $expectedSerialization, Paginator $paginator): void
23
    {
24
        $result = $this->serializePaginator($paginator);
25
        $this->assertEquals($expectedSerialization, $result);
26
    }
27
28
    public function providePaginatorAdapters(): iterable
29
    {
30
        yield 'empty' => [
31
            [
32
                'data' => [],
33
                'pagination' => [
34
                    'currentPage' => 1,
35
                    'pagesCount' => 0,
36
                    'itemsPerPage' => 10,
37
                    'itemsInCurrentPage' => 0,
38
                    'totalItems' => 0,
39
                ],
40
            ],
41
            new Paginator(new ArrayAdapter([])),
42
        ];
43
44
        yield 'with two pages' => [
45
            [
46
                'data' => [1, 2],
47
                'pagination' => [
48
                    'currentPage' => 1,
49
                    'pagesCount' => 2,
50
                    'itemsPerPage' => 2,
51
                    'itemsInCurrentPage' => 2,
52
                    'totalItems' => 3,
53
                ],
54
            ],
55
            (new Paginator(new ArrayAdapter(range(1, 3))))->setItemCountPerPage(2),
56
        ];
57
58
        yield 'not in first page' => [
59
            [
60
                'data' => [7, 8, 9],
61
                'pagination' => [
62
                    'currentPage' => 3,
63
                    'pagesCount' => 5,
64
                    'itemsPerPage' => 3,
65
                    'itemsInCurrentPage' => 3,
66
                    'totalItems' => 15,
67
                ],
68
            ],
69
            (new Paginator(new ArrayAdapter(range(1, 15))))->setItemCountPerPage(3)->setCurrentPageNumber(3),
70
        ];
71
72
        yield 'last incomplete page' => [
73
            [
74
                'data' => [13],
75
                'pagination' => [
76
                    'currentPage' => 5,
77
                    'pagesCount' => 5,
78
                    'itemsPerPage' => 3,
79
                    'itemsInCurrentPage' => 1,
80
                    'totalItems' => 13,
81
                ],
82
            ],
83
            (new Paginator(new ArrayAdapter(range(1, 13))))->setItemCountPerPage(3)->setCurrentPageNumber(5),
84
        ];
85
    }
86
87
    /**
88
     * @test
89
     * @dataProvider providePaginatorsInPage
90
     */
91
    public function properlyTellsIfInLastPage(bool $expectedInLastPage, Paginator $paginator): void
92
    {
93
        $this->assertEquals($expectedInLastPage, $this->isLastPage($paginator));
94
    }
95
96
    public function providePaginatorsInPage(): iterable
97
    {
98
        yield 'empty in last page' => [true, new Paginator(new ArrayAdapter([]))];
99
        yield 'not empty in last page' => [
100
            true,
101
            (new Paginator(new ArrayAdapter(range(1, 15))))->setCurrentPageNumber(5),
102
        ];
103
        yield 'not in last page' => [false, new Paginator(new ArrayAdapter(range(1, 15)))];
104
    }
105
106
    /**
107
     * @test
108
     * @dataProvider providePaginatorsToFormat
109
     */
110
    public function pageMessageIsProperlyFormatted(string $expectedMessage, string $pattern, Paginator $paginator): void
111
    {
112
        $this->assertEquals($expectedMessage, $this->formatCurrentPageMessage($paginator, $pattern));
113
    }
114
115
    public function providePaginatorsToFormat(): iterable
116
    {
117
        yield [
118
            'Page "5" out of "10"',
119
            'Page "%s" out of "%s"',
120
            (new Paginator(new ArrayAdapter(range(1, 20))))->setCurrentPageNumber(5)->setItemCountPerPage(2),
121
        ];
122
        yield [
123
            'Current: 1. Total: 1',
124
            'Current: %s. Total: %s',
125
            new Paginator(new ArrayAdapter([1])),
126
        ];
127
    }
128
}
129