Skip to content

Commit

Permalink
Test a job can be queued
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 27, 2023
1 parent 6de30ac commit 4191243
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/Http/Controllers/MediaUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Http\Requests\UploadMultipleFilesRequest;
use App\Jobs\ImageUploadAndResizingJob;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -95,4 +96,14 @@ public function download($filename)

return response()->download($privateFilepath, $filename);
}

public function uploadAndResizing()
{
$image = request()->file('image');

if ($image instanceof UploadedFile) {
ImageUploadAndResizingJob::dispatch($image->getMimeType(), base64_encode($image->getContent()))
->delay(now()->addSeconds(5));
}
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

Route::post('/upload/resize', 'resize')->name('upload.resize');

Route::post('/upload/resize/via/queue', 'uploadAndResizing')->name('upload.resize.via.queue');

Route::post('/upload/private', 'uploadPrivate')->name('upload.private');

Route::get('/upload/download/{filename}', 'download')->name('upload.download');
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace Tests\Feature;

use App\Jobs\ImageUploadAndResizingJob;
use App\Models\User;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class JobTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_job_is_dispatchable()
{
Expand All @@ -22,4 +27,20 @@ public function a_job_is_dispatchable()
'handle'
));
}

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

Queue::fake();

$this->post(route('upload.resize.via.queue'), [
'image' => $image = UploadedFile::fake()
->image('image.jpg', 50, 50)
->mimeType('image/jpeg')
]);

Queue::assertPushed(ImageUploadAndResizingJob::class);
}
}

0 comments on commit 4191243

Please sign in to comment.