Testing File Uploads
Laravel Test HTTP Tests: Testing File Uploads
Categories:
Testing File Uploads
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_avatars_can_be_uploaded()
{
Storage::fake('avatars');
$file = UploadedFile::fake()->image('avatar.jpg');
$response = $this->post('/avatar', [
'avatar' => $file,
]);
Storage::disk('avatars')->assertExists($file->hashName());
}
}
Storage::fake('avatars');
// ...
Storage::disk('avatars')->assertMissing('missing.jpg');
Fake File Customization
UploadedFile::fake()->image('avatar.jpg', $width, $height)->size(100);
UploadedFile::fake()->create('document.pdf', $sizeInKilobytes);
UploadedFile::fake()->create(
'document.pdf', $sizeInKilobytes, 'application/pdf'
);