5
5
namespace Codeception \Module \Symfony ;
6
6
7
7
use Symfony \Component \Form \Extension \DataCollector \FormDataCollector ;
8
+ use Symfony \Component \VarDumper \Cloner \Data ;
8
9
use function array_key_exists ;
9
10
use function in_array ;
11
+ use function is_array ;
10
12
use function is_int ;
11
13
use function sprintf ;
12
14
@@ -22,10 +24,15 @@ trait FormAssertionsTrait
22
24
*/
23
25
public function assertFormValue (string $ formSelector , string $ fieldName , string $ value , string $ message = '' ): void
24
26
{
25
- $ node = $ this ->getCLient ()->getCrawler ()->filter ($ formSelector );
27
+ $ node = $ this ->getClient ()->getCrawler ()->filter ($ formSelector );
26
28
$ this ->assertNotEmpty ($ node , sprintf ('Form "%s" not found. ' , $ formSelector ));
29
+
27
30
$ values = $ node ->form ()->getValues ();
28
- $ this ->assertArrayHasKey ($ fieldName , $ values , $ message ?: sprintf ('Field "%s" not found in form "%s". ' , $ fieldName , $ formSelector ));
31
+ $ this ->assertArrayHasKey (
32
+ $ fieldName ,
33
+ $ values ,
34
+ $ message ?: sprintf ('Field "%s" not found in form "%s". ' , $ fieldName , $ formSelector )
35
+ );
29
36
$ this ->assertSame ($ value , $ values [$ fieldName ]);
30
37
}
31
38
@@ -39,10 +46,15 @@ public function assertFormValue(string $formSelector, string $fieldName, string
39
46
*/
40
47
public function assertNoFormValue (string $ formSelector , string $ fieldName , string $ message = '' ): void
41
48
{
42
- $ node = $ this ->getCLient ()->getCrawler ()->filter ($ formSelector );
49
+ $ node = $ this ->getClient ()->getCrawler ()->filter ($ formSelector );
43
50
$ this ->assertNotEmpty ($ node , sprintf ('Form "%s" not found. ' , $ formSelector ));
51
+
44
52
$ values = $ node ->form ()->getValues ();
45
- $ this ->assertArrayNotHasKey ($ fieldName , $ values , $ message ?: sprintf ('Field "%s" has a value in form "%s". ' , $ fieldName , $ formSelector ));
53
+ $ this ->assertArrayNotHasKey (
54
+ $ fieldName ,
55
+ $ values ,
56
+ $ message ?: sprintf ('Field "%s" has a value in form "%s". ' , $ fieldName , $ formSelector )
57
+ );
46
58
}
47
59
48
60
/**
@@ -56,14 +68,9 @@ public function assertNoFormValue(string $formSelector, string $fieldName, strin
56
68
public function dontSeeFormErrors (): void
57
69
{
58
70
$ formCollector = $ this ->grabFormCollector (__FUNCTION__ );
71
+ $ errors = $ this ->extractFormCollectorScalar ($ formCollector , 'nb_errors ' );
59
72
60
- $ errors = (int )$ formCollector ->getData ()->offsetGet ('nb_errors ' );
61
-
62
- $ this ->assertSame (
63
- 0 ,
64
- $ errors ,
65
- 'Expecting that the form does not have errors, but there were! '
66
- );
73
+ $ this ->assertSame (0 , $ errors , 'Expecting that the form does not have errors, but there were! ' );
67
74
}
68
75
69
76
/**
@@ -79,38 +86,51 @@ public function dontSeeFormErrors(): void
79
86
public function seeFormErrorMessage (string $ field , ?string $ message = null ): void
80
87
{
81
88
$ formCollector = $ this ->grabFormCollector (__FUNCTION__ );
89
+ /** @var list<array<string, mixed>> $forms */
90
+ $ forms = $ this ->extractFormCollectorArray ($ formCollector , 'forms ' );
82
91
83
- if (! $ forms = $ formCollector -> getData ()-> getValue ( true )[ ' forms ' ]) {
92
+ if ($ forms === [ ]) {
84
93
$ this ->fail ('No forms found on the current page. ' );
85
94
}
86
95
87
96
$ fields = [];
88
97
$ errors = [];
89
98
90
99
foreach ($ forms as $ form ) {
100
+ if (!isset ($ form ['children ' ]) || !is_array ($ form ['children ' ])) {
101
+ continue ;
102
+ }
91
103
foreach ($ form ['children ' ] as $ child ) {
104
+ if (!is_array ($ child ) || !array_key_exists ('name ' , $ child ) || !is_string ($ child ['name ' ])) {
105
+ continue ;
106
+ }
92
107
$ fieldName = $ child ['name ' ];
93
- $ fields [] = $ fieldName ;
108
+ $ fields [] = $ fieldName ;
94
109
95
- if (!array_key_exists ( 'errors ' , $ child )) {
110
+ if (!isset ( $ child [ 'errors ' ]) || ! is_array ( $ child[ ' errors ' ] )) {
96
111
continue ;
97
112
}
98
113
99
114
foreach ($ child ['errors ' ] as $ error ) {
100
- $ errors [$ fieldName ] = $ error ['message ' ];
115
+ if (is_array ($ error )
116
+ && array_key_exists ('message ' , $ error )
117
+ && is_string ($ error ['message ' ])
118
+ ) {
119
+ $ errors [$ fieldName ] = $ error ['message ' ];
120
+ }
101
121
}
102
122
}
103
123
}
104
124
105
- if (!in_array ($ field , $ fields )) {
125
+ if (!in_array ($ field , $ fields, true )) {
106
126
$ this ->fail ("The field ' {$ field }' does not exist in the form. " );
107
127
}
108
128
109
129
if (!array_key_exists ($ field , $ errors )) {
110
130
$ this ->fail ("No form error message for field ' {$ field }'. " );
111
131
}
112
132
113
- if (! $ message ) {
133
+ if ($ message === null ) {
114
134
return ;
115
135
}
116
136
@@ -164,15 +184,15 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
164
184
* ]);
165
185
* ```
166
186
*
167
- * @param string[] $expectedErrors
187
+ * @param array<int| string, string|null> $expectedErrors
168
188
*/
169
189
public function seeFormErrorMessages (array $ expectedErrors ): void
170
190
{
171
- foreach ($ expectedErrors as $ field => $ message ) {
191
+ foreach ($ expectedErrors as $ field => $ msg ) {
172
192
if (is_int ($ field )) {
173
- $ this ->seeFormErrorMessage ($ message );
193
+ $ this ->seeFormErrorMessage (( string ) $ msg );
174
194
} else {
175
- $ this ->seeFormErrorMessage ($ field , $ message );
195
+ $ this ->seeFormErrorMessage ($ field , $ msg );
176
196
}
177
197
}
178
198
}
@@ -188,16 +208,60 @@ public function seeFormErrorMessages(array $expectedErrors): void
188
208
public function seeFormHasErrors (): void
189
209
{
190
210
$ formCollector = $ this ->grabFormCollector (__FUNCTION__ );
211
+ $ errors = $ this ->extractFormCollectorScalar ($ formCollector , 'nb_errors ' );
191
212
192
- $ this ->assertGreaterThan (
193
- 0 ,
194
- $ formCollector ->getData ()->offsetGet ('nb_errors ' ),
195
- 'Expecting that the form has errors, but there were none! '
196
- );
213
+ $ this ->assertGreaterThan (0 , $ errors , 'Expecting that the form has errors, but there were none! ' );
214
+ }
215
+
216
+ /** @return list<array<string, mixed>> */
217
+ private function extractFormCollectorArray (FormDataCollector $ collector , string $ key ): array
218
+ {
219
+ $ data = $ collector ->getData ();
220
+
221
+ if ($ data instanceof Data) {
222
+ $ rawData = $ data ->getValue (true );
223
+ } else {
224
+ $ rawData = $ data ;
225
+ }
226
+
227
+ if (is_array ($ rawData )
228
+ && array_key_exists ($ key , $ rawData )
229
+ && is_array ($ rawData [$ key ])
230
+ ) {
231
+ /** @var array<string, mixed> $slice */
232
+ $ slice = $ rawData [$ key ];
233
+ } else {
234
+ $ slice = [];
235
+ }
236
+
237
+ /** @var list<array<string, mixed>> $forms */
238
+ $ forms = array_values ($ slice );
239
+
240
+ return $ forms ;
241
+ }
242
+
243
+ private function extractFormCollectorScalar (FormDataCollector $ collector , string $ key ): int
244
+ {
245
+ $ data = $ collector ->getData ();
246
+ $ valueRaw = null ;
247
+
248
+ if ($ data instanceof Data) {
249
+ $ valueRaw = $ data ->offsetGet ($ key );
250
+ } elseif (array_key_exists ($ key , $ data )) {
251
+ $ valueRaw = $ data [$ key ];
252
+ }
253
+
254
+ if (is_numeric ($ valueRaw )) {
255
+ return (int ) $ valueRaw ;
256
+ }
257
+
258
+ return 0 ;
197
259
}
198
260
199
261
protected function grabFormCollector (string $ function ): FormDataCollector
200
262
{
201
- return $ this ->grabCollector ('form ' , $ function );
263
+ /** @var FormDataCollector $collector */
264
+ $ collector = $ this ->grabCollector ('form ' , $ function );
265
+ return $ collector ;
202
266
}
203
267
}
0 commit comments