公式サイトのサンプルコードが参考になる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithoutMiddleware; class ExampleTest extends TestCase{ public function testAvatarUpload() { Storage::fake('avatars'); $file = UploadedFile::fake()->image('avatar.jpg'); $response = $this->json('POST', '/avatar', [ 'avatar' => $file, ]); // Assert the file was stored... Storage::disk('avatars')->assertExists($file->hashName()); // Assert a file does not exist... Storage::disk('avatars')->assertMissing('missing.jpg'); } } |
参考サイト
HTTP Tests – Laravel – The PHP Framework For Web Artisans
https://laravel.com/docs/5.7/http-tests#testing-file-uploads
Testing File Uploads With Laravel – Laravel News
https://laravel-news.com/testing-file-uploads-with-laravel