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\AdminBundle\Tests\Filter; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Filter\FilterFactory; |
18
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Filter\FooFilter; |
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
20
|
|
|
|
21
|
|
|
class FilterTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testFilter(): void |
24
|
|
|
{ |
25
|
|
|
$filter = new FooFilter(); |
26
|
|
|
|
27
|
|
|
$this->assertSame(TextType::class, $filter->getFieldType()); |
28
|
|
|
$this->assertSame(['required' => false], $filter->getFieldOptions()); |
29
|
|
|
$this->assertNull($filter->getLabel()); |
30
|
|
|
|
31
|
|
|
$options = [ |
32
|
|
|
'label' => 'foo', |
33
|
|
|
'field_type' => 'integer', |
34
|
|
|
'field_options' => ['required' => true], |
35
|
|
|
'field_name' => 'name', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$filter->setOptions($options); |
39
|
|
|
|
40
|
|
|
$this->assertSame('foo', $filter->getOption('label')); |
41
|
|
|
$this->assertSame('foo', $filter->getLabel()); |
42
|
|
|
|
43
|
|
|
$expected = array_merge([ |
44
|
|
|
'show_filter' => null, |
45
|
|
|
'advanced_filter' => true, |
46
|
|
|
'foo' => 'bar', |
47
|
|
|
], $options); |
48
|
|
|
|
49
|
|
|
$this->assertSame($expected, $filter->getOptions()); |
50
|
|
|
$this->assertSame('name', $filter->getFieldName()); |
51
|
|
|
|
52
|
|
|
$this->assertSame('default', $filter->getOption('fake', 'default')); |
53
|
|
|
|
54
|
|
|
$filter->setValue(42); |
55
|
|
|
$this->assertSame(42, $filter->getValue()); |
56
|
|
|
|
57
|
|
|
$filter->setCondition('>'); |
58
|
|
|
$this->assertSame('>', $filter->getCondition()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetFieldOption(): void |
62
|
|
|
{ |
63
|
|
|
$filter = new FooFilter(); |
64
|
|
|
$filter->initialize('name', [ |
65
|
|
|
'field_options' => ['foo' => 'bar', 'baz' => 12345], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->assertSame(['foo' => 'bar', 'baz' => 12345], $filter->getFieldOptions()); |
69
|
|
|
$this->assertSame('bar', $filter->getFieldOption('foo')); |
70
|
|
|
$this->assertSame(12345, $filter->getFieldOption('baz')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testSetFieldOption(): void |
74
|
|
|
{ |
75
|
|
|
$filter = new FooFilter(); |
76
|
|
|
$this->assertSame(['required' => false], $filter->getFieldOptions()); |
77
|
|
|
|
78
|
|
|
$filter->setFieldOption('foo', 'bar'); |
79
|
|
|
$filter->setFieldOption('baz', 12345); |
80
|
|
|
|
81
|
|
|
$this->assertSame(['foo' => 'bar', 'baz' => 12345], $filter->getFieldOptions()); |
82
|
|
|
$this->assertSame('bar', $filter->getFieldOption('foo')); |
83
|
|
|
$this->assertSame(12345, $filter->getFieldOption('baz')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testInitialize(): void |
87
|
|
|
{ |
88
|
|
|
$filter = new FooFilter(); |
89
|
|
|
$filter->initialize('name', [ |
90
|
|
|
'field_name' => 'bar', |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$this->assertSame('name', $filter->getName()); |
94
|
|
|
$this->assertSame('bar', $filter->getOption('field_name')); |
95
|
|
|
$this->assertSame('bar', $filter->getFieldName()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testLabel(): void |
99
|
|
|
{ |
100
|
|
|
$filter = new FooFilter(); |
101
|
|
|
$filter->setLabel('foo'); |
102
|
|
|
|
103
|
|
|
$this->assertSame('foo', $filter->getLabel()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testExceptionOnNonDefinedFilterName(): void |
107
|
|
|
{ |
108
|
|
|
$filter = new FooFilter(); |
109
|
|
|
$filter->getName(); |
110
|
|
|
|
111
|
|
|
$this->expectException(\RuntimeException::class); |
112
|
|
|
$this->expectExceptionMessage(sprintf( |
113
|
|
|
'Seems like you didn\'t call `initialize()` on the filter `%s`. Did you create it through `%s::create()`?', |
114
|
|
|
FooFilter::class, |
115
|
|
|
FilterFactory::class |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testExceptionOnNonDefinedFieldName(): void |
120
|
|
|
{ |
121
|
|
|
$filter = new FooFilter(); |
122
|
|
|
$filter->initialize('foo'); |
123
|
|
|
|
124
|
|
|
$filter->getFieldName(); |
125
|
|
|
|
126
|
|
|
$this->expectException(\RuntimeException::class); |
127
|
|
|
$this->expectExceptionMessage('The option `field_name` must be set for field: `foo`'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @dataProvider isActiveData |
132
|
|
|
* |
133
|
|
|
* @param $expected |
134
|
|
|
* @param $value |
135
|
|
|
*/ |
136
|
|
|
public function testIsActive(bool $expected, array $value): void |
137
|
|
|
{ |
138
|
|
|
$filter = new FooFilter(); |
139
|
|
|
$filter->setValue($value); |
140
|
|
|
|
141
|
|
|
$this->assertSame($expected, $filter->isActive()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function isActiveData(): array |
145
|
|
|
{ |
146
|
|
|
return [ |
147
|
|
|
[false, []], |
148
|
|
|
[false, ['value' => null]], |
149
|
|
|
[false, ['value' => '']], |
150
|
|
|
[false, ['value' => false]], |
151
|
|
|
[true, ['value' => 'active']], |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetTranslationDomain(): void |
156
|
|
|
{ |
157
|
|
|
$filter = new FooFilter(); |
158
|
|
|
$this->assertNull($filter->getTranslationDomain()); |
159
|
|
|
$filter->setOption('translation_domain', 'baz'); |
160
|
|
|
$this->assertSame('baz', $filter->getTranslationDomain()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testGetFieldMappingException(): void |
164
|
|
|
{ |
165
|
|
|
$filter = new FooFilter(); |
166
|
|
|
$filter->initialize('foo'); |
167
|
|
|
|
168
|
|
|
$filter->getFieldMapping(); |
169
|
|
|
|
170
|
|
|
$this->expectException(\RuntimeException::class); |
171
|
|
|
$this->expectExceptionMessage('The option `field_mapping` must be set for field: `foo`'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testGetFieldMapping(): void |
175
|
|
|
{ |
176
|
|
|
$fieldMapping = [ |
177
|
|
|
'fieldName' => 'username', |
178
|
|
|
'type' => 'string', |
179
|
|
|
'columnName' => 'username', |
180
|
|
|
'length' => 200, |
181
|
|
|
'unique' => true, |
182
|
|
|
'nullable' => false, |
183
|
|
|
'declared' => 'Foo\Bar\User', |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$filter = new FooFilter(); |
187
|
|
|
$filter->setOption('field_mapping', $fieldMapping); |
188
|
|
|
$this->assertSame($fieldMapping, $filter->getFieldMapping()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testGetParentAssociationMappings(): void |
192
|
|
|
{ |
193
|
|
|
$parentAssociationMapping = [ |
194
|
|
|
0 => ['fieldName' => 'user', |
195
|
|
|
'targetEntity' => 'Foo\Bar\User', |
196
|
|
|
'joinColumns' => [ |
197
|
|
|
0 => [ |
198
|
|
|
'name' => 'user_id', |
199
|
|
|
'referencedColumnName' => 'user_id', |
200
|
|
|
], |
201
|
|
|
], |
202
|
|
|
'type' => 2, |
203
|
|
|
'mappedBy' => null, |
204
|
|
|
], |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
$filter = new FooFilter(); |
208
|
|
|
$this->assertSame([], $filter->getParentAssociationMappings()); |
209
|
|
|
$filter->setOption('parent_association_mappings', $parentAssociationMapping); |
210
|
|
|
$this->assertSame($parentAssociationMapping, $filter->getParentAssociationMappings()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testGetAssociationMappingException(): void |
214
|
|
|
{ |
215
|
|
|
|
216
|
|
|
$filter = new FooFilter(); |
217
|
|
|
$filter->initialize('foo'); |
218
|
|
|
|
219
|
|
|
$filter->getAssociationMapping(); |
220
|
|
|
|
221
|
|
|
$this->expectException(\RuntimeException::class); |
222
|
|
|
$this->expectExceptionMessage('The option `association_mapping` must be set for field: `foo`'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testGetAssociationMapping(): void |
226
|
|
|
{ |
227
|
|
|
$associationMapping = [ |
228
|
|
|
'fieldName' => 'user', |
229
|
|
|
'targetEntity' => 'Foo\Bar\User', |
230
|
|
|
'joinColumns' => [ |
231
|
|
|
0 => [ |
232
|
|
|
'name' => 'user_id', |
233
|
|
|
'referencedColumnName' => 'user_id', |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
'type' => 2, |
237
|
|
|
'mappedBy' => null, |
238
|
|
|
]; |
239
|
|
|
|
240
|
|
|
$filter = new FooFilter(); |
241
|
|
|
$filter->setOption('association_mapping', $associationMapping); |
242
|
|
|
$this->assertSame($associationMapping, $filter->getAssociationMapping()); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|