как вставить файл в http запрос

у меня есть тестовый пример:

$response = $this->postJson('api/unit/'.$unit->id.'/import',['file' =>Storage::get('file/file.xlsx')]);
    $response->assertJsonFragment(['a'=>'b']);

мой контроллер:

public function import(Request $request, Unit $unit)
{

    $this->validate($request, [
        'file' => 'file|required_without:rows',
        'rows' => 'array|required_without:file',
        'dry_run' => 'boolean',
    ]);
    if ($request->has('rows')) {
        //
    } else {
        $results = $request->file('file');
    }

    return "ok";
}

но я думаю, что мой тестовый пример неверен, потому что, когда я dd($reuqest->file('file')) в моем контроллере, он возвращает ноль. Итак, как я могу запросить файл в свой контроллер. пожалуйста помоги


person the manh Nguyen    schedule 12.10.2018    source источник
comment
Вы сериализовали файл?   -  person Dimitri Mostrey    schedule 12.10.2018
comment
что такое сериализованный файл и как это сделать?! @DimitriMostrey   -  person the manh Nguyen    schedule 15.10.2018


Ответы (2)


Вы можете использовать UploadFile следующим образом:

$fileName= 'file.png';
$filePath=sys_get_temp_dir().'/'.$fileName;
$mimeTypeFile=mime_content_type($filePath);
$file=new UploadedFile($filePath, $fileName, $mimeTypeFile, filesize('$filePath'), null, true)
$response = $this->postJson('api/unit/'.$unit->id.'/import',['file' =>$file]);
$response->assertJsonFragment(['a'=>'b']);

with null — это константа ошибки загрузки, true — это тестовый режим. rel="nofollow noreferrer">это

person Community    schedule 05.11.2018

Как упоминалось в этом https://laravel.com/docs/5.4/http-tests#testing-file-uploads.

Попробуйте что-нибудь вроде этого. Импорт используйте Illuminate\Http\UploadedFile;

UploadedFile::fake()->create('file.xlsx', $size);
Storage::disk('file')->assertExists('file.xlsx');
person Orgil    schedule 12.10.2018
comment
я хочу использовать свой файл - person the manh Nguyen; 12.10.2018