Completed
Push — 3.x ( 3e834f...38b337 )
by Grégoire
03:36
created

tests/Action/SetObjectFieldValueActionTest.php (49 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\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\Component\DependencyInjection\ContainerInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\PropertyAccess\PropertyAccessor;
30
use Symfony\Component\Validator\ConstraintViolation;
31
use Symfony\Component\Validator\ConstraintViolationList;
32
use Symfony\Component\Validator\Validator\ValidatorInterface;
33
use Symfony\Contracts\Translation\TranslatorInterface;
34
use Twig\Environment;
35
use Twig\Loader\ArrayLoader;
36
37
final class SetObjectFieldValueActionTest extends TestCase
38
{
39
    /**
40
     * @var Pool
41
     */
42
    private $pool;
43
44
    /**
45
     * @var Environment
46
     */
47
    private $twig;
48
49
    /**
50
     * @var GetShortObjectDescriptionAction
51
     */
52
    private $action;
53
54
    /**
55
     * @var AbstractAdmin
56
     */
57
    private $admin;
58
59
    /**
60
     * @var ValidatorInterface
61
     */
62
    private $validator;
63
64
    protected function setUp(): void
65
    {
66
        $this->twig = new Environment(new ArrayLoader([
67
            'admin_template' => 'renderedTemplate',
68
            'field_template' => 'renderedTemplate',
69
        ]));
70
        $this->pool = $this->prophesize(Pool::class);
71
        $this->admin = $this->prophesize(AbstractAdmin::class);
72
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
73
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
74
        $this->validator = $this->prophesize(ValidatorInterface::class);
75
        $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...
76
            $this->twig,
77
            $this->pool->reveal(),
78
            $this->validator->reveal()
79
        );
80
    }
81
82
    public function testSetObjectFieldValueAction(): void
83
    {
84
        $object = new Foo();
85
        $request = new Request([
86
            'code' => 'sonata.post.admin',
87
            'objectId' => 42,
88
            'field' => 'enabled',
89
            'value' => 1,
90
            'context' => 'list',
91
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
92
93
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
94
        $pool = $this->prophesize(Pool::class);
95
        $translator = $this->prophesize(TranslatorInterface::class);
96
        $propertyAccessor = new PropertyAccessor();
97
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
98
        $container = $this->prophesize(ContainerInterface::class);
99
100
        $this->admin->getObject(42)->willReturn($object);
101
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
102
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
103
        $this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
104
        $this->admin->update($object)->shouldBeCalled();
105
        // NEXT_MAJOR: Remove this line
106
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
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 sonata-project/admin-bundle 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...
107
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
108
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
109
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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...
110
        $this->twig->addExtension(new SonataAdminExtension(
111
            $pool->reveal(),
112
            null,
113
            $translator->reveal(),
114
            $container->reveal()
115
        ));
116
        $fieldDescription->getOption('editable')->willReturn(true);
117
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
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...
118
        $fieldDescription->getType()->willReturn('boolean');
119
        $fieldDescription->getTemplate()->willReturn('field_template');
120
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
121
122
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
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...
123
124
        $response = ($this->action)($request);
125
126
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
127
    }
128
129
    public function testSetObjectFieldValueActionWithDate(): void
130
    {
131
        $object = new Bafoo();
132
        $request = new Request([
133
            'code' => 'sonata.post.admin',
134
            'objectId' => 42,
135
            'field' => 'dateProp',
136
            'value' => '2020-12-12',
137
            'context' => 'list',
138
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
139
140
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
141
        $pool = $this->prophesize(Pool::class);
142
        $translator = $this->prophesize(TranslatorInterface::class);
143
        $propertyAccessor = new PropertyAccessor();
144
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
145
        $container = $this->prophesize(ContainerInterface::class);
146
147
        $this->admin->getObject(42)->willReturn($object);
148
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
149
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
150
        $this->admin->getListFieldDescription('dateProp')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
151
        $this->admin->update($object)->shouldBeCalled();
152
153
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
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 sonata-project/admin-bundle 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...
154
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
155
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
156
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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...
157
        $this->twig->addExtension(new SonataAdminExtension(
158
            $pool->reveal(),
159
            null,
160
            $translator->reveal(),
161
            $container->reveal()
162
        ));
163
        $fieldDescription->getOption('editable')->willReturn(true);
164
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
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...
165
        $fieldDescription->getType()->willReturn('date');
166
        $fieldDescription->getTemplate()->willReturn('field_template');
167
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
168
169
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
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...
170
171
        $response = ($this->action)($request);
172
173
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
174
    }
175
176
    public function testSetObjectFieldValueActionWithDateTime(): void
177
    {
178
        $object = new Bafoo();
179
        $request = new Request([
180
            'code' => 'sonata.post.admin',
181
            'objectId' => 42,
182
            'field' => 'datetimeProp',
183
            'value' => '2020-12-12 23:11:23',
184
            'context' => 'list',
185
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
186
187
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
188
        $pool = $this->prophesize(Pool::class);
189
        $translator = $this->prophesize(TranslatorInterface::class);
190
        $propertyAccessor = new PropertyAccessor();
191
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
192
        $container = $this->prophesize(ContainerInterface::class);
193
194
        $this->admin->getObject(42)->willReturn($object);
195
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
196
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
197
        $this->admin->getListFieldDescription('datetimeProp')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
198
        $this->admin->update($object)->shouldBeCalled();
199
200
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
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 sonata-project/admin-bundle 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...
201
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
202
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
203
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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
        $this->twig->addExtension(new SonataAdminExtension(
205
            $pool->reveal(),
206
            null,
207
            $translator->reveal(),
208
            $container->reveal()
209
        ));
210
        $fieldDescription->getOption('editable')->willReturn(true);
211
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
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...
212
        $fieldDescription->getType()->willReturn('datetime');
213
        $fieldDescription->getTemplate()->willReturn('field_template');
214
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
215
216
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
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...
217
218
        $response = ($this->action)($request);
219
220
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
221
    }
222
223
    public function testSetObjectFieldValueActionOnARelationField(): void
224
    {
225
        $object = new Baz();
226
        $associationObject = new Bar();
227
        $request = new Request([
228
            'code' => 'sonata.post.admin',
229
            'objectId' => 42,
230
            'field' => 'bar',
231
            'value' => 1,
232
            'context' => 'list',
233
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
234
235
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
236
        $modelManager = $this->prophesize(ModelManagerInterface::class);
237
        $translator = $this->prophesize(TranslatorInterface::class);
238
        $propertyAccessor = new PropertyAccessor();
239
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
240
        $container = $this->prophesize(ContainerInterface::class);
241
242
        $this->admin->getObject(42)->willReturn($object);
243
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
244
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
245
        $this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
246
        $this->admin->getClass()->willReturn(\get_class($object));
0 ignored issues
show
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...
247
        $this->admin->update($object)->shouldBeCalled();
248
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
249
        // NEXT_MAJOR: Remove this line
250
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
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 sonata-project/admin-bundle 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...
251
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
252
        $this->admin->getModelManager()->willReturn($modelManager->reveal());
0 ignored issues
show
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...
253
        $this->twig->addExtension(new SonataAdminExtension(
254
            $this->pool->reveal(),
0 ignored issues
show
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...
255
            null,
256
            $translator->reveal(),
257
            $container->reveal()
258
        ));
259
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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...
260
        $fieldDescription->getType()->willReturn('choice');
261
        $fieldDescription->getOption('editable')->willReturn(true);
262
        $fieldDescription->getOption('class')->willReturn(Bar::class);
263
        $fieldDescription->getTargetEntity()->willReturn(Bar::class);
264
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
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...
265
        $fieldDescription->getTemplate()->willReturn('field_template');
266
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
267
        $modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
268
269
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
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...
270
271
        $response = ($this->action)($request);
272
273
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
274
    }
275
276
    public function testSetObjectFieldValueActionWithViolations(): void
277
    {
278
        $bar = new Bar();
279
        $object = new Baz();
280
        $object->setBar($bar);
281
        $request = new Request([
282
            'code' => 'sonata.post.admin',
283
            'objectId' => 42,
284
            'field' => 'bar.enabled',
285
            'value' => 1,
286
            'context' => 'list',
287
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
288
289
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
290
        $propertyAccessor = new PropertyAccessor();
291
292
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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...
293
        $this->admin->getObject(42)->willReturn($object);
294
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
295
        $this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
296
        $this->validator->validate($bar)->willReturn(new ConstraintViolationList([
0 ignored issues
show
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...
297
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
298
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
299
        ]));
300
        $fieldDescription->getOption('editable')->willReturn(true);
301
        $fieldDescription->getType()->willReturn('boolean');
302
303
        $response = ($this->action)($request);
304
305
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
306
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
307
    }
308
309
    public function testSetObjectFieldEditableMultipleValue(): void
310
    {
311
        $object = new StatusMultiple();
312
        $request = new Request([
313
            'code' => 'sonata.post.admin',
314
            'objectId' => 42,
315
            'field' => 'status',
316
            'value' => [1, 2],
317
            'context' => 'list',
318
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
319
320
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
321
        $pool = $this->prophesize(Pool::class);
322
        $template = $this->prophesize(Template::class);
0 ignored issues
show
$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...
323
        $translator = $this->prophesize(TranslatorInterface::class);
324
        $propertyAccessor = new PropertyAccessor();
325
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
326
        $container = $this->prophesize(ContainerInterface::class);
327
328
        $this->admin->getObject(42)->willReturn($object);
329
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
330
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
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...
331
        $this->admin->getListFieldDescription('status')->willReturn($fieldDescription->reveal());
0 ignored issues
show
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...
332
        $this->admin->update($object)->shouldBeCalled();
333
        // NEXT_MAJOR: Remove this line
334
        $this->admin->getTemplate('base_list_field')->willReturn('admin_template');
0 ignored issues
show
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 sonata-project/admin-bundle 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...
335
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
336
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
337
        $this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
0 ignored issues
show
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...
338
        $this->twig->addExtension(new SonataAdminExtension(
339
            $pool->reveal(),
340
            null,
341
            $translator->reveal(),
342
            $container->reveal()
343
        ));
344
        $fieldDescription->getOption('editable')->willReturn(true);
345
        $fieldDescription->getOption('multiple')->willReturn(true);
346
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
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...
347
        $fieldDescription->getType()->willReturn('boolean');
348
        $fieldDescription->getTemplate()->willReturn('field_template');
349
        $fieldDescription->getValue(Argument::cetera())->willReturn(['some value']);
350
351
        $this->validator->validate($object)->willReturn(new ConstraintViolationList([]));
0 ignored issues
show
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...
352
353
        $response = ($this->action)($request);
354
355
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
356
    }
357
}
358