Friday, March 31, 2017

Upload file with post in laravel phpunit test

In my test, I load an existing file and attempt to pass it as a parameter when making a local api call:

        $this->user = $this->getAuthenticatableUser();

        $path = __DIR__.'/assets/MOCK_DATA.csv';
        $name = str_random(8).'.csv';
        $file = new UploadedFile($path, $name, 'text/csv', filesize($path), null, true);

        $this->actingAs($this->user)
            ->withSession(['companyId' => $this->user->companyusers()->first()->companyId])
            ->post('/api/v2/addByCsv', [
                'CSVUpload' => $file
            ])
            ->see('test');

However, the controller does not receive the data as a file. Using request->input, I can return the location of the file as a string, but the current code is not executing because hasFile does not return true:

if($request->hasFile($this->csvFieldName)){
    $csvContents = file_get_contents($request->file($this->csvFieldName));

Why is this not working correctly, and how can I get laravel to realize that I am passing a file as one of the post parameters?



via Zach P

Advertisement