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

testAppendFormFieldElementAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.312
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\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);
71
        $this->pool = $this->prophesize(Pool::class);
72
        $this->admin = $this->prophesize(AbstractAdmin::class);
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);
76
        $this->helper = $this->prophesize(AdminHelper::class);
77
        $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...
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));
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...
103
        $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...
104
        $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...
105
        $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...
106
            $this->prophesize(FieldDescriptionInterface::class),
107
            $form->reveal(),
108
        ]);
109
        $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...
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);
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());
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...
132
            $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...
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();
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...
144
145
            return $twigRuntime;
146
        }
147
148
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
149
150
        return $runtime;
151
    }
152
}
153