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\Admin; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\Collection; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminHelper; |
19
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
21
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
22
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
27
|
|
|
use Symfony\Component\Form\FormBuilder; |
28
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
29
|
|
|
use Symfony\Component\Form\FormView; |
30
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
31
|
|
|
use Symfony\Component\HttpFoundation\Request; |
32
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorBuilder; |
33
|
|
|
|
34
|
|
|
class AdminHelperTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var AdminHelper |
38
|
|
|
*/ |
39
|
|
|
protected $helper; |
40
|
|
|
|
41
|
|
|
public function setUp(): void |
42
|
|
|
{ |
43
|
|
|
$container = $this->createMock(ContainerInterface::class); |
44
|
|
|
|
45
|
|
|
$pool = new Pool($container, 'title', 'logo.png'); |
|
|
|
|
46
|
|
|
$this->helper = new AdminHelper($pool); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGetChildFormBuilder(): void |
50
|
|
|
{ |
51
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
52
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
53
|
|
|
|
54
|
|
|
$formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory); |
|
|
|
|
57
|
|
|
$formBuilder->add($childFormBuilder); |
58
|
|
|
|
59
|
|
|
$this->assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo')); |
60
|
|
|
$this->isInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'test_elementId')); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetChildFormView(): void |
64
|
|
|
{ |
65
|
|
|
$formView = new FormView(); |
66
|
|
|
$formView->vars['id'] = 'test'; |
67
|
|
|
$child = new FormView($formView); |
|
|
|
|
68
|
|
|
$child->vars['id'] = 'test_elementId'; |
69
|
|
|
|
70
|
|
|
$this->assertNull($this->helper->getChildFormView($formView, 'foo')); |
71
|
|
|
$this->isInstanceOf(FormView::class, $this->helper->getChildFormView($formView, 'test_elementId')); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testAddNewInstance(): void |
75
|
|
|
{ |
76
|
|
|
$admin = $this->createMock(AdminInterface::class); |
77
|
|
|
$admin->expects($this->once())->method('getNewInstance')->willReturn(new \stdClass()); |
78
|
|
|
|
79
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
80
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin); |
81
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'fooBar']); |
82
|
|
|
|
83
|
|
|
$object = $this->getMockBuilder('stdClass') |
84
|
|
|
->setMethods(['addFooBar']) |
85
|
|
|
->getMock(); |
86
|
|
|
$object->expects($this->once())->method('addFooBar'); |
87
|
|
|
|
88
|
|
|
$this->helper->addNewInstance($object, $fieldDescription); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testAddNewInstancePlural(): void |
92
|
|
|
{ |
93
|
|
|
$admin = $this->createMock(AdminInterface::class); |
94
|
|
|
$admin->expects($this->once())->method('getNewInstance')->willReturn(new \stdClass()); |
95
|
|
|
|
96
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
97
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin); |
98
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'fooBars']); |
99
|
|
|
|
100
|
|
|
$object = $this->getMockBuilder('stdClass') |
101
|
|
|
->setMethods(['addFooBar']) |
102
|
|
|
->getMock(); |
103
|
|
|
$object->expects($this->once())->method('addFooBar'); |
104
|
|
|
|
105
|
|
|
$this->helper->addNewInstance($object, $fieldDescription); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testAddNewInstanceInflector(): void |
109
|
|
|
{ |
110
|
|
|
$admin = $this->createMock(AdminInterface::class); |
111
|
|
|
$admin->expects($this->once())->method('getNewInstance')->willReturn(new \stdClass()); |
112
|
|
|
|
113
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
114
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin); |
115
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'entries']); |
116
|
|
|
|
117
|
|
|
$object = $this->getMockBuilder('stdClass') |
118
|
|
|
->setMethods(['addEntry']) |
119
|
|
|
->getMock(); |
120
|
|
|
$object->expects($this->once())->method('addEntry'); |
121
|
|
|
|
122
|
|
|
$this->helper->addNewInstance($object, $fieldDescription); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetElementAccessPath(): void |
126
|
|
|
{ |
127
|
|
|
$object = $this->getMockBuilder('stdClass') |
128
|
|
|
->setMethods(['getPathToObject']) |
129
|
|
|
->getMock(); |
130
|
|
|
$subObject = $this->getMockBuilder('stdClass') |
131
|
|
|
->setMethods(['getAnother']) |
132
|
|
|
->getMock(); |
133
|
|
|
$sub2Object = $this->getMockBuilder('stdClass') |
134
|
|
|
->setMethods(['getMoreThings']) |
135
|
|
|
->getMock(); |
136
|
|
|
|
137
|
|
|
$object->expects($this->atLeastOnce())->method('getPathToObject')->willReturn([$subObject]); |
138
|
|
|
$subObject->expects($this->atLeastOnce())->method('getAnother')->willReturn($sub2Object); |
139
|
|
|
$sub2Object->expects($this->atLeastOnce())->method('getMoreThings')->willReturn('Value'); |
140
|
|
|
|
141
|
|
|
$path = $this->helper->getElementAccessPath('uniquePartOfId_path_to_object_0_another_more_things', $object); |
142
|
|
|
|
143
|
|
|
$this->assertSame('path_to_object[0].another.more_things', $path); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testItThrowsExceptionWhenDoesNotFindTheFullPath(): void |
147
|
|
|
{ |
148
|
|
|
$path = 'uniquePartOfId_path_to_object_0_more_calls'; |
149
|
|
|
$object = $this->getMockBuilder('stdClass') |
150
|
|
|
->setMethods(['getPathToObject']) |
151
|
|
|
->getMock(); |
152
|
|
|
$subObject = $this->getMockBuilder('stdClass') |
153
|
|
|
->setMethods(['getMore']) |
154
|
|
|
->getMock(); |
155
|
|
|
|
156
|
|
|
$object->expects($this->atLeastOnce())->method('getPathToObject')->willReturn([$subObject]); |
157
|
|
|
$subObject->expects($this->atLeastOnce())->method('getMore')->willReturn('Value'); |
158
|
|
|
|
159
|
|
|
$this->expectException(\Exception::class, 'Could not get element id from '.$path.' Failing part: calls'); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->helper->getElementAccessPath($path, $object); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testAppendFormFieldElement(): void |
165
|
|
|
{ |
166
|
|
|
$container = $this->createMock(ContainerInterface::class); |
167
|
|
|
|
168
|
|
|
$propertyAccessorBuilder = new PropertyAccessorBuilder(); |
169
|
|
|
$propertyAccesor = $propertyAccessorBuilder->getPropertyAccessor(); |
170
|
|
|
$pool = new Pool($container, 'title', 'logo.png', [], $propertyAccesor); |
|
|
|
|
171
|
|
|
$helper = new AdminHelper($pool); |
172
|
|
|
|
173
|
|
|
$admin = $this->createMock(AdminInterface::class); |
174
|
|
|
$admin |
175
|
|
|
->expects($this->any()) |
176
|
|
|
->method('getClass') |
177
|
|
|
->willReturn(Foo::class); |
178
|
|
|
|
179
|
|
|
$associationMapping = [ |
180
|
|
|
'fieldName' => 'bar', |
181
|
|
|
'targetEntity' => Foo::class, |
182
|
|
|
'sourceEntity' => Foo::class, |
183
|
|
|
'isOwningSide' => false, |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
187
|
|
|
$fieldDescription->expects($this->any())->method('getAssociationAdmin')->willReturn($admin); |
188
|
|
|
$fieldDescription->expects($this->any())->method('getAssociationMapping')->willReturn($associationMapping); |
189
|
|
|
|
190
|
|
|
$admin |
191
|
|
|
->expects($this->any()) |
192
|
|
|
->method('getFormFieldDescription') |
193
|
|
|
->willReturn($fieldDescription); |
194
|
|
|
|
195
|
|
|
$admin |
196
|
|
|
->expects($this->any()) |
197
|
|
|
->method('getFormFieldDescriptions') |
198
|
|
|
->willReturn([ |
199
|
|
|
'bar' => $fieldDescription, |
200
|
|
|
]); |
201
|
|
|
|
202
|
|
|
$request = $this->createMock(Request::class); |
203
|
|
|
$request |
204
|
|
|
->expects($this->any()) |
205
|
|
|
->method('get') |
206
|
|
|
->willReturn([ |
207
|
|
|
'bar' => [ |
208
|
|
|
[ |
209
|
|
|
'baz' => [ |
210
|
|
|
'baz' => true, |
211
|
|
|
], |
212
|
|
|
], |
213
|
|
|
['_delete' => true], |
214
|
|
|
], |
215
|
|
|
]); |
216
|
|
|
|
217
|
|
|
$request->request = new ParameterBag(); |
|
|
|
|
218
|
|
|
|
219
|
|
|
$admin |
220
|
|
|
->expects($this->any()) |
221
|
|
|
->method('getRequest') |
222
|
|
|
->will($this->onConsecutiveCalls($request, $request, $request, null, $request, $request, $request, $request, null, $request)); |
223
|
|
|
|
224
|
|
|
$foo = $this->createMock(Foo::class); |
225
|
|
|
|
226
|
|
|
$collection = $this->createMock(Collection::class); |
227
|
|
|
$foo->setBar($collection); |
228
|
|
|
|
229
|
|
|
$dataMapper = $this->createMock(DataMapperInterface::class); |
230
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
231
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
232
|
|
|
$formBuilder = new FormBuilder('test', \get_class($foo), $eventDispatcher, $formFactory); |
|
|
|
|
233
|
|
|
$childFormBuilder = new FormBuilder('bar', \stdClass::class, $eventDispatcher, $formFactory); |
|
|
|
|
234
|
|
|
$childFormBuilder->setCompound(true); |
235
|
|
|
$childFormBuilder->setDataMapper($dataMapper); |
|
|
|
|
236
|
|
|
$subChildFormBuilder = new FormBuilder('baz', \stdClass::class, $eventDispatcher, $formFactory); |
|
|
|
|
237
|
|
|
$subChildFormBuilder->setCompound(true); |
238
|
|
|
$subChildFormBuilder->setDataMapper($dataMapper); |
|
|
|
|
239
|
|
|
$childFormBuilder->add($subChildFormBuilder); |
240
|
|
|
|
241
|
|
|
$formBuilder->setCompound(true); |
242
|
|
|
$formBuilder->setDataMapper($dataMapper); |
|
|
|
|
243
|
|
|
$formBuilder->add($childFormBuilder); |
244
|
|
|
|
245
|
|
|
$admin->expects($this->any())->method('getFormBuilder')->willReturn($formBuilder); |
246
|
|
|
$admin->expects($this->any())->method('getSubject')->willReturn($foo); |
247
|
|
|
|
248
|
|
|
$finalForm = $helper->appendFormFieldElement($admin, $foo, 'test_bar')[1]; |
|
|
|
|
249
|
|
|
|
250
|
|
|
foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
251
|
|
|
$this->assertFalse($childField->has('_delete')); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$deleteFormBuilder = new FormBuilder('_delete', null, $eventDispatcher, $formFactory); |
|
|
|
|
255
|
|
|
$subChildFormBuilder->add($deleteFormBuilder, CheckboxType::class, ['delete' => false]); |
256
|
|
|
|
257
|
|
|
$finalForm = $helper->appendFormFieldElement($admin, $foo, 'test_bar')[1]; |
|
|
|
|
258
|
|
|
|
259
|
|
|
foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
260
|
|
|
$this->assertTrue($childField->has('_delete')); |
261
|
|
|
$this->assertSame('', $childField->get('_delete')->getData()); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testAppendFormFieldElementNested(): void |
266
|
|
|
{ |
267
|
|
|
$admin = $this->createMock(AdminInterface::class); |
268
|
|
|
$object = $this->getMockBuilder('stdClass') |
269
|
|
|
->setMethods(['getSubObject']) |
270
|
|
|
->getMock(); |
271
|
|
|
$simpleObject = $this->getMockBuilder('stdClass') |
272
|
|
|
->setMethods(['getSubObject']) |
273
|
|
|
->getMock(); |
274
|
|
|
$subObject = $this->getMockBuilder('stdClass') |
275
|
|
|
->setMethods(['getAnd']) |
276
|
|
|
->getMock(); |
277
|
|
|
$sub2Object = $this->getMockBuilder('stdClass') |
278
|
|
|
->setMethods(['getMore']) |
279
|
|
|
->getMock(); |
280
|
|
|
$sub3Object = $this->getMockBuilder('stdClass') |
281
|
|
|
->setMethods(['getFinalData']) |
282
|
|
|
->getMock(); |
283
|
|
|
$dataMapper = $this->createMock(DataMapperInterface::class); |
284
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
285
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
286
|
|
|
$formBuilder = new FormBuilder('test', \get_class($simpleObject), $eventDispatcher, $formFactory); |
|
|
|
|
287
|
|
|
$childFormBuilder = new FormBuilder('subObject', \get_class($subObject), $eventDispatcher, $formFactory); |
|
|
|
|
288
|
|
|
|
289
|
|
|
$object->expects($this->atLeastOnce())->method('getSubObject')->willReturn([$subObject]); |
290
|
|
|
$subObject->expects($this->atLeastOnce())->method('getAnd')->willReturn($sub2Object); |
291
|
|
|
$sub2Object->expects($this->atLeastOnce())->method('getMore')->willReturn([$sub3Object]); |
292
|
|
|
$sub3Object->expects($this->atLeastOnce())->method('getFinalData')->willReturn('value'); |
293
|
|
|
|
294
|
|
|
$formBuilder->setCompound(true); |
295
|
|
|
$formBuilder->setDataMapper($dataMapper); |
|
|
|
|
296
|
|
|
$formBuilder->add($childFormBuilder); |
297
|
|
|
|
298
|
|
|
$admin->expects($this->once())->method('getFormBuilder')->willReturn($formBuilder); |
299
|
|
|
$admin->expects($this->once())->method('getSubject')->willReturn($object); |
300
|
|
|
|
301
|
|
|
$this->expectException(\Exception::class, 'unknown collection class'); |
|
|
|
|
302
|
|
|
|
303
|
|
|
$this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data'); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: