Mmh, I don't think you should be referencing $this-file->path() in your store method. Instead, you need to store $path in there
Feb 5, 2021
9
Level 3
Cannot test livewire file upload
Hey,
I am trying to test a livewire file upload.
This is my test case:
public function test_task_document_can_be_uploaded()
{
$this->withExceptionHandling();
Storage::fake('local');
$file = UploadedFile::fake()->create('document.pdf', 256);
$category = DocumentCategory::factory()->create();
#dump('In Test: ' . $file->hashName());
Livewire::test(DocumentsComponent::class, ['model' => $this->task])
->set('file', $file)
->set('category', $category->id)
->call('store');
$this->assertDatabaseHas('documents', [
'tenant_id' => $this->tenant->id,
'name' => $file->getClientOriginalName(),
'path' => 'files/' . $file->hashName()
]);
Storage::disk('local')->assertExists('files/' . $file->hashname());
}
The assertExists fails, because the hash name of the file that is created in the component is different from that one in my test case. But how can I get the right name?
Here is my component code:
public function store()
{
$this->validate([
'file' => 'mimes:pdf|max:1024',
'category' => 'required'
]);
$path = $this->file->store('files');
#dd($this->file->hashName(), $this->file->path(), $path);
$this->model->documents()->create([
'name' => $this->file->getClientOriginalName(),
'path' => $this->file->path(),
'document_category_id' => $this->category
]);
$this->refresh();
}
Please or to participate in this conversation.