Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

tests/Datagrid/PagerTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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')
0 ignored issues
show
The property proxyQuery does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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