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\Filter; |
13
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
15
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Filter\CallbackFilter; |
16
|
|
|
|
17
|
|
|
class CallBackFilterTest extends BaseTestCase |
18
|
|
|
{ |
19
|
|
|
public function testFilterNullData() |
20
|
|
|
{ |
21
|
|
|
$filter = new CallbackFilter(); |
22
|
|
|
$filter->initialize('field_name', array('callback' => function () { |
23
|
|
|
return; |
24
|
|
|
})); |
25
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
26
|
|
|
$this->assertNull($res); |
27
|
|
|
$this->assertFalse($filter->isActive()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testFilterEmptyArrayData() |
31
|
|
|
{ |
32
|
|
|
$filter = new CallbackFilter(); |
33
|
|
|
|
34
|
|
|
$filter->initialize('field_name', array('callback' => function () { |
35
|
|
|
return; |
36
|
|
|
})); |
37
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', array()); |
|
|
|
|
38
|
|
|
$this->assertNull($res); |
39
|
|
|
$this->assertFalse($filter->isActive()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testFilterMethod() |
43
|
|
|
{ |
44
|
|
|
$this->proxyQuery->expects($this->once()) |
45
|
|
|
->method('getQueryBuilder') |
46
|
|
|
->will($this->returnValue($this->qb)); |
47
|
|
|
|
48
|
|
|
$filter = new CallbackFilter(); |
49
|
|
|
$filter->initialize('field_name', array( |
50
|
|
|
'callback' => array($this, 'callbackMethod'), |
51
|
|
|
)); |
52
|
|
|
|
53
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue')); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
56
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
57
|
|
|
|
58
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
59
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
60
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($filter->isActive()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
66
|
|
|
{ |
67
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
68
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testFilterClosure() |
74
|
|
|
{ |
75
|
|
|
$this->proxyQuery->expects($this->once()) |
76
|
|
|
->method('getQueryBuilder') |
77
|
|
|
->will($this->returnValue($this->qb)); |
78
|
|
|
|
79
|
|
|
$filter = new CallbackFilter(); |
80
|
|
|
$filter->initialize('field_name', array( |
81
|
|
|
'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) { |
82
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
83
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
}, |
87
|
|
|
)); |
88
|
|
|
|
89
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue')); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
92
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
95
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
96
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
97
|
|
|
|
98
|
|
|
$this->assertTrue($filter->isActive()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @expectedException RuntimeException |
103
|
|
|
*/ |
104
|
|
|
public function testWithoutCallback() |
105
|
|
|
{ |
106
|
|
|
$filter = new CallbackFilter(); |
107
|
|
|
|
108
|
|
|
$filter->setOption('callback', null); |
109
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @expectedException RuntimeException |
114
|
|
|
*/ |
115
|
|
|
public function testCallbackNotCallable() |
116
|
|
|
{ |
117
|
|
|
$filter = new CallbackFilter(); |
118
|
|
|
|
119
|
|
|
$filter->setOption('callback', 'someCallback'); |
120
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.