Skip to content

Commit

Permalink
Test uploaded image has a new name
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 22, 2022
1 parent e08dbd0 commit adde095
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/Http/Controllers/MediaUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ public function upload()

// Use $path to get the image path
}

public function rename()
{
$newImageName = 'you-name-it.jpg';

$path = \request()->file('image')->storeAs('renamed', $newImageName, 'local');

return ['path' => $path];
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@

Route::prefix('media')->controller(MediaUploaderController::class)->group(function() {
Route::post('/upload', 'upload')->name('upload.common');

Route::post('/upload/renamed', 'rename')->name('upload.renamed');
});
});
19 changes: 17 additions & 2 deletions tests/Feature/UploadModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
Expand All @@ -18,7 +17,6 @@ public function a_user_can_upload_an_image()
{
$this->actingAs(User::factory()->create());

// You may fake different disk "local" for example
Storage::fake('public');

$this->post(route('upload.common'), [
Expand All @@ -27,4 +25,21 @@ public function a_user_can_upload_an_image()

Storage::disk('public')->assertExists('common/' . $file->hashName());
}

/** @test */
public function uploaded_image_has_a_new_name()
{
$this->actingAs(User::factory()->create());

Storage::fake('local');

$response = $this->post(route('upload.renamed'), [
'image' => $file = UploadedFile::fake()->image($originalName = 'image-name.jpg'),
]);

Storage::disk('local')
->assertMissing('renamed/' . $originalName)
->assertMissing('renamed/' . $file->hashName())
->assertExists($response->json('path')); // Note this line
}
}

0 comments on commit adde095

Please sign in to comment.