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\Form\Type; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Prophecy\Argument\Token\AnyValueToken; |
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
20
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
21
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
22
|
|
|
use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension; |
23
|
|
|
use Sonata\AdminBundle\Form\Type\AdminType; |
24
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
25
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo; |
26
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\TestExtension; |
27
|
|
|
use Symfony\Component\Form\FormTypeGuesserInterface; |
28
|
|
|
use Symfony\Component\Form\Test\TypeTestCase; |
29
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
30
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
31
|
|
|
|
32
|
|
|
class AdminTypeTest extends TypeTestCase |
33
|
|
|
{ |
34
|
|
|
public function testGetDefaultOptions(): void |
35
|
|
|
{ |
36
|
|
|
$type = new AdminType(); |
37
|
|
|
|
38
|
|
|
$optionResolver = new OptionsResolver(); |
39
|
|
|
|
40
|
|
|
$type->configureOptions($optionResolver); |
41
|
|
|
|
42
|
|
|
$options = $optionResolver->resolve(); |
43
|
|
|
|
44
|
|
|
$this->assertTrue($options['delete']); |
45
|
|
|
$this->assertFalse($options['auto_initialize']); |
46
|
|
|
$this->assertSame('link_add', $options['btn_add']); |
47
|
|
|
$this->assertSame('link_list', $options['btn_list']); |
48
|
|
|
$this->assertSame('link_delete', $options['btn_delete']); |
49
|
|
|
$this->assertSame('SonataAdminBundle', $options['btn_catalogue']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testSubmitValidData(): void |
53
|
|
|
{ |
54
|
|
|
$parentAdmin = $this->prophesize(AdminInterface::class); |
55
|
|
|
$parentAdmin->hasSubject()->shouldBeCalled()->willReturn(false); |
56
|
|
|
$parentField = $this->prophesize(FieldDescriptionInterface::class); |
57
|
|
|
$parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled(); |
58
|
|
|
$parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal()); |
59
|
|
|
|
60
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
61
|
|
|
|
62
|
|
|
$foo = new Foo(); |
63
|
|
|
|
64
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
65
|
|
|
$admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true); |
66
|
|
|
$admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal()); |
67
|
|
|
$admin->hasAccess('delete')->shouldBeCalled()->willReturn(false); |
68
|
|
|
$admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled(); |
69
|
|
|
$admin->getModelManager()->shouldBeCalled()->willReturn($modelManager); |
70
|
|
|
$admin->getClass()->shouldBeCalled()->willReturn(Foo::class); |
71
|
|
|
$admin->getNewInstance()->shouldBeCalled()->willReturn($foo); |
72
|
|
|
$admin->setSubject($foo)->shouldBeCalled(); |
73
|
|
|
|
74
|
|
|
$field = $this->prophesize(FieldDescriptionInterface::class); |
75
|
|
|
$field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal()); |
76
|
|
|
$field->getAdmin()->shouldBeCalled(); |
77
|
|
|
$field->getName()->shouldBeCalled(); |
78
|
|
|
$field->getOption('edit', 'standard')->shouldBeCalled(); |
79
|
|
|
$field->getOption('inline', 'natural')->shouldBeCalled(); |
80
|
|
|
$field->getOption('block_name', false)->shouldBeCalled(); |
81
|
|
|
$formData = []; |
82
|
|
|
|
83
|
|
|
$form = $this->factory->create( |
84
|
|
|
AdminType::class, |
85
|
|
|
null, |
86
|
|
|
[ |
87
|
|
|
'sonata_field_description' => $field->reveal(), |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
$form->submit($formData); |
91
|
|
|
$this->assertTrue($form->isSynchronized()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testDotFields(): void |
95
|
|
|
{ |
96
|
|
|
$foo = new \stdClass(); |
97
|
|
|
$foo->bar = 1; |
98
|
|
|
|
99
|
|
|
$parentSubject = new \stdClass(); |
100
|
|
|
$parentSubject->foo = $foo; |
101
|
|
|
|
102
|
|
|
$parentAdmin = $this->prophesize(AdminInterface::class); |
103
|
|
|
$parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject); |
104
|
|
|
$parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true); |
105
|
|
|
$parentField = $this->prophesize(FieldDescriptionInterface::class); |
106
|
|
|
$parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled(); |
107
|
|
|
$parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal()); |
108
|
|
|
|
109
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
110
|
|
|
|
111
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
112
|
|
|
$admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true); |
113
|
|
|
$admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal()); |
114
|
|
|
$admin->setSubject(1)->shouldBeCalled(); |
115
|
|
|
$admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled(); |
116
|
|
|
$admin->getModelManager()->shouldBeCalled()->willReturn($modelManager); |
117
|
|
|
$admin->getClass()->shouldBeCalled()->willReturn(Foo::class); |
118
|
|
|
|
119
|
|
|
$field = $this->prophesize(FieldDescriptionInterface::class); |
120
|
|
|
$field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal()); |
121
|
|
|
$field->getFieldName()->shouldBeCalled()->willReturn('bar'); |
122
|
|
|
$field->getParentAssociationMappings()->shouldBeCalled()->willReturn([['fieldName' => 'foo']]); |
123
|
|
|
|
124
|
|
|
$this->builder->add('foo.bar'); |
125
|
|
|
|
126
|
|
|
$type = new AdminType(); |
127
|
|
|
try { |
128
|
|
|
$type->buildForm($this->builder, [ |
129
|
|
|
'sonata_field_description' => $field->reveal(), |
130
|
|
|
'delete' => false, // not needed |
131
|
|
|
'property_path' => 'bar', // actual test case |
132
|
|
|
]); |
133
|
|
|
} catch (NoSuchPropertyException $exception) { |
134
|
|
|
$this->fail($exception->getMessage()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testArrayCollection(): void |
139
|
|
|
{ |
140
|
|
|
$foo = new Foo(); |
141
|
|
|
|
142
|
|
|
$parentSubject = new \stdClass(); |
143
|
|
|
$parentSubject->foo = new ArrayCollection([$foo]); |
144
|
|
|
|
145
|
|
|
$parentAdmin = $this->prophesize(AdminInterface::class); |
146
|
|
|
$parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject); |
147
|
|
|
$parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true); |
148
|
|
|
$parentField = $this->prophesize(FieldDescriptionInterface::class); |
149
|
|
|
$parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled(); |
150
|
|
|
$parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal()); |
151
|
|
|
|
152
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
153
|
|
|
|
154
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
155
|
|
|
$admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true); |
156
|
|
|
$admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal()); |
157
|
|
|
$admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled(); |
158
|
|
|
$admin->getModelManager()->shouldBeCalled()->willReturn($modelManager); |
159
|
|
|
$admin->getClass()->shouldBeCalled()->willReturn(Foo::class); |
160
|
|
|
$admin->setSubject($foo)->shouldBeCalled(); |
161
|
|
|
|
162
|
|
|
$field = $this->prophesize(FieldDescriptionInterface::class); |
163
|
|
|
$field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal()); |
164
|
|
|
$field->getFieldName()->shouldBeCalled()->willReturn('foo'); |
165
|
|
|
$field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]); |
166
|
|
|
|
167
|
|
|
$this->builder->add('foo'); |
168
|
|
|
|
169
|
|
|
$type = new AdminType(); |
170
|
|
|
try { |
171
|
|
|
$type->buildForm($this->builder, [ |
172
|
|
|
'sonata_field_description' => $field->reveal(), |
173
|
|
|
'delete' => false, // not needed |
174
|
|
|
'property_path' => '[0]', // actual test case |
175
|
|
|
]); |
176
|
|
|
} catch (NoSuchPropertyException $exception) { |
177
|
|
|
$this->fail($exception->getMessage()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testArrayCollectionNotFound(): void |
182
|
|
|
{ |
183
|
|
|
$parentSubject = new \stdClass(); |
184
|
|
|
$parentSubject->foo = new ArrayCollection(); |
185
|
|
|
|
186
|
|
|
$parentAdmin = $this->prophesize(AdminInterface::class); |
187
|
|
|
$parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject); |
188
|
|
|
$parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true); |
189
|
|
|
$parentField = $this->prophesize(FieldDescriptionInterface::class); |
190
|
|
|
$parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled(); |
191
|
|
|
$parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal()); |
192
|
|
|
|
193
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
194
|
|
|
|
195
|
|
|
$foo = new Foo(); |
196
|
|
|
|
197
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
198
|
|
|
$admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true); |
199
|
|
|
$admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal()); |
200
|
|
|
$admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled(); |
201
|
|
|
$admin->getModelManager()->shouldBeCalled()->willReturn($modelManager); |
202
|
|
|
$admin->getClass()->shouldBeCalled()->willReturn(Foo::class); |
203
|
|
|
$admin->getNewInstance()->shouldBeCalled()->willReturn($foo); |
204
|
|
|
$admin->setSubject($foo)->shouldBeCalled(); |
205
|
|
|
|
206
|
|
|
$field = $this->prophesize(FieldDescriptionInterface::class); |
207
|
|
|
$field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal()); |
208
|
|
|
$field->getFieldName()->shouldBeCalled()->willReturn('foo'); |
209
|
|
|
$field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]); |
210
|
|
|
|
211
|
|
|
$this->builder->add('foo'); |
212
|
|
|
|
213
|
|
|
$type = new AdminType(); |
214
|
|
|
try { |
215
|
|
|
$type->buildForm($this->builder, [ |
216
|
|
|
'sonata_field_description' => $field->reveal(), |
217
|
|
|
'delete' => false, // not needed |
218
|
|
|
'property_path' => '[0]', // actual test case |
219
|
|
|
]); |
220
|
|
|
} catch (NoSuchPropertyException $exception) { |
221
|
|
|
$this->fail($exception->getMessage()); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
protected function getExtensions() |
226
|
|
|
{ |
227
|
|
|
$extensions = parent::getExtensions(); |
228
|
|
|
$guesser = $this->prophesize(FormTypeGuesserInterface::class)->reveal(); |
229
|
|
|
$extension = new TestExtension($guesser); |
230
|
|
|
|
231
|
|
|
$extension->addTypeExtension(new FormTypeFieldExtension([], [])); |
232
|
|
|
$extensions[] = $extension; |
233
|
|
|
|
234
|
|
|
return $extensions; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|