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