PagerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 102
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testInitNumPages() 0 20 1
A testInitOffset() 0 22 1
A testNoPagesForNoResults() 0 18 1
A testInitNoQuery() 0 5 1
A testNoPagesPerConfig() 0 23 1
A setUp() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Datagrid;
15
16
use Doctrine\ODM\PHPCR\Query\Query as PHPCRQuery;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\Pager;
19
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery;
20
21
class PagerTest extends TestCase
22
{
23
    protected function setUp(): void
24
    {
25
        $this->pager = new Pager(10);
26
27
        $this->proxyQuery = $this->createMock(ProxyQuery::class);
28
    }
29
30
    public function testInitNumPages(): void
31
    {
32
        $this->proxyQuery->expects($this->once())
33
            ->method('execute')
34
            ->with([], PHPCRQuery::HYDRATE_PHPCR)
35
            ->willReturn(range(0, 12));
36
37
        $this->proxyQuery->expects($this->once())
38
            ->method('setMaxResults')
39
            ->with($this->equalTo(10));
40
41
        $this->proxyQuery->expects($this->once())
42
            ->method('setFirstResult')
43
            ->with($this->equalTo(0));
44
45
        $this->pager->setQuery($this->proxyQuery);
46
        $this->pager->init();
47
48
        $this->assertSame(2, $this->pager->getLastPage());
49
    }
50
51
    public function testInitOffset(): void
52
    {
53
        $this->proxyQuery->expects($this->once())
54
            ->method('execute')
55
            ->with([], PHPCRQuery::HYDRATE_PHPCR)
56
            ->willReturn(range(0, 12));
57
58
        $this->proxyQuery->expects($this->once())
59
            ->method('setMaxResults')
60
            ->with($this->equalTo(10));
61
62
        // Asserting that the offset will be set correctly
63
        $this->proxyQuery->expects($this->once())
64
            ->method('setFirstResult')
65
            ->with($this->equalTo(10));
66
67
        $this->pager->setQuery($this->proxyQuery);
68
        $this->pager->setPage(2);
69
        $this->pager->init();
70
71
        $this->assertSame(2, $this->pager->getLastPage());
72
    }
73
74
    public function testNoPagesPerConfig(): void
75
    {
76
        $this->proxyQuery->expects($this->once())
77
            ->method('execute')
78
            ->with([], PHPCRQuery::HYDRATE_PHPCR)
79
            ->willReturn([]);
80
81
        $this->proxyQuery->expects($this->once())
82
            ->method('setMaxResults')
83
            ->with($this->equalTo(0));
84
85
        $this->proxyQuery->expects($this->once())
86
            ->method('setFirstResult')
87
            ->with($this->equalTo(0));
88
89
        $this->pager->setQuery($this->proxyQuery);
90
91
        // Max per page 0 means no pagination
92
        $this->pager->setMaxPerPage(0);
93
        $this->pager->init();
94
95
        $this->assertSame(0, $this->pager->getLastPage());
96
    }
97
98
    public function testNoPagesForNoResults(): void
99
    {
100
        $this->proxyQuery->expects($this->once())
101
            ->method('execute')
102
            ->with([], PHPCRQuery::HYDRATE_PHPCR)
103
            ->willReturn([]);
104
105
        $this->proxyQuery->expects($this->once())
106
            ->method('setMaxResults')
107
            ->with($this->equalTo(0));
108
        $this->proxyQuery->expects($this->once())
109
            ->method('setFirstResult')
110
            ->with($this->equalTo(0));
111
112
        $this->pager->setQuery($this->proxyQuery);
113
        $this->pager->init();
114
        $this->AssertEquals(0, $this->pager->getLastPage());
115
    }
116
117
    public function testInitNoQuery(): void
118
    {
119
        $this->expectException(\RuntimeException::class);
120
        $this->pager->init();
121
    }
122
}
123