Completed
Push — master ( a97f84...b5df1f )
by
unknown
11:58
created

Action/RetrieveFormFieldElementActionTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\GetShortObjectDescriptionAction;
19
use Sonata\AdminBundle\Action\RetrieveFormFieldElementAction;
20
use Sonata\AdminBundle\Admin\AbstractAdmin;
21
use Sonata\AdminBundle\Admin\AdminHelper;
22
use Sonata\AdminBundle\Admin\Pool;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Symfony\Bridge\Twig\AppVariable;
25
use Symfony\Bridge\Twig\Command\DebugCommand;
26
use Symfony\Bridge\Twig\Extension\FormExtension;
27
use Symfony\Bridge\Twig\Form\TwigRenderer;
28
use Symfony\Component\Form\Form;
29
use Symfony\Component\Form\FormBuilder;
30
use Symfony\Component\Form\FormRenderer;
31
use Symfony\Component\Form\FormView;
32
use Symfony\Component\HttpFoundation\Request;
33
use Twig\Environment;
34
35
final class RetrieveFormFieldElementActionTest extends TestCase
36
{
37
    /**
38
     * @var Pool
39
     */
40
    private $pool;
41
42
    /**
43
     * @var GetShortObjectDescriptionAction
44
     */
45
    private $action;
46
47
    /**
48
     * @var AbstractAdmin
49
     */
50
    private $admin;
51
52
    /**
53
     * @var Environment
54
     */
55
    private $twig;
56
57
    /**
58
     * @var AdminHelper
59
     */
60
    private $helper;
61
62
    protected function setUp(): void
63
    {
64
        $this->twig = $this->prophesize(Environment::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Twig\Environment::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Twig\Environment> of property $twig.

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...
65
        $this->admin = $this->prophesize(AbstractAdmin::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...n\AbstractAdmin::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\AbstractAdmin> of property $admin.

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...
66
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
67
        $this->pool = $this->prophesize(Pool::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...ndle\Admin\Pool::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\Pool> of property $pool.

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...
68
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
69
        $this->helper = $this->prophesize(AdminHelper::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...min\AdminHelper::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\AdminHelper> of property $helper.

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...
70
        $this->action = new RetrieveFormFieldElementAction(
71
            $this->twig->reveal(),
72
            $this->pool->reveal(),
73
            $this->helper->reveal()
74
        );
75
    }
76
77
    public function testRetrieveFormFieldElementAction(): void
78
    {
79
        $object = new \stdClass();
80
        $request = new Request([
81
            'code' => 'sonata.post.admin',
82
            'objectId' => 42,
83
            'field' => 'enabled',
84
            'value' => 1,
85
            'context' => 'list',
86
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
87
88
        $modelManager = $this->prophesize(ModelManagerInterface::class);
89
        $formView = new FormView();
90
        $form = $this->prophesize(Form::class);
91
        $formBuilder = $this->prophesize(FormBuilder::class);
92
93
        $renderer = $this->configureFormRenderer();
94
95
        $this->admin->getObject(42)->willReturn($object);
96
        $this->admin->getClass()->willReturn(\get_class($object));
97
        $this->admin->setSubject($object)->shouldBeCalled();
98
        $this->admin->getFormTheme()->willReturn($formView);
99
        $this->admin->getFormBuilder()->willReturn($formBuilder->reveal());
100
        $this->helper->getChildFormView($formView, null)
101
            ->willReturn($formView);
102
        $modelManager->find(\get_class($object), 42)->willReturn($object);
103
        $form->setData($object)->shouldBeCalled();
104
        $form->handleRequest($request)->shouldBeCalled();
105
        $form->createView()->willReturn($formView);
106
        $formBuilder->getForm()->willReturn($form->reveal());
107
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
108
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
109
110
        $action = $this->action;
111
        $response = $action($request);
112
113
        $this->isInstanceOf(Response::class, $response);
0 ignored issues
show
The call to RetrieveFormFieldElementActionTest::isInstanceOf() has too many arguments starting with $response.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
        $this->assertSame($response->getContent(), 'block');
115
    }
116
117
    private function configureFormRenderer()
118
    {
119
        $runtime = $this->prophesize(FormRenderer::class);
120
121
        // Remove the condition when dropping sf < 3.2
122
        if (!method_exists(AppVariable::class, 'getToken')) {
123
            $extension = $this->prophesize(FormExtension::class);
124
125
            $this->twig->getExtension(FormExtension::class)->willReturn($extension->reveal());
126
            $extension->initRuntime($this->twig->reveal())->shouldBeCalled();
127
            $extension->renderer = $runtime->reveal();
128
129
            return $runtime;
130
        }
131
132
        // Remove the condition when dropping sf < 3.4
133
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
134
            $twigRuntime = $this->prophesize(TwigRenderer::class);
135
136
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
137
            $twigRuntime->setEnvironment($this->twig->reveal())->shouldBeCalled();
138
139
            return $twigRuntime;
140
        }
141
142
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
143
144
        return $runtime;
145
    }
146
}
147