Completed
Push — 3.x ( 6b78b0...2579fc )
by Grégoire
04:21
created

HelperControllerTest   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 503
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 18
c 6
b 1
f 0
lcom 1
cbo 19
dl 0
loc 503
rs 6.875

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
A testGetShortObjectDescriptionActionInvalidAdmin() 0 15 1
A testGetShortObjectDescriptionActionObjectDoesNotExist() 0 16 1
A testGetShortObjectDescriptionActionEmptyObjectId() 0 16 1
B testGetShortObjectDescriptionActionObject() 0 25 1
B testSetObjectFieldValueAction() 0 39 1
A testSetObjectFieldValueActionOnARelationField() 0 53 1
B testAppendFormFieldElementAction() 0 37 1
B testRetrieveFormFieldElementAction() 0 38 1
B testSetObjectFieldValueActionWithViolations() 0 32 1
A testRetrieveAutocompleteItemsActionNotGranted() 0 13 1
B testRetrieveAutocompleteItemsActionDisabledFormelememt() 0 26 1
B testRetrieveAutocompleteItemsTooShortSearchString() 0 30 1
A testRetrieveAutocompleteItems() 0 47 1
A configureFormConfig() 0 18 1
B configureFormRenderer() 0 29 3
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 Doctrine\Common\Persistence\ManagerRegistry;
15
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Argument;
19
use Sonata\AdminBundle\Admin\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\AdminHelper;
21
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
22
use Sonata\AdminBundle\Admin\Pool;
23
use Sonata\AdminBundle\Controller\HelperController;
24
use Sonata\AdminBundle\Datagrid\DatagridInterface;
25
use Sonata\AdminBundle\Datagrid\Pager;
26
use Sonata\AdminBundle\Model\ModelManagerInterface;
27
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
28
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
29
use Sonata\CoreBundle\Model\Metadata;
30
use Symfony\Bridge\Twig\AppVariable;
31
use Symfony\Bridge\Twig\Command\DebugCommand;
32
use Symfony\Bridge\Twig\Extension\FormExtension;
33
use Symfony\Bridge\Twig\Form\TwigRenderer;
34
use Symfony\Component\DependencyInjection\ContainerInterface;
35
use Symfony\Component\Form\Form;
36
use Symfony\Component\Form\FormBuilder;
37
use Symfony\Component\Form\FormConfigInterface;
38
use Symfony\Component\Form\FormRenderer;
39
use Symfony\Component\Form\FormView;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\HttpFoundation\Response;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
use Symfony\Component\PropertyAccess\PropertyAccessor;
44
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
45
use Symfony\Component\Translation\TranslatorInterface;
46
use Symfony\Component\Validator\ConstraintViolation;
47
use Symfony\Component\Validator\ConstraintViolationList;
48
use Symfony\Component\Validator\Validator\ValidatorInterface;
49
use Twig\Environment;
50
use Twig\Template;
51
use Twig\TemplateWrapper;
52
53
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...
54
{
55
    private $bar;
56
57
    public function getAdminTitle()
58
    {
59
        return 'foo';
60
    }
61
62
    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...
63
    {
64
    }
65
66
    public function setBar(AdminControllerHelper_Bar $bar)
67
    {
68
        $this->bar = $bar;
69
    }
70
71
    public function getBar()
72
    {
73
        return $this->bar;
74
    }
75
}
76
77
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...
78
{
79
    public function getAdminTitle()
80
    {
81
        return 'bar';
82
    }
83
84
    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...
85
    {
86
    }
87
88
    public function getEnabled()
89
    {
90
    }
91
}
92
93
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...
94
{
95
    /**
96
     * @var Pool
97
     */
98
    private $pool;
99
100
    /**
101
     * @var Environment
102
     */
103
    private $twig;
104
105
    /**
106
     * @var AdminHelper
107
     */
108
    private $helper;
109
110
    /**
111
     * @var ValidatorInterface
112
     */
113
    private $validator;
114
115
    /**
116
     * @var AbstractAdmin
117
     */
118
    private $admin;
119
120
    /**
121
     * @var HelperController
122
     */
123
    private $controller;
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    protected function setUp()
129
    {
130
        $this->pool = $this->prophesize(Pool::class);
131
        $this->twig = $this->prophesize(Environment::class);
132
        $this->helper = $this->prophesize(AdminHelper::class);
133
        $this->validator = $this->prophesize(ValidatorInterface::class);
134
        $this->admin = $this->prophesize(AbstractAdmin::class);
135
136
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
137
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
138
139
        $this->controller = new HelperController(
140
            $this->twig->reveal(),
141
            $this->pool->reveal(),
142
            $this->helper->reveal(),
143
            $this->validator->reveal()
144
        );
145
    }
146
147
    public function testGetShortObjectDescriptionActionInvalidAdmin()
148
    {
149
        $this->expectException(NotFoundHttpException::class);
150
151
        $request = new Request([
152
            'code' => 'sonata.post.admin',
153
            'objectId' => 42,
154
            'uniqid' => 'asdasd123',
155
        ]);
156
157
        $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...
158
        $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...
159
160
        $this->controller->getShortObjectDescriptionAction($request);
161
    }
162
163
    public function testGetShortObjectDescriptionActionObjectDoesNotExist()
164
    {
165
        $this->expectException(\RuntimeException::class);
166
        $this->expectExceptionMessage('Invalid format');
167
168
        $request = new Request([
169
            'code' => 'sonata.post.admin',
170
            'objectId' => 42,
171
            'uniqid' => 'asdasd123',
172
        ]);
173
174
        $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...
175
        $this->admin->getObject(42)->willReturn(false);
176
177
        $this->controller->getShortObjectDescriptionAction($request);
178
    }
179
180
    public function testGetShortObjectDescriptionActionEmptyObjectId()
181
    {
182
        $request = new Request([
183
            'code' => 'sonata.post.admin',
184
            'objectId' => '',
185
            'uniqid' => 'asdasd123',
186
            '_format' => 'html',
187
        ]);
188
189
        $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...
190
        $this->admin->getObject(null)->willReturn(false);
191
192
        $response = $this->controller->getShortObjectDescriptionAction($request);
193
194
        $this->assertInstanceOf(Response::class, $response);
195
    }
196
197
    public function testGetShortObjectDescriptionActionObject()
198
    {
199
        $request = new Request([
200
            'code' => 'sonata.post.admin',
201
            'objectId' => 42,
202
            'uniqid' => 'asdasd123',
203
            '_format' => 'html',
204
        ]);
205
        $object = new AdminControllerHelper_Foo();
206
207
        $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...
208
        $this->admin->getObject(42)->willReturn($object);
209
        $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...
210
        $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...
211
        $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...
212
            '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...
213
            'description' => 'bar',
214
            'object' => $object,
215
            'link_parameters' => [],
216
        ])->willReturn('renderedTemplate');
217
218
        $response = $this->controller->getShortObjectDescriptionAction($request);
219
220
        $this->assertSame('renderedTemplate', $response->getContent());
221
    }
222
223
    public function testSetObjectFieldValueAction()
224
    {
225
        $object = new AdminControllerHelper_Foo();
226
        $request = new Request([
227
            'code' => 'sonata.post.admin',
228
            'objectId' => 42,
229
            'field' => 'enabled',
230
            'value' => 1,
231
            'context' => 'list',
232
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
233
234
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
235
        $pool = $this->prophesize(Pool::class);
236
        $template = $this->prophesize(Template::class);
237
        $translator = $this->prophesize(TranslatorInterface::class);
238
        $propertyAccessor = new PropertyAccessor();
239
240
        $this->admin->getObject(42)->willReturn($object);
241
        $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...
242
        $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...
243
        $this->admin->update($object)->shouldBeCalled();
244
        $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 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...
245
        $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...
246
        $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...
247
            new SonataAdminExtension($pool->reveal(), null, $translator->reveal())
248
        );
249
        $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...
250
        $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...
251
        $fieldDescription->getOption('editable')->willReturn(true);
252
        $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...
253
        $fieldDescription->getType()->willReturn('boolean');
254
        $fieldDescription->getTemplate()->willReturn(false);
255
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
256
        $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...
257
258
        $response = $this->controller->setObjectFieldValueAction($request);
259
260
        $this->assertEquals(200, $response->getStatusCode());
261
    }
262
263
    public function testSetObjectFieldValueActionOnARelationField()
264
    {
265
        $object = new AdminControllerHelper_Foo();
266
        $associationObject = new AdminControllerHelper_Bar();
267
        $request = new Request([
268
            'code' => 'sonata.post.admin',
269
            'objectId' => 42,
270
            'field' => 'bar',
271
            'value' => 1,
272
            'context' => 'list',
273
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
274
275
        $container = $this->prophesize(ContainerInterface::class);
276
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
277
        $managerRegistry = $this->prophesize(ManagerRegistry::class);
278
        $objectManager = $this->prophesize(ObjectManager::class);
279
        $classMetadata = $this->prophesize(ClassMetadata::class);
280
        $pool = $this->prophesize(Pool::class);
281
        $template = $this->prophesize(Template::class);
282
        $translator = $this->prophesize(TranslatorInterface::class);
283
        $propertyAccessor = new PropertyAccessor();
284
285
        $this->admin->getObject(42)->willReturn($object);
286
        $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...
287
        $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...
288
        $this->admin->getManagerType()->willReturn('doctrine_orm');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getManagerType() (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...
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
        $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 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...
292
        $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...
293
        $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...
294
            new SonataAdminExtension($pool->reveal(), null, $translator->reveal())
295
        );
296
        $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...
297
        $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...
298
        $this->pool->getContainer()->willReturn($container->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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->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...
300
        $fieldDescription->getType()->willReturn('choice');
301
        $fieldDescription->getOption('editable')->willReturn(true);
302
        $fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class);
303
        $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...
304
        $fieldDescription->getTemplate()->willReturn('field_template');
305
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
306
        $container->get('doctrine_orm')->willReturn($managerRegistry->reveal());
307
        $managerRegistry->getManager()->willReturn($objectManager->reveal());
308
        $objectManager->getClassMetadata(get_class($object))->willReturn($classMetadata->reveal());
309
        $objectManager->find(get_class($associationObject), 1)->willReturn($associationObject);
310
        $classMetadata->hasAssociation('bar')->willReturn(true);
311
312
        $response = $this->controller->setObjectFieldValueAction($request);
313
314
        $this->assertEquals(200, $response->getStatusCode());
315
    }
316
317
    public function testAppendFormFieldElementAction()
318
    {
319
        $object = new AdminControllerHelper_Foo();
320
        $request = new Request([
321
            'code' => 'sonata.post.admin',
322
            'objectId' => 42,
323
            'field' => 'enabled',
324
            'value' => 1,
325
            'context' => 'list',
326
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
327
328
        $modelManager = $this->prophesize(ModelManagerInterface::class);
329
        $formView = new FormView();
330
        $form = $this->prophesize(Form::class);
331
332
        $renderer = $this->configureFormRenderer();
333
334
        $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...
335
        $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...
336
        $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...
337
        $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...
338
        $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...
339
            $this->prophesize(FieldDescriptionInterface::class),
340
            $form->reveal(),
341
        ]);
342
        $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...
343
            ->willReturn($formView);
344
        $modelManager->find(get_class($object), 42)->willReturn($object);
345
        $form->createView()->willReturn($formView);
346
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
347
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
348
349
        $response = $this->controller->appendFormFieldElementAction($request);
350
351
        $this->isInstanceOf(Response::class, $response);
352
        $this->assertSame($response->getContent(), 'block');
353
    }
354
355
    public function testRetrieveFormFieldElementAction()
356
    {
357
        $object = new AdminControllerHelper_Foo();
358
        $request = new Request([
359
            'code' => 'sonata.post.admin',
360
            'objectId' => 42,
361
            'field' => 'enabled',
362
            'value' => 1,
363
            'context' => 'list',
364
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
365
366
        $modelManager = $this->prophesize(ModelManagerInterface::class);
367
        $formView = new FormView();
368
        $form = $this->prophesize(Form::class);
369
        $formBuilder = $this->prophesize(FormBuilder::class);
370
371
        $renderer = $this->configureFormRenderer();
372
373
        $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...
374
        $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...
375
        $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...
376
        $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...
377
        $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...
378
        $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...
379
            ->willReturn($formView);
380
        $modelManager->find(get_class($object), 42)->willReturn($object);
381
        $form->setData($object)->shouldBeCalled();
382
        $form->handleRequest($request)->shouldBeCalled();
383
        $form->createView()->willReturn($formView);
384
        $formBuilder->getForm()->willReturn($form->reveal());
385
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
386
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
387
388
        $response = $this->controller->retrieveFormFieldElementAction($request);
389
390
        $this->isInstanceOf(Response::class, $response);
391
        $this->assertSame($response->getContent(), 'block');
392
    }
393
394
    public function testSetObjectFieldValueActionWithViolations()
395
    {
396
        $bar = new AdminControllerHelper_Bar();
397
        $object = new AdminControllerHelper_Foo();
398
        $object->setBar($bar);
399
        $request = new Request([
400
            'code' => 'sonata.post.admin',
401
            'objectId' => 42,
402
            'field' => 'bar.enabled',
403
            'value' => 1,
404
            'context' => 'list',
405
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
406
407
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
408
        $propertyAccessor = new PropertyAccessor();
409
410
        $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...
411
        $this->admin->getObject(42)->willReturn($object);
412
        $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...
413
        $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...
414
        $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...
415
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
416
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
417
        ]));
418
        $fieldDescription->getOption('editable')->willReturn(true);
419
        $fieldDescription->getType()->willReturn('boolean');
420
421
        $response = $this->controller->setObjectFieldValueAction($request);
422
423
        $this->assertEquals(400, $response->getStatusCode());
424
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
425
    }
426
427
    public function testRetrieveAutocompleteItemsActionNotGranted()
428
    {
429
        $this->expectException(AccessDeniedException::class);
430
431
        $request = new Request([
432
            'admin_code' => 'foo.admin',
433
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
434
435
        $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...
436
        $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...
437
438
        $this->controller->retrieveAutocompleteItemsAction($request);
439
    }
440
441
    public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
442
    {
443
        $this->expectException(AccessDeniedException::class);
444
        $this->expectExceptionMessage('Autocomplete list can`t be retrieved because the form element is disabled or read_only.');
445
446
        $object = new AdminControllerHelper_Foo();
447
        $request = new Request([
448
            'admin_code' => 'foo.admin',
449
            'field' => 'barField',
450
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
451
452
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
453
454
        $this->configureFormConfig('barField', true);
455
456
        $this->admin->getNewInstance()->willReturn($object);
457
        $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...
458
        $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...
459
        $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...
460
        $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...
461
462
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
463
        $fieldDescription->getName()->willReturn('barField');
464
465
        $this->controller->retrieveAutocompleteItemsAction($request);
466
    }
467
468
    public function testRetrieveAutocompleteItemsTooShortSearchString()
469
    {
470
        $object = new AdminControllerHelper_Foo();
471
        $request = new Request([
472
            'admin_code' => 'foo.admin',
473
            'field' => 'barField',
474
            'q' => 'so',
475
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
476
477
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
478
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
479
480
        $this->configureFormConfig('barField');
481
482
        $this->admin->getNewInstance()->willReturn($object);
483
        $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...
484
        $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...
485
        $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...
486
        $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...
487
        $targetAdmin->checkAccess('list')->willReturn(null);
488
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
489
        $fieldDescription->getName()->willReturn('barField');
490
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
491
492
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
493
494
        $this->isInstanceOf(Response::class, $response);
495
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
496
        $this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent());
497
    }
498
499
    public function testRetrieveAutocompleteItems()
500
    {
501
        $entity = new Foo();
502
        $request = new Request([
503
            'admin_code' => 'foo.admin',
504
            'field' => 'barField',
505
            'q' => 'sonata',
506
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
507
508
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
509
        $datagrid = $this->prophesize(DatagridInterface::class);
510
        $metadata = $this->prophesize(Metadata::class);
511
        $pager = $this->prophesize(Pager::class);
512
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
513
514
        $this->configureFormConfig('barField');
515
516
        $this->admin->getNewInstance()->willReturn($entity);
517
        $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...
518
        $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...
519
        $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...
520
        $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...
521
        $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...
522
        $targetAdmin->checkAccess('list')->willReturn(null);
523
        $targetAdmin->setPersistFilters(false)->willReturn(null);
524
        $targetAdmin->setFilterPersister(null)->willReturn(null);
525
        $targetAdmin->getDatagrid()->willReturn($datagrid->reveal());
526
        $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal());
527
        $metadata->getTitle()->willReturn('FOO');
528
        $datagrid->hasFilter('foo')->willReturn(true);
529
        $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled();
530
        $datagrid->setValue('_per_page', null, 10)->shouldBeCalled();
531
        $datagrid->setValue('_page', null, 1)->shouldBeCalled();
532
        $datagrid->buildPager()->willReturn(null);
533
        $datagrid->getPager()->willReturn($pager->reveal());
534
        $pager->getResults()->willReturn([$entity]);
535
        $pager->isLastPage()->willReturn(true);
536
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
537
        $fieldDescription->getName()->willReturn('barField');
538
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
539
540
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
541
542
        $this->isInstanceOf(Response::class, $response);
543
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
544
        $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent());
545
    }
546
547
    private function configureFormConfig($field, $disabled = false)
548
    {
549
        $form = $this->prophesize(Form::class);
550
        $formType = $this->prophesize(Form::class);
551
        $formConfig = $this->prophesize(FormConfigInterface::class);
552
553
        $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...
554
        $form->get($field)->willReturn($formType->reveal());
555
        $formType->getConfig()->willReturn($formConfig->reveal());
556
        $formConfig->getAttribute('disabled')->willReturn($disabled);
557
        $formConfig->getAttribute('property')->willReturn('foo');
558
        $formConfig->getAttribute('callback')->willReturn(null);
559
        $formConfig->getAttribute('minimum_input_length')->willReturn(3);
560
        $formConfig->getAttribute('items_per_page')->willReturn(10);
561
        $formConfig->getAttribute('req_param_name_page_number')->willReturn('_page');
562
        $formConfig->getAttribute('to_string_callback')->willReturn(null);
563
        $formConfig->getAttribute('target_admin_access_action')->willReturn('list');
564
    }
565
566
    private function configureFormRenderer()
567
    {
568
        $runtime = $this->prophesize(FormRenderer::class);
569
570
        // Remove the condition when dropping sf < 3.2
571
        if (!method_exists(AppVariable::class, 'getToken')) {
572
            $extension = $this->prophesize(FormExtension::class);
573
574
            $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...
575
            $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...
576
            $extension->renderer = $runtime->reveal();
577
578
            return $runtime;
579
        }
580
581
        // Remove the condition when dropping sf < 3.4
582
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
583
            $twigRuntime = $this->prophesize(TwigRenderer::class);
584
585
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
586
            $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...
587
588
            return $twigRuntime;
589
        }
590
591
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
592
593
        return $runtime;
594
    }
595
}
596