|
1 | 1 | package fr.adrienbrault.idea.symfony2plugin.tests.form.util; |
2 | 2 |
|
3 | 3 | import com.jetbrains.php.lang.psi.PhpPsiElementFactory; |
| 4 | +import com.jetbrains.php.lang.psi.elements.Method; |
4 | 5 | import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 6 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
5 | 7 | import com.jetbrains.php.lang.psi.elements.impl.ClassConstantReferenceImpl; |
6 | 8 | import com.jetbrains.php.lang.psi.elements.impl.PhpTypedElementImpl; |
7 | 9 | import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl; |
8 | 10 | import fr.adrienbrault.idea.symfony2plugin.form.dict.FormTypeClass; |
9 | 11 | import fr.adrienbrault.idea.symfony2plugin.form.util.FormUtil; |
10 | 12 | import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase; |
11 | 13 |
|
12 | | -import java.util.ArrayList; |
13 | | -import java.util.Arrays; |
14 | | -import java.util.Collection; |
15 | | -import java.util.Map; |
| 14 | +import java.util.*; |
| 15 | +import java.util.stream.Collectors; |
16 | 16 |
|
17 | 17 | /** |
18 | 18 | * @author Daniel Espendiller <daniel@espendiller.net> |
@@ -288,4 +288,63 @@ public void testGetFormExtendedTypesAsYield() { |
288 | 288 |
|
289 | 289 | assertContainsElements(FormUtil.getFormExtendedType(phpClass), "Foobar", "test"); |
290 | 290 | } |
| 291 | + |
| 292 | + public void testThatFormFieldsOfBuildFormForGetFormBuilderTypes() { |
| 293 | + Method method = PhpPsiElementFactory.createFromText(getProject(), Method.class, "<?php\n" + |
| 294 | + "class Foobar\n" + |
| 295 | + "{\n" + |
| 296 | + " public public function buildForm(\\Symfony\\Component\\Form\\FormBuilderInterface $builder, array $options)\n" + |
| 297 | + " {\n" + |
| 298 | + " $builder\n" + |
| 299 | + " ->add('task', TextType::class)\n" + |
| 300 | + " ->add('dueDate', DateType::class)\n" + |
| 301 | + " ->add('save', SubmitType::class)\n" + |
| 302 | + " ;" + |
| 303 | + " }\n" + |
| 304 | + "}\n" |
| 305 | + ); |
| 306 | + |
| 307 | + Set<String> collect = Arrays.stream(FormUtil.getFormBuilderTypes(method)) |
| 308 | + .map(methodReference -> ((StringLiteralExpression) Objects.requireNonNull(methodReference.getParameter(0))).getContents()) |
| 309 | + .collect(Collectors.toSet()); |
| 310 | + |
| 311 | + assertContainsElements(collect, "task"); |
| 312 | + assertContainsElements(collect, "dueDate"); |
| 313 | + assertContainsElements(collect, "save"); |
| 314 | + } |
| 315 | + |
| 316 | + public void testThatFormFieldsOfBuildFormForGetFormBuilderTypesForCodeFlow() { |
| 317 | + Method method = PhpPsiElementFactory.createFromText(getProject(), Method.class, "<?php\n" + |
| 318 | + "class Foobar\n" + |
| 319 | + "{\n" + |
| 320 | + " public function foo(array $options, \\Symfony\\Component\\Form\\FormBuilderInterface $builder2)\n" + |
| 321 | + " {\n" + |
| 322 | + " $builder2->add('builder2');\n" + |
| 323 | + " $builder->addEventListener('foo', function (\\Symfony\\Component\\Form\\FormEvent $event) {\n" + |
| 324 | + " $form = $event->getForm();\n" + |
| 325 | + " $form->add('email2');\n" + |
| 326 | + " });" + |
| 327 | + " }\n" + |
| 328 | + " public public function buildForm(\\Symfony\\Component\\Form\\FormBuilderInterface $builder, array $options)\n" + |
| 329 | + " {\n" + |
| 330 | + " $builder->addEventListener('foo', function (\\Symfony\\Component\\Form\\FormEvent $event) {\n" + |
| 331 | + " $form = $event->getForm();\n" + |
| 332 | + " $form->add('email');\n" + |
| 333 | + " });" + |
| 334 | + " $builder->add('task', TextType::class);\n" + |
| 335 | + " $this->foo([], $builder);\n" + |
| 336 | + " }\n" + |
| 337 | + "}\n" |
| 338 | + ); |
| 339 | + |
| 340 | + Set<String> collect = Arrays.stream(FormUtil.getFormBuilderTypes(method)) |
| 341 | + .map(methodReference -> ((StringLiteralExpression) Objects.requireNonNull(methodReference.getParameter(0))).getContents()) |
| 342 | + .collect(Collectors.toSet()); |
| 343 | + |
| 344 | + assertContainsElements(collect, "email"); |
| 345 | + assertContainsElements(collect, "task"); |
| 346 | + |
| 347 | + assertContainsElements(collect, "email2"); |
| 348 | + assertContainsElements(collect, "builder2"); |
| 349 | + } |
291 | 350 | } |
0 commit comments