Skip to content

Commit 5ff4b35

Browse files
committed
Test handle method returns false on upload fail
1 parent b49cd29 commit 5ff4b35

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

app/Jobs/ImageUploadAndResizingJob.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Jobs;
44

55
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Filesystem\Filesystem;
67
use Illuminate\Contracts\Queue\ShouldBeUnique;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Foundation\Bus\Dispatchable;
@@ -24,34 +25,45 @@ class ImageUploadAndResizingJob implements ShouldQueue
2425
'640x480' => [640, 480],
2526
];
2627

28+
private ?Filesystem $storage;
29+
2730
/**
2831
* Create a new job instance.
2932
*
3033
* @return void
3134
*/
32-
public function __construct(string $mimeType, string $imageContent)
35+
public function __construct(string $mimeType, string $imageContent, ?Filesystem $storage = null)
3336
{
3437
$this->mimeType = $mimeType;
3538
$this->imageContent = $imageContent;
39+
$this->storage = $storage;
40+
}
41+
42+
public function storage()
43+
{
44+
if (! $this->storage)
45+
$this->storage = Storage::disk('public');
46+
47+
return $this->storage;
3648
}
3749

3850
/**
3951
* Execute the job.
4052
*
41-
* @return array
53+
* @return array|bool
4254
*/
4355
public function handle()
4456
{
45-
$storage = Storage::disk('public');
46-
4757
$path = 'fake-image-name.jpg';
4858

49-
$storage->put($path, base64_decode($this->imageContent));
59+
if (! $this->storage()->put($path, base64_decode($this->imageContent))) {
60+
return false;
61+
}
5062

5163
$paths = [];
5264

5365
foreach ($this->resolutions as $key => $resolution) {
54-
$image = Image::make($absolutePath = $storage->path($path))
66+
$image = Image::make($absolutePath = $this->storage()->path($path))
5567
->resize($resolution[0], $resolution[1])
5668
->save($this->absolutePathWithResolutionKey($absolutePath, $key));
5769

tests/Feature/JobTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Jobs\ImageUploadAndResizingJob;
66
use App\Models\User;
7+
use Illuminate\Contracts\Filesystem\Filesystem;
78
use Illuminate\Foundation\Bus\Dispatchable;
89
use Illuminate\Foundation\Testing\RefreshDatabase;
910
use Illuminate\Foundation\Testing\WithFaker;
@@ -58,4 +59,22 @@ public function an_image_can_be_uploaded_and_resized_via_a_queued_job()
5859

5960
Storage::disk('public')->assertExists($job->handle());
6061
}
62+
63+
/** @test */
64+
public function handle_method_returns_false_on_upload_fail()
65+
{
66+
$image = UploadedFile::fake()
67+
->image('image.jpg', 50, 50)
68+
->mimeType('image/jpeg');
69+
70+
$storage = $this->mock(Filesystem::class);
71+
72+
// Make the put() method return false so that the image upload fails
73+
$storage->shouldReceive('put')->andReturn(false);
74+
75+
// Pass mocked storage object for returning false from put() method
76+
$job = new ImageUploadAndResizingJob($image->getMimeType(), base64_encode($image->getContent()), $storage);
77+
78+
$this->assertFalse($job->handle());
79+
}
6180
}

0 commit comments

Comments
 (0)