Completed
Pull Request — 3.x (#5937)
by Peter
04:57 queued 25s
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 GetShortObjectDescriptionAction
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Sonata\AdminBundle\...eal(), $this->resolver) of type object<Sonata\AdminBundl...ObjectFieldValueAction> is incompatible with the declared type object<Sonata\AdminBundl...bjectDescriptionAction> of property $action.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 testSetObjectFieldValueActionWithDate(): void
147
    {
148
        $object = new Bafoo();
149
        $request = new Request([
150
            'code' => 'sonata.post.admin',
151
            'objectId' => 42,
152
            'field' => 'dateProp',
153
            'value' => '2020-12-12',
154
            'context' => 'list',
155
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
156
157
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
158
        $pool = $this->prophesize(Pool::class);
159
        $translator = $this->prophesize(TranslatorInterface::class);
160
        $propertyAccessor = new PropertyAccessor();
161
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
162
        $container = $this->prophesize(ContainerInterface::class);
163
164
        $this->admin->getObject(42)->willReturn($object);
165
        $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...
166
        $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...
167
        $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...
168
        $this->admin->update($object)->shouldBeCalled();
169
170
        $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...
171
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
172
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
173
        $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...
174
        $this->twig->addExtension(new SonataAdminExtension(
175
            $pool->reveal(),
176
            null,
177
            $translator->reveal(),
178
            $container->reveal()
179
        ));
180
        $fieldDescription->getOption('editable')->willReturn(true);
181
        $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...
182
        $fieldDescription->getType()->willReturn('date');
183
        $fieldDescription->getTemplate()->willReturn('field_template');
184
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
185
        $fieldDescription->getOption('data_transformer')->willReturn(null);
186
187
        $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...
188
189
        $response = ($this->action)($request);
190
191
        $this->assertInstanceOf(\DateTime::class, $object->getDateProp());
192
        $this->assertSame('2020-12-12', $object->getDateProp()->format('Y-m-d'));
193
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
194
    }
195
196
    public function testSetObjectFieldValueActionWithDateTime(): void
197
    {
198
        $object = new Bafoo();
199
        $request = new Request([
200
            'code' => 'sonata.post.admin',
201
            'objectId' => 42,
202
            'field' => 'datetimeProp',
203
            'value' => '2020-12-12 23:11:23',
204
            'context' => 'list',
205
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
206
207
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
208
        $pool = $this->prophesize(Pool::class);
209
        $translator = $this->prophesize(TranslatorInterface::class);
210
        $propertyAccessor = new PropertyAccessor();
211
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
212
        $container = $this->prophesize(ContainerInterface::class);
213
214
        $this->admin->getObject(42)->willReturn($object);
215
        $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...
216
        $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...
217
        $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...
218
        $this->admin->update($object)->shouldBeCalled();
219
220
        $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...
221
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
222
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
223
        $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...
224
        $this->twig->addExtension(new SonataAdminExtension(
225
            $pool->reveal(),
226
            null,
227
            $translator->reveal(),
228
            $container->reveal()
229
        ));
230
        $fieldDescription->getOption('editable')->willReturn(true);
231
        $fieldDescription->getAdmin()->willReturn($this->admin->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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