1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata 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() { return; })); |
23
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
24
|
|
|
$this->assertNull($res); |
25
|
|
|
$this->assertFalse($filter->isActive()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testFilterEmptyArrayData() |
29
|
|
|
{ |
30
|
|
|
$filter = new CallbackFilter(); |
31
|
|
|
|
32
|
|
|
$filter->initialize('field_name', array('callback' => function() { return; })); |
33
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', array()); |
|
|
|
|
34
|
|
|
$this->assertNull($res); |
35
|
|
|
$this->assertFalse($filter->isActive()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testFilterMethod() |
39
|
|
|
{ |
40
|
|
|
$this->proxyQuery->expects($this->once()) |
41
|
|
|
->method('getQueryBuilder') |
42
|
|
|
->will($this->returnValue($this->qb)); |
43
|
|
|
|
44
|
|
|
$filter = new CallbackFilter(); |
45
|
|
|
$filter->initialize('field_name', array( |
46
|
|
|
'callback' => array($this, 'callbackMethod') |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue')); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
52
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
55
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
56
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($filter->isActive()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
62
|
|
|
{ |
63
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
64
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testFilterClosure() |
70
|
|
|
{ |
71
|
|
|
$this->proxyQuery->expects($this->once()) |
72
|
|
|
->method('getQueryBuilder') |
73
|
|
|
->will($this->returnValue($this->qb)); |
74
|
|
|
|
75
|
|
|
$filter = new CallbackFilter(); |
76
|
|
|
$filter->initialize('field_name', array( |
77
|
|
|
'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) { |
78
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
79
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
)); |
83
|
|
|
|
84
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue')); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
87
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
88
|
|
|
|
89
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
90
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
91
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
92
|
|
|
|
93
|
|
|
$this->assertTrue($filter->isActive()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException RuntimeException |
98
|
|
|
*/ |
99
|
|
|
public function testWithoutCallback() |
100
|
|
|
{ |
101
|
|
|
$filter = new CallbackFilter(); |
102
|
|
|
|
103
|
|
|
$filter->setOption('callback', null); |
104
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @expectedException RuntimeException |
109
|
|
|
*/ |
110
|
|
|
public function testCallbackNotCallable() |
111
|
|
|
{ |
112
|
|
|
$filter = new CallbackFilter(); |
113
|
|
|
|
114
|
|
|
$filter->setOption('callback', 'someCallback'); |
115
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.