I'm having trouble getting a unit test working for file upload, and storing the file using Spatie's medialibrary.
The code works fine in the browser: the file saves correctly.
However, this unit test fails:
Storage::fake('resource-files');
$file = UploadedFile::fake()->create('courtform.pdf', 1024);
$response = $this->ActingAs($this->adminUser())
->json('PATCH', '/forms/' . $dbentry->id, [
'name' => 'Form 800',
'state' => 'VT',
'description' => 'General info form',
'file' => $file,
]);
// Assert the file was stored...
Storage::disk('resource-files')->assertExists('1/courtform.pdf');
It fails because the mime type is empty:
Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection^ {#1485
#message: "The file with properties `name: courtform.pdf, size: 0, mime: inode/x-empty` was not accepted into the collection named `source-pdfs` of model `App\Form` with id `1`"
... and I have required that the files in this collection be PDFs:
public function registerMediaCollections()
{
// Keep only one file per model
$this
->addMediaCollection('source-pdfs')
->useDisk('resource-files')
->singleFile()
->acceptsFile(function (File $file) {
return $file->mimeType === 'application/pdf';
});
$this
->addMediaCollection('source-fdfs')
->singleFile();
}
But this empty mimeType is only an issue with the unit test — it's not a problem in the browser.
Does anyone know why the file properties might be size: 0, mime: inode/x-empty
when evaluated by registerMediaCollections()?
Thank you!