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\GetShortObjectDescriptionAction; |
||
19 | use Sonata\AdminBundle\Action\SetObjectFieldValueAction; |
||
20 | use Sonata\AdminBundle\Admin\AbstractAdmin; |
||
21 | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
||
22 | use Sonata\AdminBundle\Admin\Pool; |
||
23 | use Sonata\AdminBundle\Model\ModelManagerInterface; |
||
24 | use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
||
25 | use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension; |
||
26 | use Symfony\Bridge\Twig\AppVariable; |
||
27 | use Symfony\Bridge\Twig\Command\DebugCommand; |
||
28 | use Symfony\Bridge\Twig\Extension\FormExtension; |
||
29 | use Symfony\Bridge\Twig\Form\TwigRenderer; |
||
30 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
31 | use Symfony\Component\Form\FormRenderer; |
||
32 | use Symfony\Component\Form\FormRendererEngineInterface; |
||
33 | use Symfony\Component\HttpFoundation\Request; |
||
34 | use Symfony\Component\PropertyAccess\PropertyAccessor; |
||
35 | use Symfony\Component\Translation\TranslatorInterface; |
||
36 | use Symfony\Component\Validator\ConstraintViolation; |
||
37 | use Symfony\Component\Validator\ConstraintViolationList; |
||
38 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
||
39 | use Twig\Environment; |
||
40 | use Twig\Loader\ArrayLoader; |
||
41 | use Twig\RuntimeLoader\FactoryRuntimeLoader; |
||
42 | use Twig\Template; |
||
43 | |||
44 | final class SetObjectFieldValueActionTest extends TestCase |
||
45 | { |
||
46 | /** |
||
47 | * @var Pool |
||
48 | */ |
||
49 | private $pool; |
||
50 | |||
51 | /** |
||
52 | * @var Environment |
||
53 | */ |
||
54 | private $twig; |
||
55 | |||
56 | /** |
||
57 | * @var GetShortObjectDescriptionAction |
||
58 | */ |
||
59 | private $action; |
||
60 | |||
61 | /** |
||
62 | * @var AbstractAdmin |
||
63 | */ |
||
64 | private $admin; |
||
65 | |||
66 | /** |
||
67 | * @var ValidatorInterface |
||
68 | */ |
||
69 | private $validator; |
||
70 | |||
71 | protected function setUp(): void |
||
72 | { |
||
73 | $this->twig = new Environment(new ArrayLoader([ |
||
74 | 'admin_template' => 'renderedTemplate', |
||
75 | 'field_template' => 'renderedTemplate', |
||
76 | ])); |
||
77 | $this->pool = $this->prophesize(Pool::class); |
||
0 ignored issues
–
show
|
|||
78 | $this->admin = $this->prophesize(AbstractAdmin::class); |
||
0 ignored issues
–
show
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...
|
|||
79 | $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
||
80 | $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
||
81 | $this->validator = $this->prophesize(ValidatorInterface::class); |
||
0 ignored issues
–
show
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...
|
|||
82 | $this->action = new SetObjectFieldValueAction( |
||
83 | $this->twig, |
||
84 | $this->pool->reveal(), |
||
85 | $this->validator->reveal() |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | public function testSetObjectFieldValueAction(): void |
||
90 | { |
||
91 | $object = new Foo(); |
||
92 | $request = new Request([ |
||
93 | 'code' => 'sonata.post.admin', |
||
94 | 'objectId' => 42, |
||
95 | 'field' => 'enabled', |
||
96 | 'value' => 1, |
||
97 | 'context' => 'list', |
||
98 | ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
99 | |||
100 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
101 | $pool = $this->prophesize(Pool::class); |
||
102 | $template = $this->prophesize(Template::class); |
||
103 | $translator = $this->prophesize(TranslatorInterface::class); |
||
104 | $propertyAccessor = new PropertyAccessor(); |
||
105 | $templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
106 | $container = $this->prophesize(ContainerInterface::class); |
||
107 | |||
108 | $this->admin->getObject(42)->willReturn($object); |
||
109 | $this->admin->getCode()->willReturn('sonata.post.admin'); |
||
110 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
111 | $this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
||
112 | $this->admin->update($object)->shouldBeCalled(); |
||
113 | // NEXT_MAJOR: Remove this line |
||
114 | $this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
||
115 | $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
||
116 | $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
||
117 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
118 | $this->twig->addExtension(new SonataAdminExtension( |
||
119 | $pool->reveal(), |
||
120 | null, |
||
121 | $translator->reveal(), |
||
122 | $container->reveal() |
||
123 | )); |
||
124 | $fieldDescription->getOption('editable')->willReturn(true); |
||
125 | $fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
||
126 | $fieldDescription->getType()->willReturn('boolean'); |
||
127 | $fieldDescription->getTemplate()->willReturn(false); |
||
128 | $fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
||
129 | |||
130 | $this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
||
131 | $action = $this->action; |
||
132 | $response = $action($request); |
||
133 | |||
134 | $this->assertSame(200, $response->getStatusCode()); |
||
135 | } |
||
136 | |||
137 | public function testSetObjectFieldValueActionOnARelationField(): void |
||
138 | { |
||
139 | $object = new Baz(); |
||
140 | $associationObject = new Bar(); |
||
141 | $request = new Request([ |
||
142 | 'code' => 'sonata.post.admin', |
||
143 | 'objectId' => 42, |
||
144 | 'field' => 'bar', |
||
145 | 'value' => 1, |
||
146 | 'context' => 'list', |
||
147 | ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
148 | |||
149 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
150 | $modelManager = $this->prophesize(ModelManagerInterface::class); |
||
151 | $template = $this->prophesize(Template::class); |
||
152 | $translator = $this->prophesize(TranslatorInterface::class); |
||
153 | $propertyAccessor = new PropertyAccessor(); |
||
154 | $templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
155 | $container = $this->prophesize(ContainerInterface::class); |
||
156 | |||
157 | $this->admin->getObject(42)->willReturn($object); |
||
158 | $this->admin->getCode()->willReturn('sonata.post.admin'); |
||
159 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
160 | $this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal()); |
||
161 | $this->admin->getClass()->willReturn(\get_class($object)); |
||
162 | $this->admin->update($object)->shouldBeCalled(); |
||
163 | $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
||
164 | // NEXT_MAJOR: Remove this line |
||
165 | $this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
||
166 | $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
||
167 | $this->admin->getModelManager()->willReturn($modelManager->reveal()); |
||
168 | $this->twig->addExtension(new SonataAdminExtension( |
||
169 | $this->pool->reveal(), |
||
170 | null, |
||
171 | $translator->reveal(), |
||
172 | $container->reveal() |
||
173 | )); |
||
174 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
175 | $fieldDescription->getType()->willReturn('choice'); |
||
176 | $fieldDescription->getOption('editable')->willReturn(true); |
||
177 | $fieldDescription->getOption('class')->willReturn(Bar::class); |
||
178 | $fieldDescription->getTargetEntity()->willReturn(Bar::class); |
||
179 | $fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
||
180 | $fieldDescription->getTemplate()->willReturn('field_template'); |
||
181 | $fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
||
182 | $modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject); |
||
183 | |||
184 | $this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
||
185 | $action = $this->action; |
||
186 | $response = $action($request); |
||
187 | |||
188 | $this->assertSame(200, $response->getStatusCode()); |
||
189 | } |
||
190 | |||
191 | public function testSetObjectFieldValueActionWithViolations(): void |
||
192 | { |
||
193 | $bar = new Bar(); |
||
194 | $object = new Baz(); |
||
195 | $object->setBar($bar); |
||
196 | $request = new Request([ |
||
197 | 'code' => 'sonata.post.admin', |
||
198 | 'objectId' => 42, |
||
199 | 'field' => 'bar.enabled', |
||
200 | 'value' => 1, |
||
201 | 'context' => 'list', |
||
202 | ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
203 | |||
204 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
205 | $propertyAccessor = new PropertyAccessor(); |
||
206 | |||
207 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
208 | $this->admin->getObject(42)->willReturn($object); |
||
209 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
210 | $this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
||
211 | $this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
||
212 | new ConstraintViolation('error1', null, [], null, 'enabled', null), |
||
213 | new ConstraintViolation('error2', null, [], null, 'enabled', null), |
||
214 | ])); |
||
215 | $fieldDescription->getOption('editable')->willReturn(true); |
||
216 | $fieldDescription->getType()->willReturn('boolean'); |
||
217 | |||
218 | $action = $this->action; |
||
219 | $response = $action($request); |
||
220 | |||
221 | $this->assertSame(400, $response->getStatusCode()); |
||
222 | $this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
||
223 | } |
||
224 | |||
225 | private function configureFormRenderer() |
||
226 | { |
||
227 | $runtime = new FormRenderer($this->createMock( |
||
0 ignored issues
–
show
$this->createMock(\Symfo...anagerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject> , but the function expects a object<Symfony\Component...endererEngineInterface> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
228 | FormRendererEngineInterface::class, |
||
229 | CsrfTokenManagerInterface::class |
||
0 ignored issues
–
show
The call to
SetObjectFieldValueActionTest::createMock() has too many arguments starting with \Sonata\AdminBundle\Test...ManagerInterface::class .
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
Loading history...
|
|||
230 | )); |
||
231 | |||
232 | // Remove the condition when dropping sf < 3.2 |
||
233 | if (!method_exists(AppVariable::class, 'getToken')) { |
||
234 | $extension = new FormExtension(); |
||
235 | |||
236 | $this->twig->addExtension($extension); |
||
237 | $extension->renderer = $runtime; |
||
238 | |||
239 | return $runtime; |
||
240 | } |
||
241 | |||
242 | // Remove the condition when dropping sf < 3.4 |
||
243 | if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { |
||
244 | $twigRuntime = $this->prophesize(TwigRenderer::class); |
||
245 | |||
246 | $this->twig->addRuntimeLoader(new FactoryRuntimeLoader( |
||
247 | FormRenderer::class, |
||
248 | function () use ($runtime) { |
||
249 | return $runtime; |
||
250 | } |
||
251 | )); |
||
252 | $this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal()); |
||
253 | $twigRuntime->setEnvironment($this->twig)->shouldBeCalled(); |
||
254 | |||
255 | return $twigRuntime; |
||
256 | } |
||
257 | |||
258 | $this->twig->addRuntimeLoader(new FactoryRuntimeLoader( |
||
259 | FormRenderer::class, |
||
260 | function () use ($runtime) { |
||
261 | return $runtime; |
||
262 | } |
||
263 | )); |
||
264 | |||
265 | return $runtime; |
||
266 | } |
||
267 | } |
||
268 |
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..