Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

testSetObjectFieldValueAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
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\Action;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
19
use Sonata\AdminBundle\Action\SetObjectFieldValueAction;
20
use Sonata\AdminBundle\Admin\AbstractAdmin;
21
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
22
use Sonata\AdminBundle\Admin\Pool;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
25
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
26
use Symfony\Bridge\Twig\AppVariable;
27
use Symfony\Bridge\Twig\Command\DebugCommand;
28
use Symfony\Bridge\Twig\Extension\FormExtension;
29
use Symfony\Bridge\Twig\Form\TwigRenderer;
30
use Symfony\Component\DependencyInjection\ContainerInterface;
31
use Symfony\Component\Form\FormRenderer;
32
use Symfony\Component\Form\FormRendererEngineInterface;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\PropertyAccess\PropertyAccessor;
35
use Symfony\Component\Translation\TranslatorInterface;
36
use Symfony\Component\Validator\ConstraintViolation;
37
use Symfony\Component\Validator\ConstraintViolationList;
38
use Symfony\Component\Validator\Validator\ValidatorInterface;
39
use Twig\Environment;
40
use Twig\Loader\ArrayLoader;
41
use Twig\RuntimeLoader\FactoryRuntimeLoader;
42
use Twig\Template;
43
44
class Foo
45
{
46
    public function setEnabled($value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
    }
49
}
50
51
class Bar
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
52
{
53
    public function setEnabled($value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
    }
56
}
57
58
class Baz
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
59
{
60
    private $bar;
61
62
    public function setBar(Bar $bar): void
63
    {
64
        $this->bar = $bar;
65
    }
66
67
    public function getBar()
68
    {
69
        return $this->bar;
70
    }
71
}
72
73
final class SetObjectFieldValueActionTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
74
{
75
    /**
76
     * @var Pool
77
     */
78
    private $pool;
79
80
    /**
81
     * @var Environment
82
     */
83
    private $twig;
84
85
    /**
86
     * @var GetShortObjectDescriptionAction
87
     */
88
    private $action;
89
90
    /**
91
     * @var AbstractAdmin
92
     */
93
    private $admin;
94
95
    /**
96
     * @var ValidatorInterface
97
     */
98
    private $validator;
99
100
    protected function setUp(): void
101
    {
102
        $this->twig = new Environment(new ArrayLoader([
103
            'admin_template' => 'renderedTemplate',
104
            'field_template' => 'renderedTemplate',
105
        ]));
106
        $this->pool = $this->prophesize(Pool::class);
107
        $this->admin = $this->prophesize(AbstractAdmin::class);
108
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
109
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
110
        $this->validator = $this->prophesize(ValidatorInterface::class);
111
        $this->action = new SetObjectFieldValueAction(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Sonata\AdminBundle\...s->validator->reveal()) of type object<Sonata\AdminBundl...ObjectFieldValueAction> is incompatible with the declared type object<Sonata\AdminBundl...bjectDescriptionAction> of property $action.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112
            $this->twig,
113
            $this->pool->reveal(),
114
            $this->validator->reveal()
115
        );
116
    }
117
118
    public function testSetObjectFieldValueAction(): void
119
    {
120
        $object = new Foo();
121
        $request = new Request([
122
            'code' => 'sonata.post.admin',
123
            'objectId' => 42,
124
            'field' => 'enabled',
125
            'value' => 1,
126
            'context' => 'list',
127
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
128
129
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
130
        $pool = $this->prophesize(Pool::class);
131
        $template = $this->prophesize(Template::class);
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
        $translator = $this->prophesize(TranslatorInterface::class);
133
        $propertyAccessor = new PropertyAccessor();
134
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
135
        $container = $this->prophesize(ContainerInterface::class);
136
137
        $this->admin->getObject(42)->willReturn($object);
138
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getCode() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('edit', $object) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
        $this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...ldDescriptionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
        $this->admin->update($object)->shouldBeCalled();
142
        // NEXT_MAJOR: Remove this line
143
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getTemplate('base_list_field') (of type null|string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
144
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
145
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
146
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...pertyAccessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
        $this->twig->addExtension(new SonataAdminExtension(
148
            $pool->reveal(),
149
            null,
150
            $translator->reveal(),
151
            $container->reveal()
152
        ));
153
        $fieldDescription->getOption('editable')->willReturn(true);
154
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
        $fieldDescription->getType()->willReturn('boolean');
156
        $fieldDescription->getTemplate()->willReturn(false);
157
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
158
159
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
        $action = $this->action;
161
        $response = $action($request);
162
163
        $this->assertEquals(200, $response->getStatusCode());
164
    }
165
166
    public function testSetObjectFieldValueActionOnARelationField(): void
167
    {
168
        $object = new Baz();
169
        $associationObject = new Bar();
170
        $request = new Request([
171
            'code' => 'sonata.post.admin',
172
            'objectId' => 42,
173
            'field' => 'bar',
174
            'value' => 1,
175
            'context' => 'list',
176
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
177
178
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
179
        $modelManager = $this->prophesize(ModelManagerInterface::class);
180
        $template = $this->prophesize(Template::class);
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
181
        $translator = $this->prophesize(TranslatorInterface::class);
182
        $propertyAccessor = new PropertyAccessor();
183
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
184
        $container = $this->prophesize(ContainerInterface::class);
185
186
        $this->admin->getObject(42)->willReturn($object);
187
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getCode() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
188
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('edit', $object) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
189
        $this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...ldDescriptionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190
        $this->admin->getClass()->willReturn(get_class($object));
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
191
        $this->admin->update($object)->shouldBeCalled();
192
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
193
        // NEXT_MAJOR: Remove this line
194
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getTemplate('base_list_field') (of type null|string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
195
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
196
        $this->admin->getModelManager()->willReturn($modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...\ModelManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
        $this->twig->addExtension(new SonataAdminExtension(
198
            $this->pool->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
            null,
200
            $translator->reveal(),
201
            $container->reveal()
202
        ));
203
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...pertyAccessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
        $fieldDescription->getType()->willReturn('choice');
205
        $fieldDescription->getOption('editable')->willReturn(true);
206
        $fieldDescription->getOption('class')->willReturn(Bar::class);
207
        $fieldDescription->getTargetEntity()->willReturn(Bar::class);
208
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
        $fieldDescription->getTemplate()->willReturn('field_template');
210
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
211
        $modelManager->find(get_class($associationObject), 1)->willReturn($associationObject);
212
213
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
        $action = $this->action;
215
        $response = $action($request);
216
217
        $this->assertEquals(200, $response->getStatusCode());
218
    }
219
220
    public function testSetObjectFieldValueActionWithViolations(): void
221
    {
222
        $bar = new Bar();
223
        $object = new Baz();
224
        $object->setBar($bar);
225
        $request = new Request([
226
            'code' => 'sonata.post.admin',
227
            'objectId' => 42,
228
            'field' => 'bar.enabled',
229
            'value' => 1,
230
            'context' => 'list',
231
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
232
233
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
234
        $propertyAccessor = new PropertyAccessor();
235
236
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...pertyAccessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237
        $this->admin->getObject(42)->willReturn($object);
238
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('edit', $object) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
239
        $this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...ldDescriptionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
240
        $this->validator->validate($bar)->willReturn(new ConstraintViolationList([
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
242
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
243
        ]));
244
        $fieldDescription->getOption('editable')->willReturn(true);
245
        $fieldDescription->getType()->willReturn('boolean');
246
247
        $action = $this->action;
248
        $response = $action($request);
249
250
        $this->assertEquals(400, $response->getStatusCode());
251
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
252
    }
253
254
    private function configureFormRenderer()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
255
    {
256
        $runtime = new FormRenderer($this->createMock(
257
            FormRendererEngineInterface::class,
258
            CsrfTokenManagerInterface::class
259
        ));
260
261
        // Remove the condition when dropping sf < 3.2
262
        if (!method_exists(AppVariable::class, 'getToken')) {
263
            $extension = new FormExtension();
264
265
            $this->twig->addExtension($extension);
266
            $extension->renderer = $runtime;
0 ignored issues
show
Bug introduced by
The property renderer does not seem to exist in Symfony\Bridge\Twig\Extension\FormExtension.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
267
268
            return $runtime;
269
        }
270
271
        // Remove the condition when dropping sf < 3.4
272
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
273
            $twigRuntime = $this->prophesize(TwigRenderer::class);
274
275
            $this->twig->addRuntimeLoader(new FactoryRuntimeLoader(
276
                FormRenderer::class,
277
                function () use ($runtime) {
0 ignored issues
show
Unused Code introduced by
The call to FactoryRuntimeLoader::__construct() has too many arguments starting with function () use($runtime) { return $runtime; }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
278
                    return $runtime;
279
                }
280
            ));
281
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
282
            $twigRuntime->setEnvironment($this->twig)->shouldBeCalled();
283
284
            return $twigRuntime;
285
        }
286
287
        $this->twig->addRuntimeLoader(new FactoryRuntimeLoader(
288
            FormRenderer::class,
289
            function () use ($runtime) {
0 ignored issues
show
Unused Code introduced by
The call to FactoryRuntimeLoader::__construct() has too many arguments starting with function () use($runtime) { return $runtime; }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
290
                return $runtime;
291
            }
292
        ));
293
294
        return $runtime;
295
    }
296
}
297