|
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\DoctrineORMAdminBundle\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\DoctrineORMAdminBundle\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_existent', ['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_existent' => [ |
|
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('integer', $field->getMappingType()); |
|
98
|
|
|
$this->assertSame('position', $field->getFieldName()); |
|
99
|
|
|
|
|
100
|
|
|
// cannot overwrite defined definition |
|
101
|
|
|
$field->setAssociationMapping([ |
|
102
|
|
|
'type' => 'overwrite?', |
|
103
|
|
|
'fieldName' => 'overwritten', |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertSame('integer', $field->getType()); |
|
107
|
|
|
$this->assertSame('integer', $field->getMappingType()); |
|
108
|
|
|
$this->assertSame('overwritten', $field->getFieldName()); |
|
109
|
|
|
|
|
110
|
|
|
$field->setMappingType('string'); |
|
111
|
|
|
$this->assertSame('string', $field->getMappingType()); |
|
112
|
|
|
$this->assertSame('integer', $field->getType()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testSetName(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$field = new FieldDescription(); |
|
118
|
|
|
$field->setName('New field description name'); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame($field->getName(), 'New field description name'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testSetNameSetFieldNameToo(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$field = new FieldDescription(); |
|
126
|
|
|
$field->setName('New field description name'); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertSame($field->getFieldName(), 'New field description name'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void |
|
132
|
|
|
{ |
|
133
|
|
|
$field = new FieldDescription(); |
|
134
|
|
|
$field->setFieldName('field name'); |
|
135
|
|
|
$field->setName('New field description name'); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSame($field->getFieldName(), 'field name'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testGetParent(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$adminMock = $this->createMock(AdminInterface::class); |
|
143
|
|
|
$field = new FieldDescription(); |
|
144
|
|
|
$field->setParent($adminMock); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertSame($adminMock, $field->getParent()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testGetAdmin(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$adminMock = $this->createMock(AdminInterface::class); |
|
152
|
|
|
$field = new FieldDescription(); |
|
153
|
|
|
$field->setAdmin($adminMock); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertSame($adminMock, $field->getAdmin()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function testGetAssociationAdmin(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
|
161
|
|
|
$adminMock->expects($this->once()) |
|
162
|
|
|
->method('setParentFieldDescription') |
|
163
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
|
164
|
|
|
|
|
165
|
|
|
$field = new FieldDescription(); |
|
166
|
|
|
$field->setAssociationAdmin($adminMock); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertSame($adminMock, $field->getAssociationAdmin()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function testHasAssociationAdmin(): void |
|
172
|
|
|
{ |
|
173
|
|
|
$adminMock = $this->createMock(AbstractAdmin::class); |
|
174
|
|
|
$adminMock->expects($this->once()) |
|
175
|
|
|
->method('setParentFieldDescription') |
|
176
|
|
|
->with($this->isInstanceOf(FieldDescriptionInterface::class)); |
|
177
|
|
|
|
|
178
|
|
|
$field = new FieldDescription(); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertFalse($field->hasAssociationAdmin()); |
|
181
|
|
|
|
|
182
|
|
|
$field->setAssociationAdmin($adminMock); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertTrue($field->hasAssociationAdmin()); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testGetValue(): void |
|
188
|
|
|
{ |
|
189
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
|
190
|
|
|
->setMethods(['myMethod']) |
|
191
|
|
|
->getMock(); |
|
192
|
|
|
$mockedObject->expects($this->once()) |
|
193
|
|
|
->method('myMethod') |
|
194
|
|
|
->willReturn('myMethodValue'); |
|
195
|
|
|
|
|
196
|
|
|
$field = new FieldDescription(); |
|
197
|
|
|
$field->setFieldName('any string, but not null'); |
|
198
|
|
|
$field->setOption('code', 'myMethod'); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertSame($field->getValue($mockedObject), 'myMethodValue'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testGetValueWhenCannotRetrieve(): void |
|
204
|
|
|
{ |
|
205
|
|
|
$this->expectException(NoValueException::class); |
|
206
|
|
|
|
|
207
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
|
208
|
|
|
->setMethods(['myMethod']) |
|
209
|
|
|
->getMock(); |
|
210
|
|
|
$mockedObject->expects($this->never()) |
|
211
|
|
|
->method('myMethod') |
|
212
|
|
|
->willReturn('myMethodValue'); |
|
213
|
|
|
|
|
214
|
|
|
$field = new FieldDescription(); |
|
215
|
|
|
$field->setFieldName('any string, but not null'); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertSame($field->getValue($mockedObject), 'myMethodValue'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function testGetAssociationMapping(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$assocationMapping = [ |
|
223
|
|
|
'type' => 'integer', |
|
224
|
|
|
'fieldName' => 'position', |
|
225
|
|
|
]; |
|
226
|
|
|
|
|
227
|
|
|
$field = new FieldDescription(); |
|
228
|
|
|
$field->setAssociationMapping($assocationMapping); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertSame($assocationMapping, $field->getAssociationMapping()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function testSetFieldMappingSetType(): void |
|
234
|
|
|
{ |
|
235
|
|
|
$fieldMapping = [ |
|
236
|
|
|
'type' => 'integer', |
|
237
|
|
|
'fieldName' => 'position', |
|
238
|
|
|
]; |
|
239
|
|
|
|
|
240
|
|
|
$field = new FieldDescription(); |
|
241
|
|
|
$field->setFieldMapping($fieldMapping); |
|
242
|
|
|
|
|
243
|
|
|
$this->assertSame('integer', $field->getType()); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testSetFieldMappingSetMappingType(): void |
|
247
|
|
|
{ |
|
248
|
|
|
$fieldMapping = [ |
|
249
|
|
|
'type' => 'integer', |
|
250
|
|
|
'fieldName' => 'position', |
|
251
|
|
|
]; |
|
252
|
|
|
|
|
253
|
|
|
$field = new FieldDescription(); |
|
254
|
|
|
$field->setFieldMapping($fieldMapping); |
|
255
|
|
|
|
|
256
|
|
|
$this->assertSame('integer', $field->getMappingType()); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function testSetFieldMappingSetFieldName(): void |
|
260
|
|
|
{ |
|
261
|
|
|
$fieldMapping = [ |
|
262
|
|
|
'type' => 'integer', |
|
263
|
|
|
'fieldName' => 'position', |
|
264
|
|
|
]; |
|
265
|
|
|
|
|
266
|
|
|
$field = new FieldDescription(); |
|
267
|
|
|
$field->setFieldMapping($fieldMapping); |
|
268
|
|
|
|
|
269
|
|
|
$this->assertSame('position', $field->getFieldName()); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function testGetTargetEntity(): void |
|
273
|
|
|
{ |
|
274
|
|
|
$assocationMapping = [ |
|
275
|
|
|
'type' => 'integer', |
|
276
|
|
|
'fieldName' => 'position', |
|
277
|
|
|
'targetEntity' => 'someValue', |
|
278
|
|
|
]; |
|
279
|
|
|
|
|
280
|
|
|
$field = new FieldDescription(); |
|
281
|
|
|
|
|
282
|
|
|
$this->assertNull($field->getTargetModel()); |
|
283
|
|
|
|
|
284
|
|
|
$field->setAssociationMapping($assocationMapping); |
|
285
|
|
|
|
|
286
|
|
|
$this->assertSame('someValue', $field->getTargetModel()); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function testIsIdentifierFromFieldMapping(): void |
|
290
|
|
|
{ |
|
291
|
|
|
$fieldMapping = [ |
|
292
|
|
|
'type' => 'integer', |
|
293
|
|
|
'fieldName' => 'position', |
|
294
|
|
|
'id' => true, |
|
295
|
|
|
]; |
|
296
|
|
|
|
|
297
|
|
|
$field = new FieldDescription(); |
|
298
|
|
|
$field->setFieldMapping($fieldMapping); |
|
299
|
|
|
|
|
300
|
|
|
$this->assertTrue($field->isIdentifier()); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function testGetFieldMapping(): void |
|
304
|
|
|
{ |
|
305
|
|
|
$fieldMapping = [ |
|
306
|
|
|
'type' => 'integer', |
|
307
|
|
|
'fieldName' => 'position', |
|
308
|
|
|
'id' => 'someId', |
|
309
|
|
|
]; |
|
310
|
|
|
|
|
311
|
|
|
$field = new FieldDescription(); |
|
312
|
|
|
$field->setFieldMapping($fieldMapping); |
|
313
|
|
|
|
|
314
|
|
|
$this->assertSame($fieldMapping, $field->getFieldMapping()); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function testGetValueForEmbeddedObject(): void |
|
318
|
|
|
{ |
|
319
|
|
|
$mockedEmbeddedObject = $this->getMockBuilder('stdClass') |
|
320
|
|
|
->setMethods(['myMethod']) |
|
321
|
|
|
->getMock(); |
|
322
|
|
|
$mockedEmbeddedObject->expects($this->once()) |
|
323
|
|
|
->method('myMethod') |
|
324
|
|
|
->willReturn('myMethodValue'); |
|
325
|
|
|
|
|
326
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
|
327
|
|
|
->setMethods(['getMyEmbeddedObject']) |
|
328
|
|
|
->getMock(); |
|
329
|
|
|
$mockedObject->expects($this->once()) |
|
330
|
|
|
->method('getMyEmbeddedObject') |
|
331
|
|
|
->willReturn($mockedEmbeddedObject); |
|
332
|
|
|
|
|
333
|
|
|
$field = new FieldDescription(); |
|
334
|
|
|
$field->setFieldMapping([ |
|
335
|
|
|
'declaredField' => 'myEmbeddedObject', 'type' => 'string', 'fieldName' => 'myEmbeddedObject.myMethod', |
|
336
|
|
|
]); |
|
337
|
|
|
$field->setFieldName('myMethod'); |
|
338
|
|
|
$field->setOption('code', 'myMethod'); |
|
339
|
|
|
|
|
340
|
|
|
$this->assertSame('myMethodValue', $field->getValue($mockedObject)); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
public function testGetValueForMultiLevelEmbeddedObject(): void |
|
344
|
|
|
{ |
|
345
|
|
|
$mockedChildEmbeddedObject = $this->getMockBuilder('stdClass') |
|
346
|
|
|
->setMethods(['myMethod']) |
|
347
|
|
|
->getMock(); |
|
348
|
|
|
$mockedChildEmbeddedObject->expects($this->once()) |
|
349
|
|
|
->method('myMethod') |
|
350
|
|
|
->willReturn('myMethodValue'); |
|
351
|
|
|
$mockedEmbeddedObject = $this->getMockBuilder('stdClass') |
|
352
|
|
|
->setMethods(['getChild']) |
|
353
|
|
|
->getMock(); |
|
354
|
|
|
$mockedEmbeddedObject->expects($this->once()) |
|
355
|
|
|
->method('getChild') |
|
356
|
|
|
->willReturn($mockedChildEmbeddedObject); |
|
357
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
|
358
|
|
|
->setMethods(['getMyEmbeddedObject']) |
|
359
|
|
|
->getMock(); |
|
360
|
|
|
$mockedObject->expects($this->once()) |
|
361
|
|
|
->method('getMyEmbeddedObject') |
|
362
|
|
|
->willReturn($mockedEmbeddedObject); |
|
363
|
|
|
$field = new FieldDescription(); |
|
364
|
|
|
$field->setFieldMapping([ |
|
365
|
|
|
'declaredField' => 'myEmbeddedObject.child', 'type' => 'string', 'fieldName' => 'myMethod', |
|
366
|
|
|
]); |
|
367
|
|
|
$field->setFieldName('myMethod'); |
|
368
|
|
|
$field->setOption('code', 'myMethod'); |
|
369
|
|
|
$this->assertSame('myMethodValue', $field->getValue($mockedObject)); |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
|