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\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\StringFilter; |
20
|
|
|
|
21
|
|
|
class StringFilterTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testEmpty(): void |
24
|
|
|
{ |
25
|
|
|
$filter = new StringFilter(); |
26
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
27
|
|
|
|
28
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
|
|
|
|
31
|
|
|
$filter->filter($builder, 'alias', 'field', ''); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
34
|
|
|
$this->assertFalse($filter->isActive()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testNullValue(): void |
38
|
|
|
{ |
39
|
|
|
$filter = new StringFilter(); |
40
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
41
|
|
|
|
42
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
43
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => null, 'type' => ContainsOperatorType::TYPE_EQUAL]); |
46
|
|
|
$this->assertFalse($filter->isActive()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testContains(): void |
50
|
|
|
{ |
51
|
|
|
$filter = new StringFilter(); |
52
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
53
|
|
|
|
54
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
55
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]); |
58
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
59
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
62
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => null]); |
65
|
|
|
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); |
|
|
|
|
66
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
67
|
|
|
$this->assertTrue($filter->isActive()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testNotContains(): void |
71
|
|
|
{ |
72
|
|
|
$filter = new StringFilter(); |
73
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
74
|
|
|
|
75
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
76
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]); |
79
|
|
|
$this->assertSame(['alias.field NOT LIKE :field_name_0 OR alias.field IS NULL'], $builder->query); |
|
|
|
|
80
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
81
|
|
|
$this->assertTrue($filter->isActive()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testEquals(): 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' => ContainsOperatorType::TYPE_EQUAL]); |
93
|
|
|
$this->assertSame(['alias.field = :field_name_0'], $builder->query); |
|
|
|
|
94
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
95
|
|
|
$this->assertTrue($filter->isActive()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testEqualsWithValidParentAssociationMappings(): void |
99
|
|
|
{ |
100
|
|
|
$filter = new StringFilter(); |
101
|
|
|
$filter->initialize('field_name', [ |
102
|
|
|
'format' => '%s', |
103
|
|
|
'field_name' => 'field_name', |
104
|
|
|
'parent_association_mappings' => [ |
105
|
|
|
[ |
106
|
|
|
'fieldName' => 'association_mapping', |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
'fieldName' => 'sub_association_mapping', |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
'fieldName' => 'sub_sub_association_mapping', |
113
|
|
|
], |
114
|
|
|
], |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
118
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$filter->apply($builder, ['type' => ContainsOperatorType::TYPE_EQUAL, 'value' => 'asd']); |
121
|
|
|
|
122
|
|
|
$this->assertSame( |
123
|
|
|
'o.association_mapping', |
124
|
|
|
$builder->query[0] |
|
|
|
|
125
|
|
|
); |
126
|
|
|
$this->assertSame( |
127
|
|
|
's_association_mapping.sub_association_mapping', |
128
|
|
|
$builder->query[1] |
|
|
|
|
129
|
|
|
); |
130
|
|
|
$this->assertSame( |
131
|
|
|
's_association_mapping_sub_association_mapping.sub_sub_association_mapping', |
132
|
|
|
$builder->query[2] |
|
|
|
|
133
|
|
|
); |
134
|
|
|
$this->assertSame( |
135
|
|
|
's_association_mapping_sub_association_mapping_sub_sub_association_mapping.field_name = :field_name_0', |
136
|
|
|
$builder->query[3] |
|
|
|
|
137
|
|
|
); |
138
|
|
|
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters); |
|
|
|
|
139
|
|
|
$this->assertTrue($filter->isActive()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @dataProvider caseSensitiveDataProvider |
144
|
|
|
*/ |
145
|
|
|
public function testCaseSensitive(bool $caseSensitive, int $operatorType, string $expectedQuery, string $expectedParameter): void |
146
|
|
|
{ |
147
|
|
|
$filter = new StringFilter(); |
148
|
|
|
$filter->initialize('field_name', ['case_sensitive' => $caseSensitive]); |
149
|
|
|
|
150
|
|
|
$builder = new ProxyQuery(new QueryBuilder()); |
|
|
|
|
151
|
|
|
$this->assertSame([], $builder->query); |
|
|
|
|
152
|
|
|
|
153
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => $operatorType]); |
154
|
|
|
$this->assertSame([$expectedQuery], $builder->query); |
|
|
|
|
155
|
|
|
$this->assertSame(['field_name_0' => $expectedParameter], $builder->parameters); |
|
|
|
|
156
|
|
|
$this->assertTrue($filter->isActive()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function caseSensitiveDataProvider(): array |
160
|
|
|
{ |
161
|
|
|
return [ |
162
|
|
|
[false, ContainsOperatorType::TYPE_CONTAINS, 'LOWER(alias.field) LIKE :field_name_0', '%foobar%'], |
163
|
|
|
[false, ContainsOperatorType::TYPE_NOT_CONTAINS, 'LOWER(alias.field) NOT LIKE :field_name_0 OR alias.field IS NULL', '%foobar%'], |
164
|
|
|
[false, ContainsOperatorType::TYPE_EQUAL, 'LOWER(alias.field) = :field_name_0', 'foobar'], |
165
|
|
|
[true, ContainsOperatorType::TYPE_CONTAINS, 'alias.field LIKE :field_name_0', '%FooBar%'], |
166
|
|
|
[true, ContainsOperatorType::TYPE_NOT_CONTAINS, 'alias.field NOT LIKE :field_name_0 OR alias.field IS NULL', '%FooBar%'], |
167
|
|
|
[true, ContainsOperatorType::TYPE_EQUAL, 'alias.field = :field_name_0', 'FooBar'], |
168
|
|
|
|
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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: