Completed
Pull Request — 3.x (#6167)
by Peter
03:06
created

SetObjectFieldValueActionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

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