feat: firsts unit tests for ScrapeChapterHandler.php

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-03 10:38:53 +01:00
parent 21fcdd1084
commit 89570ad951
31 changed files with 1105 additions and 291 deletions

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
class InMemoryEventBus implements MessageBusInterface
{
private array $dispatchedMessages = [];
public function dispatch(object $message, array $stamps = []): Envelope
{
$this->dispatchedMessages[] = $message;
return new Envelope($message);
}
public function getDispatchedMessages(): array
{
return $this->dispatchedMessages;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Service\ScraperInterface;
use App\Domain\Scraping\Domain\Model\ScrapingJob;
use Ramsey\Uuid\Uuid;
class InMemoryScraperAdapter implements ScraperInterface
{
private array $jobs = [];
private ?\Exception $shouldThrowException = null;
public function createScrapingJob(string $mangaId, string $chapterId, string $sourceId): ScrapingJob
{
if ($this->shouldThrowException) {
throw $this->shouldThrowException;
}
$job = new ScrapingJob(Uuid::uuid4(), $mangaId, $chapterId, $sourceId);
$this->jobs[] = $job;
return $job;
}
public function scrape(ScrapingJob $job): void
{
if ($this->shouldThrowException) {
throw $this->shouldThrowException;
}
}
public function simulateError(\Exception $exception): void
{
$this->shouldThrowException = $exception;
}
public function getJobs(): array
{
return $this->jobs;
}
public function supports(string $sourceType): bool
{
return true;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
use App\Domain\Scraping\Domain\Model\ScrapingJob;
class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
{
/** @var ScrapingJob[] */
private array $jobs = [];
public function save(ScrapingJob $job): void
{
$this->jobs[] = $job;
}
public function getJobs(): array
{
return $this->jobs;
}
public function findById(string $id): ?ScrapingJob
{
foreach ($this->jobs as $job) {
if ($job->getId() === $id) {
return $job;
}
}
return null;
}
public function findByChapterId(string $chapterId): ?ScrapingJob
{
foreach ($this->jobs as $job) {
if ($job->getChapterId() === $chapterId) {
return $job;
}
}
return null;
}
}

View File

@@ -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());
}
}
}