configureFormRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Action;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Action\AppendFormFieldElementAction;
19
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
20
use Sonata\AdminBundle\Admin\AbstractAdmin;
21
use Sonata\AdminBundle\Admin\AdminHelper;
22
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
23
use Sonata\AdminBundle\Admin\Pool;
24
use Sonata\AdminBundle\Model\ModelManagerInterface;
25
use Symfony\Component\Form\Form;
26
use Symfony\Component\Form\FormRenderer;
27
use Symfony\Component\Form\FormView;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\Validator\Validator\ValidatorInterface;
31
use Twig\Environment;
32
33
final class AppendFormFieldElementActionTest extends TestCase
34
{
35
    /**
36
     * @var Pool
37
     */
38
    private $pool;
39
40
    /**
41
     * @var Environment
42
     */
43
    private $twig;
44
45
    /**
46
     * @var GetShortObjectDescriptionAction
47
     */
48
    private $action;
49
50
    /**
51
     * @var AbstractAdmin
52
     */
53
    private $admin;
54
55
    /**
56
     * @var ValidatorInterface
57
     */
58
    private $validator;
59
60
    /**
61
     * @var AdminHelper
62
     */
63
    private $helper;
64
65
    protected function setUp(): void
66
    {
67
        $this->twig = $this->prophesize(Environment::class);
68
        $this->pool = $this->prophesize(Pool::class);
69
        $this->admin = $this->prophesize(AbstractAdmin::class);
70
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
71
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
72
        $this->validator = $this->prophesize(ValidatorInterface::class);
73
        $this->helper = $this->prophesize(AdminHelper::class);
74
        $this->action = new AppendFormFieldElementAction(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Sonata\AdminBundle\...this->helper->reveal()) of type object<Sonata\AdminBundl...FormFieldElementAction> is incompatible with the declared type object<Sonata\AdminBundl...bjectDescriptionAction> of property $action.

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

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

Loading history...
75
            $this->twig->reveal(),
76
            $this->pool->reveal(),
77
            $this->helper->reveal()
78
        );
79
    }
80
81
    public function testAppendFormFieldElementAction(): void
82
    {
83
        $object = new \stdClass();
84
        $request = new Request([
85
            'code' => 'sonata.post.admin',
86
            'objectId' => 42,
87
            'elementId' => 'element_42',
88
            'field' => 'enabled',
89
            'value' => 1,
90
            'context' => 'list',
91
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST]);
92
93
        $modelManager = $this->prophesize(ModelManagerInterface::class);
94
        $formView = new FormView();
95
        $form = $this->prophesize(Form::class);
96
        $renderer = $this->configureFormRenderer();
97
98
        $this->admin->getObject(42)->willReturn($object);
99
        $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...
100
        $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...
101
        $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<integer,string>).

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

Loading history...
102
        $this->helper->appendFormFieldElement($this->admin->reveal(), $object, 'element_42')->willReturn([
0 ignored issues
show
Documentation introduced by
$object is of type object<stdClass>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method willReturn cannot be called on $this->helper->appendFor... $object, 'element_42') (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...
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...
103
            $this->prophesize(FieldDescriptionInterface::class),
104
            $form->reveal(),
105
        ]);
106
        $this->helper->getChildFormView($formView, 'element_42')
107
            ->willReturn($formView);
108
        $modelManager->find(\get_class($object), 42)->willReturn($object);
109
        $form->createView()->willReturn($formView);
110
        $renderer->setTheme($formView, [$formView])->shouldBeCalled();
111
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
112
113
        $response = ($this->action)($request);
114
115
        $this->assertInstanceOf(Response::class, $response);
116
        $this->assertSame($response->getContent(), 'block');
117
    }
118
119
    private function configureFormRenderer()
120
    {
121
        $runtime = $this->prophesize(FormRenderer::class);
122
123
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
124
125
        return $runtime;
126
    }
127
}
128