Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
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
final class SetObjectFieldValueActionTest extends TestCase
45
{
46
    /**
47
     * @var Pool
48
     */
49
    private $pool;
50
51
    /**
52
     * @var Environment
53
     */
54
    private $twig;
55
56
    /**
57
     * @var GetShortObjectDescriptionAction
58
     */
59
    private $action;
60
61
    /**
62
     * @var AbstractAdmin
63
     */
64
    private $admin;
65
66
    /**
67
     * @var ValidatorInterface
68
     */
69
    private $validator;
70
71
    protected function setUp(): void
72
    {
73
        $this->twig = new Environment(new ArrayLoader([
74
            'admin_template' => 'renderedTemplate',
75
            'field_template' => 'renderedTemplate',
76
        ]));
77
        $this->pool = $this->prophesize(Pool::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...ndle\Admin\Pool::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\Pool> of property $pool.

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...
78
        $this->admin = $this->prophesize(AbstractAdmin::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...n\AbstractAdmin::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\AbstractAdmin> of property $admin.

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...
79
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
80
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
81
        $this->validator = $this->prophesize(ValidatorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Symfo...idatorInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Symfony\Component...tor\ValidatorInterface> of property $validator.

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...
82
        $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...
83
            $this->twig,
84
            $this->pool->reveal(),
85
            $this->validator->reveal()
86
        );
87
    }
88
89
    public function testSetObjectFieldValueAction(): void
90
    {
91
        $object = new Foo();
92
        $request = new Request([
93
            'code' => 'sonata.post.admin',
94
            'objectId' => 42,
95
            'field' => 'enabled',
96
            'value' => 1,
97
            'context' => 'list',
98
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
99
100
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
101
        $pool = $this->prophesize(Pool::class);
102
        $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...
103
        $translator = $this->prophesize(TranslatorInterface::class);
104
        $propertyAccessor = new PropertyAccessor();
105
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
106
        $container = $this->prophesize(ContainerInterface::class);
107
108
        $this->admin->getObject(42)->willReturn($object);
109
        $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...
110
        $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...
111
        $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...
112
        $this->admin->update($object)->shouldBeCalled();
113
        // NEXT_MAJOR: Remove this line
114
        $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 string|null).

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...
115
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
116
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
117
        $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...
118
        $this->twig->addExtension(new SonataAdminExtension(
119
            $pool->reveal(),
120
            null,
121
            $translator->reveal(),
122
            $container->reveal()
123
        ));
124
        $fieldDescription->getOption('editable')->willReturn(true);
125
        $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...
126
        $fieldDescription->getType()->willReturn('boolean');
127
        $fieldDescription->getTemplate()->willReturn(false);
128
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
129
130
        $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...
131
        $action = $this->action;
132
        $response = $action($request);
133
134
        $this->assertSame(200, $response->getStatusCode());
135
    }
136
137
    public function testSetObjectFieldValueActionOnARelationField(): void
138
    {
139
        $object = new Baz();
140
        $associationObject = new Bar();
141
        $request = new Request([
142
            'code' => 'sonata.post.admin',
143
            'objectId' => 42,
144
            'field' => 'bar',
145
            'value' => 1,
146
            'context' => 'list',
147
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
148
149
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
150
        $modelManager = $this->prophesize(ModelManagerInterface::class);
151
        $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...
152
        $translator = $this->prophesize(TranslatorInterface::class);
153
        $propertyAccessor = new PropertyAccessor();
154
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
155
        $container = $this->prophesize(ContainerInterface::class);
156
157
        $this->admin->getObject(42)->willReturn($object);
158
        $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...
159
        $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...
160
        $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...
161
        $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...
162
        $this->admin->update($object)->shouldBeCalled();
163
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
164
        // NEXT_MAJOR: Remove this line
165
        $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 string|null).

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...
166
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
167
        $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...
168
        $this->twig->addExtension(new SonataAdminExtension(
169
            $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...
170
            null,
171
            $translator->reveal(),
172
            $container->reveal()
173
        ));
174
        $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...
175
        $fieldDescription->getType()->willReturn('choice');
176
        $fieldDescription->getOption('editable')->willReturn(true);
177
        $fieldDescription->getOption('class')->willReturn(Bar::class);
178
        $fieldDescription->getTargetEntity()->willReturn(Bar::class);
179
        $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...
180
        $fieldDescription->getTemplate()->willReturn('field_template');
181
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
182
        $modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
183
184
        $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...
185
        $action = $this->action;
186
        $response = $action($request);
187
188
        $this->assertSame(200, $response->getStatusCode());
189
    }
190
191
    public function testSetObjectFieldValueActionWithViolations(): void
192
    {
193
        $bar = new Bar();
194
        $object = new Baz();
195
        $object->setBar($bar);
196
        $request = new Request([
197
            'code' => 'sonata.post.admin',
198
            'objectId' => 42,
199
            'field' => 'bar.enabled',
200
            'value' => 1,
201
            'context' => 'list',
202
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
203
204
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
205
        $propertyAccessor = new PropertyAccessor();
206
207
        $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...
208
        $this->admin->getObject(42)->willReturn($object);
209
        $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...
210
        $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...
211
        $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...
212
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
213
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
214
        ]));
215
        $fieldDescription->getOption('editable')->willReturn(true);
216
        $fieldDescription->getType()->willReturn('boolean');
217
218
        $action = $this->action;
219
        $response = $action($request);
220
221
        $this->assertSame(400, $response->getStatusCode());
222
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
223
    }
224
225
    private function configureFormRenderer()
226
    {
227
        $runtime = new FormRenderer($this->createMock(
0 ignored issues
show
Documentation introduced by
$this->createMock(\Symfo...anagerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...endererEngineInterface>.

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...
228
            FormRendererEngineInterface::class,
229
            CsrfTokenManagerInterface::class
0 ignored issues
show
Unused Code introduced by
The call to SetObjectFieldValueActionTest::createMock() has too many arguments starting with \Sonata\AdminBundle\Test...ManagerInterface::class.

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...
230
        ));
231
232
        // Remove the condition when dropping sf < 3.2
233
        if (!method_exists(AppVariable::class, 'getToken')) {
234
            $extension = new FormExtension();
235
236
            $this->twig->addExtension($extension);
237
            $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...
238
239
            return $runtime;
240
        }
241
242
        // Remove the condition when dropping sf < 3.4
243
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
244
            $twigRuntime = $this->prophesize(TwigRenderer::class);
245
246
            $this->twig->addRuntimeLoader(new FactoryRuntimeLoader(
247
                FormRenderer::class,
248
                static function () use ($runtime) {
0 ignored issues
show
Unused Code introduced by
The call to FactoryRuntimeLoader::__construct() has too many arguments starting with static function () use($... 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...
249
                    return $runtime;
250
                }
251
            ));
252
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
253
            $twigRuntime->setEnvironment($this->twig)->shouldBeCalled();
254
255
            return $twigRuntime;
256
        }
257
258
        $this->twig->addRuntimeLoader(new FactoryRuntimeLoader(
259
            FormRenderer::class,
260
            static function () use ($runtime) {
0 ignored issues
show
Unused Code introduced by
The call to FactoryRuntimeLoader::__construct() has too many arguments starting with static function () use($... 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...
261
                return $runtime;
262
            }
263
        ));
264
265
        return $runtime;
266
    }
267
}
268