feat: firsts unit tests for ScrapeChapterHandler.php
This commit is contained in:
parent
21fcdd1084
commit
89570ad951
@@ -0,0 +1,85 @@
|
||||
<?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);
|
||||
|
||||
// Vérifier que le job a été créé
|
||||
$scrapingJobs = $this->scraper->getJobs();
|
||||
$this->assertCount(1, $scrapingJobs);
|
||||
$job = $scrapingJobs[0];
|
||||
|
||||
// Vérifier que le job a été sauvegardé
|
||||
$savedJobs = $this->repository->getJobs();
|
||||
$this->assertCount(1, $savedJobs);
|
||||
$this->assertSame($job, $savedJobs[0]);
|
||||
|
||||
// Vérifier que l'événement a été dispatché
|
||||
$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 {
|
||||
// Vérifier que l'événement d'échec a été dispatché
|
||||
$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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user