我无法让单元测试用于文件上传,并使用 Spatie 的medialibrary存储文件。
该代码在浏览器中运行良好:文件正确保存。
但是,此单元测试失败:
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');
它失败了,因为 mime 类型为空:
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`"
...并且我要求此集合中的文件为 PDF:
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();
}
但是这个空的 mimeType 只是单元测试的问题——它不是浏览器的问题。
有谁知道为什么在size: 0, mime: inode/x-empty
registerMediaCollections() 评估时文件属性可能是?
谢谢!