|
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\DoctrineORMAdminBundle\Tests\Filter; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter; |
|
15
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
|
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
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.