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\Admin; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
20
|
|
|
use Sonata\AdminBundle\Exception\NoValueException; |
21
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
22
|
|
|
|
23
|
|
|
class FieldDescriptionTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testOptions(): void |
26
|
|
|
{ |
27
|
|
|
$field = new FieldDescription(); |
28
|
|
|
$field->setOptions([ |
29
|
|
|
'template' => 'foo', |
30
|
|
|
'type' => 'bar', |
31
|
|
|
'misc' => 'foobar', |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
// test method shortcut |
35
|
|
|
$this->assertNull($field->getOption('template')); |
36
|
|
|
$this->assertNull($field->getOption('type')); |
37
|
|
|
|
38
|
|
|
$this->assertSame('foo', $field->getTemplate()); |
39
|
|
|
$this->assertSame('bar', $field->getType()); |
40
|
|
|
|
41
|
|
|
// test the default value option |
42
|
|
|
$this->assertSame('default', $field->getOption('template', 'default')); |
43
|
|
|
|
44
|
|
|
// test the merge options |
45
|
|
|
$field->setOption('array', ['key1' => 'val1']); |
46
|
|
|
$field->mergeOption('array', ['key1' => 'key_1', 'key2' => 'key_2']); |
47
|
|
|
|
48
|
|
|
$this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array')); |
49
|
|
|
|
50
|
|
|
$field->mergeOption('non_existant', ['key1' => 'key_1', 'key2' => 'key_2']); |
51
|
|
|
|
52
|
|
|
$this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array')); |
53
|
|
|
|
54
|
|
|
$field->mergeOptions(['array' => ['key3' => 'key_3']]); |
55
|
|
|
|
56
|
|
|
$this->assertSame(['key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'], $field->getOption('array')); |
57
|
|
|
|
58
|
|
|
$field->setOption('integer', 1); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$field->mergeOption('integer', []); |
62
|
|
|
$this->fail('no exception raised !!'); |
63
|
|
|
} catch (\RuntimeException $e) { |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$field->mergeOptions(['final' => 'test']); |
67
|
|
|
|
68
|
|
|
$expected = [ |
69
|
|
|
'misc' => 'foobar', |
70
|
|
|
'placeholder' => 'short_object_description_placeholder', |
71
|
|
|
'link_parameters' => [], |
72
|
|
|
'array' => [ |
73
|
|
|
'key1' => 'key_1', |
74
|
|
|
'key2' => 'key_2', |
75
|
|
|
'key3' => 'key_3', |
76
|
|
|
], |
77
|
|
|
'non_existant' => [ |
78
|
|
|
'key1' => 'key_1', |
79
|
|
|
'key2' => 'key_2', |
80
|
|
|
], |
81
|
|
|
'integer' => 1, |
82
|
|
|
'final' => 'test', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$this->assertSame($expected, $field->getOptions()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testAssociationMapping(): void |
89
|
|
|
{ |
90
|
|
|
$field = new FieldDescription(); |
91
|
|
|
$field->setAssociationMapping([ |
92
|
|
|
'type' => 'integer', |
93
|
|
|
'fieldName' => 'position', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$this->assertSame('integer', $field->getType()); |
97
|
|
|
$this->assertSame('position', $field->getFieldName()); |
98
|
|
|
|
99
|
|
|
// cannot overwrite defined definition |
100
|
|
|
$field->setAssociationMapping([ |
101
|
|
|
'type' => 'overwrite?', |
102
|
|
|
'fieldName' => 'overwritten', |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$this->assertSame('integer', $field->getType()); |
106
|
|
|
$this->assertSame('overwritten', $field->getFieldName()); |
107
|
|
|
|
108
|
|
|
$field->setMappingType('string'); |
109
|
|
|
$this->assertSame('string', $field->getMappingType()); |
110
|
|
|
$this->assertSame('integer', $field->getType()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testSetName(): void |
114
|
|
|
{ |
115
|
|
|
$field = new FieldDescription(); |
116
|
|
|
$field->setName('New field description name'); |
117
|
|
|
|
118
|
|
|
$this->assertSame($field->getName(), 'New field description name'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testSetNameSetFieldNameToo(): void |
122
|
|
|
{ |
123
|
|
|
$field = new FieldDescription(); |
124
|
|
|
$field->setName('New field description name'); |
125
|
|
|
|
126
|
|
|
$this->assertSame($field->getFieldName(), 'New field description name'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void |
130
|
|
|
{ |
131
|
|
|
$field = new FieldDescription(); |
132
|
|
|
$field->setFieldName('field name'); |
133
|
|
|
$field->setName('New field description name'); |
134
|
|
|
|
135
|
|
|
$this->assertSame($field->getFieldName(), 'field name'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testGetParent(): void |
139
|
|
|
{ |
140
|
|
|
$adminMock = $this->createMock(AdminInterface::class); |
141
|
|
|
$field = new FieldDescription(); |
142
|
|
|
$field->setParent($adminMock); |
143
|
|
|
|
144
|
|
|
$this->assertSame($adminMock, $field->getParent()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testGetHelp(): void |
148
|
|
|
{ |
149
|
|
|
$field = new FieldDescription(); |
150
|
|
|
$field->setHelp('help message'); |
151
|
|
|
|
152
|
|
|
$this->assertSame($field->getHelp(), 'help message'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetAdmin(): void |
156
|
|
|
{ |
157
|
|
|
$adminMock = $this->createMock(AdminInterface::class); |
158
|
|
|
$field = new FieldDescription(); |
159
|
|
|
$field->setAdmin($adminMock); |
160
|
|
|
|
161
|
|
|
$this->assertSame($adminMock, $field->getAdmin()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetAssociationAdmin(): void |
165
|
|
|
{ |
166
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
167
|
|
|
$adminMock->expects($this->once()) |
168
|
|
|
->method('setParentFieldDescription') |
169
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
170
|
|
|
|
171
|
|
|
$field = new FieldDescription(); |
172
|
|
|
$field->setAssociationAdmin($adminMock); |
173
|
|
|
|
174
|
|
|
$this->assertSame($adminMock, $field->getAssociationAdmin()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testHasAssociationAdmin(): void |
178
|
|
|
{ |
179
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
180
|
|
|
$adminMock->expects($this->once()) |
181
|
|
|
->method('setParentFieldDescription') |
182
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
183
|
|
|
|
184
|
|
|
$field = new FieldDescription(); |
185
|
|
|
|
186
|
|
|
$this->assertFalse($field->hasAssociationAdmin()); |
187
|
|
|
|
188
|
|
|
$field->setAssociationAdmin($adminMock); |
189
|
|
|
|
190
|
|
|
$this->assertTrue($field->hasAssociationAdmin()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testGetValue(): void |
194
|
|
|
{ |
195
|
|
|
$object = new class() { |
196
|
|
|
public function myMethod() |
197
|
|
|
{ |
198
|
|
|
return 'myMethodValue'; |
199
|
|
|
} |
200
|
|
|
}; |
201
|
|
|
|
202
|
|
|
$field = new FieldDescription(); |
203
|
|
|
$field->setOption('code', 'myMethod'); |
204
|
|
|
|
205
|
|
|
$this->assertSame($field->getValue($object), 'myMethodValue'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testGetValueWhenCannotRetrieve(): void |
209
|
|
|
{ |
210
|
|
|
$object = new class() { |
211
|
|
|
public function myMethod() |
212
|
|
|
{ |
213
|
|
|
return 'myMethodValue'; |
214
|
|
|
} |
215
|
|
|
}; |
216
|
|
|
|
217
|
|
|
$field = new FieldDescription(); |
218
|
|
|
|
219
|
|
|
$this->expectException(NoValueException::class); |
220
|
|
|
|
221
|
|
|
$this->assertSame($field->getValue($object), 'myMethodValue'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testGetAssociationMapping(): void |
225
|
|
|
{ |
226
|
|
|
$assocationMapping = [ |
227
|
|
|
'type' => 'integer', |
228
|
|
|
'fieldName' => 'position', |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
$field = new FieldDescription(); |
232
|
|
|
$field->setAssociationMapping($assocationMapping); |
233
|
|
|
|
234
|
|
|
$this->assertSame($assocationMapping, $field->getAssociationMapping()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testSetAssociationMappingAllowOnlyForArray(): void |
238
|
|
|
{ |
239
|
|
|
$this->expectException(\RuntimeException::class); |
240
|
|
|
|
241
|
|
|
$field = new FieldDescription(); |
242
|
|
|
$field->setAssociationMapping('test'); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testSetFieldMappingAllowOnlyForArray(): void |
246
|
|
|
{ |
247
|
|
|
$this->expectException(\RuntimeException::class); |
248
|
|
|
|
249
|
|
|
$field = new FieldDescription(); |
250
|
|
|
$field->setFieldMapping('test'); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testSetFieldMappingSetType(): void |
254
|
|
|
{ |
255
|
|
|
$fieldMapping = [ |
256
|
|
|
'type' => 'integer', |
257
|
|
|
'fieldName' => 'position', |
258
|
|
|
]; |
259
|
|
|
|
260
|
|
|
$field = new FieldDescription(); |
261
|
|
|
$field->setFieldMapping($fieldMapping); |
262
|
|
|
|
263
|
|
|
$this->assertSame('integer', $field->getType()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testSetFieldMappingSetMappingType(): void |
267
|
|
|
{ |
268
|
|
|
$fieldMapping = [ |
269
|
|
|
'type' => 'integer', |
270
|
|
|
'fieldName' => 'position', |
271
|
|
|
]; |
272
|
|
|
|
273
|
|
|
$field = new FieldDescription(); |
274
|
|
|
$field->setFieldMapping($fieldMapping); |
275
|
|
|
|
276
|
|
|
$this->assertSame('integer', $field->getMappingType()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testSetFieldMappingSetFieldName(): void |
280
|
|
|
{ |
281
|
|
|
$fieldMapping = [ |
282
|
|
|
'type' => 'integer', |
283
|
|
|
'fieldName' => 'position', |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
$field = new FieldDescription(); |
287
|
|
|
$field->setFieldMapping($fieldMapping); |
288
|
|
|
|
289
|
|
|
$this->assertSame('position', $field->getFieldName()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* NEXT_MAJOR: Remove this test. |
294
|
|
|
* |
295
|
|
|
* @group legacy |
296
|
|
|
*/ |
297
|
|
|
public function testGetTargetEntity(): void |
298
|
|
|
{ |
299
|
|
|
$assocationMapping = [ |
300
|
|
|
'type' => 'integer', |
301
|
|
|
'fieldName' => 'position', |
302
|
|
|
'targetDocument' => 'someValue', |
303
|
|
|
]; |
304
|
|
|
|
305
|
|
|
$field = new FieldDescription(); |
306
|
|
|
|
307
|
|
|
$this->assertNull($field->getTargetEntity()); |
|
|
|
|
308
|
|
|
|
309
|
|
|
$field->setAssociationMapping($assocationMapping); |
310
|
|
|
|
311
|
|
|
$this->assertSame('someValue', $field->getTargetEntity()); |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function testGetTargetModel(): void |
315
|
|
|
{ |
316
|
|
|
$assocationMapping = [ |
317
|
|
|
'type' => 'integer', |
318
|
|
|
'fieldName' => 'position', |
319
|
|
|
'targetDocument' => 'someValue', |
320
|
|
|
]; |
321
|
|
|
|
322
|
|
|
$field = new FieldDescription(); |
323
|
|
|
|
324
|
|
|
$this->assertNull($field->getTargetModel()); |
325
|
|
|
|
326
|
|
|
$field->setAssociationMapping($assocationMapping); |
327
|
|
|
|
328
|
|
|
$this->assertSame('someValue', $field->getTargetModel()); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function testIsIdentifierFromFieldMapping(): void |
332
|
|
|
{ |
333
|
|
|
$fieldMapping = [ |
334
|
|
|
'type' => 'integer', |
335
|
|
|
'fieldName' => 'position', |
336
|
|
|
'id' => 'someId', |
337
|
|
|
]; |
338
|
|
|
|
339
|
|
|
$field = new FieldDescription(); |
340
|
|
|
$field->setFieldMapping($fieldMapping); |
341
|
|
|
|
342
|
|
|
$this->assertSame('someId', $field->isIdentifier()); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function testGetFieldMapping(): void |
346
|
|
|
{ |
347
|
|
|
$fieldMapping = [ |
348
|
|
|
'type' => 'integer', |
349
|
|
|
'fieldName' => 'position', |
350
|
|
|
'id' => 'someId', |
351
|
|
|
]; |
352
|
|
|
|
353
|
|
|
$field = new FieldDescription(); |
354
|
|
|
$field->setFieldMapping($fieldMapping); |
355
|
|
|
|
356
|
|
|
$this->assertSame($fieldMapping, $field->getFieldMapping()); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|