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