Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

PagerTest::testInitNumPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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