108 lines
3.8 KiB
PHP
108 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Domain\Scraping\Application\CommandHandler;
|
|
|
|
use App\Domain\Scraping\Application\Command\ScrapeChapter;
|
|
use App\Domain\Scraping\Application\CommandHandler\ScrapeChapterHandler;
|
|
use App\Domain\Scraping\Domain\Event\ChapterScraped;
|
|
use App\Domain\Scraping\Domain\Event\ChapterScrapingFailed;
|
|
use App\Domain\Scraping\Domain\Event\ChapterScrapingStarted;
|
|
use App\Domain\Scraping\Domain\Model\Chapter;
|
|
use App\Domain\Scraping\Domain\Model\ScrapingStatus;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryChapterRepository;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryEventBus;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryScraperAdapter;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryScrapingJobRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ScrapeChapterHandlerTest extends TestCase
|
|
{
|
|
private InMemoryScraperAdapter $scraper;
|
|
private InMemoryScrapingJobRepository $scrapingJobRepository;
|
|
private InMemoryChapterRepository $chapterRepository;
|
|
private InMemoryEventBus $eventBus;
|
|
private ScrapeChapterHandler $handler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->scraper = new InMemoryScraperAdapter();
|
|
$this->scrapingJobRepository = new InMemoryScrapingJobRepository();
|
|
$this->chapterRepository = new InMemoryChapterRepository();
|
|
|
|
$this->chapterRepository->save(new Chapter(
|
|
id: '1',
|
|
mangaId: '1',
|
|
chapterNumber: '2',
|
|
volumeNumber: 1,
|
|
cbzPath: null,
|
|
));
|
|
|
|
$this->eventBus = new InMemoryEventBus();
|
|
$this->handler = new ScrapeChapterHandler(
|
|
$this->scraper,
|
|
$this->scrapingJobRepository,
|
|
$this->chapterRepository,
|
|
$this->eventBus
|
|
);
|
|
}
|
|
|
|
public function testHandleSuccessfully(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
mangaId: '1',
|
|
chapterNumber: '2',
|
|
sourceId: '3',
|
|
);
|
|
|
|
$this->handler->handle($command);
|
|
|
|
$scrapingJobs = $this->scrapingJobRepository->getJobs();
|
|
$this->assertCount(1, $scrapingJobs);
|
|
$job = $scrapingJobs[0];
|
|
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
$this->assertCount(2, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
|
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[1]);
|
|
$this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId());
|
|
|
|
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('1', '2');
|
|
$this->assertNotNull($chapter->cbzPath);
|
|
}
|
|
|
|
public function testHandleThrowsException(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
mangaId: '1',
|
|
chapterNumber: '2',
|
|
sourceId: '3',
|
|
);
|
|
|
|
$exception = new \Exception('Scraping failed');
|
|
$this->scraper->simulateError($exception);
|
|
|
|
$this->handler->handle($command);
|
|
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
|
|
$this->assertCount(2, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
|
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[1]);
|
|
$this->assertEquals('1', $dispatchedMessages[1]->getMangaId());
|
|
$this->assertEquals('2', $dispatchedMessages[1]->getChapterNumber());
|
|
$this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason());
|
|
|
|
$jobs = $this->scrapingJobRepository->getJobs();
|
|
|
|
$this->assertCount(1, $jobs);
|
|
$this->assertEquals(ScrapingStatus::FAILED, $jobs[0]->status);
|
|
$this->assertEquals('Scraping failed', $jobs[0]->failureReason);
|
|
}
|
|
|
|
public function tearDown(): void
|
|
{
|
|
$this->scrapingJobRepository->clear();
|
|
$this->chapterRepository->clear();
|
|
}
|
|
}
|