Files
Test-Laravel-File-Upload/tests/Feature/FileUploadTest.php
T

78 lines
2.2 KiB
PHP
Raw Normal View History

2021-12-05 13:20:12 +02:00
<?php
namespace Tests\Feature;
2021-12-06 07:58:56 +02:00
use App\Models\House;
2021-12-05 13:20:12 +02:00
use App\Models\Project;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FileUploadTest extends TestCase
{
use RefreshDatabase;
public function test_original_filename_upload()
{
$filename = 'logo.jpg';
$response = $this->post('projects', [
'name' => 'Some name',
'logo' => UploadedFile::fake()->image($filename)
]);
$response->assertStatus(200);
$this->assertDatabaseHas('projects', [
'name' => 'Some name',
'logo' => $filename
]);
}
2021-12-05 13:44:24 +02:00
public function test_file_size_validation()
{
$response = $this->post('projects', [
'name' => 'Some name',
'logo' => UploadedFile::fake()->create('logo.jpg', 2000)
]);
$response->assertInvalid();
$response = $this->post('projects', [
'name' => 'Some name',
'logo' => UploadedFile::fake()->create('logo.jpg', 500)
]);
$response->assertValid();
}
2021-12-06 07:58:56 +02:00
public function test_update_file_remove_old_one()
{
$response = $this->post('houses', [
'name' => 'Some name',
'photo' => UploadedFile::fake()->image('photo.jpg')
]);
$response->assertStatus(200);
$house = House::first();
$this->assertTrue(Storage::exists($house->photo));
2021-12-06 08:10:53 +02:00
$response = $this->put('houses/' . $house->id, [
2021-12-06 07:58:56 +02:00
'name' => 'Some name',
'photo' => UploadedFile::fake()->image('photo2.jpg')
]);
$response->assertStatus(200);
$this->assertFalse(Storage::exists($house->photo));
}
2021-12-06 08:10:53 +02:00
public function test_download_uploaded_file()
{
$this->post('houses', [
'name' => 'Some name',
'photo' => UploadedFile::fake()->image('photo.jpg')
]);
$house = House::first();
$response = $this->get('houses/download/' . $house->id);
$response->assertStatus(200);
$response->assertDownload(str_replace('houses/', '', $house->photo));
}
2021-12-05 13:20:12 +02:00
}