Completed
Pull Request — master (#2)
by Kevin
01:41
created

PagerTestCase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 24.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 22
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A it_properly_handles_page1() 0 15 1
A it_properly_handles_page2() 11 11 1
A it_properly_handles_the_last_page() 11 11 1
A it_properly_handles_an_empty_page() 0 14 1
A it_properly_handles_a_single_page() 0 14 1
createPager() 0 1 ?

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 Zenstruck\Porpaginas\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\Porpaginas\Pager;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
abstract class PagerTestCase extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function it_properly_handles_page1()
17
    {
18
        $pager = $this->createPager(\range(1, 504), 1, 20);
19
20
        $this->assertSame(1, $pager->getCurrentPage());
21
        $this->assertSame(1, $pager->getFirstPage());
22
        $this->assertSame(2, $pager->getNextPage());
23
        $this->assertNull($pager->getPreviousPage());
24
        $this->assertSame(26, $pager->getLastPage());
25
        $this->assertSame(26, $pager->pagesCount());
26
        $this->assertSame(504, $pager->totalCount());
27
        $this->assertSame(20, $pager->getLimit());
28
        $this->assertCount(20, $pager);
29
        $this->assertSame(\range(1, 20), \iterator_to_array($pager));
30
    }
31
32
    /**
33
     * @test
34
     */
35 View Code Duplication
    public function it_properly_handles_page2()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
    {
37
        $pager = $this->createPager(\range(1, 504), 2, 20);
38
39
        $this->assertSame(2, $pager->getCurrentPage());
40
        $this->assertSame(1, $pager->getFirstPage());
41
        $this->assertSame(3, $pager->getNextPage());
42
        $this->assertSame(1, $pager->getPreviousPage());
43
        $this->assertCount(20, $pager);
44
        $this->assertSame(\range(21, 40), \iterator_to_array($pager));
45
    }
46
47
    /**
48
     * @test
49
     */
50 View Code Duplication
    public function it_properly_handles_the_last_page()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
51
    {
52
        $pager = $this->createPager(\range(1, 504), 26, 20);
53
54
        $this->assertSame(26, $pager->getCurrentPage());
55
        $this->assertSame(1, $pager->getFirstPage());
56
        $this->assertNull($pager->getNextPage());
57
        $this->assertSame(25, $pager->getPreviousPage());
58
        $this->assertCount(4, $pager);
59
        $this->assertSame(\range(501, 504), \iterator_to_array($pager));
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function it_properly_handles_an_empty_page()
66
    {
67
        $pager = $this->createPager([], 1, 20);
68
69
        $this->assertSame(1, $pager->getCurrentPage());
70
        $this->assertSame(1, $pager->getFirstPage());
71
        $this->assertNull($pager->getNextPage());
72
        $this->assertNull($pager->getPreviousPage());
73
        $this->assertSame(1, $pager->getLastPage());
74
        $this->assertSame(1, $pager->pagesCount());
75
        $this->assertSame(0, $pager->totalCount());
76
        $this->assertCount(0, $pager);
77
        $this->assertSame([], \iterator_to_array($pager));
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function it_properly_handles_a_single_page()
84
    {
85
        $pager = $this->createPager(\range(1, 10), 1, 20);
86
87
        $this->assertSame(1, $pager->getCurrentPage());
88
        $this->assertSame(1, $pager->getFirstPage());
89
        $this->assertNull($pager->getNextPage());
90
        $this->assertNull($pager->getPreviousPage());
91
        $this->assertSame(1, $pager->getLastPage());
92
        $this->assertSame(1, $pager->pagesCount());
93
        $this->assertSame(10, $pager->totalCount());
94
        $this->assertCount(10, $pager);
95
        $this->assertSame(\range(1, 10), \iterator_to_array($pager));
96
    }
97
98
    abstract protected function createPager(array $results, int $page, int $limit): Pager;
99
}
100