Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

tests/Action/AppendFormFieldElementActionTest.php (6 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\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\Bridge\Twig\AppVariable;
26
use Symfony\Bridge\Twig\Command\DebugCommand;
27
use Symfony\Bridge\Twig\Extension\FormExtension;
28
use Symfony\Bridge\Twig\Form\TwigRenderer;
29
use Symfony\Component\Form\Form;
30
use Symfony\Component\Form\FormRenderer;
31
use Symfony\Component\Form\FormView;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\Validator\Validator\ValidatorInterface;
34
use Twig\Environment;
35
36
final class AppendFormFieldElementActionTest extends TestCase
37
{
38
    /**
39
     * @var Pool
40
     */
41
    private $pool;
42
43
    /**
44
     * @var Environment
45
     */
46
    private $twig;
47
48
    /**
49
     * @var GetShortObjectDescriptionAction
50
     */
51
    private $action;
52
53
    /**
54
     * @var AbstractAdmin
55
     */
56
    private $admin;
57
58
    /**
59
     * @var ValidatorInterface
60
     */
61
    private $validator;
62
63
    /**
64
     * @var AdminHelper
65
     */
66
    private $helper;
67
68
    protected function setUp(): void
69
    {
70
        $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...
71
        $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...
72
        $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...
73
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
74
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
75
        $this->validator = $this->prophesize(ValidatorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Symfo...idatorInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Symfony\Component...tor\ValidatorInterface> of property $validator.

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...
76
        $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...
77
        $this->action = new AppendFormFieldElementAction(
78
            $this->twig->reveal(),
79
            $this->pool->reveal(),
80
            $this->helper->reveal()
81
        );
82
    }
83
84
    public function testAppendFormFieldElementAction(): void
85
    {
86
        $object = new \stdClass();
87
        $request = new Request([
88
            'code' => 'sonata.post.admin',
89
            'objectId' => 42,
90
            'field' => 'enabled',
91
            'value' => 1,
92
            'context' => 'list',
93
        ], [], [], [], [], ['REQUEST_METHOD' => 'POST']);
94
95
        $modelManager = $this->prophesize(ModelManagerInterface::class);
96
        $formView = new FormView();
97
        $form = $this->prophesize(Form::class);
98
99
        $renderer = $this->configureFormRenderer();
100
101
        $this->admin->getObject(42)->willReturn($object);
102
        $this->admin->getClass()->willReturn(\get_class($object));
103
        $this->admin->setSubject($object)->shouldBeCalled();
104
        $this->admin->getFormTheme()->willReturn($formView);
105
        $this->helper->appendFormFieldElement($this->admin->reveal(), $object, null)->willReturn([
106
            $this->prophesize(FieldDescriptionInterface::class),
107
            $form->reveal(),
108
        ]);
109
        $this->helper->getChildFormView($formView, null)
110
            ->willReturn($formView);
111
        $modelManager->find(\get_class($object), 42)->willReturn($object);
112
        $form->createView()->willReturn($formView);
113
        $renderer->setTheme($formView, $formView)->shouldBeCalled();
114
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
115
116
        $action = $this->action;
117
        $response = $action($request);
118
119
        $this->isInstanceOf(Response::class, $response);
0 ignored issues
show
The call to AppendFormFieldElementActionTest::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...
120
        $this->assertSame($response->getContent(), 'block');
121
    }
122
123
    private function configureFormRenderer()
124
    {
125
        $runtime = $this->prophesize(FormRenderer::class);
126
127
        // Remove the condition when dropping sf < 3.2
128
        if (!method_exists(AppVariable::class, 'getToken')) {
129
            $extension = $this->prophesize(FormExtension::class);
130
131
            $this->twig->getExtension(FormExtension::class)->willReturn($extension->reveal());
132
            $extension->initRuntime($this->twig->reveal())->shouldBeCalled();
133
            $extension->renderer = $runtime->reveal();
134
135
            return $runtime;
136
        }
137
138
        // Remove the condition when dropping sf < 3.4
139
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
140
            $twigRuntime = $this->prophesize(TwigRenderer::class);
141
142
            $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal());
143
            $twigRuntime->setEnvironment($this->twig->reveal())->shouldBeCalled();
144
145
            return $twigRuntime;
146
        }
147
148
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
149
150
        return $runtime;
151
    }
152
}
153