Testing PhpUnit, how to check a checkbox if there are several check boxes with the same name in a form

advertisements

I am testing a form. In the form, there are some checkboxes which are with the same name as there are multiple checkboxes to select from.

So my check boxes are like this:

<div class="col-sm-10">
    <div class="checkbox">
        <input id="department_1" name="departments[]" type="checkbox" value="1">
        <label for="department_1">Sales</label>
    </div>
                                            <div class="checkbox">
        <input id="department_2" name="departments[]" type="checkbox" value="2">
        <label for="department_2">Marketing</label>
    </div>
                                            <div class="checkbox">
        <input id="department_3" name="departments[]" type="checkbox" value="3">
        <label for="department_3">Tech Help</label>
    </div>
</div>

My testing code is like this:

public function testUserCreation()
    {
        $this->be(User::find(10));

        $this->visit('/users/create')
            ->type('First', 'first_name')
            ->type('Last', 'last_name')
            ->type('[email protected]', 'email')
            ->type('123456', 'password')
            ->type('123456', 'password_confirmation')
            ->check('departments')
            ->press('Submit')
            ->seePageIs('/users');
    }

When I am trying to check if throws error:

InvalidArgumentException: Nothing matched the filter [permissions] CSS query provided for


The only way I managed this was:

$this->visit('/users/create')
    ->submitForm('Submit', [
        ...
        ...
        'departments[0]' => '1',
        'departments[1]' => '2'
    ])
    ->seePageIs('/users');

Note that if you want to check the first and last item, you have to follow the order the inputs are placed.

$this->visit('/users/create')
        ->submitForm('Submit', [
            ...
            ...
            'departments[0]' => '1',
            'departments[2]' => '3' // index 2 instead 1.
        ])
        ->seePageIs('/users');