Skip to content

Commit

Permalink
Test handle method returns false on upload fail
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 27, 2023
1 parent b49cd29 commit 5ff4b35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
24 changes: 18 additions & 6 deletions app/Jobs/ImageUploadAndResizingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -24,34 +25,45 @@ class ImageUploadAndResizingJob implements ShouldQueue
'640x480' => [640, 480],
];

private ?Filesystem $storage;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(string $mimeType, string $imageContent)
public function __construct(string $mimeType, string $imageContent, ?Filesystem $storage = null)
{
$this->mimeType = $mimeType;
$this->imageContent = $imageContent;
$this->storage = $storage;
}

public function storage()
{
if (! $this->storage)
$this->storage = Storage::disk('public');

return $this->storage;
}

/**
* Execute the job.
*
* @return array
* @return array|bool
*/
public function handle()
{
$storage = Storage::disk('public');

$path = 'fake-image-name.jpg';

$storage->put($path, base64_decode($this->imageContent));
if (! $this->storage()->put($path, base64_decode($this->imageContent))) {
return false;
}

$paths = [];

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

Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Jobs\ImageUploadAndResizingJob;
use App\Models\User;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
Expand Down Expand Up @@ -58,4 +59,22 @@ public function an_image_can_be_uploaded_and_resized_via_a_queued_job()

Storage::disk('public')->assertExists($job->handle());
}

/** @test */
public function handle_method_returns_false_on_upload_fail()
{
$image = UploadedFile::fake()
->image('image.jpg', 50, 50)
->mimeType('image/jpeg');

$storage = $this->mock(Filesystem::class);

// Make the put() method return false so that the image upload fails
$storage->shouldReceive('put')->andReturn(false);

// Pass mocked storage object for returning false from put() method
$job = new ImageUploadAndResizingJob($image->getMimeType(), base64_encode($image->getContent()), $storage);

$this->assertFalse($job->handle());
}
}

0 comments on commit 5ff4b35

Please sign in to comment.