Completed
Push — 3.x ( a3fef6...67e73a )
by Grégoire
03:55
created

testSetObjectFieldValueAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 43
rs 8.8571
c 2
b 1
f 0
cc 1
eloc 36
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Controller;
13
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Admin\AdminHelper;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Controller\HelperController;
21
use Sonata\AdminBundle\Datagrid\DatagridInterface;
22
use Sonata\AdminBundle\Datagrid\Pager;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
25
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
26
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
27
use Sonata\CoreBundle\Model\Metadata;
28
use Symfony\Bridge\Twig\AppVariable;
29
use Symfony\Bridge\Twig\Command\DebugCommand;
30
use Symfony\Bridge\Twig\Extension\FormExtension;
31
use Symfony\Bridge\Twig\Form\TwigRenderer;
32
use Symfony\Component\DependencyInjection\ContainerInterface;
33
use Symfony\Component\Form\Form;
34
use Symfony\Component\Form\FormBuilder;
35
use Symfony\Component\Form\FormConfigInterface;
36
use Symfony\Component\Form\FormRenderer;
37
use Symfony\Component\Form\FormView;
38
use Symfony\Component\HttpFoundation\Request;
39
use Symfony\Component\HttpFoundation\Response;
40
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
41
use Symfony\Component\PropertyAccess\PropertyAccessor;
42
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
43
use Symfony\Component\Translation\TranslatorInterface;
44
use Symfony\Component\Validator\ConstraintViolation;
45
use Symfony\Component\Validator\ConstraintViolationList;
46
use Symfony\Component\Validator\Validator\ValidatorInterface;
47
use Twig\Environment;
48
use Twig\Template;
49
use Twig\TemplateWrapper;
50
51
class AdminControllerHelper_Foo
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
52
{
53
    private $bar;
54
55
    public function getAdminTitle()
56
    {
57
        return 'foo';
58
    }
59
60
    public function setEnabled($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
    }
63
64
    public function setBar(AdminControllerHelper_Bar $bar)
65
    {
66
        $this->bar = $bar;
67
    }
68
69
    public function getBar()
70
    {
71
        return $this->bar;
72
    }
73
}
74
75
class AdminControllerHelper_Bar
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
76
{
77
    public function getAdminTitle()
78
    {
79
        return 'bar';
80
    }
81
82
    public function setEnabled($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
    }
85
86
    public function getEnabled()
87
    {
88
    }
89
}
90
91
class HelperControllerTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
92
{
93
    /**
94
     * @var Pool
95
     */
96
    private $pool;
97
98
    /**
99
     * @var Environment
100
     */
101
    private $twig;
102
103
    /**
104
     * @var AdminHelper
105
     */
106
    private $helper;
107
108
    /**
109
     * @var ValidatorInterface
110
     */
111
    private $validator;
112
113
    /**
114
     * @var AbstractAdmin
115
     */
116
    private $admin;
117
118
    /**
119
     * @var HelperController
120
     */
121
    private $controller;
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function setUp()
127
    {
128
        $this->pool = $this->prophesize(Pool::class);
129
        $this->twig = $this->prophesize(Environment::class);
130
        $this->helper = $this->prophesize(AdminHelper::class);
131
        $this->validator = $this->prophesize(ValidatorInterface::class);
132
        $this->admin = $this->prophesize(AbstractAdmin::class);
133
134
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
135
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
136
137
        $this->controller = new HelperController(
138
            $this->twig->reveal(),
139
            $this->pool->reveal(),
140
            $this->helper->reveal(),
141
            $this->validator->reveal()
142
        );
143
    }
144
145
    public function testGetShortObjectDescriptionActionInvalidAdmin()
146
    {
147
        $this->expectException(NotFoundHttpException::class);
148
149
        $request = new Request([
150
            'code' => 'sonata.post.admin',
151
            'objectId' => 42,
152
            'uniqid' => 'asdasd123',
153
        ]);
154
155
        $this->pool->getInstance('sonata.post.admin')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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...
156
        $this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->admin->setRequest...dation\Request::class)) (of type 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...
157
158
        $this->controller->getShortObjectDescriptionAction($request);
159
    }
160
161
    public function testGetShortObjectDescriptionActionObjectDoesNotExist()
162
    {
163
        $this->expectException(\RuntimeException::class);
164
        $this->expectExceptionMessage('Invalid format');
165
166
        $request = new Request([
167
            'code' => 'sonata.post.admin',
168
            'objectId' => 42,
169
            'uniqid' => 'asdasd123',
170
        ]);
171
172
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setUniqid('asdasd123') (of type 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...
173
        $this->admin->getObject(42)->willReturn(false);
174
175
        $this->controller->getShortObjectDescriptionAction($request);
176
    }
177
178
    public function testGetShortObjectDescriptionActionEmptyObjectId()
179
    {
180
        $request = new Request([
181
            'code' => 'sonata.post.admin',
182
            'objectId' => '',
183
            'uniqid' => 'asdasd123',
184
            '_format' => 'html',
185
        ]);
186
187
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setUniqid('asdasd123') (of type 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...
188
        $this->admin->getObject(null)->willReturn(false);
189
190
        $response = $this->controller->getShortObjectDescriptionAction($request);
191
192
        $this->assertInstanceOf(Response::class, $response);
193
    }
194
195
    public function testGetShortObjectDescriptionActionObject()
196
    {
197
        $request = new Request([
198
            'code' => 'sonata.post.admin',
199
            'objectId' => 42,
200
            'uniqid' => 'asdasd123',
201
            '_format' => 'html',
202
        ]);
203
        $object = new AdminControllerHelper_Foo();
204
205
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setUniqid('asdasd123') (of type 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...
206
        $this->admin->getObject(42)->willReturn($object);
207
        $this->admin->getTemplate('short_object_description')->willReturn('template');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getTemplat...rt_object_description') (of type null|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...
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since 3.x, 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...
208
        $this->admin->toString($object)->willReturn('bar');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->toString($object) (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...
209
        $this->twig->render('template', [
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->twig->render('tem...arameters' => array())) (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...
210
            'admin' => $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...
211
            'description' => 'bar',
212
            'object' => $object,
213
            'link_parameters' => [],
214
        ])->willReturn('renderedTemplate');
215
216
        $response = $this->controller->getShortObjectDescriptionAction($request);
217
218
        $this->assertSame('renderedTemplate', $response->getContent());
219
    }
220
221
    public function testSetObjectFieldValueAction()
222
    {
223
        $object = new AdminControllerHelper_Foo();
224
        $request = new Request([
225
            'code' => 'sonata.post.admin',
226
            'objectId' => 42,
227
            'field' => 'enabled',
228
            'value' => 1,
229
            'context' => 'list',
230
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
231
232
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
233
        $pool = $this->prophesize(Pool::class);
234
        $template = $this->prophesize(Template::class);
235
        $translator = $this->prophesize(TranslatorInterface::class);
236
        $propertyAccessor = new PropertyAccessor();
237
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
238
        $container = $this->prophesize(ContainerInterface::class);
239
240
        $this->admin->getObject(42)->willReturn($object);
241
        $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...
242
        $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...
243
        $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...
244
        $this->admin->update($object)->shouldBeCalled();
245
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
246
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
247
        $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...
248
        $this->twig->getExtension(SonataAdminExtension::class)->willReturn(
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Twig_ExtensionInterface>.

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...
249
            new SonataAdminExtension($pool->reveal(), null, $translator->reveal(), $container->reveal())
250
        );
251
        $this->twig->load('admin_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal()));
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Twig\Environment>.

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...
Bug introduced by
The method willReturn() does not seem to exist on object<Twig_TemplateWrapper>.

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

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

Loading history...
252
        $this->twig->isDebug()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->twig->isDebug() (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...
253
        $fieldDescription->getOption('editable')->willReturn(true);
254
        $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...
255
        $fieldDescription->getType()->willReturn('boolean');
256
        $fieldDescription->getTemplate()->willReturn(false);
257
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
258
        $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...
259
260
        $response = $this->controller->setObjectFieldValueAction($request);
261
262
        $this->assertEquals(200, $response->getStatusCode());
263
    }
264
265
    public function testSetObjectFieldValueActionOnARelationField()
266
    {
267
        $object = new AdminControllerHelper_Foo();
268
        $associationObject = new AdminControllerHelper_Bar();
269
        $request = new Request([
270
            'code' => 'sonata.post.admin',
271
            'objectId' => 42,
272
            'field' => 'bar',
273
            'value' => 1,
274
            'context' => 'list',
275
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
276
277
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
278
        $modelManager = $this->prophesize(ModelManagerInterface::class);
279
        $template = $this->prophesize(Template::class);
280
        $translator = $this->prophesize(TranslatorInterface::class);
281
        $propertyAccessor = new PropertyAccessor();
282
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
283
        $container = $this->prophesize(ContainerInterface::class);
284
285
        $this->admin->getObject(42)->willReturn($object);
286
        $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...
287
        $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...
288
        $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...
289
        $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...
290
        $this->admin->update($object)->shouldBeCalled();
291
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
292
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
293
        $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...
294
        $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...
295
        $this->twig->getExtension(SonataAdminExtension::class)->willReturn(
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Twig_ExtensionInterface>.

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...
296
            new SonataAdminExtension($this->pool->reveal(), null, $translator->reveal(), $container->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...
297
        );
298
        $this->twig->load('field_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal()));
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Twig\Environment>.

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...
Bug introduced by
The method willReturn() does not seem to exist on object<Twig_TemplateWrapper>.

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...
299
        $this->twig->isDebug()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->twig->isDebug() (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...
300
        $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...
301
        $fieldDescription->getType()->willReturn('choice');
302
        $fieldDescription->getOption('editable')->willReturn(true);
303
        $fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class);
304
        $fieldDescription->getTargetEntity()->willReturn(AdminControllerHelper_Bar::class);
305
        $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...
306
        $fieldDescription->getTemplate()->willReturn('field_template');
307
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
308
        $modelManager->find(get_class($associationObject), 1)->willReturn($associationObject);
309
310
        $response = $this->controller->setObjectFieldValueAction($request);
311
312
        $this->assertEquals(200, $response->getStatusCode());
313
    }
314
315
    public function testAppendFormFieldElementAction()
316
    {
317
        $object = new AdminControllerHelper_Foo();
318
        $request = new Request([
319
            'code' => 'sonata.post.admin',
320
            'objectId' => 42,
321
            'field' => 'enabled',
322
            'value' => 1,
323
            'context' => 'list',
324
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
325
326
        $modelManager = $this->prophesize(ModelManagerInterface::class);
327
        $formView = new FormView();
328
        $form = $this->prophesize(Form::class);
329
330
        $renderer = $this->configureFormRenderer();
331
332
        $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...
333
        $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...
334
        $this->admin->setSubject($object)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setSubject($object) (of type 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...
335
        $this->admin->getFormTheme()->willReturn($formView);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getFormTheme() (of type array).

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...
336
        $this->helper->appendFormFieldElement($this->admin->reveal(), $object, null)->willReturn([
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...
Bug introduced by
The method willReturn cannot be called on $this->helper->appendFor...eveal(), $object, null) (of type array<integer,null|objec...Form\\FormInterface>"}>).

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...
337
            $this->prophesize(FieldDescriptionInterface::class),
338
            $form->reveal(),
339
        ]);
340
        $this->helper->getChildFormView($formView, null)
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component\Form\FormView>.

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...
341
            ->willReturn($formView);
342
        $modelManager->find(get_class($object), 42)->willReturn($object);
343
        $form->createView()->willReturn($formView);
344
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
345
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
346
347
        $response = $this->controller->appendFormFieldElementAction($request);
348
349
        $this->isInstanceOf(Response::class, $response);
350
        $this->assertSame($response->getContent(), 'block');
351
    }
352
353
    public function testRetrieveFormFieldElementAction()
354
    {
355
        $object = new AdminControllerHelper_Foo();
356
        $request = new Request([
357
            'code' => 'sonata.post.admin',
358
            'objectId' => 42,
359
            'field' => 'enabled',
360
            'value' => 1,
361
            'context' => 'list',
362
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
363
364
        $modelManager = $this->prophesize(ModelManagerInterface::class);
365
        $formView = new FormView();
366
        $form = $this->prophesize(Form::class);
367
        $formBuilder = $this->prophesize(FormBuilder::class);
368
369
        $renderer = $this->configureFormRenderer();
370
371
        $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...
372
        $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...
373
        $this->admin->setSubject($object)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setSubject($object) (of type 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...
374
        $this->admin->getFormTheme()->willReturn($formView);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getFormTheme() (of type array).

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...
375
        $this->admin->getFormBuilder()->willReturn($formBuilder->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...m\FormBuilderInterface>.

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
        $this->helper->getChildFormView($formView, null)
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component\Form\FormView>.

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...
377
            ->willReturn($formView);
378
        $modelManager->find(get_class($object), 42)->willReturn($object);
379
        $form->setData($object)->shouldBeCalled();
380
        $form->handleRequest($request)->shouldBeCalled();
381
        $form->createView()->willReturn($formView);
382
        $formBuilder->getForm()->willReturn($form->reveal());
383
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
384
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
385
386
        $response = $this->controller->retrieveFormFieldElementAction($request);
387
388
        $this->isInstanceOf(Response::class, $response);
389
        $this->assertSame($response->getContent(), 'block');
390
    }
391
392
    public function testSetObjectFieldValueActionWithViolations()
393
    {
394
        $bar = new AdminControllerHelper_Bar();
395
        $object = new AdminControllerHelper_Foo();
396
        $object->setBar($bar);
397
        $request = new Request([
398
            'code' => 'sonata.post.admin',
399
            'objectId' => 42,
400
            'field' => 'bar.enabled',
401
            'value' => 1,
402
            'context' => 'list',
403
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
404
405
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
406
        $propertyAccessor = new PropertyAccessor();
407
408
        $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...
409
        $this->admin->getObject(42)->willReturn($object);
410
        $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...
411
        $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...
412
        $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...
413
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
414
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
415
        ]));
416
        $fieldDescription->getOption('editable')->willReturn(true);
417
        $fieldDescription->getType()->willReturn('boolean');
418
419
        $response = $this->controller->setObjectFieldValueAction($request);
420
421
        $this->assertEquals(400, $response->getStatusCode());
422
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
423
    }
424
425
    public function testRetrieveAutocompleteItemsActionNotGranted()
426
    {
427
        $this->expectException(AccessDeniedException::class);
428
429
        $request = new Request([
430
            'admin_code' => 'foo.admin',
431
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
432
433
        $this->admin->hasAccess('create')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('create') (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...
434
        $this->admin->hasAccess('edit')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('edit') (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...
435
436
        $this->controller->retrieveAutocompleteItemsAction($request);
437
    }
438
439
    public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
440
    {
441
        $this->expectException(AccessDeniedException::class);
442
        $this->expectExceptionMessage('Autocomplete list can`t be retrieved because the form element is disabled or read_only.');
443
444
        $object = new AdminControllerHelper_Foo();
445
        $request = new Request([
446
            'admin_code' => 'foo.admin',
447
            'field' => 'barField',
448
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
449
450
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
451
452
        $this->configureFormConfig('barField', true);
453
454
        $this->admin->getNewInstance()->willReturn($object);
455
        $this->admin->setSubject($object)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setSubject($object) (of type 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...
456
        $this->admin->hasAccess('create')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('create') (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...
457
        $this->admin->getFormFieldDescriptions()->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getFormFieldDescriptions() (of type array<integer,object<Son...dDescriptionInterface>>).

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...
458
        $this->admin->getFormFieldDescription('barField')->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...
459
460
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
461
        $fieldDescription->getName()->willReturn('barField');
462
463
        $this->controller->retrieveAutocompleteItemsAction($request);
464
    }
465
466
    public function testRetrieveAutocompleteItemsTooShortSearchString()
467
    {
468
        $object = new AdminControllerHelper_Foo();
469
        $request = new Request([
470
            'admin_code' => 'foo.admin',
471
            'field' => 'barField',
472
            'q' => 'so',
473
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
474
475
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
476
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
477
478
        $this->configureFormConfig('barField');
479
480
        $this->admin->getNewInstance()->willReturn($object);
481
        $this->admin->setSubject($object)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setSubject($object) (of type 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...
482
        $this->admin->hasAccess('create')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('create') (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...
483
        $this->admin->getFormFieldDescription('barField')->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...
484
        $this->admin->getFormFieldDescriptions()->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getFormFieldDescriptions() (of type array<integer,object<Son...dDescriptionInterface>>).

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...
485
        $targetAdmin->checkAccess('list')->willReturn(null);
486
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
487
        $fieldDescription->getName()->willReturn('barField');
488
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
489
490
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
491
492
        $this->isInstanceOf(Response::class, $response);
493
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
494
        $this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent());
495
    }
496
497
    public function testRetrieveAutocompleteItems()
498
    {
499
        $entity = new Foo();
500
        $request = new Request([
501
            'admin_code' => 'foo.admin',
502
            'field' => 'barField',
503
            'q' => 'sonata',
504
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
505
506
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
507
        $datagrid = $this->prophesize(DatagridInterface::class);
508
        $metadata = $this->prophesize(Metadata::class);
509
        $pager = $this->prophesize(Pager::class);
510
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
511
512
        $this->configureFormConfig('barField');
513
514
        $this->admin->getNewInstance()->willReturn($entity);
515
        $this->admin->setSubject($entity)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->admin->setSubject($entity) (of type 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...
516
        $this->admin->hasAccess('create')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->hasAccess('create') (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...
517
        $this->admin->getFormFieldDescription('barField')->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...
518
        $this->admin->getFormFieldDescriptions()->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getFormFieldDescriptions() (of type array<integer,object<Son...dDescriptionInterface>>).

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...
519
        $this->admin->id($entity)->willReturn(123);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->id($entity) (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...
520
        $targetAdmin->checkAccess('list')->shouldBeCalled();
521
        $targetAdmin->setFilterPersister(null)->shouldBeCalled();
522
        $targetAdmin->getDatagrid()->willReturn($datagrid->reveal());
523
        $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal());
524
        $metadata->getTitle()->willReturn('FOO');
525
        $datagrid->hasFilter('foo')->willReturn(true);
526
        $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled();
527
        $datagrid->setValue('_per_page', null, 10)->shouldBeCalled();
528
        $datagrid->setValue('_page', null, 1)->shouldBeCalled();
529
        $datagrid->buildPager()->willReturn(null);
530
        $datagrid->getPager()->willReturn($pager->reveal());
531
        $pager->getResults()->willReturn([$entity]);
532
        $pager->isLastPage()->willReturn(true);
533
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
534
        $fieldDescription->getName()->willReturn('barField');
535
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
536
537
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
538
539
        $this->isInstanceOf(Response::class, $response);
540
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
541
        $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent());
542
    }
543
544
    private function configureFormConfig($field, $disabled = false)
545
    {
546
        $form = $this->prophesize(Form::class);
547
        $formType = $this->prophesize(Form::class);
548
        $formConfig = $this->prophesize(FormConfigInterface::class);
549
550
        $this->admin->getForm()->willReturn($form->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component\Form\Form>.

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...
551
        $form->get($field)->willReturn($formType->reveal());
552
        $formType->getConfig()->willReturn($formConfig->reveal());
553
        $formConfig->getAttribute('disabled')->willReturn($disabled);
554
        $formConfig->getAttribute('property')->willReturn('foo');
555
        $formConfig->getAttribute('callback')->willReturn(null);
556
        $formConfig->getAttribute('minimum_input_length')->willReturn(3);
557
        $formConfig->getAttribute('items_per_page')->willReturn(10);
558
        $formConfig->getAttribute('req_param_name_page_number')->willReturn('_page');
559
        $formConfig->getAttribute('to_string_callback')->willReturn(null);
560
        $formConfig->getAttribute('target_admin_access_action')->willReturn('list');
561
    }
562
563
    private function configureFormRenderer()
564
    {
565
        $runtime = $this->prophesize(FormRenderer::class);
566
567
        // Remove the condition when dropping sf < 3.2
568
        if (!method_exists(AppVariable::class, 'getToken')) {
569
            $extension = $this->prophesize(FormExtension::class);
570
571
            $this->twig->getExtension(FormExtension::class)->willReturn($extension->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Twig_ExtensionInterface>.

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...
572
            $extension->initRuntime($this->twig->reveal())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Twig\Environment>.

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...
573
            $extension->renderer = $runtime->reveal();
574
575
            return $runtime;
576
        }
577
578
        // Remove the condition when dropping sf < 3.4
579
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
580
            $twigRuntime = $this->prophesize(TwigRenderer::class);
581
582
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
583
            $twigRuntime->setEnvironment($this->twig->reveal())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Twig\Environment>.

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...
584
585
            return $twigRuntime;
586
        }
587
588
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
589
590
        return $runtime;
591
    }
592
}
593