|
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
|
|
|
$this->expectException(\RuntimeException::class); |
|
109
|
|
|
$this->expectExceptionMessage(sprintf( |
|
110
|
|
|
'Seems like you didn\'t call `initialize()` on the filter `%s`. Did you create it through `%s::create()`?', |
|
111
|
|
|
FooFilter::class, |
|
112
|
|
|
FilterFactory::class |
|
113
|
|
|
)); |
|
114
|
|
|
|
|
115
|
|
|
$filter = new FooFilter(); |
|
116
|
|
|
|
|
117
|
|
|
$filter->getName(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testExceptionOnNonDefinedFieldName(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->expectException(\RuntimeException::class); |
|
123
|
|
|
$this->expectExceptionMessage('The option `field_name` must be set for field: `foo`'); |
|
124
|
|
|
|
|
125
|
|
|
$filter = new FooFilter(); |
|
126
|
|
|
$filter->initialize('foo'); |
|
127
|
|
|
|
|
128
|
|
|
$filter->getFieldName(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @dataProvider isActiveData |
|
133
|
|
|
* |
|
134
|
|
|
* @param $expected |
|
135
|
|
|
* @param $value |
|
136
|
|
|
*/ |
|
137
|
|
|
public function testIsActive(bool $expected, array $value): void |
|
138
|
|
|
{ |
|
139
|
|
|
$filter = new FooFilter(); |
|
140
|
|
|
$filter->setValue($value); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertSame($expected, $filter->isActive()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function isActiveData(): array |
|
146
|
|
|
{ |
|
147
|
|
|
return [ |
|
148
|
|
|
[false, []], |
|
149
|
|
|
[false, ['value' => null]], |
|
150
|
|
|
[false, ['value' => '']], |
|
151
|
|
|
[false, ['value' => false]], |
|
152
|
|
|
[true, ['value' => 'active']], |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testGetTranslationDomain(): void |
|
157
|
|
|
{ |
|
158
|
|
|
$filter = new FooFilter(); |
|
159
|
|
|
$this->assertNull($filter->getTranslationDomain()); |
|
160
|
|
|
$filter->setOption('translation_domain', 'baz'); |
|
161
|
|
|
$this->assertSame('baz', $filter->getTranslationDomain()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testGetFieldMappingException(): void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->expectException(\RuntimeException::class); |
|
167
|
|
|
$this->expectExceptionMessage('The option `field_mapping` must be set for field: `foo`'); |
|
168
|
|
|
|
|
169
|
|
|
$filter = new FooFilter(); |
|
170
|
|
|
$filter->initialize('foo'); |
|
171
|
|
|
|
|
172
|
|
|
$filter->getFieldMapping(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function testGetFieldMapping(): void |
|
176
|
|
|
{ |
|
177
|
|
|
$fieldMapping = [ |
|
178
|
|
|
'fieldName' => 'username', |
|
179
|
|
|
'type' => 'string', |
|
180
|
|
|
'columnName' => 'username', |
|
181
|
|
|
'length' => 200, |
|
182
|
|
|
'unique' => true, |
|
183
|
|
|
'nullable' => false, |
|
184
|
|
|
'declared' => 'Foo\Bar\User', |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
|
|
$filter = new FooFilter(); |
|
188
|
|
|
$filter->setOption('field_mapping', $fieldMapping); |
|
189
|
|
|
$this->assertSame($fieldMapping, $filter->getFieldMapping()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testGetParentAssociationMappings(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$parentAssociationMapping = [ |
|
195
|
|
|
0 => ['fieldName' => 'user', |
|
196
|
|
|
'targetEntity' => 'Foo\Bar\User', |
|
197
|
|
|
'joinColumns' => [ |
|
198
|
|
|
0 => [ |
|
199
|
|
|
'name' => 'user_id', |
|
200
|
|
|
'referencedColumnName' => 'user_id', |
|
201
|
|
|
], |
|
202
|
|
|
], |
|
203
|
|
|
'type' => 2, |
|
204
|
|
|
'mappedBy' => null, |
|
205
|
|
|
], |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
$filter = new FooFilter(); |
|
209
|
|
|
$this->assertSame([], $filter->getParentAssociationMappings()); |
|
210
|
|
|
$filter->setOption('parent_association_mappings', $parentAssociationMapping); |
|
211
|
|
|
$this->assertSame($parentAssociationMapping, $filter->getParentAssociationMappings()); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testGetAssociationMappingException(): void |
|
215
|
|
|
{ |
|
216
|
|
|
$this->expectException(\RuntimeException::class); |
|
217
|
|
|
$this->expectExceptionMessage('The option `association_mapping` must be set for field: `foo`'); |
|
218
|
|
|
|
|
219
|
|
|
$filter = new FooFilter(); |
|
220
|
|
|
$filter->initialize('foo'); |
|
221
|
|
|
|
|
222
|
|
|
$filter->getAssociationMapping(); |
|
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
|
|
|
|