mirror of
https://github.com/10h30/Test-Laravel-File-Upload.git
synced 2026-07-11 18:56:13 +09:00
Task 4 - download file
This commit is contained in:
@@ -48,3 +48,11 @@ In `app/Http/Controllers/HouseController.php` file, in the `update()` method, we
|
|||||||
Test method `test_update_file_remove_old_one()`.
|
Test method `test_update_file_remove_old_one()`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Task 4. Download the Uploaded File
|
||||||
|
|
||||||
|
In `app/Http/Controllers/HouseController.php` file, in the `download()` method, return the response that would automatically download the file with `$house->photo` filename from `storage/app/houses` folder.
|
||||||
|
|
||||||
|
Test method `test_download_uploaded_file()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|||||||
@@ -33,4 +33,10 @@ class HouseController extends Controller
|
|||||||
|
|
||||||
return 'Success';
|
return 'Success';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function download(House $house)
|
||||||
|
{
|
||||||
|
// TASK: Return the $house->photo file from "storage/app/houses" folder
|
||||||
|
// for download in browser
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,6 @@ Route::get('/', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store']);
|
Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store']);
|
||||||
|
|
||||||
|
Route::get('houses/download/{house}', [\App\Http\Controllers\HouseController::class, 'download']);
|
||||||
Route::resource('houses', \App\Http\Controllers\HouseController::class);
|
Route::resource('houses', \App\Http\Controllers\HouseController::class);
|
||||||
|
|||||||
@@ -54,11 +54,24 @@ class FileUploadTest extends TestCase
|
|||||||
$house = House::first();
|
$house = House::first();
|
||||||
$this->assertTrue(Storage::exists($house->photo));
|
$this->assertTrue(Storage::exists($house->photo));
|
||||||
|
|
||||||
$response = $this->put('houses/1', [
|
$response = $this->put('houses/' . $house->id, [
|
||||||
'name' => 'Some name',
|
'name' => 'Some name',
|
||||||
'photo' => UploadedFile::fake()->image('photo2.jpg')
|
'photo' => UploadedFile::fake()->image('photo2.jpg')
|
||||||
]);
|
]);
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
$this->assertFalse(Storage::exists($house->photo));
|
$this->assertFalse(Storage::exists($house->photo));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user