Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

testRetrieveFormFieldElementAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 9.296
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\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);
65
        $this->admin = $this->prophesize(AbstractAdmin::class);
66
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
67
        $this->pool = $this->prophesize(Pool::class);
68
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
69
        $this->helper = $this->prophesize(AdminHelper::class);
70
        $this->action = new RetrieveFormFieldElementAction(
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...
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));
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...
97
        $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...
98
        $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...
99
        $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...
100
        $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...
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);
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());
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...
126
            $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...
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();
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...
138
139
            return $twigRuntime;
140
        }
141
142
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
143
144
        return $runtime;
145
    }
146
}
147