Completed
Push — master ( 5b1ac4...a1db3b )
by David
17:06
created

tests/Unit/Datagrid/ProxyQueryTest.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\Unit\Datagrid;
13
14
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
15
use PHPUnit\Framework\TestCase;
16
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery;
17
18
class ProxyQueryTest extends TestCase
19
{
20
    /**
21
     * @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject
22
     */
23
    private $qb;
24
25
    /**
26
     * @var ProxyQuery
27
     */
28
    private $pq;
29
30
    public function setUp()
31
    {
32
        $this->qb = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
        $this->query = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Query')
0 ignored issues
show
The property query 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...
36
            ->disableOriginalConstructor()
37
            ->getMock();
38
39
        $this->pq = new ProxyQuery($this->qb, 'a');
40
    }
41
42
    public function testConstructor()
43
    {
44
        $this->assertInstanceOf('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder', $this->pq->getQueryBuilder());
45
    }
46
47
    public function testSetSortBy()
48
    {
49
        $this->pq->setSortBy([], ['fieldName' => 'field']);
50
        $this->assertEquals('field', $this->pq->getSortBy());
51
    }
52
53
    public function testSetSortOrder()
54
    {
55
        $this->pq->setSortOrder('ASC');
56
        $this->assertEquals('ASC', $this->pq->getSortOrder());
57
    }
58
59
    /**
60
     * @expectedException \InvalidArgumentException
61
     */
62
    public function testSetSortOrderInvalid()
63
    {
64
        $this->pq->setSortOrder('SOME_ORDER');
65
        $this->assertEquals('SOME_ORDER', $this->pq->getSortOrder());
66
    }
67
68
    public function testSetFirstResult()
69
    {
70
        $this->qb->expects($this->once())
71
            ->method('setFirstResult')
72
            ->with($this->equalTo(19));
73
74
        $this->pq->setFirstResult(19);
75
    }
76
77
    public function testGetFirstResult()
78
    {
79
        $this->qb->expects($this->once())
80
            ->method('getFirstResult');
81
82
        $this->pq->getFirstResult();
83
    }
84
85
    public function testSetMaxResults()
86
    {
87
        $this->qb->expects($this->once())
88
            ->method('setMaxResults')
89
            ->with($this->equalTo(29));
90
91
        $this->pq->setMaxResults(29);
92
    }
93
94
    public function testGetMaxResults()
95
    {
96
        $this->qb->expects($this->once())
97
            ->method('getMaxResults');
98
99
        $this->pq->getMaxResults();
100
    }
101
102
    public function testExecute()
103
    {
104
        $this->qb->expects($this->once())
105
            ->method('getQuery')
106
            ->will($this->returnValue($this->query));
107
        $this->query->expects($this->once())
108
            ->method('execute')
109
            ->will($this->returnValue('test'));
110
111
        $res = $this->pq->execute();
112
        $this->assertEquals('test', $res);
113
    }
114
115
    public function testGetAndSetDocumentManager()
116
    {
117
        $dm = $this->getMockBuilder('Doctrine\\ODM\\PHPCR\\DocumentManager')
118
            ->disableOriginalConstructor()
119
            ->getMock();
120
        $this->pq->setDocumentManager($dm);
121
        $this->assertEquals($dm, $this->pq->getDocumentManager());
122
    }
123
}
124