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\DoctrineMongoDBAdminBundle\Tests\Filter; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
17
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType; |
18
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; |
19
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\Filter; |
20
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\StringFilter; |
21
|
|
|
|
22
|
|
|
class StringFilterTest extends FilterWithQueryBuilderTest |
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($this->getQueryBuilder()); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
32
|
|
|
->expects($this->never()) |
33
|
|
|
->method('field') |
34
|
|
|
; |
35
|
|
|
|
36
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
37
|
|
|
$filter->filter($builder, 'alias', 'field', ''); |
38
|
|
|
|
39
|
|
|
$this->assertFalse($filter->isActive()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testContains(): void |
43
|
|
|
{ |
44
|
|
|
$filter = new StringFilter(); |
45
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
46
|
|
|
|
47
|
|
|
$builder = new ProxyQuery($this->getQueryBuilder()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
50
|
|
|
->expects($this->exactly(2)) |
51
|
|
|
->method('equals') |
52
|
|
|
->with($this->isInstanceOf($this->getMongoRegexClass())) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_CONTAINS]); |
|
|
|
|
56
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => null]); |
|
|
|
|
57
|
|
|
$this->assertTrue($filter->isActive()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testNotContains(): void |
61
|
|
|
{ |
62
|
|
|
$filter = new StringFilter(); |
63
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
64
|
|
|
|
65
|
|
|
$builder = new ProxyQuery($this->getQueryBuilder()); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
68
|
|
|
->expects($this->once()) |
69
|
|
|
->method('not') |
70
|
|
|
->with($this->isInstanceOf($this->getMongoRegexClass())) |
71
|
|
|
; |
72
|
|
|
|
73
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_NOT_CONTAINS]); |
|
|
|
|
74
|
|
|
$this->assertTrue($filter->isActive()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testEquals(): void |
78
|
|
|
{ |
79
|
|
|
$filter = new StringFilter(); |
80
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
81
|
|
|
|
82
|
|
|
$builder = new ProxyQuery($this->getQueryBuilder()); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
85
|
|
|
->expects($this->once()) |
86
|
|
|
->method('equals') |
87
|
|
|
->with('asd') |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_EQUAL]); |
|
|
|
|
91
|
|
|
$this->assertTrue($filter->isActive()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testEqualsWithValidParentAssociationMappings(): void |
95
|
|
|
{ |
96
|
|
|
$filter = new StringFilter(); |
97
|
|
|
$filter->initialize('field_name', [ |
98
|
|
|
'format' => '%s', |
99
|
|
|
'field_name' => 'field_name', |
100
|
|
|
'parent_association_mappings' => [ |
101
|
|
|
[ |
102
|
|
|
'fieldName' => 'association_mapping', |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
'fieldName' => 'sub_association_mapping', |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
'fieldName' => 'sub_sub_association_mapping', |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$queryBuilder = $this->createMock(Builder::class); |
114
|
|
|
|
115
|
|
|
$builder = new ProxyQuery($queryBuilder); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
118
|
|
|
->method('field') |
119
|
|
|
->with('field_name') |
120
|
|
|
->willReturnSelf() |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
124
|
|
|
->expects($this->once()) |
125
|
|
|
->method('equals') |
126
|
|
|
->with('asd') |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$filter->apply($builder, ['type' => ChoiceType::TYPE_EQUAL, 'value' => 'asd']); |
130
|
|
|
$this->assertTrue($filter->isActive()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testOr(): void |
134
|
|
|
{ |
135
|
|
|
$filter = new StringFilter(); |
136
|
|
|
$filter->initialize('field_name', ['format' => '%s']); |
137
|
|
|
$filter->setCondition(Filter::CONDITION_OR); |
138
|
|
|
|
139
|
|
|
$builder = new ProxyQuery($this->getQueryBuilder()); |
|
|
|
|
140
|
|
|
$builder->getQueryBuilder()->expects($this->once())->method('addOr'); |
|
|
|
|
141
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_CONTAINS]); |
|
|
|
|
142
|
|
|
$this->assertTrue($filter->isActive()); |
143
|
|
|
|
144
|
|
|
$filter->setCondition(Filter::CONDITION_AND); |
145
|
|
|
|
146
|
|
|
$builder = new ProxyQuery($this->getQueryBuilder()); |
|
|
|
|
147
|
|
|
$builder->getQueryBuilder()->expects($this->never())->method('addOr'); |
|
|
|
|
148
|
|
|
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_CONTAINS]); |
|
|
|
|
149
|
|
|
$this->assertTrue($filter->isActive()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function getMongoRegexClass() |
153
|
|
|
{ |
154
|
|
|
return \MongoRegex::class; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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: