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\DoctrineORMAdminBundle\Tests\Filter; |
13
|
|
|
|
14
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
15
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter; |
16
|
|
|
|
17
|
|
|
class CallbackFilterTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testFilterClosure() |
20
|
|
|
{ |
21
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$filter = new CallbackFilter(); |
24
|
|
|
$filter->initialize('field_name', array( |
25
|
|
|
'callback' => function ($builder, $alias, $field, $value) { |
26
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
27
|
|
|
$builder->setParameter('value', $value); |
28
|
|
|
|
29
|
|
|
return true; |
30
|
|
|
}, |
31
|
|
|
)); |
32
|
|
|
|
33
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
34
|
|
|
|
35
|
|
|
$this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query); |
|
|
|
|
36
|
|
|
$this->assertEquals(array('value' => 'myValue'), $builder->parameters); |
|
|
|
|
37
|
|
|
$this->assertEquals(true, $filter->isActive()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testFilterMethod() |
41
|
|
|
{ |
42
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$filter = new CallbackFilter(); |
45
|
|
|
$filter->initialize('field_name', array( |
46
|
|
|
'callback' => array($this, 'customCallback'), |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query); |
|
|
|
|
52
|
|
|
$this->assertEquals(array('value' => 'myValue'), $builder->parameters); |
|
|
|
|
53
|
|
|
$this->assertEquals(true, $filter->isActive()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function customCallback($builder, $alias, $field, $value) |
57
|
|
|
{ |
58
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
59
|
|
|
$builder->setParameter('value', $value); |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @expectedException RuntimeException |
66
|
|
|
*/ |
67
|
|
|
public function testFilterException() |
68
|
|
|
{ |
69
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$filter = new CallbackFilter(); |
72
|
|
|
$filter->initialize('field_name', array()); |
73
|
|
|
|
74
|
|
|
$filter->filter($builder, 'alias', 'field', 'myValue'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testApplyMethod() |
78
|
|
|
{ |
79
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$filter = new CallbackFilter(); |
82
|
|
|
$filter->initialize('field_name_test', array( |
83
|
|
|
'callback' => function ($builder, $alias, $field, $value) { |
84
|
|
|
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field)); |
85
|
|
|
$builder->setParameter('value', $value['value']); |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
}, |
89
|
|
|
'field_name' => 'field_name_test', |
90
|
|
|
)); |
91
|
|
|
|
92
|
|
|
$filter->apply($builder, array('value' => 'myValue')); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(array('CUSTOM QUERY o.field_name_test'), $builder->query); |
|
|
|
|
95
|
|
|
$this->assertEquals(array('value' => 'myValue'), $builder->parameters); |
|
|
|
|
96
|
|
|
$this->assertEquals(true, $filter->isActive()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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: