Completed
Push — 3.x ( de79cd...799626 )
by Grégoire
04:03
created

testGetShortObjectDescriptionActionEmptyObjectId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Controller;
13
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Admin\AdminHelper;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Controller\HelperController;
21
use Sonata\AdminBundle\Datagrid\DatagridInterface;
22
use Sonata\AdminBundle\Datagrid\Pager;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
25
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
26
use Sonata\CoreBundle\Model\Metadata;
27
use Symfony\Bridge\Twig\AppVariable;
28
use Symfony\Bridge\Twig\Command\DebugCommand;
29
use Symfony\Bridge\Twig\Extension\FormExtension;
30
use Symfony\Bridge\Twig\Form\TwigRenderer;
31
use Symfony\Component\Form\Form;
32
use Symfony\Component\Form\FormBuilder;
33
use Symfony\Component\Form\FormConfigInterface;
34
use Symfony\Component\Form\FormRenderer;
35
use Symfony\Component\Form\FormView;
36
use Symfony\Component\HttpFoundation\Request;
37
use Symfony\Component\HttpFoundation\Response;
38
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
39
use Symfony\Component\PropertyAccess\PropertyAccessor;
40
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
41
use Symfony\Component\Translation\TranslatorInterface;
42
use Symfony\Component\Validator\ConstraintViolation;
43
use Symfony\Component\Validator\ConstraintViolationList;
44
use Symfony\Component\Validator\Validator\ValidatorInterface;
45
use Twig\Environment;
46
use Twig\Template;
47
use Twig\TemplateWrapper;
48
49
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...
50
{
51
    private $bar;
52
53
    public function getAdminTitle()
54
    {
55
        return 'foo';
56
    }
57
58
    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...
59
    {
60
    }
61
62
    public function setBar(AdminControllerHelper_Bar $bar)
63
    {
64
        $this->bar = $bar;
65
    }
66
67
    public function getBar()
68
    {
69
        return $this->bar;
70
    }
71
}
72
73
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...
74
{
75
    public function getAdminTitle()
76
    {
77
        return 'bar';
78
    }
79
80
    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...
81
    {
82
    }
83
84
    public function getEnabled()
85
    {
86
    }
87
}
88
89
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...
90
{
91
    /**
92
     * @var Pool
93
     */
94
    private $pool;
95
96
    /**
97
     * @var Environment
98
     */
99
    private $twig;
100
101
    /**
102
     * @var AdminHelper
103
     */
104
    private $helper;
105
106
    /**
107
     * @var ValidatorInterface
108
     */
109
    private $validator;
110
111
    /**
112
     * @var AbstractAdmin
113
     */
114
    private $admin;
115
116
    /**
117
     * @var HelperController
118
     */
119
    private $controller;
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function setUp()
125
    {
126
        $this->pool = $this->prophesize(Pool::class);
127
        $this->twig = $this->prophesize(Environment::class);
128
        $this->helper = $this->prophesize(AdminHelper::class);
129
        $this->validator = $this->prophesize(ValidatorInterface::class);
130
        $this->admin = $this->prophesize(AbstractAdmin::class);
131
132
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
133
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
134
135
        $this->controller = new HelperController(
136
            $this->twig->reveal(),
137
            $this->pool->reveal(),
138
            $this->helper->reveal(),
139
            $this->validator->reveal()
140
        );
141
    }
142
143
    public function testGetShortObjectDescriptionActionInvalidAdmin()
144
    {
145
        $this->expectException(NotFoundHttpException::class);
146
147
        $request = new Request([
148
            'code' => 'sonata.post.admin',
149
            'objectId' => 42,
150
            'uniqid' => 'asdasd123',
151
        ]);
152
153
        $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...
154
        $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...
155
156
        $this->controller->getShortObjectDescriptionAction($request);
157
    }
158
159
    public function testGetShortObjectDescriptionActionObjectDoesNotExist()
160
    {
161
        $this->expectException(\RuntimeException::class);
162
        $this->expectExceptionMessage('Invalid format');
163
164
        $request = new Request([
165
            'code' => 'sonata.post.admin',
166
            'objectId' => 42,
167
            'uniqid' => 'asdasd123',
168
        ]);
169
170
        $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...
171
        $this->admin->getObject(42)->willReturn(false);
172
173
        $this->controller->getShortObjectDescriptionAction($request);
174
    }
175
176
    public function testGetShortObjectDescriptionActionEmptyObjectId()
177
    {
178
        $request = new Request([
179
            'code' => 'sonata.post.admin',
180
            'objectId' => '',
181
            'uniqid' => 'asdasd123',
182
            '_format' => 'html',
183
        ]);
184
185
        $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...
186
        $this->admin->getObject(null)->willReturn(false);
187
188
        $response = $this->controller->getShortObjectDescriptionAction($request);
189
190
        $this->assertInstanceOf(Response::class, $response);
191
    }
192
193
    public function testGetShortObjectDescriptionActionObject()
194
    {
195
        $request = new Request([
196
            'code' => 'sonata.post.admin',
197
            'objectId' => 42,
198
            'uniqid' => 'asdasd123',
199
            '_format' => 'html',
200
        ]);
201
        $object = new AdminControllerHelper_Foo();
202
203
        $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...
204
        $this->admin->getObject(42)->willReturn($object);
205
        $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...
206
        $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...
207
        $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...
208
            '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...
209
            'description' => 'bar',
210
            'object' => $object,
211
            'link_parameters' => [],
212
        ])->willReturn('renderedTemplate');
213
214
        $response = $this->controller->getShortObjectDescriptionAction($request);
215
216
        $this->assertSame('renderedTemplate', $response->getContent());
217
    }
218
219
    public function testSetObjectFieldValueAction()
220
    {
221
        $object = new AdminControllerHelper_Foo();
222
        $request = new Request([
223
            'code' => 'sonata.post.admin',
224
            'objectId' => 42,
225
            'field' => 'enabled',
226
            'value' => 1,
227
            'context' => 'list',
228
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
229
230
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
231
        $pool = $this->prophesize(Pool::class);
232
        $template = $this->prophesize(Template::class);
233
        $translator = $this->prophesize(TranslatorInterface::class);
234
        $propertyAccessor = new PropertyAccessor();
235
236
        $this->admin->getObject(42)->willReturn($object);
237
        $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...
238
        $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...
239
        $this->admin->update($object)->shouldBeCalled();
240
        $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...
241
        $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...
242
        $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...
243
            new SonataAdminExtension($pool->reveal(), null, $translator->reveal())
244
        );
245
        $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...
246
        $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...
247
        $fieldDescription->getOption('editable')->willReturn(true);
248
        $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...
249
        $fieldDescription->getType()->willReturn('boolean');
250
        $fieldDescription->getTemplate()->willReturn(false);
251
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
252
        $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...
253
254
        $response = $this->controller->setObjectFieldValueAction($request);
255
256
        $this->assertEquals(200, $response->getStatusCode());
257
    }
258
259
    public function testSetObjectFieldValueActionOnARelationField()
260
    {
261
        $object = new AdminControllerHelper_Foo();
262
        $associationObject = new AdminControllerHelper_Bar();
263
        $request = new Request([
264
            'code' => 'sonata.post.admin',
265
            'objectId' => 42,
266
            'field' => 'bar',
267
            'value' => 1,
268
            'context' => 'list',
269
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
270
271
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
272
        $modelManager = $this->prophesize(ModelManagerInterface::class);
273
        $template = $this->prophesize(Template::class);
274
        $translator = $this->prophesize(TranslatorInterface::class);
275
        $propertyAccessor = new PropertyAccessor();
276
277
        $this->admin->getObject(42)->willReturn($object);
278
        $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...
279
        $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...
280
        $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...
281
        $this->admin->update($object)->shouldBeCalled();
282
        $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...
283
        $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...
284
        $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...
285
        $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...
286
            new SonataAdminExtension($this->pool->reveal(), null, $translator->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...
287
        );
288
        $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...
289
        $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...
290
        $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...
291
        $fieldDescription->getType()->willReturn('choice');
292
        $fieldDescription->getOption('editable')->willReturn(true);
293
        $fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class);
294
        $fieldDescription->getTargetEntity()->willReturn(AdminControllerHelper_Bar::class);
295
        $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...
296
        $fieldDescription->getTemplate()->willReturn('field_template');
297
        $fieldDescription->getValue(Argument::cetera())->willReturn('some value');
298
        $modelManager->find(get_class($associationObject), 1)->willReturn($associationObject);
299
300
        $response = $this->controller->setObjectFieldValueAction($request);
301
302
        $this->assertEquals(200, $response->getStatusCode());
303
    }
304
305
    public function testAppendFormFieldElementAction()
306
    {
307
        $object = new AdminControllerHelper_Foo();
308
        $request = new Request([
309
            'code' => 'sonata.post.admin',
310
            'objectId' => 42,
311
            'field' => 'enabled',
312
            'value' => 1,
313
            'context' => 'list',
314
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
315
316
        $modelManager = $this->prophesize(ModelManagerInterface::class);
317
        $formView = new FormView();
318
        $form = $this->prophesize(Form::class);
319
320
        $renderer = $this->configureFormRenderer();
321
322
        $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...
323
        $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...
324
        $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...
325
        $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...
326
        $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...
327
            $this->prophesize(FieldDescriptionInterface::class),
328
            $form->reveal(),
329
        ]);
330
        $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...
331
            ->willReturn($formView);
332
        $modelManager->find(get_class($object), 42)->willReturn($object);
333
        $form->createView()->willReturn($formView);
334
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
335
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
336
337
        $response = $this->controller->appendFormFieldElementAction($request);
338
339
        $this->isInstanceOf(Response::class, $response);
340
        $this->assertSame($response->getContent(), 'block');
341
    }
342
343
    public function testRetrieveFormFieldElementAction()
344
    {
345
        $object = new AdminControllerHelper_Foo();
346
        $request = new Request([
347
            'code' => 'sonata.post.admin',
348
            'objectId' => 42,
349
            'field' => 'enabled',
350
            'value' => 1,
351
            'context' => 'list',
352
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
353
354
        $modelManager = $this->prophesize(ModelManagerInterface::class);
355
        $formView = new FormView();
356
        $form = $this->prophesize(Form::class);
357
        $formBuilder = $this->prophesize(FormBuilder::class);
358
359
        $renderer = $this->configureFormRenderer();
360
361
        $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...
362
        $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...
363
        $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...
364
        $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...
365
        $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...
366
        $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...
367
            ->willReturn($formView);
368
        $modelManager->find(get_class($object), 42)->willReturn($object);
369
        $form->setData($object)->shouldBeCalled();
370
        $form->handleRequest($request)->shouldBeCalled();
371
        $form->createView()->willReturn($formView);
372
        $formBuilder->getForm()->willReturn($form->reveal());
373
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
374
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
375
376
        $response = $this->controller->retrieveFormFieldElementAction($request);
377
378
        $this->isInstanceOf(Response::class, $response);
379
        $this->assertSame($response->getContent(), 'block');
380
    }
381
382
    public function testSetObjectFieldValueActionWithViolations()
383
    {
384
        $bar = new AdminControllerHelper_Bar();
385
        $object = new AdminControllerHelper_Foo();
386
        $object->setBar($bar);
387
        $request = new Request([
388
            'code' => 'sonata.post.admin',
389
            'objectId' => 42,
390
            'field' => 'bar.enabled',
391
            'value' => 1,
392
            'context' => 'list',
393
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
394
395
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
396
        $propertyAccessor = new PropertyAccessor();
397
398
        $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...
399
        $this->admin->getObject(42)->willReturn($object);
400
        $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...
401
        $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...
402
        $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...
403
            new ConstraintViolation('error1', null, [], null, 'enabled', null),
404
            new ConstraintViolation('error2', null, [], null, 'enabled', null),
405
        ]));
406
        $fieldDescription->getOption('editable')->willReturn(true);
407
        $fieldDescription->getType()->willReturn('boolean');
408
409
        $response = $this->controller->setObjectFieldValueAction($request);
410
411
        $this->assertEquals(400, $response->getStatusCode());
412
        $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
413
    }
414
415
    public function testRetrieveAutocompleteItemsActionNotGranted()
416
    {
417
        $this->expectException(AccessDeniedException::class);
418
419
        $request = new Request([
420
            'admin_code' => 'foo.admin',
421
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
422
423
        $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...
424
        $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...
425
426
        $this->controller->retrieveAutocompleteItemsAction($request);
427
    }
428
429
    public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
430
    {
431
        $this->expectException(AccessDeniedException::class);
432
        $this->expectExceptionMessage('Autocomplete list can`t be retrieved because the form element is disabled or read_only.');
433
434
        $object = new AdminControllerHelper_Foo();
435
        $request = new Request([
436
            'admin_code' => 'foo.admin',
437
            'field' => 'barField',
438
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
439
440
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
441
442
        $this->configureFormConfig('barField', true);
443
444
        $this->admin->getNewInstance()->willReturn($object);
445
        $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...
446
        $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...
447
        $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...
448
        $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...
449
450
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
451
        $fieldDescription->getName()->willReturn('barField');
452
453
        $this->controller->retrieveAutocompleteItemsAction($request);
454
    }
455
456
    public function testRetrieveAutocompleteItemsTooShortSearchString()
457
    {
458
        $object = new AdminControllerHelper_Foo();
459
        $request = new Request([
460
            'admin_code' => 'foo.admin',
461
            'field' => 'barField',
462
            'q' => 'so',
463
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
464
465
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
466
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
467
468
        $this->configureFormConfig('barField');
469
470
        $this->admin->getNewInstance()->willReturn($object);
471
        $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...
472
        $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...
473
        $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...
474
        $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...
475
        $targetAdmin->checkAccess('list')->willReturn(null);
476
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
477
        $fieldDescription->getName()->willReturn('barField');
478
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
479
480
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
481
482
        $this->isInstanceOf(Response::class, $response);
483
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
484
        $this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent());
485
    }
486
487
    public function testRetrieveAutocompleteItems()
488
    {
489
        $entity = new Foo();
490
        $request = new Request([
491
            'admin_code' => 'foo.admin',
492
            'field' => 'barField',
493
            'q' => 'sonata',
494
        ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);
495
496
        $targetAdmin = $this->prophesize(AbstractAdmin::class);
497
        $datagrid = $this->prophesize(DatagridInterface::class);
498
        $metadata = $this->prophesize(Metadata::class);
499
        $pager = $this->prophesize(Pager::class);
500
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
501
502
        $this->configureFormConfig('barField');
503
504
        $this->admin->getNewInstance()->willReturn($entity);
505
        $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...
506
        $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...
507
        $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...
508
        $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...
509
        $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...
510
        $targetAdmin->checkAccess('list')->willReturn(null);
511
        $targetAdmin->setPersistFilters(false)->willReturn(null);
512
        $targetAdmin->setFilterPersister(null)->willReturn(null);
513
        $targetAdmin->getDatagrid()->willReturn($datagrid->reveal());
514
        $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal());
515
        $metadata->getTitle()->willReturn('FOO');
516
        $datagrid->hasFilter('foo')->willReturn(true);
517
        $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled();
518
        $datagrid->setValue('_per_page', null, 10)->shouldBeCalled();
519
        $datagrid->setValue('_page', null, 1)->shouldBeCalled();
520
        $datagrid->buildPager()->willReturn(null);
521
        $datagrid->getPager()->willReturn($pager->reveal());
522
        $pager->getResults()->willReturn([$entity]);
523
        $pager->isLastPage()->willReturn(true);
524
        $fieldDescription->getTargetEntity()->willReturn(Foo::class);
525
        $fieldDescription->getName()->willReturn('barField');
526
        $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal());
527
528
        $response = $this->controller->retrieveAutocompleteItemsAction($request);
529
530
        $this->isInstanceOf(Response::class, $response);
531
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
532
        $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent());
533
    }
534
535
    private function configureFormConfig($field, $disabled = false)
536
    {
537
        $form = $this->prophesize(Form::class);
538
        $formType = $this->prophesize(Form::class);
539
        $formConfig = $this->prophesize(FormConfigInterface::class);
540
541
        $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...
542
        $form->get($field)->willReturn($formType->reveal());
543
        $formType->getConfig()->willReturn($formConfig->reveal());
544
        $formConfig->getAttribute('disabled')->willReturn($disabled);
545
        $formConfig->getAttribute('property')->willReturn('foo');
546
        $formConfig->getAttribute('callback')->willReturn(null);
547
        $formConfig->getAttribute('minimum_input_length')->willReturn(3);
548
        $formConfig->getAttribute('items_per_page')->willReturn(10);
549
        $formConfig->getAttribute('req_param_name_page_number')->willReturn('_page');
550
        $formConfig->getAttribute('to_string_callback')->willReturn(null);
551
        $formConfig->getAttribute('target_admin_access_action')->willReturn('list');
552
    }
553
554
    private function configureFormRenderer()
555
    {
556
        $runtime = $this->prophesize(FormRenderer::class);
557
558
        // Remove the condition when dropping sf < 3.2
559
        if (!method_exists(AppVariable::class, 'getToken')) {
560
            $extension = $this->prophesize(FormExtension::class);
561
562
            $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...
563
            $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...
564
            $extension->renderer = $runtime->reveal();
565
566
            return $runtime;
567
        }
568
569
        // Remove the condition when dropping sf < 3.4
570
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
571
            $twigRuntime = $this->prophesize(TwigRenderer::class);
572
573
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
574
            $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...
575
576
            return $twigRuntime;
577
        }
578
579
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
580
581
        return $runtime;
582
    }
583
}
584