Completed
Push — master ( 5b57aa...78681d )
by Vincent
15s queued 11s
created

LegacyAdminTypeTest::testGetDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
/**
33
 * @group legacy
34
 */
35
class LegacyAdminTypeTest extends TypeTestCase
36
{
37
    /**
38
     * @var AdminType
39
     */
40
    private $adminType;
41
42
    protected function setUp(): void
43
    {
44
        $this->adminType = new AdminType();
45
46
        parent::setUp();
47
    }
48
49
    /**
50
     * @expectedDeprecation Calling Sonata\AdminBundle\Form\Type\AdminType::__construct without passing an Sonata\AdminBundle\Admin\AdminHelper as argument is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
51
     */
52
    public function testGetDefaultOptions(): void
53
    {
54
        $optionResolver = new OptionsResolver();
55
56
        $this->adminType->configureOptions($optionResolver);
57
58
        $options = $optionResolver->resolve();
59
60
        $this->assertTrue($options['delete']);
61
        $this->assertFalse($options['auto_initialize']);
62
        $this->assertSame('link_add', $options['btn_add']);
63
        $this->assertSame('link_list', $options['btn_list']);
64
        $this->assertSame('link_delete', $options['btn_delete']);
65
        $this->assertSame('SonataAdminBundle', $options['btn_catalogue']);
66
    }
67
68
    /**
69
     * @expectedDeprecation Calling Sonata\AdminBundle\Form\Type\AdminType::__construct without passing an Sonata\AdminBundle\Admin\AdminHelper as argument is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
70
     */
71
    public function testSubmitValidData(): void
72
    {
73
        $parentAdmin = $this->prophesize(AdminInterface::class);
74
        $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(false);
75
        $parentField = $this->prophesize(FieldDescriptionInterface::class);
76
        $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
77
        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
78
79
        $modelManager = $this->prophesize(ModelManagerInterface::class);
80
81
        $foo = new Foo();
82
83
        $admin = $this->prophesize(AbstractAdmin::class);
84
        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
85
        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
86
        $admin->hasAccess('delete')->shouldBeCalled()->willReturn(false);
87
        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
88
        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
89
        $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
90
        $admin->getNewInstance()->shouldBeCalled()->willReturn($foo);
91
        $admin->setSubject($foo)->shouldBeCalled();
92
93
        $field = $this->prophesize(FieldDescriptionInterface::class);
94
        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
95
        $field->getAdmin()->shouldBeCalled();
96
        $field->getName()->shouldBeCalled();
97
        $field->getOption('edit', 'standard')->shouldBeCalled();
98
        $field->getOption('inline', 'natural')->shouldBeCalled();
99
        $field->getOption('block_name', false)->shouldBeCalled();
100
        $formData = [];
101
102
        $form = $this->factory->create(
103
            AdminType::class,
104
            null,
105
            [
106
                'sonata_field_description' => $field->reveal(),
107
            ]
108
        );
109
        $form->submit($formData);
110
        $this->assertTrue($form->isSynchronized());
111
    }
112
113
    /**
114
     * @expectedDeprecation Calling Sonata\AdminBundle\Form\Type\AdminType::__construct without passing an Sonata\AdminBundle\Admin\AdminHelper as argument is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
115
     */
116
    public function testDotFields(): void
117
    {
118
        $foo = new \stdClass();
119
        $foo->bar = 1;
120
121
        $parentSubject = new \stdClass();
122
        $parentSubject->foo = $foo;
123
124
        $parentAdmin = $this->prophesize(AdminInterface::class);
125
        $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
126
        $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
127
        $parentField = $this->prophesize(FieldDescriptionInterface::class);
128
        $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
129
        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
130
131
        $modelManager = $this->prophesize(ModelManagerInterface::class);
132
133
        $admin = $this->prophesize(AbstractAdmin::class);
134
        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
135
        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
136
        $admin->setSubject(1)->shouldBeCalled();
137
        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
138
        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
139
        $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
140
141
        $field = $this->prophesize(FieldDescriptionInterface::class);
142
        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
143
        $field->getFieldName()->shouldBeCalled()->willReturn('bar');
144
        $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([['fieldName' => 'foo']]);
145
146
        $this->builder->add('foo.bar');
147
148
        try {
149
            $this->adminType->buildForm($this->builder, [
150
                'sonata_field_description' => $field->reveal(),
151
                'delete' => false, // not needed
152
                'property_path' => 'bar', // actual test case
153
            ]);
154
        } catch (NoSuchPropertyException $exception) {
155
            $this->fail($exception->getMessage());
156
        }
157
    }
158
159
    /**
160
     * @expectedDeprecation Calling Sonata\AdminBundle\Form\Type\AdminType::__construct without passing an Sonata\AdminBundle\Admin\AdminHelper as argument is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
161
     */
162
    public function testArrayCollection(): void
163
    {
164
        $foo = new Foo();
165
166
        $parentSubject = new \stdClass();
167
        $parentSubject->foo = new ArrayCollection([$foo]);
168
169
        $parentAdmin = $this->prophesize(AdminInterface::class);
170
        $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
171
        $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
172
        $parentField = $this->prophesize(FieldDescriptionInterface::class);
173
        $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
174
        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
175
176
        $modelManager = $this->prophesize(ModelManagerInterface::class);
177
178
        $admin = $this->prophesize(AbstractAdmin::class);
179
        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
180
        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
181
        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
182
        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
183
        $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
184
        $admin->setSubject($foo)->shouldBeCalled();
185
186
        $field = $this->prophesize(FieldDescriptionInterface::class);
187
        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
188
        $field->getFieldName()->shouldBeCalled()->willReturn('foo');
189
        $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]);
190
191
        $this->builder->add('foo');
192
193
        try {
194
            $this->adminType->buildForm($this->builder, [
195
                'sonata_field_description' => $field->reveal(),
196
                'delete' => false, // not needed
197
                'property_path' => '[0]', // actual test case
198
            ]);
199
        } catch (NoSuchPropertyException $exception) {
200
            $this->fail($exception->getMessage());
201
        }
202
    }
203
204
    /**
205
     * @expectedDeprecation Calling Sonata\AdminBundle\Form\Type\AdminType::__construct without passing an Sonata\AdminBundle\Admin\AdminHelper as argument is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
206
     */
207
    public function testArrayCollectionNotFound(): void
208
    {
209
        $parentSubject = new \stdClass();
210
        $parentSubject->foo = new ArrayCollection();
211
212
        $parentAdmin = $this->prophesize(AdminInterface::class);
213
        $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
214
        $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
215
        $parentField = $this->prophesize(FieldDescriptionInterface::class);
216
        $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
217
        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
218
219
        $modelManager = $this->prophesize(ModelManagerInterface::class);
220
221
        $foo = new Foo();
222
223
        $admin = $this->prophesize(AbstractAdmin::class);
224
        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
225
        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
226
        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
227
        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
228
        $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
229
        $admin->getNewInstance()->shouldBeCalled()->willReturn($foo);
230
        $admin->setSubject($foo)->shouldBeCalled();
231
232
        $field = $this->prophesize(FieldDescriptionInterface::class);
233
        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
234
        $field->getFieldName()->shouldBeCalled()->willReturn('foo');
235
        $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]);
236
237
        $this->builder->add('foo');
238
239
        try {
240
            $this->adminType->buildForm($this->builder, [
241
                'sonata_field_description' => $field->reveal(),
242
                'delete' => false, // not needed
243
                'property_path' => '[0]', // actual test case
244
            ]);
245
        } catch (NoSuchPropertyException $exception) {
246
            $this->fail($exception->getMessage());
247
        }
248
    }
249
250
    protected function getExtensions()
251
    {
252
        $extensions = parent::getExtensions();
253
254
        $guesser = $this->prophesize(FormTypeGuesserInterface::class)->reveal();
255
        $extension = new TestExtension($guesser);
256
257
        $extension->addTypeExtension(new FormTypeFieldExtension([], []));
258
        $extensions[] = $extension;
259
260
        return $extensions;
261
    }
262
}
263