|
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\StringFilter; |
|
15
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType; |
|
16
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
|
17
|
|
|
|
|
18
|
|
|
class StringFilterTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testEmpty() |
|
21
|
|
|
{ |
|
22
|
|
|
$filter = new StringFilter; |
|
23
|
|
|
$filter->initialize('field_name', array('field_options' => array('class' => 'FooBar'))); |
|
24
|
|
|
|
|
25
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
26
|
|
|
|
|
27
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
|
28
|
|
|
$filter->filter($builder, 'alias', 'field', ''); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
31
|
|
|
$this->assertEquals(false, $filter->isActive()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testContains() |
|
35
|
|
|
{ |
|
36
|
|
|
$filter = new StringFilter; |
|
37
|
|
|
$filter->initialize('field_name', array('format' => '%s')); |
|
38
|
|
|
|
|
39
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
40
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$filter->filter($builder, 'alias', 'field', array('value' => 'asd', 'type' => ChoiceType::TYPE_CONTAINS)); |
|
|
|
|
|
|
43
|
|
|
$this->assertEquals(array('alias.field LIKE :field_name_0'), $builder->query); |
|
|
|
|
|
|
44
|
|
|
$this->assertEquals(array('field_name_0' => 'asd'), $builder->parameters); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
47
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$filter->filter($builder, 'alias', 'field', array('value' => 'asd', 'type' => null)); |
|
|
|
|
|
|
50
|
|
|
$this->assertEquals(array('alias.field LIKE :field_name_0'), $builder->query); |
|
|
|
|
|
|
51
|
|
|
$this->assertEquals(array('field_name_0' => 'asd'), $builder->parameters); |
|
|
|
|
|
|
52
|
|
|
$this->assertEquals(true, $filter->isActive()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testNotContains() |
|
56
|
|
|
{ |
|
57
|
|
|
$filter = new StringFilter; |
|
58
|
|
|
$filter->initialize('field_name', array('format' => '%s')); |
|
59
|
|
|
|
|
60
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
61
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$filter->filter($builder, 'alias', 'field', array('value' => 'asd', 'type' => ChoiceType::TYPE_NOT_CONTAINS)); |
|
|
|
|
|
|
64
|
|
|
$this->assertEquals(array('alias.field NOT LIKE :field_name_0'), $builder->query); |
|
|
|
|
|
|
65
|
|
|
$this->assertEquals(array('field_name_0' => 'asd'), $builder->parameters); |
|
|
|
|
|
|
66
|
|
|
$this->assertEquals(true, $filter->isActive()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testEquals() |
|
70
|
|
|
{ |
|
71
|
|
|
$filter = new StringFilter; |
|
72
|
|
|
$filter->initialize('field_name', array('format' => '%s')); |
|
73
|
|
|
|
|
74
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
75
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$filter->filter($builder, 'alias', 'field', array('value' => 'asd', 'type' => ChoiceType::TYPE_EQUAL)); |
|
|
|
|
|
|
78
|
|
|
$this->assertEquals(array('alias.field = :field_name_0'), $builder->query); |
|
|
|
|
|
|
79
|
|
|
$this->assertEquals(array('field_name_0' => 'asd'), $builder->parameters); |
|
|
|
|
|
|
80
|
|
|
$this->assertEquals(true, $filter->isActive()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testEqualsWithValidParentAssociationMappings() |
|
84
|
|
|
{ |
|
85
|
|
|
$filter = new StringFilter; |
|
86
|
|
|
$filter->initialize('field_name', array( |
|
87
|
|
|
'format' => '%s', |
|
88
|
|
|
'field_name' => 'field_name', |
|
89
|
|
|
'parent_association_mappings' => array( |
|
90
|
|
|
array( |
|
91
|
|
|
'fieldName' => 'association_mapping' |
|
92
|
|
|
), |
|
93
|
|
|
array( |
|
94
|
|
|
'fieldName' => 'sub_association_mapping' |
|
95
|
|
|
), |
|
96
|
|
|
array( |
|
97
|
|
|
'fieldName' => 'sub_sub_association_mapping' |
|
98
|
|
|
), |
|
99
|
|
|
) |
|
100
|
|
|
)); |
|
101
|
|
|
|
|
102
|
|
|
$builder = new ProxyQuery(new QueryBuilder); |
|
103
|
|
|
$this->assertEquals(array(), $builder->query); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$filter->apply($builder, array('type' => ChoiceType::TYPE_EQUAL, 'value' => 'asd')); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertEquals(array( |
|
108
|
|
|
'o.association_mapping', |
|
109
|
|
|
's_association_mapping.sub_association_mapping', |
|
110
|
|
|
's_association_mapping_sub_association_mapping.sub_sub_association_mapping', |
|
111
|
|
|
's_association_mapping_sub_association_mapping_sub_sub_association_mapping.field_name = :field_name_0' |
|
112
|
|
|
), $builder->query); |
|
|
|
|
|
|
113
|
|
|
$this->assertEquals(array('field_name_0' => 'asd'), $builder->parameters); |
|
|
|
|
|
|
114
|
|
|
$this->assertEquals(true, $filter->isActive()); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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.