|
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\DoctrinePHPCRAdminBundle\Tests\Unit\Filter; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Filter\StringFilter; |
|
17
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType; |
|
18
|
|
|
|
|
19
|
|
|
class StringFilterTest extends BaseTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var StringFilter |
|
23
|
|
|
*/ |
|
24
|
|
|
private $filter; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp(): void |
|
27
|
|
|
{ |
|
28
|
|
|
parent::setUp(); |
|
29
|
|
|
$this->filter = new StringFilter(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testFilterNullData(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$res = $this->filter->filter($this->proxyQuery, null, 'somefield', null); |
|
|
|
|
|
|
35
|
|
|
$this->assertNull($res); |
|
36
|
|
|
$this->assertFalse($this->filter->isActive()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testFilterEmptyArrayData(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$res = $this->filter->filter($this->proxyQuery, null, 'somefield', []); |
|
|
|
|
|
|
42
|
|
|
$this->assertNull($res); |
|
43
|
|
|
$this->assertFalse($this->filter->isActive()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testFilterEmptyArrayDataSpecifiedType(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$res = $this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL]); |
|
|
|
|
|
|
49
|
|
|
$this->assertNull($res); |
|
50
|
|
|
$this->assertFalse($this->filter->isActive()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testFilterEmptyArrayDataWithMeaninglessValue(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->proxyQuery->expects($this->never()) |
|
|
|
|
|
|
56
|
|
|
->method('getQueryBuilder'); |
|
57
|
|
|
|
|
58
|
|
|
$this->filter->filter($this->proxyQuery, null, 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => ' ']); |
|
|
|
|
|
|
59
|
|
|
$this->assertFalse($this->filter->isActive()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getFilters() |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
[ChoiceType::TYPE_EQUAL, [ |
|
|
|
|
|
|
66
|
|
|
'where.constraint.operand_dynamic' => [ |
|
67
|
|
|
'getAlias' => 'a', |
|
68
|
|
|
'getField' => 'somefield', |
|
69
|
|
|
], |
|
70
|
|
|
'where.constraint.operand_static' => [ |
|
71
|
|
|
'getValue' => 'somevalue', |
|
72
|
|
|
], |
|
73
|
|
|
]], |
|
74
|
|
|
[ChoiceType::TYPE_NOT_CONTAINS, [ |
|
|
|
|
|
|
75
|
|
|
'where.constraint' => [ |
|
76
|
|
|
'getField' => 'somefield', |
|
77
|
|
|
'getFullTextSearchExpression' => '* -somevalue', ], |
|
78
|
|
|
]], |
|
79
|
|
|
[ChoiceType::TYPE_CONTAINS, [ |
|
|
|
|
|
|
80
|
|
|
'where.constraint.operand_dynamic' => [ |
|
81
|
|
|
'getAlias' => 'a', |
|
82
|
|
|
'getField' => 'somefield', |
|
83
|
|
|
], |
|
84
|
|
|
'where.constraint.operand_static' => [ |
|
85
|
|
|
'getValue' => '%somevalue%', |
|
86
|
|
|
], |
|
87
|
|
|
]], |
|
88
|
|
|
[ChoiceType::TYPE_CONTAINS_WORDS, [ |
|
89
|
|
|
'where.constraint' => [ |
|
90
|
|
|
'getField' => 'somefield', |
|
91
|
|
|
'getFullTextSearchExpression' => 'somevalue', ], |
|
92
|
|
|
]], |
|
93
|
|
|
'equalCaseInsensitiveComparision' => [ChoiceType::TYPE_EQUAL, [ |
|
|
|
|
|
|
94
|
|
|
'where.constraint.operand_dynamic.operand_dynamic' => [ |
|
95
|
|
|
'getAlias' => 'a', |
|
96
|
|
|
'getField' => 'somefield', |
|
97
|
|
|
], |
|
98
|
|
|
'where.constraint.operand_static' => [ |
|
99
|
|
|
'getValue' => 'somevalue', |
|
100
|
|
|
], |
|
101
|
|
|
], true], |
|
102
|
|
|
'containsCaseInsensitiveComparision' => [ChoiceType::TYPE_CONTAINS, [ |
|
|
|
|
|
|
103
|
|
|
'where.constraint.operand_dynamic.operand_dynamic' => [ |
|
104
|
|
|
'getAlias' => 'a', |
|
105
|
|
|
'getField' => 'somefield', |
|
106
|
|
|
], |
|
107
|
|
|
'where.constraint.operand_static' => [ |
|
108
|
|
|
'getValue' => '%somevalue%', |
|
109
|
|
|
], |
|
110
|
|
|
], true], |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @dataProvider getFilters |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testFilterSwitch($choiceType, $assertPaths, $isLowerCase = false): void |
|
118
|
|
|
{ |
|
119
|
|
|
if ($isLowerCase) { |
|
120
|
|
|
$this->filter->setOption('compare_case_insensitive', true); |
|
121
|
|
|
} |
|
122
|
|
|
$this->filter->filter( |
|
123
|
|
|
$this->proxyQuery, |
|
124
|
|
|
null, |
|
125
|
|
|
'somefield', |
|
126
|
|
|
['type' => $choiceType, 'value' => 'somevalue'] |
|
127
|
|
|
); |
|
128
|
|
|
$this->assertTrue($this->filter->isActive()); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($assertPaths as $path => $assertions) { |
|
131
|
|
|
$node = $this->qbTester->getNode($path); |
|
132
|
|
|
foreach ($assertions as $methodName => $expectedValue) { |
|
133
|
|
|
$res = $node->$methodName(); |
|
134
|
|
|
$this->assertSame($expectedValue, $res); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->assertTrue($this->filter->isActive()); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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: