Test FileUpload

I can't test my FileUpload I have this test:
Livewire::actingAs($user)
->test(FormForm::class, [
'formType' => FormType::DFD,
'userForm' => $form,
'operation' => 'create',
])
->set('data', [
'name' => 'Test',
// 'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
])
->call('createOrEdit');
Livewire::actingAs($user)
->test(FormForm::class, [
'formType' => FormType::DFD,
'userForm' => $form,
'operation' => 'create',
])
->set('data', [
'name' => 'Test',
// 'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
])
->call('createOrEdit');
This test PASSES. If I uncomment the line
'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
the test fails on line
->call('createOrEdit')
->call('createOrEdit')
with the following error:
ErrorException: Trying to access array offset on null
at vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:256
at vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php:88
at vendor/livewire/livewire/src/LivewireManager.php:102
at vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php:94
[...]
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:252
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:219
at tests/Feature/Livewire/FormTest.php:84
ErrorException: Trying to access array offset on null
at vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:256
at vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php:88
at vendor/livewire/livewire/src/LivewireManager.php:102
at vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php:94
[...]
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:252
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:219
at tests/Feature/Livewire/FormTest.php:84
I tried all the solutions I could find online, without success...
Solution:
I found the solution! ```php ->set('data.name', 'Test') ->set('data.file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf'))...
Jump to solution
2 Replies
awcodes
awcodes4mo ago
I think you have to somehow persist the fake() to the livewire-tmp directory. UploadedFile doesn’t return an instance of TemporaryFileUpload, so it doesn’t know how to handle it. Probably need to fake the Storage too.
Solution
charlie
charlie4mo ago
I found the solution!
->set('data.name', 'Test')
->set('data.file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf'))
->set('data.name', 'Test')
->set('data.file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf'))
I got it from a Dan Harrin's answer on github. You have to add .0 after the field name. Be careful, the following doesn't work:
->set('data', [
'name' => 'Test',
'file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
])
->set('data', [
'name' => 'Test',
'file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
])

Did you find this page helpful?