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