Begin work on unit tests

This commit is contained in:
Jonny Barnes 2023-01-20 16:02:04 +00:00
parent 45c71bbb47
commit c410e344a0
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
4 changed files with 102 additions and 15 deletions

View file

@ -6,10 +6,11 @@ namespace Tests\Unit\Jobs;
use App\Exceptions\InternetArchiveException;
use App\Jobs\ProcessBookmark;
use App\Jobs\SaveScreenshot;
use App\Models\Bookmark;
use App\Services\BookmarkService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class ProcessBookmarkJobTest extends TestCase
@ -17,13 +18,12 @@ class ProcessBookmarkJobTest extends TestCase
use RefreshDatabase;
/** @test */
public function screenshotAndArchiveLinkAreSavedByJob(): void
public function archiveLinkIsSavedByJobAndScreenshotJobIsQueued(): void
{
Queue::fake();
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
->willReturn($uuid->toString());
$service->method('getArchiveLink')
->willReturn('https://web.archive.org/web/1234');
$this->app->instance(BookmarkService::class, $service);
@ -32,19 +32,19 @@ class ProcessBookmarkJobTest extends TestCase
$job->handle();
$this->assertDatabaseHas('bookmarks', [
'screenshot' => $uuid->toString(),
'archive' => 'https://web.archive.org/web/1234',
]);
Queue::assertPushed(SaveScreenshot::class);
}
/** @test */
public function archiveLinkSavedAsNullWhenExceptionThrown(): void
{
Queue::fake();
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
->willReturn($uuid->toString());
$service->method('getArchiveLink')
->will($this->throwException(new InternetArchiveException()));
$this->app->instance(BookmarkService::class, $service);
@ -53,7 +53,7 @@ class ProcessBookmarkJobTest extends TestCase
$job->handle();
$this->assertDatabaseHas('bookmarks', [
'screenshot' => $uuid->toString(),
'id' => $bookmark->id,
'archive' => null,
]);
}