132 lines
5.1 KiB
PHP
132 lines
5.1 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\Shared\Domain\Model\JobStatus;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryChapterRepository;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryCbzGenerator;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryEventBus;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryImageDownloader;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryMangaRepository;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemoryScraperAdapter;
|
|
use App\Tests\Domain\Scraping\Adapter\InMemorySourceRepository;
|
|
use App\Tests\Domain\Shared\Adapter\InMemoryJobRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
class ScrapeChapterHandlerTest extends TestCase
|
|
{
|
|
private InMemoryScraperAdapter $scraper;
|
|
private InMemoryImageDownloader $imageDownloader;
|
|
private InMemoryCbzGenerator $cbzGenerator;
|
|
private InMemoryJobRepository $jobRepository;
|
|
private InMemoryChapterRepository $chapterRepository;
|
|
private InMemoryMangaRepository $mangaRepository;
|
|
private InMemorySourceRepository $sourceRepository;
|
|
private InMemoryEventBus $eventBus;
|
|
private EntityManagerInterface|MockObject $entityManager;
|
|
private ScrapeChapterHandler $handler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->scraper = new InMemoryScraperAdapter();
|
|
$this->imageDownloader = new InMemoryImageDownloader();
|
|
$this->cbzGenerator = new InMemoryCbzGenerator('/test/project/dir');
|
|
$this->jobRepository = new InMemoryJobRepository();
|
|
$this->chapterRepository = new InMemoryChapterRepository();
|
|
$this->mangaRepository = new InMemoryMangaRepository();
|
|
$this->sourceRepository = new InMemorySourceRepository();
|
|
$this->eventBus = new InMemoryEventBus();
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->entityManager->method('beginTransaction')->willReturn(null);
|
|
$this->entityManager->method('commit')->willReturn(null);
|
|
$this->entityManager->method('rollback')->willReturn(null);
|
|
|
|
$this->chapterRepository->save(new Chapter(
|
|
id: '1',
|
|
mangaId: 'test-manga',
|
|
chapterNumber: 2,
|
|
volumeNumber: 1,
|
|
cbzPath: null,
|
|
));
|
|
|
|
$this->handler = new ScrapeChapterHandler(
|
|
$this->scraper,
|
|
$this->imageDownloader,
|
|
$this->cbzGenerator,
|
|
$this->jobRepository,
|
|
$this->chapterRepository,
|
|
$this->mangaRepository,
|
|
$this->sourceRepository,
|
|
$this->eventBus,
|
|
$this->entityManager
|
|
);
|
|
}
|
|
|
|
public function testHandleSuccessfully(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
mangaId: 'test-manga',
|
|
chapterNumber: '2',
|
|
sourceId: 'test-source'
|
|
);
|
|
|
|
$this->handler->handle($command);
|
|
|
|
$job = $this->jobRepository->findByType('scraping_job');
|
|
$this->assertCount(1, $job);
|
|
$job = array_values($job)[0];
|
|
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
$this->assertCount(1, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[0]);
|
|
$this->assertEquals($job->id, $dispatchedMessages[0]->getJobId());
|
|
|
|
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('test-manga', 2);
|
|
$this->assertNotNull($chapter->cbzPath);
|
|
}
|
|
|
|
public function testHandleThrowsException(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
mangaId: 'test-manga',
|
|
chapterNumber: '2',
|
|
sourceId: 'test-source'
|
|
);
|
|
|
|
$exception = new \Exception('Scraping failed');
|
|
$this->scraper->simulateError($exception);
|
|
|
|
$this->handler->handle($command);
|
|
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
$this->assertCount(1, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[0]);
|
|
$this->assertEquals('test-manga', $dispatchedMessages[0]->getMangaId());
|
|
$this->assertEquals('2', $dispatchedMessages[0]->getChapterNumber());
|
|
$this->assertEquals('Scraping failed', $dispatchedMessages[0]->getReason());
|
|
|
|
$jobs = $this->jobRepository->findByType('scraping_job');
|
|
$job = array_values($jobs)[0];
|
|
$this->assertCount(1, $jobs);
|
|
$this->assertEquals(JobStatus::FAILED, $job->status);
|
|
$this->assertEquals('Scraping failed', $job->failureReason);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$this->jobRepository->clear();
|
|
$this->chapterRepository->clear();
|
|
$this->mangaRepository->clear();
|
|
$this->sourceRepository->clear();
|
|
}
|
|
}
|