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\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', ['callback' => function () { |
23
|
|
|
}]); |
24
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
25
|
|
|
$this->assertNull($res); |
26
|
|
|
$this->assertFalse($filter->isActive()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testFilterEmptyArrayData() |
30
|
|
|
{ |
31
|
|
|
$filter = new CallbackFilter(); |
32
|
|
|
|
33
|
|
|
$filter->initialize('field_name', ['callback' => function () { |
34
|
|
|
}]); |
35
|
|
|
$res = $filter->filter($this->proxyQuery, null, 'somefield', []); |
|
|
|
|
36
|
|
|
$this->assertNull($res); |
37
|
|
|
$this->assertFalse($filter->isActive()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testFilterMethod() |
41
|
|
|
{ |
42
|
|
|
$this->proxyQuery->expects($this->once()) |
|
|
|
|
43
|
|
|
->method('getQueryBuilder') |
44
|
|
|
->will($this->returnValue($this->qb)); |
45
|
|
|
|
46
|
|
|
$filter = new CallbackFilter(); |
47
|
|
|
$filter->initialize('field_name', [ |
48
|
|
|
'callback' => [$this, 'callbackMethod'], |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']); |
52
|
|
|
|
53
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
54
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
55
|
|
|
|
56
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
57
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
58
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($filter->isActive()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
64
|
|
|
{ |
65
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
66
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testFilterClosure() |
72
|
|
|
{ |
73
|
|
|
$this->proxyQuery->expects($this->once()) |
|
|
|
|
74
|
|
|
->method('getQueryBuilder') |
75
|
|
|
->will($this->returnValue($this->qb)); |
76
|
|
|
|
77
|
|
|
$filter = new CallbackFilter(); |
78
|
|
|
$filter->initialize('field_name', [ |
79
|
|
|
'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) { |
80
|
|
|
$queryBuilder = $proxyQuery->getQueryBuilder(); |
81
|
|
|
$queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']); |
82
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
}, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']); |
88
|
|
|
|
89
|
|
|
$opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic'); |
90
|
|
|
$opStatic = $this->qbTester->getNode('where.constraint.operand_static'); |
91
|
|
|
|
92
|
|
|
$this->assertEquals('a', $opDynamic->getAlias()); |
93
|
|
|
$this->assertEquals('somefield', $opDynamic->getField()); |
94
|
|
|
$this->assertEquals('somevalue', $opStatic->getValue()); |
95
|
|
|
|
96
|
|
|
$this->assertTrue($filter->isActive()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testWithoutCallback() |
100
|
|
|
{ |
101
|
|
|
$this->expectException(\RuntimeException::class); |
102
|
|
|
|
103
|
|
|
$filter = new CallbackFilter(); |
104
|
|
|
|
105
|
|
|
$filter->setOption('callback', null); |
106
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testCallbackNotCallable() |
110
|
|
|
{ |
111
|
|
|
$this->expectException(\RuntimeException::class); |
112
|
|
|
|
113
|
|
|
$filter = new CallbackFilter(); |
114
|
|
|
|
115
|
|
|
$filter->setOption('callback', 'someCallback'); |
116
|
|
|
$filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: