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 testGetAdmin(): void |
148
|
|
|
{ |
149
|
|
|
$adminMock = $this->createMock(AdminInterface::class); |
150
|
|
|
$field = new FieldDescription(); |
151
|
|
|
$field->setAdmin($adminMock); |
152
|
|
|
|
153
|
|
|
$this->assertSame($adminMock, $field->getAdmin()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testGetAssociationAdmin(): void |
157
|
|
|
{ |
158
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
159
|
|
|
$adminMock->expects($this->once()) |
160
|
|
|
->method('setParentFieldDescription') |
161
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
162
|
|
|
|
163
|
|
|
$field = new FieldDescription(); |
164
|
|
|
$field->setAssociationAdmin($adminMock); |
165
|
|
|
|
166
|
|
|
$this->assertSame($adminMock, $field->getAssociationAdmin()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testHasAssociationAdmin(): void |
170
|
|
|
{ |
171
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
172
|
|
|
$adminMock->expects($this->once()) |
173
|
|
|
->method('setParentFieldDescription') |
174
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
175
|
|
|
|
176
|
|
|
$field = new FieldDescription(); |
177
|
|
|
|
178
|
|
|
$this->assertFalse($field->hasAssociationAdmin()); |
179
|
|
|
|
180
|
|
|
$field->setAssociationAdmin($adminMock); |
181
|
|
|
|
182
|
|
|
$this->assertTrue($field->hasAssociationAdmin()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testGetValue(): void |
186
|
|
|
{ |
187
|
|
|
$object = new class() { |
188
|
|
|
public function myMethod() |
189
|
|
|
{ |
190
|
|
|
return 'myMethodValue'; |
191
|
|
|
} |
192
|
|
|
}; |
193
|
|
|
|
194
|
|
|
$field = new FieldDescription(); |
195
|
|
|
$field->setOption('code', 'myMethod'); |
196
|
|
|
|
197
|
|
|
$this->assertSame($field->getValue($object), 'myMethodValue'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testGetValueWhenCannotRetrieve(): void |
201
|
|
|
{ |
202
|
|
|
$object = new class() { |
203
|
|
|
public function myMethod() |
204
|
|
|
{ |
205
|
|
|
return 'myMethodValue'; |
206
|
|
|
} |
207
|
|
|
}; |
208
|
|
|
|
209
|
|
|
$field = new FieldDescription(); |
210
|
|
|
|
211
|
|
|
$this->expectException(NoValueException::class); |
212
|
|
|
|
213
|
|
|
$this->assertSame($field->getValue($object), 'myMethodValue'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testGetAssociationMapping(): void |
217
|
|
|
{ |
218
|
|
|
$assocationMapping = [ |
219
|
|
|
'type' => 'integer', |
220
|
|
|
'fieldName' => 'position', |
221
|
|
|
]; |
222
|
|
|
|
223
|
|
|
$field = new FieldDescription(); |
224
|
|
|
$field->setAssociationMapping($assocationMapping); |
225
|
|
|
|
226
|
|
|
$this->assertSame($assocationMapping, $field->getAssociationMapping()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function testSetAssociationMappingAllowOnlyForArray(): void |
230
|
|
|
{ |
231
|
|
|
$this->expectException(\RuntimeException::class); |
232
|
|
|
|
233
|
|
|
$field = new FieldDescription(); |
234
|
|
|
$field->setAssociationMapping('test'); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testSetFieldMappingAllowOnlyForArray(): void |
238
|
|
|
{ |
239
|
|
|
$this->expectException(\RuntimeException::class); |
240
|
|
|
|
241
|
|
|
$field = new FieldDescription(); |
242
|
|
|
$field->setFieldMapping('test'); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testSetFieldMappingSetType(): void |
246
|
|
|
{ |
247
|
|
|
$fieldMapping = [ |
248
|
|
|
'type' => 'integer', |
249
|
|
|
'fieldName' => 'position', |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$field = new FieldDescription(); |
253
|
|
|
$field->setFieldMapping($fieldMapping); |
254
|
|
|
|
255
|
|
|
$this->assertSame('integer', $field->getType()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testSetFieldMappingSetMappingType(): void |
259
|
|
|
{ |
260
|
|
|
$fieldMapping = [ |
261
|
|
|
'type' => 'integer', |
262
|
|
|
'fieldName' => 'position', |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
$field = new FieldDescription(); |
266
|
|
|
$field->setFieldMapping($fieldMapping); |
267
|
|
|
|
268
|
|
|
$this->assertSame('integer', $field->getMappingType()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testSetFieldMappingSetFieldName(): void |
272
|
|
|
{ |
273
|
|
|
$fieldMapping = [ |
274
|
|
|
'type' => 'integer', |
275
|
|
|
'fieldName' => 'position', |
276
|
|
|
]; |
277
|
|
|
|
278
|
|
|
$field = new FieldDescription(); |
279
|
|
|
$field->setFieldMapping($fieldMapping); |
280
|
|
|
|
281
|
|
|
$this->assertSame('position', $field->getFieldName()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* NEXT_MAJOR: Remove this test. |
286
|
|
|
* |
287
|
|
|
* @group legacy |
288
|
|
|
*/ |
289
|
|
|
public function testGetTargetEntity(): void |
290
|
|
|
{ |
291
|
|
|
$assocationMapping = [ |
292
|
|
|
'type' => 'integer', |
293
|
|
|
'fieldName' => 'position', |
294
|
|
|
'targetDocument' => 'someValue', |
295
|
|
|
]; |
296
|
|
|
|
297
|
|
|
$field = new FieldDescription(); |
298
|
|
|
|
299
|
|
|
$this->assertNull($field->getTargetEntity()); |
|
|
|
|
300
|
|
|
|
301
|
|
|
$field->setAssociationMapping($assocationMapping); |
302
|
|
|
|
303
|
|
|
$this->assertSame('someValue', $field->getTargetEntity()); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function testGetTargetModel(): void |
307
|
|
|
{ |
308
|
|
|
$assocationMapping = [ |
309
|
|
|
'type' => 'integer', |
310
|
|
|
'fieldName' => 'position', |
311
|
|
|
'targetDocument' => 'someValue', |
312
|
|
|
]; |
313
|
|
|
|
314
|
|
|
$field = new FieldDescription(); |
315
|
|
|
|
316
|
|
|
$this->assertNull($field->getTargetModel()); |
317
|
|
|
|
318
|
|
|
$field->setAssociationMapping($assocationMapping); |
319
|
|
|
|
320
|
|
|
$this->assertSame('someValue', $field->getTargetModel()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testIsIdentifierFromFieldMapping(): void |
324
|
|
|
{ |
325
|
|
|
$fieldMapping = [ |
326
|
|
|
'type' => 'integer', |
327
|
|
|
'fieldName' => 'position', |
328
|
|
|
'id' => 'someId', |
329
|
|
|
]; |
330
|
|
|
|
331
|
|
|
$field = new FieldDescription(); |
332
|
|
|
$field->setFieldMapping($fieldMapping); |
333
|
|
|
|
334
|
|
|
$this->assertSame('someId', $field->isIdentifier()); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testGetFieldMapping(): void |
338
|
|
|
{ |
339
|
|
|
$fieldMapping = [ |
340
|
|
|
'type' => 'integer', |
341
|
|
|
'fieldName' => 'position', |
342
|
|
|
'id' => 'someId', |
343
|
|
|
]; |
344
|
|
|
|
345
|
|
|
$field = new FieldDescription(); |
346
|
|
|
$field->setFieldMapping($fieldMapping); |
347
|
|
|
|
348
|
|
|
$this->assertSame($fieldMapping, $field->getFieldMapping()); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|