88use Symfony \Component \Form \Extension \DataCollector \FormDataCollector ;
99use Symfony \Component \VarDumper \Cloner \Data ;
1010
11- use function count ;
1211use function implode ;
1312use function is_array ;
1413use function is_int ;
@@ -26,18 +25,18 @@ trait FormAssertionsTrait
2625 * $I->assertFormValue('#loginForm', 'username', 'john_doe');
2726 * ```
2827 */
29- public function assertFormValue (string $ formSelector , string $ fieldName , string $ value , string $ message = '' ): void
28+ public function assertFormValue (string $ formSelector , string $ fieldName , string $ expectedValue , string $ message = '' ): void
3029 {
3130 $ node = $ this ->getClient ()->getCrawler ()->filter ($ formSelector );
32- $ this ->assertGreaterThan (0 , count ( $ node ), sprintf ('Form "%s" not found. ' , $ formSelector ));
31+ $ this ->assertGreaterThan (0 , $ node-> count ( ), sprintf ('Form "%s" not found. ' , $ formSelector ));
3332
3433 $ values = $ node ->form ()->getValues ();
3534 $ this ->assertArrayHasKey (
3635 $ fieldName ,
3736 $ values ,
3837 $ message ?: sprintf ('Field "%s" not found in form "%s". ' , $ fieldName , $ formSelector )
3938 );
40- $ this ->assertSame ($ value , $ values [$ fieldName ]);
39+ $ this ->assertSame ($ expectedValue , $ values [$ fieldName ]);
4140 }
4241
4342 /**
@@ -51,7 +50,7 @@ public function assertFormValue(string $formSelector, string $fieldName, string
5150 public function assertNoFormValue (string $ formSelector , string $ fieldName , string $ message = '' ): void
5251 {
5352 $ node = $ this ->getClient ()->getCrawler ()->filter ($ formSelector );
54- $ this ->assertGreaterThan (0 , count ( $ node ), sprintf ('Form "%s" not found. ' , $ formSelector ));
53+ $ this ->assertGreaterThan (0 , $ node-> count ( ), sprintf ('Form "%s" not found. ' , $ formSelector ));
5554
5655 $ values = $ node ->form ()->getValues ();
5756 $ this ->assertArrayNotHasKey (
@@ -86,19 +85,11 @@ public function dontSeeFormErrors(): void
8685 */
8786 public function seeFormErrorMessage (string $ field , ?string $ message = null ): void
8887 {
89- $ errors = $ this ->getErrorsForField ($ field );
90-
91- if ($ errors === []) {
92- Assert::fail ("No form error message for field ' {$ field }'. " );
93- }
88+ $ collector = $ this ->grabFormCollector (__FUNCTION__ );
89+ /** @var array<string, mixed> $formsData */
90+ $ formsData = $ this ->getRawCollectorData ($ collector )['forms ' ] ?? [];
9491
95- if ($ message !== null ) {
96- $ this ->assertStringContainsString (
97- $ message ,
98- implode ("\n" , $ errors ),
99- sprintf ("There is an error message for the field '%s', but it does not match the expected message. " , $ field )
100- );
101- }
92+ $ this ->assertFormErrorMessage ($ field , $ message , $ formsData );
10293 }
10394
10495 /**
@@ -140,8 +131,34 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
140131 */
141132 public function seeFormErrorMessages (array $ expectedErrors ): void
142133 {
134+ $ collector = $ this ->grabFormCollector (__FUNCTION__ );
135+ /** @var array<string, mixed> $formsData */
136+ $ formsData = $ this ->getRawCollectorData ($ collector )['forms ' ] ?? [];
137+
143138 foreach ($ expectedErrors as $ field => $ msg ) {
144- is_int ($ field ) ? $ this ->seeFormErrorMessage ((string ) $ msg ) : $ this ->seeFormErrorMessage ($ field , $ msg );
139+ if (is_int ($ field )) {
140+ $ this ->assertFormErrorMessage ((string ) $ msg , null , $ formsData );
141+ } else {
142+ $ this ->assertFormErrorMessage ($ field , $ msg , $ formsData );
143+ }
144+ }
145+ }
146+
147+ /** @param array<string, mixed> $formsData */
148+ private function assertFormErrorMessage (string $ field , ?string $ message , array $ formsData ): void
149+ {
150+ $ errors = $ this ->getErrorsForField ($ field , $ formsData );
151+
152+ if ($ errors === []) {
153+ Assert::fail ("No form error message for field ' {$ field }'. " );
154+ }
155+
156+ if ($ message !== null ) {
157+ $ this ->assertStringContainsString (
158+ $ message ,
159+ implode ("\n" , $ errors ),
160+ sprintf ("There is an error message for the field '%s', but it does not match the expected message. " , $ field )
161+ );
145162 }
146163 }
147164
@@ -172,16 +189,11 @@ private function getFormErrorsCount(string $function): int
172189 }
173190
174191 /**
192+ * @param array<string, mixed> $formsData
175193 * @return list<string>
176194 */
177- private function getErrorsForField (string $ field ): array
195+ private function getErrorsForField (string $ field, array $ formsData ): array
178196 {
179- $ collector = $ this ->grabFormCollector ('seeFormErrorMessage ' );
180- $ formsData = $ this ->getRawCollectorData ($ collector )['forms ' ] ?? [];
181- if (!is_array ($ formsData )) {
182- return [];
183- }
184-
185197 $ errorsForField = [];
186198 $ fieldFound = false ;
187199
@@ -215,11 +227,11 @@ private function getErrorsForField(string $field): array
215227 /** @return array<string, mixed> */
216228 private function getRawCollectorData (FormDataCollector $ collector ): array
217229 {
218- $ data = $ collector ->getData ();
219- if ($ data instanceof Data) {
220- $ data = $ data ->getValue (true );
230+ $ collectorData = $ collector ->getData ();
231+ if ($ collectorData instanceof Data) {
232+ $ collectorData = $ collectorData ->getValue (true );
221233 }
222234 /** @var array<string, mixed> */
223- return is_array ($ data ) ? $ data : [];
235+ return is_array ($ collectorData ) ? $ collectorData : [];
224236 }
225237}
0 commit comments