Completed
Pull Request — 3.x (#6149)
by Peter
02:57
created

SetObjectFieldValueActionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 16
dl 0
loc 341
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testSetObjectFieldValueAction() 0 46 1
A getTimeZones() 0 11 1
A testSetObjectFieldValueActionWithDate() 0 47 1
A testSetObjectFieldValueActionWithDateTime() 0 47 1
A testSetObjectFieldValueActionOnARelationField() 0 52 1
A testSetObjectFieldValueActionWithViolations() 0 32 1
A testSetObjectFieldEditableMultipleValue() 0 48 1
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
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...
102
        $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...
103
        $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...
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
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 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
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...
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
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...
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
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...
123
124
        $response = ($this->action)($request);
125
126
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
127
    }
128
129
    public function getTimeZones(): array
130
    {
131
        return [
132
            [null],
133
            [false],
134
            [\date_default_timezone_get()],
135
            [new \DateTimeZone(\date_default_timezone_get())],
136
            ['Europe/Rome'],
137
            [new \DateTimeZone('Europe/Rome')],
138
        ];
139
    }
140
141
    /**
142
     * @dataProvider getTimeZones
143
     */
144
    public function testSetObjectFieldValueActionWithDate($timezone): void
145
    {
146
        $object = new Bafoo();
147
        $request = new Request([
148
            'code' => 'sonata.post.admin',
149
            'objectId' => 42,
150
            'field' => 'dateProp',
151
            'value' => '2020-12-12',
152
            'context' => 'list',
153
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
154
155
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
156
        $pool = $this->prophesize(Pool::class);
157
        $translator = $this->prophesize(TranslatorInterface::class);
158
        $propertyAccessor = new PropertyAccessor();
159
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
160
        $container = $this->prophesize(ContainerInterface::class);
161
162
        $this->admin->getObject(42)->willReturn($object);
163
        $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...
164
        $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...
165
        $this->admin->getListFieldDescription('dateProp')->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...
166
        $this->admin->update($object)->shouldBeCalled();
167
168
        $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 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...
169
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
170
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
171
        $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...
172
        $this->twig->addExtension(new SonataAdminExtension(
173
            $pool->reveal(),
174
            null,
175
            $translator->reveal(),
176
            $container->reveal()
177
        ));
178
        $fieldDescription->getOption('editable')->willReturn(true);
179
        $fieldDescription->getOption('timezone')->willReturn($timezone);
180
        $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...
181
        $fieldDescription->getType()->willReturn('date');
182
        $fieldDescription->getTemplate()->willReturn('field_template');
183
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
184
185
        $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...
186
187
        $response = ($this->action)($request);
188
189
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
190
    }
191
192
    /**
193
     * @dataProvider getTimeZones
194
     */
195
    public function testSetObjectFieldValueActionWithDateTime($timezone): void
196
    {
197
        $object = new Bafoo();
198
        $request = new Request([
199
            'code' => 'sonata.post.admin',
200
            'objectId' => 42,
201
            'field' => 'datetimeProp',
202
            'value' => '2020-12-12 23:11:23',
203
            'context' => 'list',
204
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
205
206
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
207
        $pool = $this->prophesize(Pool::class);
208
        $translator = $this->prophesize(TranslatorInterface::class);
209
        $propertyAccessor = new PropertyAccessor();
210
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
211
        $container = $this->prophesize(ContainerInterface::class);
212
213
        $this->admin->getObject(42)->willReturn($object);
214
        $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...
215
        $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...
216
        $this->admin->getListFieldDescription('datetimeProp')->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...
217
        $this->admin->update($object)->shouldBeCalled();
218
219
        $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 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...
220
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
221
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
222
        $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...
223
        $this->twig->addExtension(new SonataAdminExtension(
224
            $pool->reveal(),
225
            null,
226
            $translator->reveal(),
227
            $container->reveal()
228
        ));
229
        $fieldDescription->getOption('editable')->willReturn(true);
230
        $fieldDescription->getOption('timezone')->willReturn($timezone);
231
        $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...
232
        $fieldDescription->getType()->willReturn('datetime');
233
        $fieldDescription->getTemplate()->willReturn('field_template');
234
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
235
236
        $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...
237
238
        $response = ($this->action)($request);
239
240
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
241
    }
242
243
    public function testSetObjectFieldValueActionOnARelationField(): void
244
    {
245
        $object = new Baz();
246
        $associationObject = new Bar();
247
        $request = new Request([
248
            'code' => 'sonata.post.admin',
249
            'objectId' => 42,
250
            'field' => 'bar',
251
            'value' => 1,
252
            'context' => 'list',
253
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
254
255
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
256
        $modelManager = $this->prophesize(ModelManagerInterface::class);
257
        $translator = $this->prophesize(TranslatorInterface::class);
258
        $propertyAccessor = new PropertyAccessor();
259
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
260
        $container = $this->prophesize(ContainerInterface::class);
261
262
        $this->admin->getObject(42)->willReturn($object);
263
        $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...
264
        $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...
265
        $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...
266
        $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...
267
        $this->admin->update($object)->shouldBeCalled();
268
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
269
        // NEXT_MAJOR: Remove this line
270
        $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 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...
271
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
272
        $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...
273
        $this->twig->addExtension(new SonataAdminExtension(
274
            $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...
275
            null,
276
            $translator->reveal(),
277
            $container->reveal()
278
        ));
279
        $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...
280
        $fieldDescription->getType()->willReturn('choice');
281
        $fieldDescription->getOption('editable')->willReturn(true);
282
        $fieldDescription->getOption('class')->willReturn(Bar::class);
283
        $fieldDescription->getTargetEntity()->willReturn(Bar::class);
284
        $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...
285
        $fieldDescription->getTemplate()->willReturn('field_template');
286
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
287
        $modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
288
289
        $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...
290
291
        $response = ($this->action)($request);
292
293
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
294
    }
295
296
    public function testSetObjectFieldValueActionWithViolations(): void
297
    {
298
        $bar = new Bar();
299
        $object = new Baz();
300
        $object->setBar($bar);
301
        $request = new Request([
302
            'code' => 'sonata.post.admin',
303
            'objectId' => 42,
304
            'field' => 'bar.enabled',
305
            'value' => 1,
306
            'context' => 'list',
307
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
308
309
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
310
        $propertyAccessor = new PropertyAccessor();
311
312
        $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...
313
        $this->admin->getObject(42)->willReturn($object);
314
        $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...
315
        $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...
316
        $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...
317
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
318
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
319
        ]));
320
        $fieldDescription->getOption('editable')->willReturn(true);
321
        $fieldDescription->getType()->willReturn('boolean');
322
323
        $response = ($this->action)($request);
324
325
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
326
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
327
    }
328
329
    public function testSetObjectFieldEditableMultipleValue(): void
330
    {
331
        $object = new StatusMultiple();
332
        $request = new Request([
333
            'code' => 'sonata.post.admin',
334
            'objectId' => 42,
335
            'field' => 'status',
336
            'value' => [1, 2],
337
            'context' => 'list',
338
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
339
340
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
341
        $pool = $this->prophesize(Pool::class);
342
        $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...
343
        $translator = $this->prophesize(TranslatorInterface::class);
344
        $propertyAccessor = new PropertyAccessor();
345
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
346
        $container = $this->prophesize(ContainerInterface::class);
347
348
        $this->admin->getObject(42)->willReturn($object);
349
        $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...
350
        $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...
351
        $this->admin->getListFieldDescription('status')->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...
352
        $this->admin->update($object)->shouldBeCalled();
353
        // NEXT_MAJOR: Remove this line
354
        $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 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...
355
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
356
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
357
        $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...
358
        $this->twig->addExtension(new SonataAdminExtension(
359
            $pool->reveal(),
360
            null,
361
            $translator->reveal(),
362
            $container->reveal()
363
        ));
364
        $fieldDescription->getOption('editable')->willReturn(true);
365
        $fieldDescription->getOption('multiple')->willReturn(true);
366
        $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...
367
        $fieldDescription->getType()->willReturn('boolean');
368
        $fieldDescription->getTemplate()->willReturn('field_template');
369
        $fieldDescription->getValue(Argument::cetera())->willReturn(['some value']);
370
371
        $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...
372
373
        $response = ($this->action)($request);
374
375
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
376
    }
377
}
378