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