Completed
Push — master ( f169c2...fc1f10 )
by Grégoire
12s
created

HelperControllerTest   C

Complexity

Total Complexity 17

Size/Duplication

Total Lines 467
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testRetrieveAutocompleteItemsActionNotGranted() 0 13 1
B testRetrieveAutocompleteItemsActionDisabledFormelememt() 0 26 1
B testRetrieveAutocompleteItemsTooShortSearchString() 0 30 1
A configureFormConfig() 0 18 1
B configureFormRenderer() 0 29 3
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 43 1
A testappendFormFieldElementAction() 0 49 1
B testRetrieveFormFieldElementAction() 0 38 1
B testSetObjectFieldValueActionWithViolations() 0 32 1
B testRetrieveAutocompleteItems() 0 46 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Controller;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Admin\AdminHelper;
20
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
21
use Sonata\AdminBundle\Admin\Pool;
22
use Sonata\AdminBundle\Controller\HelperController;
23
use Sonata\AdminBundle\Datagrid\DatagridInterface;
24
use Sonata\AdminBundle\Datagrid\Pager;
25
use Sonata\AdminBundle\Model\ModelManagerInterface;
26
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
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): void
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): void
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): void
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(): void
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(): void
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(): void
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(): void
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(): void
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(): void
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...
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...
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(): void
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
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
240
        $container = $this->prophesize(ContainerInterface::class);
241
242
        $this->admin->getObject(42)->willReturn($object);
243
        $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...
244
        $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...
245
        $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...
246
        $this->admin->update($object)->shouldBeCalled();
247
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
248
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
249
        $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...
250
        $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...
251
            new SonataAdminExtension($pool->reveal(), null, $translator->reveal(), $container->reveal())
252
        );
253
        $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...
254
        $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...
255
        $fieldDescription->getOption('editable')->willReturn(true);
256
        $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...
257
        $fieldDescription->getType()->willReturn('boolean');
258
        $fieldDescription->getTemplate()->willReturn(false);
259
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
260
        $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...
261
262
        $response = $this->controller->setObjectFieldValueAction($request);
263
264
        $this->assertEquals(200, $response->getStatusCode());
265
    }
266
267
    public function testappendFormFieldElementAction(): void
268
    {
269
        $object = new AdminControllerHelper_Foo();
270
        $associationObject = new AdminControllerHelper_Bar();
271
        $request = new Request([
272
            'code' => 'sonata.post.admin',
273
            'objectId' => 42,
274
            'field' => 'bar',
275
            'value' => 1,
276
            'context' => 'list',
277
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
278
279
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
280
        $modelManager = $this->prophesize(ModelManagerInterface::class);
281
        $template = $this->prophesize(Template::class);
282
        $translator = $this->prophesize(TranslatorInterface::class);
283
        $propertyAccessor = new PropertyAccessor();
284
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
285
        $container = $this->prophesize(ContainerInterface::class);
286
287
        $this->admin->getObject(42)->willReturn($object);
288
        $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...
289
        $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...
290
        $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...
291
        $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...
292
        $this->admin->update($object)->shouldBeCalled();
293
        $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
294
        $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
295
        $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...
296
        $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...
297
        $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...
298
            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...
299
        );
300
        $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...
301
        $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...
302
        $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...
303
        $fieldDescription->getType()->willReturn('choice');
304
        $fieldDescription->getOption('editable')->willReturn(true);
305
        $fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class);
306
        $fieldDescription->getTargetEntity()->willReturn(AdminControllerHelper_Bar::class);
307
        $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...
308
        $fieldDescription->getTemplate()->willReturn('field_template');
309
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
310
        $modelManager->find(get_class($associationObject), 1)->willReturn($associationObject);
311
312
        $response = $this->controller->setObjectFieldValueAction($request);
313
314
        $this->assertEquals(200, $response->getStatusCode());
315
    }
316
317
    public function testRetrieveFormFieldElementAction(): void
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
        $formBuilder = $this->prophesize(FormBuilder::class);
332
333
        $renderer = $this->configureFormRenderer();
334
335
        $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...
336
        $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...
337
        $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...
338
        $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...
339
        $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...
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->setData($object)->shouldBeCalled();
344
        $form->handleRequest($request)->shouldBeCalled();
345
        $form->createView()->willReturn($formView);
346
        $formBuilder->getForm()->willReturn($form->reveal());
347
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
348
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
349
350
        $response = $this->controller->retrieveFormFieldElementAction($request);
351
352
        $this->isInstanceOf(Response::class, $response);
353
        $this->assertSame($response->getContent(), 'block');
354
    }
355
356
    public function testSetObjectFieldValueActionWithViolations(): void
357
    {
358
        $bar = new AdminControllerHelper_Bar();
359
        $object = new AdminControllerHelper_Foo();
360
        $object->setBar($bar);
361
        $request = new Request([
362
            'code' => 'sonata.post.admin',
363
            'objectId' => 42,
364
            'field' => 'bar.enabled',
365
            'value' => 1,
366
            'context' => 'list',
367
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
368
369
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
370
        $propertyAccessor = new PropertyAccessor();
371
372
        $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...
373
        $this->admin->getObject(42)->willReturn($object);
374
        $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...
375
        $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...
376
        $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...
377
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
378
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
379
        ]));
380
        $fieldDescription->getOption('editable')->willReturn(true);
381
        $fieldDescription->getType()->willReturn('boolean');
382
383
        $response = $this->controller->setObjectFieldValueAction($request);
384
385
        $this->assertEquals(400, $response->getStatusCode());
386
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
387
    }
388
389
    /**
390
     * @exceptionMessage Invalid format
391
     */
392
    public function testRetrieveAutocompleteItemsActionNotGranted(): void
393
    {
394
        $this->expectException(AccessDeniedException::class);
395
396
        $request = new Request([
397
            'admin_code' => 'foo.admin',
398
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
399
400
        $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...
401
        $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...
402
403
        $this->controller->retrieveAutocompleteItemsAction($request);
404
    }
405
406
    public function testRetrieveAutocompleteItemsActionDisabledFormelememt(): void
407
    {
408
        $this->expectException(AccessDeniedException::class);
409
        $this->expectExceptionMessage('Autocomplete list can`t be retrieved because the form element is disabled or read_only.');
410
411
        $object = new AdminControllerHelper_Foo();
412
        $request = new Request([
413
            'admin_code' => 'foo.admin',
414
            'field' => 'barField',
415
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
416
417
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
418
419
        $this->configureFormConfig('barField', true);
420
421
        $this->admin->getNewInstance()->willReturn($object);
422
        $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...
423
        $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...
424
        $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...
425
        $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...
426
427
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
428
        $fieldDescription->getName()->willReturn('barField');
429
430
        $this->controller->retrieveAutocompleteItemsAction($request);
431
    }
432
433
    public function testRetrieveAutocompleteItemsTooShortSearchString(): void
434
    {
435
        $object = new AdminControllerHelper_Foo();
436
        $request = new Request([
437
            'admin_code' => 'foo.admin',
438
            'field' => 'barField',
439
            'q' => 'so',
440
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
441
442
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
443
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
444
445
        $this->configureFormConfig('barField');
446
447
        $this->admin->getNewInstance()->willReturn($object);
448
        $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...
449
        $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...
450
        $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...
451
        $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...
452
        $targetAdmin->checkAccess('list')->shouldBeCalled();
453
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
454
        $fieldDescription->getName()->willReturn('barField');
455
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
456
457
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
458
459
        $this->isInstanceOf(Response::class, $response);
460
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
461
        $this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent());
462
    }
463
464
    public function testRetrieveAutocompleteItems(): void
465
    {
466
        $entity = new Foo();
467
        $request = new Request([
468
            'admin_code' => 'foo.admin',
469
            'field' => 'barField',
470
            'q' => 'sonata',
471
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
472
473
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
474
        $datagrid = $this->prophesize(DatagridInterface::class);
475
        $metadata = $this->prophesize(Metadata::class);
476
        $pager = $this->prophesize(Pager::class);
477
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
478
479
        $this->configureFormConfig('barField');
480
481
        $this->admin->getNewInstance()->willReturn($entity);
482
        $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...
483
        $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...
484
        $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...
485
        $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...
486
        $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...
487
        $targetAdmin->checkAccess('list')->shouldBeCalled();
488
        $targetAdmin->setFilterPersister(null)->shouldBeCalled();
489
        $targetAdmin->getDatagrid()->willReturn($datagrid->reveal());
490
        $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal());
491
        $metadata->getTitle()->willReturn('FOO');
492
        $datagrid->hasFilter('foo')->willReturn(true);
493
        $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled();
494
        $datagrid->setValue('_per_page', null, 10)->shouldBeCalled();
495
        $datagrid->setValue('_page', null, 1)->shouldBeCalled();
496
        $datagrid->buildPager()->willReturn(null);
497
        $datagrid->getPager()->willReturn($pager->reveal());
498
        $pager->getResults()->willReturn([$entity]);
499
        $pager->isLastPage()->willReturn(true);
500
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
501
        $fieldDescription->getName()->willReturn('barField');
502
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
503
504
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
505
506
        $this->isInstanceOf(Response::class, $response);
507
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
508
        $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent());
509
    }
510
511
    private function configureFormConfig($field, $disabled = false): void
512
    {
513
        $form = $this->prophesize(Form::class);
514
        $formType = $this->prophesize(Form::class);
515
        $formConfig = $this->prophesize(FormConfigInterface::class);
516
517
        $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...
518
        $form->get($field)->willReturn($formType->reveal());
519
        $formType->getConfig()->willReturn($formConfig->reveal());
520
        $formConfig->getAttribute('disabled')->willReturn($disabled);
521
        $formConfig->getAttribute('property')->willReturn('foo');
522
        $formConfig->getAttribute('callback')->willReturn(null);
523
        $formConfig->getAttribute('minimum_input_length')->willReturn(3);
524
        $formConfig->getAttribute('items_per_page')->willReturn(10);
525
        $formConfig->getAttribute('req_param_name_page_number')->willReturn('_page');
526
        $formConfig->getAttribute('to_string_callback')->willReturn(null);
527
        $formConfig->getAttribute('target_admin_access_action')->willReturn('list');
528
    }
529
530
    private function configureFormRenderer()
531
    {
532
        $runtime = $this->prophesize(FormRenderer::class);
533
534
        // Remove the condition when dropping sf < 3.2
535
        if (!method_exists(AppVariable::class, 'getToken')) {
536
            $extension = $this->prophesize(FormExtension::class);
537
538
            $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...
539
            $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...
540
            $extension->renderer = $runtime->reveal();
541
542
            return $runtime;
543
        }
544
545
        // Remove the condition when dropping sf < 3.4
546
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
547
            $twigRuntime = $this->prophesize(TwigRenderer::class);
548
549
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
550
            $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...
551
552
            return $twigRuntime;
553
        }
554
555
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
556
557
        return $runtime;
558
    }
559
}
560