mazedlx's avatar

How do I test a multiple file upload field in Laravel/PHPUnit

Hi everyone!

I have the following view

<form method="POST" action="http://lego.localhost/minifigs" accept-charset="UTF-8" class="form form-horizontal" enctype="multipart/form-data"><input name="_token" type="hidden" value="Yq5jwYS7Cj9XLDoTinjgH7CSlMtgXBfKzjlgwksI">
        <div class="form-group">
    <label for="name" class="col-md-2 control-label">Name</label>
    <div class="col-md-6">
        <input id="name" class="form-control" placeholder="Name" name="name" type="text">
    </div>
</div>
<div class="form-group">
    <label for="set_id" class="col-md-2 control-label">Set</label>
    <div class="col-md-6">
        <select class="form-control" id="set_id" name="set_id"><option value="42">ARC-170 Starfighter</option><option value="29">AT-ST & Endor</option></select>
    </div>
</div>
<div class="form-group">
    <label for="files" class="col-md-2 control-label">Files</label>
    <div class="col-md-6">
        <input class="form-control" multiple="multiple" name="files[]" type="file">
    </div>
</div>
<div class="form-group">
    <div class="col-md-6 col-md-offset-2">
        <input class="btn btn-primary" type="submit" value="Create">
    </div>
</div>
</form>

Everyhting is working fine when testing it manually. Now I have the following code for testing:

public function testMinifigCreate()
{
    $user = factory(App\User::class)->create();
    $minfig = factory(App\Minifig::class)->create();
    $this->actingAs($user)
        ->visit('/minifigs/create')
        ->type('TestMinifig', 'name')
        ->select('10', 'set_id')
         ->attach('10188.png', 'files[]')
        ->press('Create')
        ->see('Minifig created');
}

The only thing that is not working is attaching anything to the file input. It has a name of files[] because it accepts multiple files. But I just can't add anything to it, PHPUnit throws the following error:

InvalidArgumentException: Unreachable field ""

How does one test fields that have [] in the name?

0 likes
5 replies
johnny's avatar

@mazedlx did you manage to figure it out?

johnny's avatar

I see. I tried everything in the last 2 days, but no luck here as well.

Please or to participate in this conversation.