Completed
Push — master ( 07e5f5...78d40f )
by Jordi Sala
14s queued 11s
created

SetObjectFieldValueActionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 352
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 17
dl 0
loc 352
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testSetObjectFieldValueAction() 0 44 1
A getTimeZones() 0 14 1
A testSetObjectFieldValueActionWithDate() 0 54 1
A testSetObjectFieldValueActionWithDateTime() 0 54 1
A testSetObjectFieldValueActionOnARelationField() 0 50 1
A testSetObjectFieldValueActionWithViolations() 0 32 1
A testSetObjectFieldEditableMultipleValue() 0 46 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Action;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
19
use Sonata\AdminBundle\Action\SetObjectFieldValueAction;
20
use Sonata\AdminBundle\Admin\AbstractAdmin;
21
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
22
use Sonata\AdminBundle\Admin\Pool;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
25
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\PropertyAccess\PropertyAccessor;
30
use Symfony\Component\Validator\ConstraintViolation;
31
use Symfony\Component\Validator\ConstraintViolationList;
32
use Symfony\Component\Validator\Validator\ValidatorInterface;
33
use Symfony\Contracts\Translation\TranslatorInterface;
34
use Twig\Environment;
35
use Twig\Loader\ArrayLoader;
36
37
final class SetObjectFieldValueActionTest extends TestCase
38
{
39
    /**
40
     * @var Pool
41
     */
42
    private $pool;
43
44
    /**
45
     * @var Environment
46
     */
47
    private $twig;
48
49
    /**
50
     * @var GetShortObjectDescriptionAction
51
     */
52
    private $action;
53
54
    /**
55
     * @var AbstractAdmin
56
     */
57
    private $admin;
58
59
    /**
60
     * @var ValidatorInterface
61
     */
62
    private $validator;
63
64
    protected function setUp(): void
65
    {
66
        $this->twig = new Environment(new ArrayLoader([
67
            'admin_template' => 'renderedTemplate',
68
            'field_template' => 'renderedTemplate',
69
        ]));
70
        $this->pool = $this->prophesize(Pool::class);
71
        $this->admin = $this->prophesize(AbstractAdmin::class);
72
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
73
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
74
        $this->validator = $this->prophesize(ValidatorInterface::class);
75
        $this->action = new SetObjectFieldValueAction(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Sonata\AdminBundle\...s->validator->reveal()) of type object<Sonata\AdminBundl...ObjectFieldValueAction> is incompatible with the declared type object<Sonata\AdminBundl...bjectDescriptionAction> of property $action.

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

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

Loading history...
76
            $this->twig,
77
            $this->pool->reveal(),
78
            $this->validator->reveal()
79
        );
80
    }
81
82
    public function testSetObjectFieldValueAction(): void
83
    {
84
        $object = new Foo();
85
        $request = new Request([
86
            'code' => 'sonata.post.admin',
87
            'objectId' => 42,
88
            'field' => 'enabled',
89
            'value' => 1,
90
            'context' => 'list',
91
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
92
93
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
94
        $pool = $this->prophesize(Pool::class);
95
        $translator = $this->prophesize(TranslatorInterface::class);
96
        $propertyAccessor = new PropertyAccessor();
97
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
98
        $container = $this->prophesize(ContainerInterface::class);
99
100
        $this->admin->getObject(42)->willReturn($object);
101
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getCode() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
102
        $this->admin->hasAccess('edit', $object)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('edit', $object) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
103
        $this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...ldDescriptionInterface>.

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

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

Loading history...
104
        $this->admin->update($object)->shouldBeCalled();
0 ignored issues
show
Documentation introduced by
$object is of type object<Sonata\AdminBundle\Tests\Action\Foo>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
106
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
107
        $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...
108
        $this->twig->addExtension(new SonataAdminExtension(
109
            $pool->reveal(),
110
            null,
111
            $translator->reveal(),
112
            $container->reveal()
113
        ));
114
        $fieldDescription->getOption('editable')->willReturn(true);
115
        $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...
116
        $fieldDescription->getType()->willReturn('boolean');
117
        $fieldDescription->getTemplate()->willReturn('field_template');
118
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
119
120
        $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...
121
122
        $response = ($this->action)($request);
123
124
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
125
    }
126
127
    public function getTimeZones(): iterable
128
    {
129
        $default = new \DateTimeZone(date_default_timezone_get());
130
        $custom = new \DateTimeZone('Europe/Rome');
131
132
        return [
133
            'empty timezone' => [null, $default],
134
            'disabled timezone' => [false, $default],
135
            'default timezone by name' => [$default->getName(), $default],
136
            'default timezone by object' => [$default, $default],
137
            'custom timezone by name' => [$custom->getName(), $custom],
138
            'custom timezone by object' => [$custom, $custom],
139
        ];
140
    }
141
142
    /**
143
     * @dataProvider getTimeZones
144
     */
145
    public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $expectedTimezone): void
146
    {
147
        $object = new Bafoo();
148
        $request = new Request([
149
            'code' => 'sonata.post.admin',
150
            'objectId' => 42,
151
            'field' => 'dateProp',
152
            'value' => '2020-12-12',
153
            'context' => 'list',
154
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
155
156
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
157
        $pool = $this->prophesize(Pool::class);
158
        $translator = $this->prophesize(TranslatorInterface::class);
159
        $propertyAccessor = new PropertyAccessor();
160
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
161
        $container = $this->prophesize(ContainerInterface::class);
162
163
        $this->admin->getObject(42)->willReturn($object);
164
        $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...
165
        $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...
166
        $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...
167
        $this->admin->update($object)->shouldBeCalled();
0 ignored issues
show
Documentation introduced by
$object is of type object<Sonata\AdminBundle\Tests\Action\Bafoo>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
168
169
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
170
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
171
        $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...
172
        $this->twig->addExtension(new SonataAdminExtension(
173
            $pool->reveal(),
174
            null,
175
            $translator->reveal(),
176
            $container->reveal()
177
        ));
178
        $fieldDescription->getOption('editable')->willReturn(true);
179
        $fieldDescription->getOption('timezone')->willReturn($timezone);
180
        $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...
181
        $fieldDescription->getType()->willReturn('date');
182
        $fieldDescription->getTemplate()->willReturn('field_template');
183
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
184
185
        $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...
186
187
        $response = ($this->action)($request);
188
189
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
190
191
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
192
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
193
        $expectedDate->setTimezone($defaultTimezone);
194
195
        $this->assertInstanceOf(\DateTime::class, $object->getDateProp());
196
        $this->assertSame($expectedDate->format('Y-m-d'), $object->getDateProp()->format('Y-m-d'));
197
        $this->assertSame($defaultTimezone->getName(), $object->getDateProp()->getTimezone()->getName());
198
    }
199
200
    /**
201
     * @dataProvider getTimeZones
202
     */
203
    public function testSetObjectFieldValueActionWithDateTime($timezone, \DateTimeZone $expectedTimezone): void
204
    {
205
        $object = new Bafoo();
206
        $request = new Request([
207
            'code' => 'sonata.post.admin',
208
            'objectId' => 42,
209
            'field' => 'datetimeProp',
210
            'value' => '2020-12-12 23:11:23',
211
            'context' => 'list',
212
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
213
214
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
215
        $pool = $this->prophesize(Pool::class);
216
        $translator = $this->prophesize(TranslatorInterface::class);
217
        $propertyAccessor = new PropertyAccessor();
218
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
219
        $container = $this->prophesize(ContainerInterface::class);
220
221
        $this->admin->getObject(42)->willReturn($object);
222
        $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...
223
        $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...
224
        $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...
225
        $this->admin->update($object)->shouldBeCalled();
0 ignored issues
show
Documentation introduced by
$object is of type object<Sonata\AdminBundle\Tests\Action\Bafoo>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
226
227
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
228
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
229
        $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...
230
        $this->twig->addExtension(new SonataAdminExtension(
231
            $pool->reveal(),
232
            null,
233
            $translator->reveal(),
234
            $container->reveal()
235
        ));
236
        $fieldDescription->getOption('editable')->willReturn(true);
237
        $fieldDescription->getOption('timezone')->willReturn($timezone);
238
        $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...
239
        $fieldDescription->getType()->willReturn('datetime');
240
        $fieldDescription->getTemplate()->willReturn('field_template');
241
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
242
243
        $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...
244
245
        $response = ($this->action)($request);
246
247
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
248
249
        $defaultTimezone = new \DateTimeZone(date_default_timezone_get());
250
        $expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
251
        $expectedDate->setTimezone($defaultTimezone);
252
253
        $this->assertInstanceOf(\DateTime::class, $object->getDatetimeProp());
254
        $this->assertSame($expectedDate->format('Y-m-d H:i:s'), $object->getDatetimeProp()->format('Y-m-d H:i:s'));
255
        $this->assertSame($defaultTimezone->getName(), $object->getDatetimeProp()->getTimezone()->getName());
256
    }
257
258
    public function testSetObjectFieldValueActionOnARelationField(): void
259
    {
260
        $object = new Baz();
261
        $associationObject = new Bar();
262
        $request = new Request([
263
            'code' => 'sonata.post.admin',
264
            'objectId' => 42,
265
            'field' => 'bar',
266
            'value' => 1,
267
            'context' => 'list',
268
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
269
270
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
271
        $modelManager = $this->prophesize(ModelManagerInterface::class);
272
        $translator = $this->prophesize(TranslatorInterface::class);
273
        $propertyAccessor = new PropertyAccessor();
274
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
275
        $container = $this->prophesize(ContainerInterface::class);
276
277
        $this->admin->getObject(42)->willReturn($object);
278
        $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...
279
        $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...
280
        $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...
281
        $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...
282
        $this->admin->update($object)->shouldBeCalled();
0 ignored issues
show
Documentation introduced by
$object is of type object<Sonata\AdminBundle\Tests\Action\Baz>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
283
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
284
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
285
        $this->admin->getModelManager()->willReturn($modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundl...\ModelManagerInterface>.

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

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

Loading history...
286
        $this->twig->addExtension(new SonataAdminExtension(
287
            $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...
288
            null,
289
            $translator->reveal(),
290
            $container->reveal()
291
        ));
292
        $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...
293
        $fieldDescription->getType()->willReturn('choice');
294
        $fieldDescription->getOption('editable')->willReturn(true);
295
        $fieldDescription->getOption('class')->willReturn(Bar::class);
296
        $fieldDescription->getTargetModel()->willReturn(Bar::class);
297
        $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...
298
        $fieldDescription->getTemplate()->willReturn('field_template');
299
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
300
        $modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject);
301
302
        $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...
303
304
        $response = ($this->action)($request);
305
306
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
307
    }
308
309
    public function testSetObjectFieldValueActionWithViolations(): void
310
    {
311
        $bar = new Bar();
312
        $object = new Baz();
313
        $object->setBar($bar);
314
        $request = new Request([
315
            'code' => 'sonata.post.admin',
316
            'objectId' => 42,
317
            'field' => 'bar.enabled',
318
            'value' => 1,
319
            'context' => 'list',
320
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
321
322
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
323
        $propertyAccessor = new PropertyAccessor();
324
325
        $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...
326
        $this->admin->getObject(42)->willReturn($object);
327
        $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...
328
        $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...
329
        $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...
330
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
331
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
332
        ]));
333
        $fieldDescription->getOption('editable')->willReturn(true);
334
        $fieldDescription->getType()->willReturn('boolean');
335
336
        $response = ($this->action)($request);
337
338
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
339
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
340
    }
341
342
    public function testSetObjectFieldEditableMultipleValue(): void
343
    {
344
        $object = new StatusMultiple();
345
        $request = new Request([
346
            'code' => 'sonata.post.admin',
347
            'objectId' => 42,
348
            'field' => 'status',
349
            'value' => [1, 2],
350
            'context' => 'list',
351
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
352
353
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
354
        $pool = $this->prophesize(Pool::class);
355
        $template = $this->prophesize(Template::class);
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
356
        $translator = $this->prophesize(TranslatorInterface::class);
357
        $propertyAccessor = new PropertyAccessor();
358
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
359
        $container = $this->prophesize(ContainerInterface::class);
360
361
        $this->admin->getObject(42)->willReturn($object);
362
        $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...
363
        $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...
364
        $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...
365
        $this->admin->update($object)->shouldBeCalled();
0 ignored issues
show
Documentation introduced by
$object is of type object<Sonata\AdminBundl...\Action\StatusMultiple>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
366
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
367
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
368
        $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...
369
        $this->twig->addExtension(new SonataAdminExtension(
370
            $pool->reveal(),
371
            null,
372
            $translator->reveal(),
373
            $container->reveal()
374
        ));
375
        $fieldDescription->getOption('editable')->willReturn(true);
376
        $fieldDescription->getOption('multiple')->willReturn(true);
377
        $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...
378
        $fieldDescription->getType()->willReturn('boolean');
379
        $fieldDescription->getTemplate()->willReturn('field_template');
380
        $fieldDescription->getValue(Argument::cetera())->willReturn(['some value']);
381
382
        $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...
383
384
        $response = ($this->action)($request);
385
386
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
387
    }
388
}
389