Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel testing form press method doesn't works

I'm trying to cover my project with test and faced with problem.

The "press" method of TestCase fails with 'InvalidArgumentException: Unreachable field ""'

However the "see" method sees the needed button

Besides another form on another page tests fine

Hours of debug show me that the issue might be in the fact that the problem form has multiple (with this brackets []) inputs

Test code that fails

$this->type($params['from'], 'from[]');
$this->type($params['event'], 'event[]');
$this->type($params['class'], 'class[]');
$this->type($params['method'], 'method[]');
$this->press('save_handlers');

With form and button everythings is okey Button:

<button type="submit" class="btn btn-primary btn-block" name="save_handlers">Save</button>

And of course button is in the form tag

like image 999
Вася Аристов Avatar asked Mar 25 '26 13:03

Вася Аристов


1 Answers

Indeed, the problem is linked with the fact that there are attributes with brackets[].

I just had the same problem. I'm using a form with multiple checkboxes, and all of them have the same name (but different id) : codes[]. I'm doing this in order to retrieve them later (in a controller) simply as an array of values.

<input id="perm-0" type="checkbox" name="codes[]" value="perm-0" />
<input id="perm-1" type="checkbox" name="codes[]" value="perm-1" />
<input id="perm-2" type="checkbox" name="codes[]" value="perm-2" />

My friend var_dump() told me that the Symfony component which parses the form inputs doesn't like it when I'm using codes[] with nothing inside the brackets. It is seen as two fields : "codes" and "" instead of codes[]. That's causing the Unreachable field "" error.

A simple solution I found is to simply add an explicit index for the codes[] array :

<input id="perm-0" type="checkbox" name="codes[0]" value="perm-0" />
<input id="perm-1" type="checkbox" name="codes[1]" value="perm-1" />
<input id="perm-2" type="checkbox" name="codes[2]" value="perm-2" />

This way each checkbox is distinct from others, and the method press() does not cause the error any more. It seems that this doesn't affect the processing of the resulting array in my controller.

like image 173
DaveBowman Avatar answered Mar 28 '26 03:03

DaveBowman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!