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

SimplePagerBasicTest::testInitNumPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 14
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 SimplePagerBasicTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function setUp()
21
    {
22
        $this->pager = new SimplePager(10, 0);
23
24
        $this->proxyQuery = $this->getMockBuilder('Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery')
25
            ->disableOriginalConstructor()
26
            ->getMock();
27
    }
28
29
    public function testInitNumPages()
30
    {
31
        $this->proxyQuery->expects($this->once())
32
            ->method('execute')
33
            ->with(array(), null)
34
            ->will($this->returnValue(new ArrayCollection(range(0, 12))));
35
36
        $this->proxyQuery->expects($this->once())
37
            ->method('setMaxResults')
38
            ->with($this->equalTo(11));
39
40
        $this->proxyQuery->expects($this->once())
41
            ->method('setFirstResult')
42
            ->with($this->equalTo(0));
43
44
        $this->pager->setQuery($this->proxyQuery);
45
        $this->pager->init();
46
47
        $this->assertEquals(2, $this->pager->getLastPage());
48
    }
49
50
    public function testInitOffset()
51
    {
52
        $this->proxyQuery->expects($this->once())
53
            ->method('execute')
54
            ->with(array(), null)
55
            ->will($this->returnValue(new ArrayCollection(range(0, 12))));
56
57
        $this->proxyQuery->expects($this->once())
58
            ->method('setMaxResults')
59
            ->with($this->equalTo(11));
60
61
        // Asserting that the offset will be set correctly
62
        $this->proxyQuery->expects($this->once())
63
            ->method('setFirstResult')
64
            ->with($this->equalTo(10));
65
66
        $this->pager->setQuery($this->proxyQuery);
67
        $this->pager->setPage(2);
68
        $this->pager->init();
69
70
        $this->assertEquals(3, $this->pager->getLastPage());
71
    }
72
73
    public function testNoPagesPerConfig()
74
    {
75
        $this->proxyQuery->expects($this->once())
76
            ->method('setMaxResults')
77
            ->with($this->equalTo(0));
78
79
        $this->proxyQuery->expects($this->once())
80
            ->method('setFirstResult')
81
            ->with($this->equalTo(0));
82
83
        $this->pager->setQuery($this->proxyQuery);
84
85
        // Max per page 0 means no pagination
86
        $this->pager->setMaxPerPage(0);
87
        $this->pager->init();
88
89
        $this->assertEquals(0, $this->pager->getLastPage());
90
    }
91
92
    public function testNoPagesForNoResults()
93
    {
94
        $this->proxyQuery->expects($this->once())
95
            ->method('execute')
96
            ->with(array(), null)
97
            ->will($this->returnValue(array()));
98
99
        $this->proxyQuery->expects($this->once())
100
            ->method('setMaxResults')
101
            ->with($this->equalTo(11));
102
        $this->proxyQuery->expects($this->once())
103
            ->method('setFirstResult')
104
            ->with($this->equalTo(0));
105
106
        $this->pager->setQuery($this->proxyQuery);
107
        $this->pager->init();
108
        $this->AssertEquals(0, $this->pager->getLastPage());
109
    }
110
111
    public function testInitNoQuery()
112
    {
113
        $this->setExpectedException('RuntimeException');
114
        $this->pager->init();
115
    }
116
}
117