Completed
Pull Request — 3.x (#5937)
by Peter
03:39
created

testSetObjectFieldTransformed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Form\DataTransformerResolver;
24
use Sonata\AdminBundle\Model\ModelManagerInterface;
25
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
26
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
use Symfony\Component\Form\CallbackTransformer;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31
use Symfony\Component\PropertyAccess\PropertyAccessor;
32
use Symfony\Component\Validator\ConstraintViolation;
33
use Symfony\Component\Validator\ConstraintViolationList;
34
use Symfony\Component\Validator\Validator\ValidatorInterface;
35
use Symfony\Contracts\Translation\TranslatorInterface;
36
use Twig\Environment;
37
use Twig\Loader\ArrayLoader;
38
39
final class SetObjectFieldValueActionTest extends TestCase
40
{
41
    /**
42
     * @var Pool
43
     */
44
    private $pool;
45
46
    /**
47
     * @var Environment
48
     */
49
    private $twig;
50
51
    /**
52
     * @var SetObjectFieldValueAction
53
     */
54
    private $action;
55
56
    /**
57
     * @var AbstractAdmin
58
     */
59
    private $admin;
60
61
    /**
62
     * @var ValidatorInterface
63
     */
64
    private $validator;
65
66
    /**
67
     * @var ModelManagerInterface
68
     */
69
    private $modelManager;
70
71
    /**
72
     * @var DataTransformerResolver
73
     */
74
    private $resolver;
75
76
    protected function setUp(): void
77
    {
78
        $this->twig = new Environment(new ArrayLoader([
79
            'admin_template' => 'renderedTemplate',
80
            'field_template' => 'renderedTemplate',
81
        ]));
82
        $this->pool = $this->prophesize(Pool::class);
83
        $this->admin = $this->prophesize(AbstractAdmin::class);
84
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
85
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
86
        $this->validator = $this->prophesize(ValidatorInterface::class);
87
        $this->modelManager = $this->prophesize(ModelManagerInterface::class);
88
        $this->resolver = new DataTransformerResolver();
89
        $this->action = new SetObjectFieldValueAction(
90
            $this->twig,
91
            $this->pool->reveal(),
92
            $this->validator->reveal(),
93
            $this->resolver
94
        );
95
        $this->admin->getModelManager()->willReturn($this->modelManager->reveal());
96
    }
97
98
    public function testSetObjectFieldValueAction(): void
99
    {
100
        $object = new Foo();
101
        $request = new Request([
102
            'code' => 'sonata.post.admin',
103
            'objectId' => 42,
104
            'field' => 'enabled',
105
            'value' => 1,
106
            'context' => 'list',
107
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
108
109
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
110
        $pool = $this->prophesize(Pool::class);
111
        $translator = $this->prophesize(TranslatorInterface::class);
112
        $propertyAccessor = new PropertyAccessor();
113
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
114
        $container = $this->prophesize(ContainerInterface::class);
115
116
        $this->admin->getObject(42)->willReturn($object);
117
        $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...
118
        $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...
119
        $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...
120
        $this->admin->update($object)->shouldBeCalled();
121
        // NEXT_MAJOR: Remove this line
122
        $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...
123
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
124
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
125
        $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...
126
        $this->twig->addExtension(new SonataAdminExtension(
127
            $pool->reveal(),
128
            null,
129
            $translator->reveal(),
130
            $container->reveal()
131
        ));
132
        $fieldDescription->getOption('editable')->willReturn(true);
133
        $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...
134
        $fieldDescription->getType()->willReturn('boolean');
135
        $fieldDescription->getTemplate()->willReturn('field_template');
136
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
137
        $fieldDescription->getOption('data_transformer')->willReturn(null);
138
139
        $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...
140
141
        $response = ($this->action)($request);
142
143
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
144
    }
145
146
    public function getTimeZones(): iterable
147
    {
148
        $default = new \DateTimeZone(date_default_timezone_get());
149
        $custom = new \DateTimeZone('Europe/Rome');
150
151
        return [
152
            'empty timezone' => [null, $default],
153
            'disabled timezone' => [false, $default],
154
            'default timezone by name' => [$default->getName(), $default],
155
            'default timezone by object' => [$default, $default],
156
            'custom timezone by name' => [$custom->getName(), $custom],
157
            'custom timezone by object' => [$custom, $custom],
158
        ];
159
    }
160
161
    /**
162
     * @dataProvider getTimeZones
163
     */
164
    public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $expectedTimezone): void
165
    {
166
        $object = new Bafoo();
167
        $request = new Request([
168
            'code' => 'sonata.post.admin',
169
            'objectId' => 42,
170
            'field' => 'dateProp',
171
            'value' => '2020-12-12',
172
            'context' => 'list',
173
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
174
175
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
176
        $pool = $this->prophesize(Pool::class);
177
        $translator = $this->prophesize(TranslatorInterface::class);
178
        $propertyAccessor = new PropertyAccessor();
179
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
180
        $container = $this->prophesize(ContainerInterface::class);
181
182
        $this->admin->getObject(42)->willReturn($object);
183
        $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...
184
        $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...
185
        $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...
186
        $this->admin->update($object)->shouldBeCalled();
187
188
        $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...
189
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
190
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
191
        $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...
192
        $this->twig->addExtension(new SonataAdminExtension(
193
            $pool->reveal(),
194
            null,
195
            $translator->reveal(),
196
            $container->reveal()
197
        ));
198
        $fieldDescription->getOption('editable')->willReturn(true);
199
        $fieldDescription->getOption('timezone')->willReturn($timezone);
200
        $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...
201
        $fieldDescription->getType()->willReturn('date');
202
        $fieldDescription->getTemplate()->willReturn('field_template');
203
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
204
        $fieldDescription->getOption('data_transformer')->willReturn(null);
205
206
        $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...
207
208
        $response = ($this->action)($request);
209
210
        $this->assertInstanceOf(\DateTime::class, $object->getDateProp());
211
        $this->assertSame('2020-12-12', $object->getDateProp()->format('Y-m-d'));
212
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
213
214
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
215
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
216
        $expectedDate->setTimezone($defaultTimezone);
217
218
        $this->assertInstanceOf(\DateTime::class, $object->getDateProp());
219
        $this->assertSame($expectedDate->format('Y-m-d'), $object->getDateProp()->format('Y-m-d'));
220
        $this->assertSame($defaultTimezone->getName(), $object->getDateProp()->getTimezone()->getName());
221
    }
222
223
    /**
224
     * @dataProvider getTimeZones
225
     */
226
    public function testSetObjectFieldValueActionWithDateTime($timezone, \DateTimeZone $expectedTimezone): void
227
    {
228
        $object = new Bafoo();
229
        $request = new Request([
230
            'code' => 'sonata.post.admin',
231
            'objectId' => 42,
232
            'field' => 'datetimeProp',
233
            'value' => '2020-12-12 23:11:23',
234
            'context' => 'list',
235
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
236
237
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
238
        $pool = $this->prophesize(Pool::class);
239
        $translator = $this->prophesize(TranslatorInterface::class);
240
        $propertyAccessor = new PropertyAccessor();
241
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
242
        $container = $this->prophesize(ContainerInterface::class);
243
244
        $this->admin->getObject(42)->willReturn($object);
245
        $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...
246
        $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...
247
        $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...
248
        $this->admin->update($object)->shouldBeCalled();
249
250
        $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...
251
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
252
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
253
        $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...
254
        $this->twig->addExtension(new SonataAdminExtension(
255
            $pool->reveal(),
256
            null,
257
            $translator->reveal(),
258
            $container->reveal()
259
        ));
260
        $fieldDescription->getOption('editable')->willReturn(true);
261
        $fieldDescription->getOption('timezone')->willReturn($timezone);
262
        $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...
263
        $fieldDescription->getType()->willReturn('datetime');
264
        $fieldDescription->getTemplate()->willReturn('field_template');
265
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
266
        $fieldDescription->getOption('data_transformer')->willReturn(null);
267
268
        $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...
269
270
        $response = ($this->action)($request);
271
272
        $this->assertInstanceOf(\DateTime::class, $object->getDatetimeProp());
273
        $this->assertSame('2020-12-12 23:11:23', $object->getDatetimeProp()->format('Y-m-d H:i:s'));
274
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
275
276
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
277
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
278
        $expectedDate->setTimezone($defaultTimezone);
279
280
        $this->assertInstanceOf(\DateTime::class, $object->getDatetimeProp());
281
        $this->assertSame($expectedDate->format('Y-m-d H:i:s'), $object->getDatetimeProp()->format('Y-m-d H:i:s'));
282
        $this->assertSame($defaultTimezone->getName(), $object->getDatetimeProp()->getTimezone()->getName());
283
    }
284
285
    public function testSetObjectFieldValueActionOnARelationField(): void
286
    {
287
        $object = new Baz();
288
        $associationObject = new Bar();
289
        $request = new Request([
290
            'code' => 'sonata.post.admin',
291
            'objectId' => 42,
292
            'field' => 'bar',
293
            'value' => 1,
294
            'context' => 'list',
295
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
296
297
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
298
        $translator = $this->prophesize(TranslatorInterface::class);
299
        $propertyAccessor = new PropertyAccessor();
300
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
301
        $container = $this->prophesize(ContainerInterface::class);
302
303
        $this->admin->getObject(42)->willReturn($object);
304
        $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...
305
        $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...
306
        $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...
307
        $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...
308
        $this->admin->update($object)->shouldBeCalled();
309
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
310
        // NEXT_MAJOR: Remove this line
311
        $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...
312
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
313
        $this->twig->addExtension(new SonataAdminExtension(
314
            $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...
315
            null,
316
            $translator->reveal(),
317
            $container->reveal()
318
        ));
319
        $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...
320
        $fieldDescription->getType()->willReturn('choice');
321
        $fieldDescription->getOption('editable')->willReturn(true);
322
        $fieldDescription->getOption('class')->willReturn(Bar::class);
323
        $fieldDescription->getTargetModel()->willReturn(Bar::class);
324
        $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...
325
        $fieldDescription->getTemplate()->willReturn('field_template');
326
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
327
        $fieldDescription->getOption('data_transformer')->willReturn(null);
328
        $this->modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
329
330
        $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...
331
332
        $response = ($this->action)($request);
333
334
        $this->assertSame($associationObject, $object->getBar());
335
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
336
    }
337
338
    public function testSetObjectFieldValueActionWithViolations(): void
339
    {
340
        $bar = new Bar();
341
        $object = new Baz();
342
        $object->setBar($bar);
343
        $request = new Request([
344
            'code' => 'sonata.post.admin',
345
            'objectId' => 42,
346
            'field' => 'bar.enabled',
347
            'value' => 1,
348
            'context' => 'list',
349
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
350
351
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
352
        $propertyAccessor = new PropertyAccessor();
353
354
        $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...
355
        $this->admin->getObject(42)->willReturn($object);
356
        $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...
357
        $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...
358
        $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...
359
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
360
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
361
        ]));
362
        $fieldDescription->getOption('editable')->willReturn(true);
363
        $fieldDescription->getType()->willReturn('boolean');
364
        $fieldDescription->getOption('data_transformer')->willReturn(null);
365
366
        $response = ($this->action)($request);
367
368
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
369
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
370
    }
371
372
    public function testSetObjectFieldEditableMultipleValue(): void
373
    {
374
        $object = new StatusMultiple();
375
        $request = new Request([
376
            'code' => 'sonata.post.admin',
377
            'objectId' => 42,
378
            'field' => 'status',
379
            'value' => [1, 2],
380
            'context' => 'list',
381
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
382
383
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
384
        $pool = $this->prophesize(Pool::class);
385
        $translator = $this->prophesize(TranslatorInterface::class);
386
        $propertyAccessor = new PropertyAccessor();
387
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
388
        $container = $this->prophesize(ContainerInterface::class);
389
390
        $this->admin->getObject(42)->willReturn($object);
391
        $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...
392
        $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...
393
        $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...
394
        $this->admin->update($object)->shouldBeCalled();
395
        // NEXT_MAJOR: Remove this line
396
        $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...
397
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
398
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
399
        $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...
400
        $this->twig->addExtension(new SonataAdminExtension(
401
            $pool->reveal(),
402
            null,
403
            $translator->reveal(),
404
            $container->reveal()
405
        ));
406
        $fieldDescription->getOption('editable')->willReturn(true);
407
        $fieldDescription->getOption('multiple')->willReturn(true);
408
        $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...
409
        $fieldDescription->getType()->willReturn(null);
410
        $fieldDescription->getTemplate()->willReturn('field_template');
411
        $fieldDescription->getValue(Argument::cetera())->willReturn(['some value']);
412
        $fieldDescription->getOption('data_transformer')->willReturn(null);
413
414
        $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...
415
416
        $response = ($this->action)($request);
417
418
        $this->assertSame([1, 2], $object->status);
419
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
420
    }
421
422
    public function testSetObjectFieldTransformed(): void
423
    {
424
        $object = new Foo();
425
        $request = new Request([
426
            'code' => 'sonata.post.admin',
427
            'objectId' => 42,
428
            'field' => 'enabled',
429
            'value' => 'yes',
430
            'context' => 'list',
431
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
432
433
        $dataTransformer = new CallbackTransformer(static function ($value): string {
434
            return (string) (int) $value;
435
        }, static function ($value): bool {
436
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
437
        });
438
439
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
440
        $pool = $this->prophesize(Pool::class);
441
        $translator = $this->prophesize(TranslatorInterface::class);
442
        $propertyAccessor = new PropertyAccessor();
443
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
444
        $container = $this->prophesize(ContainerInterface::class);
445
446
        $this->admin->getObject(42)->willReturn($object);
447
        $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...
448
        $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...
449
        $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...
450
        $this->admin->update($object)->shouldBeCalled();
451
        // NEXT_MAJOR: Remove this line
452
        $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...
453
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
454
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
455
        $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...
456
        $this->twig->addExtension(new SonataAdminExtension(
457
            $pool->reveal(),
458
            null,
459
            $translator->reveal(),
460
            $container->reveal()
461
        ));
462
        $fieldDescription->getOption('editable')->willReturn(true);
463
        $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...
464
        $fieldDescription->getType()->willReturn(null);
465
        $fieldDescription->getTemplate()->willReturn('field_template');
466
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
467
        $fieldDescription->getOption('data_transformer')->willReturn($dataTransformer);
468
469
        $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...
470
471
        $response = ($this->action)($request);
472
473
        $this->assertTrue($object->getEnabled());
474
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
475
    }
476
477
    public function testSetObjectFieldOverrideTransformer(): void
478
    {
479
        $object = new Foo();
480
        $request = new Request([
481
            'code' => 'sonata.post.admin',
482
            'objectId' => 42,
483
            'field' => 'enabled',
484
            'value' => 'yes',
485
            'context' => 'list',
486
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
487
488
        $isOverridden = false;
489
        $dataTransformer = new CallbackTransformer(static function ($value): string {
490
            return (string) (int) $value;
491
        }, static function ($value) use (&$isOverridden): bool {
492
            $isOverridden = true;
493
494
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
495
        });
496
497
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
498
        $pool = $this->prophesize(Pool::class);
499
        $translator = $this->prophesize(TranslatorInterface::class);
500
        $propertyAccessor = new PropertyAccessor();
501
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
502
        $container = $this->prophesize(ContainerInterface::class);
503
504
        $this->admin->getObject(42)->willReturn($object);
505
        $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...
506
        $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...
507
        $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...
508
        $this->admin->update($object)->shouldBeCalled();
509
        // NEXT_MAJOR: Remove this line
510
        $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...
511
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
512
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
513
        $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...
514
        $this->twig->addExtension(new SonataAdminExtension(
515
            $pool->reveal(),
516
            null,
517
            $translator->reveal(),
518
            $container->reveal()
519
        ));
520
        $fieldDescription->getOption('editable')->willReturn(true);
521
        $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...
522
        $fieldDescription->getType()->willReturn('boolean');
523
        $fieldDescription->getTemplate()->willReturn('field_template');
524
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
525
        $fieldDescription->getOption('data_transformer')->willReturn($dataTransformer);
526
527
        $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...
528
529
        $response = ($this->action)($request);
530
531
        $this->assertTrue($object->getEnabled());
532
        $this->assertTrue($isOverridden);
533
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
534
    }
535
}
536