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

testSetObjectFieldOverrideTransformer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 8.9163
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->assertSame(Response::HTTP_OK, $response->getStatusCode());
211
212
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
213
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
214
        $expectedDate->setTimezone($defaultTimezone);
215
216
        $this->assertInstanceOf(\DateTime::class, $object->getDateProp());
217
        $this->assertSame($expectedDate->format('Y-m-d'), $object->getDateProp()->format('Y-m-d'));
218
        $this->assertSame($defaultTimezone->getName(), $object->getDateProp()->getTimezone()->getName());
219
    }
220
221
    /**
222
     * @dataProvider getTimeZones
223
     */
224
    public function testSetObjectFieldValueActionWithDateTime($timezone, \DateTimeZone $expectedTimezone): void
225
    {
226
        $object = new Bafoo();
227
        $request = new Request([
228
            'code' => 'sonata.post.admin',
229
            'objectId' => 42,
230
            'field' => 'datetimeProp',
231
            'value' => '2020-12-12 23:11:23',
232
            'context' => 'list',
233
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
234
235
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
236
        $pool = $this->prophesize(Pool::class);
237
        $translator = $this->prophesize(TranslatorInterface::class);
238
        $propertyAccessor = new PropertyAccessor();
239
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
240
        $container = $this->prophesize(ContainerInterface::class);
241
242
        $this->admin->getObject(42)->willReturn($object);
243
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
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...
244
        $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...
245
        $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...
246
        $this->admin->update($object)->shouldBeCalled();
247
248
        $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...
249
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
250
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
251
        $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...
252
        $this->twig->addExtension(new SonataAdminExtension(
253
            $pool->reveal(),
254
            null,
255
            $translator->reveal(),
256
            $container->reveal()
257
        ));
258
        $fieldDescription->getOption('editable')->willReturn(true);
259
        $fieldDescription->getOption('timezone')->willReturn($timezone);
260
        $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...
261
        $fieldDescription->getType()->willReturn('datetime');
262
        $fieldDescription->getTemplate()->willReturn('field_template');
263
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
264
        $fieldDescription->getOption('data_transformer')->willReturn(null);
265
266
        $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...
267
268
        $response = ($this->action)($request);
269
270
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
271
272
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
273
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
274
        $expectedDate->setTimezone($defaultTimezone);
275
276
        $this->assertInstanceOf(\DateTime::class, $object->getDatetimeProp());
277
        $this->assertSame($expectedDate->format('Y-m-d H:i:s'), $object->getDatetimeProp()->format('Y-m-d H:i:s'));
278
        $this->assertSame($defaultTimezone->getName(), $object->getDatetimeProp()->getTimezone()->getName());
279
    }
280
281
    public function testSetObjectFieldValueActionOnARelationField(): void
282
    {
283
        $object = new Baz();
284
        $associationObject = new Bar();
285
        $request = new Request([
286
            'code' => 'sonata.post.admin',
287
            'objectId' => 42,
288
            'field' => 'bar',
289
            'value' => 1,
290
            'context' => 'list',
291
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
292
293
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
294
        $translator = $this->prophesize(TranslatorInterface::class);
295
        $propertyAccessor = new PropertyAccessor();
296
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
297
        $container = $this->prophesize(ContainerInterface::class);
298
299
        $this->admin->getObject(42)->willReturn($object);
300
        $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...
301
        $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...
302
        $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...
303
        $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...
304
        $this->admin->update($object)->shouldBeCalled();
305
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
306
        // NEXT_MAJOR: Remove this line
307
        $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...
308
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
309
        $this->twig->addExtension(new SonataAdminExtension(
310
            $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...
311
            null,
312
            $translator->reveal(),
313
            $container->reveal()
314
        ));
315
        $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...
316
        $fieldDescription->getType()->willReturn('choice');
317
        $fieldDescription->getOption('editable')->willReturn(true);
318
        $fieldDescription->getOption('class')->willReturn(Bar::class);
319
        $fieldDescription->getTargetModel()->willReturn(Bar::class);
320
        $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...
321
        $fieldDescription->getTemplate()->willReturn('field_template');
322
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
323
        $fieldDescription->getOption('data_transformer')->willReturn(null);
324
        $this->modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
325
326
        $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...
327
328
        $response = ($this->action)($request);
329
330
        $this->assertSame($associationObject, $object->getBar());
331
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
332
    }
333
334
    public function testSetObjectFieldValueActionWithViolations(): void
335
    {
336
        $bar = new Bar();
337
        $object = new Baz();
338
        $object->setBar($bar);
339
        $request = new Request([
340
            'code' => 'sonata.post.admin',
341
            'objectId' => 42,
342
            'field' => 'bar.enabled',
343
            'value' => 1,
344
            'context' => 'list',
345
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
346
347
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
348
        $propertyAccessor = new PropertyAccessor();
349
350
        $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...
351
        $this->admin->getObject(42)->willReturn($object);
352
        $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...
353
        $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...
354
        $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...
355
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
356
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
357
        ]));
358
        $fieldDescription->getOption('editable')->willReturn(true);
359
        $fieldDescription->getType()->willReturn('boolean');
360
        $fieldDescription->getOption('data_transformer')->willReturn(null);
361
362
        $response = ($this->action)($request);
363
364
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
365
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
366
    }
367
368
    public function testSetObjectFieldEditableMultipleValue(): void
369
    {
370
        $object = new StatusMultiple();
371
        $request = new Request([
372
            'code' => 'sonata.post.admin',
373
            'objectId' => 42,
374
            'field' => 'status',
375
            'value' => [1, 2],
376
            'context' => 'list',
377
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
378
379
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
380
        $pool = $this->prophesize(Pool::class);
381
        $translator = $this->prophesize(TranslatorInterface::class);
382
        $propertyAccessor = new PropertyAccessor();
383
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
384
        $container = $this->prophesize(ContainerInterface::class);
385
386
        $this->admin->getObject(42)->willReturn($object);
387
        $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...
388
        $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...
389
        $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...
390
        $this->admin->update($object)->shouldBeCalled();
391
        // NEXT_MAJOR: Remove this line
392
        $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...
393
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
394
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
395
        $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...
396
        $this->twig->addExtension(new SonataAdminExtension(
397
            $pool->reveal(),
398
            null,
399
            $translator->reveal(),
400
            $container->reveal()
401
        ));
402
        $fieldDescription->getOption('editable')->willReturn(true);
403
        $fieldDescription->getOption('multiple')->willReturn(true);
404
        $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...
405
        $fieldDescription->getType()->willReturn(null);
406
        $fieldDescription->getTemplate()->willReturn('field_template');
407
        $fieldDescription->getValue(Argument::cetera())->willReturn(['some value']);
408
        $fieldDescription->getOption('data_transformer')->willReturn(null);
409
410
        $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...
411
412
        $response = ($this->action)($request);
413
414
        $this->assertSame([1, 2], $object->status);
415
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
416
    }
417
418
    public function testSetObjectFieldTransformed(): void
419
    {
420
        $object = new Foo();
421
        $request = new Request([
422
            'code' => 'sonata.post.admin',
423
            'objectId' => 42,
424
            'field' => 'enabled',
425
            'value' => 'yes',
426
            'context' => 'list',
427
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
428
429
        $dataTransformer = new CallbackTransformer(static function ($value): string {
430
            return (string) (int) $value;
431
        }, static function ($value): bool {
432
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
433
        });
434
435
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
436
        $pool = $this->prophesize(Pool::class);
437
        $translator = $this->prophesize(TranslatorInterface::class);
438
        $propertyAccessor = new PropertyAccessor();
439
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
440
        $container = $this->prophesize(ContainerInterface::class);
441
442
        $this->admin->getObject(42)->willReturn($object);
443
        $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...
444
        $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...
445
        $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...
446
        $this->admin->update($object)->shouldBeCalled();
447
        // NEXT_MAJOR: Remove this line
448
        $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...
449
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
450
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
451
        $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...
452
        $this->twig->addExtension(new SonataAdminExtension(
453
            $pool->reveal(),
454
            null,
455
            $translator->reveal(),
456
            $container->reveal()
457
        ));
458
        $fieldDescription->getOption('editable')->willReturn(true);
459
        $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...
460
        $fieldDescription->getType()->willReturn(null);
461
        $fieldDescription->getTemplate()->willReturn('field_template');
462
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
463
        $fieldDescription->getOption('data_transformer')->willReturn($dataTransformer);
464
465
        $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...
466
467
        $response = ($this->action)($request);
468
469
        $this->assertTrue($object->getEnabled());
470
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
471
    }
472
473
    public function testSetObjectFieldOverrideTransformer(): void
474
    {
475
        $object = new Foo();
476
        $request = new Request([
477
            'code' => 'sonata.post.admin',
478
            'objectId' => 42,
479
            'field' => 'enabled',
480
            'value' => 'yes',
481
            'context' => 'list',
482
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
483
484
        $isOverridden = false;
485
        $dataTransformer = new CallbackTransformer(static function ($value): string {
486
            return (string) (int) $value;
487
        }, static function ($value) use (&$isOverridden): bool {
488
            $isOverridden = true;
489
490
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
491
        });
492
493
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
494
        $pool = $this->prophesize(Pool::class);
495
        $translator = $this->prophesize(TranslatorInterface::class);
496
        $propertyAccessor = new PropertyAccessor();
497
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
498
        $container = $this->prophesize(ContainerInterface::class);
499
500
        $this->admin->getObject(42)->willReturn($object);
501
        $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...
502
        $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...
503
        $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...
504
        $this->admin->update($object)->shouldBeCalled();
505
        // NEXT_MAJOR: Remove this line
506
        $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...
507
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
508
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
509
        $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...
510
        $this->twig->addExtension(new SonataAdminExtension(
511
            $pool->reveal(),
512
            null,
513
            $translator->reveal(),
514
            $container->reveal()
515
        ));
516
        $fieldDescription->getOption('editable')->willReturn(true);
517
        $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...
518
        $fieldDescription->getType()->willReturn('boolean');
519
        $fieldDescription->getTemplate()->willReturn('field_template');
520
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
521
        $fieldDescription->getOption('data_transformer')->willReturn($dataTransformer);
522
523
        $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...
524
525
        $response = ($this->action)($request);
526
527
        $this->assertTrue($object->getEnabled());
528
        $this->assertTrue($isOverridden);
529
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
530
    }
531
}
532