RetrieveFormFieldElementActionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
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\Component\Form\Form;
25
use Symfony\Component\Form\FormBuilder;
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 Twig\Environment;
31
32
final class RetrieveFormFieldElementActionTest extends TestCase
33
{
34
    /**
35
     * @var Pool
36
     */
37
    private $pool;
38
39
    /**
40
     * @var GetShortObjectDescriptionAction
41
     */
42
    private $action;
43
44
    /**
45
     * @var AbstractAdmin
46
     */
47
    private $admin;
48
49
    /**
50
     * @var Environment
51
     */
52
    private $twig;
53
54
    /**
55
     * @var AdminHelper
56
     */
57
    private $helper;
58
59
    protected function setUp(): void
60
    {
61
        $this->twig = $this->prophesize(Environment::class);
62
        $this->admin = $this->prophesize(AbstractAdmin::class);
63
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
64
        $this->pool = $this->prophesize(Pool::class);
65
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
66
        $this->helper = $this->prophesize(AdminHelper::class);
67
        $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...
68
            $this->twig->reveal(),
69
            $this->pool->reveal(),
70
            $this->helper->reveal()
71
        );
72
    }
73
74
    public function testRetrieveFormFieldElementAction(): void
75
    {
76
        $object = new \stdClass();
77
        $request = new Request([
78
            'code' => 'sonata.post.admin',
79
            'objectId' => 42,
80
            'elementId' => 'element_42',
81
            'field' => 'enabled',
82
            'value' => 1,
83
            'context' => 'list',
84
        ], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST]);
85
86
        $modelManager = $this->prophesize(ModelManagerInterface::class);
87
        $formView = new FormView();
88
        $form = $this->prophesize(Form::class);
89
        $formBuilder = $this->prophesize(FormBuilder::class);
90
91
        $renderer = $this->configureFormRenderer();
92
93
        $this->admin->getObject(42)->willReturn($object);
94
        $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...
95
        $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...
96
        $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...
97
        $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...
98
        $this->helper->getChildFormView($formView, 'element_42')
99
            ->willReturn($formView);
100
        $modelManager->find(\get_class($object), 42)->willReturn($object);
101
        $form->setData($object)->shouldBeCalled();
102
        $form->handleRequest($request)->shouldBeCalled();
103
        $form->createView()->willReturn($formView);
104
        $formBuilder->getForm()->willReturn($form->reveal());
105
        $renderer->setTheme($formView, [$formView])->shouldBeCalled();
106
        $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
107
108
        $response = ($this->action)($request);
109
110
        $this->assertInstanceOf(Response::class, $response);
111
        $this->assertSame($response->getContent(), 'block');
112
    }
113
114
    private function configureFormRenderer()
115
    {
116
        $runtime = $this->prophesize(FormRenderer::class);
117
118
        $this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
119
120
        return $runtime;
121
    }
122
}
123