Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

Field/Type/FormTypeFieldExtensionTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Extension\Field\Type;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\Form\Extension\Core\Type\FormType;
21
use Symfony\Component\Form\Form;
22
use Symfony\Component\Form\FormConfigBuilder;
23
use Symfony\Component\Form\FormView;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
class FormTypeFieldExtensionTest extends TestCase
27
{
28
    public function testExtendedType(): void
29
    {
30
        $extension = new FormTypeFieldExtension([], []);
31
32
        $this->assertSame(FormType::class, $extension->getExtendedType());
33
        $this->assertSame([FormType::class], FormTypeFieldExtension::getExtendedTypes());
34
    }
35
36
    public function testDefaultOptions(): void
37
    {
38
        $extension = new FormTypeFieldExtension([], []);
39
40
        $resolver = new OptionsResolver();
41
        $extension->configureOptions($resolver);
42
43
        $options = $resolver->resolve();
44
45
        $this->assertArrayHasKey('sonata_admin', $options);
46
        $this->assertArrayHasKey('sonata_field_description', $options);
47
        $this->assertArrayHasKey('sonata_help', $options);
48
49
        $this->assertNull($options['sonata_admin']);
50
        $this->assertNull($options['sonata_field_description']);
51
        $this->assertNull($options['sonata_help']);
52
    }
53
54
    public function testbuildViewWithNoSonataAdminArray(): void
55
    {
56
        $eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);
57
58
        $parentFormView = new FormView();
59
        $parentFormView->vars['sonata_admin_enabled'] = false;
60
61
        $formView = new FormView();
62
        $formView->parent = $parentFormView;
63
64
        $options = [];
65
        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        $form = new Form($config);
67
68
        $extension = new FormTypeFieldExtension([], []);
69
        $extension->buildView($formView, $form, []);
70
71
        $this->assertArrayHasKey('sonata_admin', $formView->vars);
72
        $this->assertNull($formView->vars['sonata_admin']);
73
    }
74
75
    //    public function testBuildForm()
76
    //    {
77
    //        $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
78
    //        $admin->expects($this->once())->method('getCode')->will($this->returnValue('admin_code'));
79
    //
80
    //        $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
81
    //        $fieldDescription->expects($this->once())->method('getAdmin')->will($this->returnValue($admin));
82
    //        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('name'));
83
    //
84
    //        $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
85
    //
86
    //        $extension = new FormTypeFieldExtension();
87
    //        $extension->buildForm($formBuilder, []);
88
    //    }
89
90
    public function testbuildViewWithWithSonataAdmin(): void
91
    {
92
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
93
        $admin->expects($this->exactly(2))->method('getCode')->will($this->returnValue('my.admin.reference'));
94
95
        $eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);
96
97
        $formView = new FormView();
98
        $options = [];
99
        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
        $config->setAttribute('sonata_admin', [
101
            'admin' => $admin,
102
            'name' => 'name',
103
        ]);
104
        $config->setAttribute('sonata_admin_enabled', true);
105
106
        $form = new Form($config);
107
108
        $formView->parent = new FormView();
109
        $formView->parent->vars['sonata_admin_enabled'] = false;
110
111
        $formView->vars['block_prefixes'] = ['form', 'field', 'text', '_s50b26aa76cb96_username'];
112
113
        $extension = new FormTypeFieldExtension([], []);
114
        $extension->buildView($formView, $form, [
115
            'sonata_help' => 'help text',
116
        ]);
117
118
        $this->assertArrayHasKey('block_prefixes', $formView->vars);
119
        $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
120
        $this->assertArrayHasKey('sonata_admin', $formView->vars);
121
122
        $expected = [
123
            'form',
124
            'field',
125
            'text',
126
            'my_admin_reference_text',
127
            'my_admin_reference_name_text',
128
            'my_admin_reference_name_text_username',
129
        ];
130
131
        $this->assertSame($expected, $formView->vars['block_prefixes']);
132
        $this->assertTrue($formView->vars['sonata_admin_enabled']);
133
        $this->assertSame('help text', $formView->vars['sonata_help']);
134
    }
135
136
    public function testbuildViewWithNestedForm(): void
137
    {
138
        $eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);
139
140
        $formView = new FormView();
141
        $formView->vars['name'] = 'format';
142
143
        $options = [];
144
        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
145
        $config->setAttribute('sonata_admin', ['admin' => false]);
146
147
        $form = new Form($config);
148
149
        $formView->parent = new FormView();
150
        $formView->parent->vars['sonata_admin_enabled'] = true;
151
        $formView->parent->vars['sonata_admin_code'] = 'parent_code';
152
        $formView->parent->vars['name'] = 'settings';
153
154
        $formView->vars['block_prefixes'] = ['form', 'field', 'text', '_s50b26aa76cb96_settings_format'];
155
156
        $extension = new FormTypeFieldExtension([], []);
157
        $extension->buildView($formView, $form, [
158
            'sonata_help' => 'help text',
159
        ]);
160
161
        $this->assertArrayHasKey('block_prefixes', $formView->vars);
162
        $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
163
        $this->assertArrayHasKey('sonata_admin', $formView->vars);
164
165
        $expected = [
166
            'value' => null,
167
            'attr' => [],
168
            'name' => 'format',
169
            'block_prefixes' => [
170
                'form',
171
                'field',
172
                'text',
173
                'parent_code_text',
174
                'parent_code_text_settings_format',
175
                'parent_code_text_settings_settings_format',
176
            ],
177
            'sonata_admin_enabled' => true,
178
            'sonata_admin' => [
179
                 'admin' => false,
180
                 'field_description' => false,
181
                 'name' => false,
182
                 'edit' => 'standard',
183
                 'inline' => 'natural',
184
                 'block_name' => false,
185
                 'class' => false,
186
                 'options' => [],
187
            ],
188
            'sonata_help' => 'help text',
189
            'sonata_admin_code' => 'parent_code',
190
        ];
191
192
        $this->assertSame($expected, $formView->vars);
193
    }
194
195
    public function testbuildViewWithNestedFormWithNoParent(): void
196
    {
197
        $eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);
198
199
        $formView = new FormView();
200
        $options = [];
201
        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
202
        $form = new Form($config);
203
204
        $extension = new FormTypeFieldExtension([], []);
205
        $extension->buildView($formView, $form, [
206
            'sonata_help' => 'help text',
207
        ]);
208
209
        $this->assertArrayNotHasKey('block_prefixes', $formView->vars);
210
        $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
211
        $this->assertArrayHasKey('sonata_admin', $formView->vars);
212
    }
213
}
214