PaginationTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 250
Duplicated Lines 1.2 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 3
loc 250
rs 10
wmc 17
lcom 1
cbo 3

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 4 1
A testSetAdapter() 0 8 1
A testGetAdapter() 0 4 1
A testSetMaxResultsPerPage() 0 10 1
A testSetMaxResultsPerPage_throwsException() 0 4 1
A testSetCurrentPage() 0 15 1
A getTotalNumberOfResults() 0 11 1
A testHaveToPaginate() 0 14 1
A testSetTotalNumberOfResults() 0 7 1
A testGetNumberOfPages() 0 9 1
B testGetPreviousPage() 0 40 3
A testGetCurrentPageResults() 0 18 1
B testRender() 3 25 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace KochTest\Pagination;
4
5
use Koch\Pagination\Pagination;
6
7
class PaginationTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var Pagination
11
     */
12
    protected $object;
13
    protected $adapter;
14
15
    protected function setUp()
16
    {
17
        $this->adapter = $this->getMock('Koch\Pagination\AdapterInterface');
18
19
        // constructor injection
20
        $this->object = new Pagination($this->adapter);
21
    }
22
23
    protected function tearDown()
24
    {
25
        $this->object;
26
    }
27
28
    /**
29
     * @covers Koch\Pagination\Pagination::setAdapter
30
     */
31
    public function testSetAdapter()
32
    {
33
        // setter injection
34
        $r = $this->object->setAdapter($this->adapter);
35
36
        // fluent
37
        $this->assertInstanceOf('Koch\Pagination\Pagination', $r);
38
    }
39
40
    /**
41
     * @covers Koch\Pagination\Pagination::getAdapter
42
     */
43
    public function testGetAdapter()
44
    {
45
        $this->assertSame($this->adapter, $this->object->getAdapter());
46
    }
47
48
    /**
49
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
50
     * @covers Koch\Pagination\Pagination::getMaxResultsPerPage
51
     */
52
    public function testSetMaxResultsPerPage()
53
    {
54
        $maxResultsPerPage = '15';
55
        $r                 = $this->object->setMaxResultsPerPage($maxResultsPerPage);
56
57
        $this->assertEquals($this->object->getMaxResultsPerPage(), $maxResultsPerPage);
58
59
        // fluent
60
        $this->assertInstanceOf('Koch\Pagination\Pagination', $r);
61
    }
62
63
    /**
64
     * @expectedException InvalidArgumentException
65
     * @expectedExceptionMessage There must be more than 1 MaxResultsPerPage.
66
     */
67
    public function testSetMaxResultsPerPage_throwsException()
0 ignored issues
show
Coding Style introduced by
function testSetMaxResul...rPage_throwsException() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
68
    {
69
        $this->object->setMaxResultsPerPage(-10);
70
    }
71
72
    /**
73
     * @covers Koch\Pagination\Pagination::setCurrentPage
74
     * @covers Koch\Pagination\Pagination::getCurrentPage
75
     */
76
    public function testSetCurrentPage()
77
    {
78
        // string
79
        $currentPage = '15';
80
        $this->object->setCurrentPage($currentPage);
81
        $this->assertEquals($this->object->getCurrentPage(), $currentPage);
82
83
        // int
84
        $currentPage = 15;
85
        $r           = $this->object->setCurrentPage($currentPage);
86
        $this->assertEquals($this->object->getCurrentPage(), $currentPage);
87
88
        // fluent
89
        $this->assertInstanceOf('Koch\Pagination\Pagination', $r);
90
    }
91
92
    /**
93
     * @covers Koch\Pagination\Pagination::getTotalNumberOfResults
94
     */
95
    public function getTotalNumberOfResults()
96
    {
97
        // hmm, phpunit bug? calls to the mock are not covered
98
        unset($this->object->totalNumberOfResults);
99
        $this->assertNull($this->object->totalNumberOfResults);
100
101
        $this->adapter->expects($this->any())
102
            ->method('getTotalNumberOfResults')->will($this->returnValue(666));
103
104
        $this->assertSame(666, $this->object->getTotalNumberOfResults());
105
    }
106
107
    /**
108
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
109
     * @covers Koch\Pagination\Pagination::haveToPaginate
110
     */
111
    public function testHaveToPaginate()
112
    {
113
        $this->adapter->expects($this->any())
114
            ->method('getTotalNumberOfResults')->will($this->returnValue(15));
115
116
        $this->object->setMaxResultsPerPage(16);
117
        $this->assertFalse($this->object->haveToPaginate());
118
        $this->object->setMaxResultsPerPage(15);
119
        $this->assertFalse($this->object->haveToPaginate());
120
        $this->object->setMaxResultsPerPage(14);
121
        $this->assertTrue($this->object->haveToPaginate());
122
        $this->object->setMaxResultsPerPage(1);
123
        $this->assertTrue($this->object->haveToPaginate());
124
    }
125
126
    /**
127
     * @covers Koch\Pagination\Pagination::getTotalNumberOfResults
128
     * @covers Koch\Pagination\Pagination::setTotalNumberOfResults
129
     */
130
    public function testSetTotalNumberOfResults()
131
    {
132
        $this->adapter->expects($this->never())->method('getTotalNumberOfResults');
133
134
        $this->object->setTotalNumberOfResults(2);
135
        $this->assertEquals(2, $this->object->getTotalNumberOfResults());
136
    }
137
138
    /**
139
     * @covers Koch\Pagination\Pagination::getNumberOfPages
140
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
141
     * @covers Koch\Pagination\Pagination::getLastPage
142
     */
143
    public function testGetNumberOfPages()
144
    {
145
        $this->adapter->expects($this->any())
146
            ->method('getTotalNumberOfResults')->will($this->returnValue(100));
147
148
        $this->object->setMaxResultsPerPage(10);
149
        $this->assertSame(10, $this->object->getNumberOfPages());
150
        $this->assertSame(10, $this->object->getLastPage());
151
    }
152
153
    /**
154
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
155
     * @covers Koch\Pagination\Pagination::getCurrentPageResults
156
     * @covers Koch\Pagination\Pagination::getCurrentPage
157
     */
158
    public function testGetCurrentPageResults()
159
    {
160
        $returnValues = [
161
            ['foo' => 'bar', 'bar' => 'foo'],
162
            ['fanta', 'reiner', 'kristall', 'weizen'],
163
        ];
164
165
        $this->adapter->expects($this->once())->method('getSlice')
166
            ->with($this->equalTo(20), $this->equalTo(10))
167
            ->will($this->returnValue($returnValues[0]));
168
169
        $this->object->setMaxResultsPerPage(10);
170
        $this->object->setCurrentPage(3, true);
0 ignored issues
show
Unused Code introduced by
The call to Pagination::setCurrentPage() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
171
        $this->assertSame($returnValues[0], $this->object->getCurrentPageResults());
172
173
        // cached
174
        $this->assertSame($returnValues[0], $this->object->getCurrentPageResults());
175
    }
176
177
    /**
178
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
179
     * @covers Koch\Pagination\Pagination::getCurrentPage
180
     * @covers Koch\Pagination\Pagination::hasPreviousPage
181
     * @covers Koch\Pagination\Pagination::getPreviousPage
182
     * @covers Koch\Pagination\Pagination::hasNextPage
183
     * @covers Koch\Pagination\Pagination::getNextPage
184
     */
185
    public function testGetPreviousPage()
186
    {
187
        $this->adapter->expects($this->any())
188
            ->method('getTotalNumberOfResults')->will($this->returnValue(25));
189
190
        $this->object->setMaxResultsPerPage(5);
191
        $this->object->setCurrentPage(1);
192
193
        // first page does not have a previous page
194
        $this->assertFalse($this->object->hasPreviousPage());
195
        try {
196
            $this->object->getPreviousPage();
197
            $this->fail();
198
        } catch (\Exception $e) {
199
            $this->assertInstanceOf('\LogicException', $e);
200
        }
201
202
        // first page has a next page
203
        $this->assertTrue($this->object->hasNextPage());
204
        $this->assertSame(2, $this->object->getNextPage());
205
206
        // check in between
207
        $this->object->setCurrentPage(5);
208
        $this->assertTrue($this->object->hasPreviousPage());
209
        $this->assertSame(4, $this->object->getPreviousPage());
210
211
        // last page
212
        $this->object->setCurrentPage(25);
213
        $this->assertTrue($this->object->hasPreviousPage());
214
        $this->assertSame(24, $this->object->getPreviousPage());
215
        $this->assertFalse($this->object->hasNextPage());
216
217
        // last page does not have a next page
218
        try {
219
            $this->object->getNextPage();
220
            $this->fail();
221
        } catch (\Exception $e) {
222
            $this->assertInstanceOf('\LogicException', $e);
223
        }
224
    }
225
226
    /**
227
     * @covers Koch\Pagination\Pagination::render
228
     * @covers Koch\Pagination\Pagination::setAdapter
229
     * @covers Koch\Pagination\Pagination::setMaxResultsPerPage
230
     */
231
    public function testRender()
232
    {
233
        // dataset
234
        $this->array = [];
0 ignored issues
show
Bug introduced by
The property array does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
235 View Code Duplication
        for ($i = 0; $i < 10; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
            $this->array[] = rand(1, 999);
237
        }
238
239
        // dataset adapter
240
        $adapter = new \Koch\Pagination\Adapter\NativeArray($this->array);
241
        $this->object->setAdapter($adapter);
242
243
        // settings
244
        $this->object->setMaxResultsPerPage(10);
245
246
        // expected pagination
247
        $expected = '<nav class="pagination">';
248
        $expected .= '<a href="URL">&lsaquo;&nbsp;First</a>';
249
        $expected .= '<a href="URL">1</a>';
250
        $expected .= '<a href="URL">&gt;</a>';
251
        $expected .= '<a href="URL">&nbsp;Last&rsaquo;</a>';
252
        $expected .= '</nav>';
253
254
        $this->assertEquals($expected, $this->object->render());
255
    }
256
}
257