Completed
Push — 1.x ( e997bd )
by Sullivan
19:38
created

SimplePagerTest::testInitOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Datagrid;
13
14
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\SimplePager;
15
use Doctrine\ODM\PHPCR\Query\Query as PHPCRQuery;
16
use Doctrine\Common\Collections\ArrayCollection;
17
18
class SimplePagerTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function setUp()
21
    {
22
        $this->pager = new SimplePager(10, 2);
23
        $this->proxyQuery = $this->getMockBuilder('Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery')
24
            ->disableOriginalConstructor()
25
            ->getMock();
26
    }
27
28
    public function testInitNumPages()
29
    {
30
        $pager = new SimplePager(10, 2);
31
        $this->proxyQuery->expects($this->once())
32
                ->method('execute')
33
                ->with(array(), null)
34
                ->will($this->returnValue(new ArrayCollection(range(0, 12))));
35
36
37
        $this->proxyQuery->expects($this->once())
38
            ->method('setMaxResults')
39
            ->with($this->equalTo(21));
40
41
        $this->proxyQuery->expects($this->once())
42
            ->method('setFirstResult')
43
            ->with($this->equalTo(0));
44
45
        $pager->setQuery($this->proxyQuery);
46
        $pager->init();
47
48
        $this->assertEquals(2, $pager->getLastPage());
49
    }
50
51
    public function testInitOffset()
52
    {
53
        $this->proxyQuery->expects($this->once())
54
            ->method('execute')
55
            ->with(array(), null)
56
            ->will($this->returnValue(new ArrayCollection(range(0, 12))));
57
58
        $this->proxyQuery->expects($this->once())
59
            ->method('setMaxResults')
60
            ->with($this->equalTo(21));
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->assertEquals(3, $this->pager->getLastPage());
72
    }
73
74
    public function testNoPagesPerConfig()
75
    {
76
        $this->proxyQuery->expects($this->once())
77
            ->method('setMaxResults')
78
            ->with($this->equalTo(0));
79
80
        $this->proxyQuery->expects($this->once())
81
            ->method('setFirstResult')
82
            ->with($this->equalTo(0));
83
84
        $this->pager->setQuery($this->proxyQuery);
85
86
        // Max per page 0 means no pagination
87
        $this->pager->setMaxPerPage(0);
88
        $this->pager->init();
89
90
        $this->assertEquals(0, $this->pager->getLastPage());
91
    }
92
93
    public function testNoPagesForNoResults()
94
    {
95
        $this->proxyQuery->expects($this->once())
96
            ->method('execute')
97
            ->with(array(), null)
98
            ->will($this->returnValue(array()));
99
100
        $this->proxyQuery->expects($this->once())
101
            ->method('setMaxResults')
102
            ->with($this->equalTo(21));
103
        $this->proxyQuery->expects($this->once())
104
            ->method('setFirstResult')
105
            ->with($this->equalTo(0));
106
107
        $this->pager->setQuery($this->proxyQuery);
108
        $this->pager->init();
109
        $this->AssertEquals(0, $this->pager->getLastPage());
110
    }
111
112
    public function testInitNoQuery()
113
    {
114
        $this->setExpectedException('RuntimeException');
115
        $this->pager->init();
116
    }
117
}
118