I've created an maatwebsite/excel import routine and i would like to test it.
The maatwebsite/excel testing page does not provide me with any other information than to fake it. But i need to upload my real excel-file, as i want to validate whether the data from the excel file was processed correctly.
Here's my upload input field and the corresponding button to hit the endpoint /import
<form action="/import" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control-file file-path" name="fileToUpload">
</div>
<button type="submit" class="btn btn-primary">Import File</button>
</form>
on the controller side of view, the uploaded file will be processed and imported.
...
public function store(Request $request) {
$request->validate([
'fileToUpload' => 'required|file|max:4096|mimes:xls,xlsx',
]);
// start the import
Excel::import(new SheetNavigator, request()->file('fileToUpload'));
...
The file that needs to be imported is located within my testing environment under:
/tests
/files
/myexcel.xlsx
public function test_user_can_import_file() {
Excel::fake();
$datafile = new UploadedFile(
base_path('tests/files/myfile.xlsx'),
'myfile.xlsx',
'xlsx',
13071,
true);
$res = $this->post('/import', [
'fileToUpload' => $datafile
]);
// asserting, that everything works..
}
I need a test to verify that the upload was successful and that the import-routine was triggered. I tried everything, from faking anything to using storages.
I appreciate any kind of help, thank you!
Chris