1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Tests\Filter; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType; |
18
|
|
|
use Sonata\AdminBundle\Form\Type\Operator\StringOperatorType; |
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
20
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\StringFilter; |
21
|
|
|
|
22
|
|
|
class StringFilterTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testEmpty(): void |
25
|
|
|
{ |
26
|
|
|
$filter = new StringFilter(); |
27
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
28
|
|
|
|
29
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
|
|
|
|
32
|
|
|
$filter->filter($builder, 'alias', 'field', ''); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
35
|
|
|
$this->assertFalse($filter->isActive()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testNullValue(): void |
39
|
|
|
{ |
40
|
|
|
$filter = new StringFilter(); |
41
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
42
|
|
|
|
43
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
44
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => null, 'type' => ContainsOperatorType::TYPE_EQUAL]); |
47
|
|
|
$this->assertFalse($filter->isActive()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testContains(): void |
51
|
|
|
{ |
52
|
|
|
$filter = new StringFilter(); |
53
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
54
|
|
|
|
55
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
56
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]); |
59
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
60
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
63
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => null]); |
66
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
67
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
68
|
|
|
$this->assertTrue($filter->isActive()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testStartsWith(): void |
72
|
|
|
{ |
73
|
|
|
$filter = new StringFilter(); |
74
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
75
|
|
|
|
76
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
77
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => StringOperatorType::TYPE_STARTS_WITH]); |
80
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
81
|
|
|
$this->assertSame(['field_name_0' => 'asd%'], $builder->parameters); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testEndsWith(): void |
85
|
|
|
{ |
86
|
|
|
$filter = new StringFilter(); |
87
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
88
|
|
|
|
89
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
90
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => StringOperatorType::TYPE_ENDS_WITH]); |
93
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
94
|
|
|
$this->assertSame(['field_name_0' => '%asd'], $builder->parameters); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testNotContains(): void |
98
|
|
|
{ |
99
|
|
|
$filter = new StringFilter(); |
100
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
101
|
|
|
|
102
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
103
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]); |
106
|
|
|
$this->assertSame(['alias.field NOT LIKE :field_name_0 OR alias.field IS NULL'], $builder->query); |
|
|
|
|
107
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
108
|
|
|
$this->assertTrue($filter->isActive()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testEquals(): void |
112
|
|
|
{ |
113
|
|
|
$filter = new StringFilter(); |
114
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
115
|
|
|
|
116
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
117
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_EQUAL]); |
120
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
121
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
122
|
|
|
$this->assertTrue($filter->isActive()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testEqualsWithValidParentAssociationMappings(): void |
126
|
|
|
{ |
127
|
|
|
$filter = new StringFilter(); |
128
|
|
|
$filter->initialize('field_name', [ |
129
|
|
|
'format' => '%s', |
130
|
|
|
'field_name' => 'field_name', |
131
|
|
|
'parent_association_mappings' => [ |
132
|
|
|
[ |
133
|
|
|
'fieldName' => 'association_mapping', |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
'fieldName' => 'sub_association_mapping', |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
'fieldName' => 'sub_sub_association_mapping', |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
145
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$filter->apply($builder, ['type' => ContainsOperatorType::TYPE_EQUAL, 'value' => 'asd']); |
148
|
|
|
|
149
|
|
|
$this->assertSame( |
150
|
|
|
'o.association_mapping', |
151
|
|
|
$builder->query[0] |
|
|
|
|
152
|
|
|
); |
153
|
|
|
$this->assertSame( |
154
|
|
|
's_association_mapping.sub_association_mapping', |
155
|
|
|
$builder->query[1] |
|
|
|
|
156
|
|
|
); |
157
|
|
|
$this->assertSame( |
158
|
|
|
's_association_mapping_sub_association_mapping.sub_sub_association_mapping', |
159
|
|
|
$builder->query[2] |
|
|
|
|
160
|
|
|
); |
161
|
|
|
$this->assertSame( |
162
|
|
|
's_association_mapping_sub_association_mapping_sub_sub_association_mapping.field_name = :field_name_0', |
163
|
|
|
$builder->query[3] |
|
|
|
|
164
|
|
|
); |
165
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
166
|
|
|
$this->assertTrue($filter->isActive()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @dataProvider caseSensitiveDataProvider |
171
|
|
|
*/ |
172
|
|
|
public function testCaseSensitive(bool $caseSensitive, int $operatorType, string $expectedQuery, string $expectedParameter): void |
173
|
|
|
{ |
174
|
|
|
$filter = new StringFilter(); |
175
|
|
|
$filter->initialize('field_name', ['case_sensitive' => $caseSensitive]); |
176
|
|
|
|
177
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
178
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
179
|
|
|
|
180
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => $operatorType]); |
181
|
|
|
$this->assertSame([$expectedQuery], $builder->query); |
|
|
|
|
182
|
|
|
$this->assertSame(['field_name_0' => $expectedParameter], $builder->parameters); |
|
|
|
|
183
|
|
|
$this->assertTrue($filter->isActive()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function caseSensitiveDataProvider(): array |
187
|
|
|
{ |
188
|
|
|
return [ |
189
|
|
|
[false, ContainsOperatorType::TYPE_CONTAINS, 'LOWER(alias.field) LIKE :field_name_0', '%foobar%'], |
190
|
|
|
[false, ContainsOperatorType::TYPE_NOT_CONTAINS, 'LOWER(alias.field) NOT LIKE :field_name_0 OR alias.field IS NULL', '%foobar%'], |
191
|
|
|
[false, ContainsOperatorType::TYPE_EQUAL, 'LOWER(alias.field) = :field_name_0', 'foobar'], |
192
|
|
|
[true, ContainsOperatorType::TYPE_CONTAINS, 'alias.field LIKE :field_name_0', '%FooBar%'], |
193
|
|
|
[true, ContainsOperatorType::TYPE_NOT_CONTAINS, 'alias.field NOT LIKE :field_name_0 OR alias.field IS NULL', '%FooBar%'], |
194
|
|
|
[true, ContainsOperatorType::TYPE_EQUAL, 'alias.field = :field_name_0', 'FooBar'], |
195
|
|
|
|
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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: