rephluX
4 years ago
1226
11
Testing

How to test response->download to download a PDF?

Posted 4 years ago by rephluX

In a controller i got something like this

public function download($file)
{
    $download = $this->download->getFile($file);
    $path     = realpath(base_path('resources/downloads')) . $file;

    if (!file_exists($path)) {
        abort(404);
    }

    if (!$download->belongsToUser($this->user->id)) {
        abort(404);
    }

    return response()->download($path, $this->getFilename($file), ['content-type' => ' application/pdf']);
}

Right now my test looks something like this:

// ...

$this->actingAs($user1)
            ->visit('/downloads')
            ->seePageIs('/downloads')
            ->see($downloadUser1->name)
            ->dontSee($downloadUser2->name)
            ->click($downloadUser1->name)
            ->seeStatusCode(200);

But for this i got the following error:

InvalidArgumentException: Expecting a DOMNodeList or DOMNode instance, an array, a string, or null, but got "boolean".

I driving my head around how to test this :D Is there any possibility to test the download within an integration test?

Please sign in or create an account to participate in this conversation.