|
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'); |
|
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'); |
|
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'); |
|
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'); |
|
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'); |
|
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'); |
|
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
|
|
|
'field_name' => 'field_name', |
|
130
|
|
|
'parent_association_mappings' => [ |
|
131
|
|
|
[ |
|
132
|
|
|
'fieldName' => 'association_mapping', |
|
133
|
|
|
], |
|
134
|
|
|
[ |
|
135
|
|
|
'fieldName' => 'sub_association_mapping', |
|
136
|
|
|
], |
|
137
|
|
|
[ |
|
138
|
|
|
'fieldName' => 'sub_sub_association_mapping', |
|
139
|
|
|
], |
|
140
|
|
|
], |
|
141
|
|
|
]); |
|
142
|
|
|
|
|
143
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
144
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$filter->apply($builder, ['type' => ContainsOperatorType::TYPE_EQUAL, 'value' => 'asd']); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertSame( |
|
149
|
|
|
'o.association_mapping', |
|
150
|
|
|
$builder->query[0] |
|
|
|
|
|
|
151
|
|
|
); |
|
152
|
|
|
$this->assertSame( |
|
153
|
|
|
's_association_mapping.sub_association_mapping', |
|
154
|
|
|
$builder->query[1] |
|
|
|
|
|
|
155
|
|
|
); |
|
156
|
|
|
$this->assertSame( |
|
157
|
|
|
's_association_mapping_sub_association_mapping.sub_sub_association_mapping', |
|
158
|
|
|
$builder->query[2] |
|
|
|
|
|
|
159
|
|
|
); |
|
160
|
|
|
$this->assertSame( |
|
161
|
|
|
's_association_mapping_sub_association_mapping_sub_sub_association_mapping.field_name = :field_name_0', |
|
162
|
|
|
$builder->query[3] |
|
|
|
|
|
|
163
|
|
|
); |
|
164
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
|
|
165
|
|
|
$this->assertTrue($filter->isActive()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @dataProvider caseSensitiveDataProvider |
|
170
|
|
|
*/ |
|
171
|
|
|
public function testCaseSensitive(bool $caseSensitive, int $operatorType, string $expectedQuery, string $expectedParameter): void |
|
172
|
|
|
{ |
|
173
|
|
|
$filter = new StringFilter(); |
|
174
|
|
|
$filter->initialize('field_name', ['case_sensitive' => $caseSensitive]); |
|
175
|
|
|
|
|
176
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
177
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => $operatorType]); |
|
180
|
|
|
$this->assertSame([$expectedQuery], $builder->query); |
|
|
|
|
|
|
181
|
|
|
$this->assertSame(['field_name_0' => $expectedParameter], $builder->parameters); |
|
|
|
|
|
|
182
|
|
|
$this->assertTrue($filter->isActive()); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function caseSensitiveDataProvider(): array |
|
186
|
|
|
{ |
|
187
|
|
|
return [ |
|
188
|
|
|
[false, ContainsOperatorType::TYPE_CONTAINS, 'LOWER(alias.field) LIKE :field_name_0', '%foobar%'], |
|
189
|
|
|
[false, ContainsOperatorType::TYPE_NOT_CONTAINS, 'LOWER(alias.field) NOT LIKE :field_name_0 OR alias.field IS NULL', '%foobar%'], |
|
190
|
|
|
[false, ContainsOperatorType::TYPE_EQUAL, 'LOWER(alias.field) = :field_name_0', 'foobar'], |
|
191
|
|
|
[true, ContainsOperatorType::TYPE_CONTAINS, 'alias.field LIKE :field_name_0', '%FooBar%'], |
|
192
|
|
|
[true, ContainsOperatorType::TYPE_NOT_CONTAINS, 'alias.field NOT LIKE :field_name_0 OR alias.field IS NULL', '%FooBar%'], |
|
193
|
|
|
[true, ContainsOperatorType::TYPE_EQUAL, 'alias.field = :field_name_0', 'FooBar'], |
|
194
|
|
|
|
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* NEXT_MAJOR: Remove this test. |
|
200
|
|
|
* |
|
201
|
|
|
* @group legacy |
|
202
|
|
|
* |
|
203
|
|
|
* @expectedDeprecation The "format" option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.21 and will be removed in version 4.0. |
|
204
|
|
|
*/ |
|
205
|
|
|
public function testFormatOption(): void |
|
206
|
|
|
{ |
|
207
|
|
|
$filter = new StringFilter(); |
|
208
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
|
209
|
|
|
|
|
210
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
|
|
211
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]); |
|
214
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
|
|
215
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
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: