82 lines
2.8 KiB
PHP
82 lines
2.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\ChapterScrapingFailed;
|
|
use App\Domain\Scraping\Domain\Event\ChapterScrapingStarted;
|
|
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 $repository;
|
|
private InMemoryEventBus $eventBus;
|
|
private ScrapeChapterHandler $handler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->scraper = new InMemoryScraperAdapter();
|
|
$this->repository = new InMemoryScrapingJobRepository();
|
|
$this->eventBus = new InMemoryEventBus();
|
|
$this->handler = new ScrapeChapterHandler(
|
|
$this->scraper,
|
|
$this->repository,
|
|
$this->eventBus
|
|
);
|
|
}
|
|
|
|
public function testHandleSuccessfully(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
chapterId: 2,
|
|
sourceId: 3,
|
|
mangaId: 1
|
|
);
|
|
|
|
$this->handler->handle($command);
|
|
|
|
$scrapingJobs = $this->scraper->getJobs();
|
|
$this->assertCount(1, $scrapingJobs);
|
|
$job = $scrapingJobs[0];
|
|
|
|
$savedJobs = $this->repository->getJobs();
|
|
$this->assertCount(1, $savedJobs);
|
|
$this->assertSame($job, $savedJobs[0]);
|
|
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
$this->assertCount(1, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
|
$this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId());
|
|
}
|
|
|
|
public function testHandleThrowsException(): void
|
|
{
|
|
$command = new ScrapeChapter(
|
|
chapterId: 2,
|
|
sourceId: 3,
|
|
mangaId: 1
|
|
);
|
|
|
|
$exception = new \Exception('Scraping failed');
|
|
$this->scraper->simulateError($exception);
|
|
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('Scraping failed');
|
|
|
|
try {
|
|
$this->handler->handle($command);
|
|
} finally {
|
|
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
|
$this->assertCount(1, $dispatchedMessages);
|
|
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[0]);
|
|
$this->assertEquals(2, $dispatchedMessages[0]->getChapterId());
|
|
$this->assertEquals('Scraping failed', $dispatchedMessages[0]->getReason());
|
|
}
|
|
}
|
|
}
|