|
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 Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery; |
|
16
|
|
|
|
|
17
|
|
|
class ProxyQueryTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $qb; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ProxyQuery |
|
26
|
|
|
*/ |
|
27
|
|
|
private $pq; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->qb = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder') |
|
32
|
|
|
->disableOriginalConstructor() |
|
33
|
|
|
->getMock(); |
|
34
|
|
|
$this->query = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Query') |
|
35
|
|
|
->disableOriginalConstructor() |
|
36
|
|
|
->getMock(); |
|
37
|
|
|
|
|
38
|
|
|
$this->pq = new ProxyQuery($this->qb, 'a'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testConstructor() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->assertInstanceOf('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder', $this->pq->getQueryBuilder()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testSetSortBy() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->pq->setSortBy(array(), array('fieldName' => 'field')); |
|
49
|
|
|
$this->assertEquals('field', $this->pq->getSortBy()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSetSortOrder() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->pq->setSortOrder('ASC'); |
|
55
|
|
|
$this->assertEquals('ASC', $this->pq->getSortOrder()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @expectedException \InvalidArgumentException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testSetSortOrderInvalid() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->pq->setSortOrder('SOME_ORDER'); |
|
64
|
|
|
$this->assertEquals('SOME_ORDER', $this->pq->getSortOrder()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testSetFirstResult() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->qb->expects($this->once()) |
|
70
|
|
|
->method('setFirstResult') |
|
71
|
|
|
->with($this->equalTo(19)); |
|
72
|
|
|
|
|
73
|
|
|
$this->pq->setFirstResult(19); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testGetFirstResult() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->qb->expects($this->once()) |
|
79
|
|
|
->method('getFirstResult'); |
|
80
|
|
|
|
|
81
|
|
|
$this->pq->getFirstResult(); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testSetMaxResults() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->qb->expects($this->once()) |
|
87
|
|
|
->method('setMaxResults') |
|
88
|
|
|
->with($this->equalTo(29)); |
|
89
|
|
|
|
|
90
|
|
|
$this->pq->setMaxResults(29); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testGetMaxResults() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->qb->expects($this->once()) |
|
96
|
|
|
->method('getMaxResults'); |
|
97
|
|
|
|
|
98
|
|
|
$this->pq->getMaxResults(); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testExecute() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->qb->expects($this->once()) |
|
104
|
|
|
->method('getQuery') |
|
105
|
|
|
->will($this->returnValue($this->query)); |
|
106
|
|
|
$this->query->expects($this->once()) |
|
107
|
|
|
->method('execute') |
|
108
|
|
|
->will($this->returnValue('test')); |
|
109
|
|
|
|
|
110
|
|
|
$res = $this->pq->execute(); |
|
111
|
|
|
$this->assertEquals('test', $res); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testGetAndSetDocumentManager() |
|
115
|
|
|
{ |
|
116
|
|
|
$dm = $this->getMockBuilder('Doctrine\\ODM\\PHPCR\\DocumentManager') |
|
117
|
|
|
->disableOriginalConstructor() |
|
118
|
|
|
->getMock(); |
|
119
|
|
|
$this->pq->setDocumentManager($dm); |
|
120
|
|
|
$this->assertEquals($dm, $this->pq->getDocumentManager()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: