File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414namespace Liip \FunctionalTestBundle \Validator ;
1515
1616use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
17+ use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
1718use Symfony \Component \HttpKernel \KernelEvents ;
1819use Symfony \Component \Validator \ConstraintViolationList ;
1920use Symfony \Component \Validator \ConstraintViolationListInterface ;
@@ -85,10 +86,17 @@ public function inContext(ExecutionContextInterface $context): ContextualValidat
8586 return $ this ->wrappedValidator ->inContext ($ context );
8687 }
8788
89+ public function onKernelRequest (GetResponseEvent $ event ): void
90+ {
91+ if ($ event ->isMasterRequest ()) {
92+ $ this ->clearLastErrors ();
93+ }
94+ }
95+
8896 public static function getSubscribedEvents (): array
8997 {
9098 return [
91- KernelEvents::REQUEST => ['clearLastErrors ' , 99999 ],
99+ KernelEvents::REQUEST => ['onKernelRequest ' , 99999 ],
92100 ];
93101 }
94102}
Original file line number Diff line number Diff line change @@ -62,6 +62,30 @@ public function userAction(int $userId): Response
6262 * @return Response
6363 */
6464 public function formAction (Request $ request ): Response
65+ {
66+ return $ this ->form ($ request , 'form.html.twig ' );
67+ }
68+
69+ /**
70+ * @param Request $request
71+ *
72+ * @return Response
73+ */
74+ public function formWithEmbedAction (Request $ request ): Response
75+ {
76+ return $ this ->form ($ request , 'form_with_embed.html.twig ' );
77+ }
78+
79+ /**
80+ * Common form functionality used to test form submissions both
81+ * with and without an embedded request.
82+ *
83+ * @param Request $request
84+ * @param string $template
85+ *
86+ * @return Response
87+ */
88+ private function form (Request $ request , string $ template ): Response
6589 {
6690 $ object = new \ArrayObject ();
6791 $ object ->name = null ;
@@ -83,7 +107,7 @@ public function formAction(Request $request): Response
83107 }
84108
85109 return $ this ->render (
86- ' form.html.twig ' ,
110+ $ template ,
87111 ['form ' => $ form ->createView ()]
88112 );
89113 }
@@ -100,4 +124,14 @@ public function jsonAction(): Response
100124
101125 return $ response ;
102126 }
127+
128+ /**
129+ * Used to embed content as a sub-request.
130+ *
131+ * @return Response
132+ */
133+ public function embeddedAction (): Response
134+ {
135+ return new Response ('Embedded Content ' , Response::HTTP_OK );
136+ }
103137}
Original file line number Diff line number Diff line change 1+ {% extends ' AcmeBundle::layout.html.twig' %}
2+
3+ {% block body %}
4+ <div >
5+ {{ render(controller(' Liip\\FunctionalTestBundle\\Tests\\App\\Controller\\DefaultController::embeddedAction' )) }}
6+ </div >
7+
8+ {{ form(form ) }}
9+ {% endblock %}
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ liipfunctionaltestbundle_form:
1212 path : /form
1313 defaults : { _controller: 'Liip\FunctionalTestBundle\Tests\App\Controller\DefaultController::formAction' }
1414
15+ liipfunctionaltestbundle_form_with_embed :
16+ path : /form-with-embed
17+ defaults : { _controller: 'Liip\FunctionalTestBundle\Tests\App\Controller\DefaultController::formWithEmbedAction' }
18+
1519liipfunctionaltestbundle_json :
1620 path : /json
1721 defaults : { _controller: 'Liip\FunctionalTestBundle\Tests\App\Controller\DefaultController::jsonAction' }
Original file line number Diff line number Diff line change @@ -726,6 +726,41 @@ public function testForm(): void
726726 );
727727 }
728728
729+ /**
730+ * Ensure form validation helpers still work with embedded controllers.
731+ *
732+ * @see https://github.com/liip/LiipFunctionalTestBundle/issues/273
733+ */
734+ public function testFormWithEmbed (): void
735+ {
736+ $ this ->loadFixtures ([]);
737+
738+ $ path = '/form-with-embed ' ;
739+
740+ $ crawler = $ this ->client ->request ('GET ' , $ path );
741+
742+ $ this ->assertStatusCode (200 , $ this ->client );
743+
744+ $ form = $ crawler ->selectButton ('Submit ' )->form ();
745+ $ crawler = $ this ->client ->submit ($ form );
746+
747+ $ this ->assertStatusCode (200 , $ this ->client );
748+
749+ $ this ->assertValidationErrors (['children[name].data ' ], $ this ->client ->getContainer ());
750+
751+ // Try again with the fields filled out.
752+ $ form = $ crawler ->selectButton ('Submit ' )->form ();
753+ $ form ->setValues (['form[name] ' => 'foo bar ' ]);
754+ $ crawler = $ this ->client ->submit ($ form );
755+
756+ $ this ->assertStatusCode (200 , $ this ->client );
757+
758+ $ this ->assertContains (
759+ 'Name submitted. ' ,
760+ $ crawler ->filter ('div.flash-notice ' )->text ()
761+ );
762+ }
763+
729764 /**
730765 * @depends testForm
731766 *
You can’t perform that action at this time.
0 commit comments