feat: refonte du gestionnaire de chapitres pour intégrer la génération de fichiers CBZ, le téléchargement d'images en lot et la gestion des requêtes de scraping, avec mise à jour des interfaces et des modèles associés

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-03-28 20:42:21 +01:00
parent cdee6f77fc
commit d7088b14c2
22 changed files with 620 additions and 195 deletions

View File

@@ -3,9 +3,8 @@
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Service\CbzGeneratorInterface;
use App\Domain\Scraping\Domain\Model\ScrapingJob;
use App\Domain\Scraping\Domain\Model\ValueObject\CbzGenerationRequest;
use App\Domain\Scraping\Domain\Model\ValueObject\CbzPath;
use App\Domain\Scraping\Domain\Model\ValueObject\TempDirectory;
readonly class InMemoryCbzGenerator implements CbzGeneratorInterface
{
@@ -13,8 +12,8 @@ readonly class InMemoryCbzGenerator implements CbzGeneratorInterface
{
}
public function generate(ScrapingJob $job, TempDirectory $tempDirectory): CbzPath
public function generate(CbzGenerationRequest $request): CbzPath
{
return new CbzPath('test.cbz');
return new CbzPath('/path/to/test.cbz');
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Service\ImageDownloaderInterface;
use App\Domain\Scraping\Domain\Model\ValueObject\DownloadResult;
use App\Domain\Scraping\Domain\Model\ValueObject\TempDirectory;
class InMemoryImageDownloader implements ImageDownloaderInterface
{
private array $downloadedFiles = [];
private ?\Exception $shouldThrowException = null;
public function download(string $url, string $destination): void
{
if ($this->shouldThrowException) {
throw $this->shouldThrowException;
}
$this->downloadedFiles[$url] = $destination;
}
public function downloadBatch(array $urls, TempDirectory $tempDir, string $jobId): array
{
if ($this->shouldThrowException) {
throw $this->shouldThrowException;
}
$results = [];
foreach ($urls as $index => $url) {
$destination = sprintf('%s/%03d.jpg', $tempDir->getPath(), $index + 1);
$this->download($url, $destination);
$results[] = new DownloadResult($destination, $url);
}
return $results;
}
public function simulateError(\Exception $exception): void
{
$this->shouldThrowException = $exception;
}
public function getDownloadedFiles(): array
{
return $this->downloadedFiles;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Scraping\Domain\Model\Manga;
class InMemoryMangaRepository implements MangaRepositoryInterface
{
private array $mangas = [];
public function __construct()
{
// Ajoute un manga par défaut pour les tests
$this->mangas['test-manga'] = new Manga(
'test-manga',
'Test Manga',
'test-manga',
'2024',
'Test Author',
'A test manga description'
);
}
public function getById(string $id): Manga
{
if (!isset($this->mangas[$id])) {
throw new \RuntimeException('Manga not found');
}
return $this->mangas[$id];
}
public function save(Manga $manga): void
{
$this->mangas[$manga->getId()] = $manga;
}
public function clear(): void
{
$this->mangas = [];
}
}

View File

@@ -3,24 +3,23 @@
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Service\ScraperInterface;
use App\Domain\Scraping\Domain\Model\ScrapingJob;
use App\Domain\Scraping\Domain\Model\ValueObject\CbzPath;
use Ramsey\Uuid\Uuid;
use App\Domain\Scraping\Domain\Model\ValueObject\ScrapingRequest;
use App\Domain\Scraping\Domain\Model\ValueObject\ScrapingResult;
class InMemoryScraperAdapter implements ScraperInterface
{
private ?\Exception $shouldThrowException = null;
public function scrape(ScrapingJob $job): ScrapingJob
public function scrape(ScrapingRequest $request): ScrapingResult
{
if ($this->shouldThrowException) {
$job->fail($this->shouldThrowException->getMessage());
return $job;
throw $this->shouldThrowException;
}
$job->complete();
$job->cbzPath = new CbzPath('/path/to/test.cbz');
return $job;
return new ScrapingResult(
['http://example.com/image1.jpg', 'http://example.com/image2.jpg'],
2
);
}
public function simulateError(\Exception $exception): void

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Repository\SourceRepositoryInterface;
use App\Domain\Scraping\Domain\Model\Source;
use App\Domain\Scraping\Domain\Model\ValueObject\SourceId;
use DateTimeImmutable;
class InMemorySourceRepository implements SourceRepositoryInterface
{
private array $sources = [];
public function __construct()
{
// Ajoute une source par défaut pour les tests
$this->sources['test-source'] = new Source(
new SourceId('test-source'),
'Test Source',
'A test source',
'https://example.com',
[
'imageSelector' => 'img.manga-image',
'nextPageSelector' => null,
'chapterUrlFormat' => 'https://example.com/manga/{slug}/chapter-{chapterNumber}'
],
true,
new DateTimeImmutable(),
new DateTimeImmutable()
);
}
public function getById(string $id): Source
{
if (!isset($this->sources[$id])) {
throw new \RuntimeException('Source not found');
}
return $this->sources[$id];
}
public function save(Source $source): void
{
$this->sources[$source->getId()->getValue()] = $source;
}
public function clear(): void
{
$this->sources = [];
}
}

View File

@@ -10,38 +10,54 @@ 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\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\InMemoryScrapingJobRepository;
use App\Tests\Domain\Scraping\Adapter\InMemorySourceRepository;
use PHPUnit\Framework\TestCase;
class ScrapeChapterHandlerTest extends TestCase
{
private InMemoryScraperAdapter $scraper;
private InMemoryImageDownloader $imageDownloader;
private InMemoryCbzGenerator $cbzGenerator;
private InMemoryScrapingJobRepository $scrapingJobRepository;
private InMemoryChapterRepository $chapterRepository;
private InMemoryMangaRepository $mangaRepository;
private InMemorySourceRepository $sourceRepository;
private InMemoryEventBus $eventBus;
private ScrapeChapterHandler $handler;
protected function setUp(): void
{
$this->scraper = new InMemoryScraperAdapter();
$this->imageDownloader = new InMemoryImageDownloader();
$this->cbzGenerator = new InMemoryCbzGenerator('/test/project/dir');
$this->scrapingJobRepository = new InMemoryScrapingJobRepository();
$this->chapterRepository = new InMemoryChapterRepository();
$this->mangaRepository = new InMemoryMangaRepository();
$this->sourceRepository = new InMemorySourceRepository();
$this->eventBus = new InMemoryEventBus();
$this->chapterRepository->save(new Chapter(
id: '1',
mangaId: '1',
chapterNumber: '2',
mangaId: 'test-manga',
chapterNumber: 2,
volumeNumber: 1,
cbzPath: null,
));
$this->eventBus = new InMemoryEventBus();
$this->handler = new ScrapeChapterHandler(
$this->scraper,
$this->imageDownloader,
$this->cbzGenerator,
$this->scrapingJobRepository,
$this->chapterRepository,
$this->mangaRepository,
$this->sourceRepository,
$this->eventBus
);
}
@@ -49,9 +65,9 @@ class ScrapeChapterHandlerTest extends TestCase
public function testHandleSuccessfully(): void
{
$command = new ScrapeChapter(
mangaId: '1',
mangaId: 'test-manga',
chapterNumber: '2',
sourceId: '3',
sourceId: 'test-source'
);
$this->handler->handle($command);
@@ -66,42 +82,45 @@ class ScrapeChapterHandlerTest extends TestCase
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[1]);
$this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId());
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('1', '2');
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('test-manga', 2);
$this->assertNotNull($chapter->cbzPath);
}
public function testHandleThrowsException(): void
{
$command = new ScrapeChapter(
mangaId: '1',
mangaId: 'test-manga',
chapterNumber: '2',
sourceId: '3',
sourceId: 'test-source'
);
$exception = new \Exception('Scraping failed');
$this->scraper->simulateError($exception);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Scraping failed');
$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('test-manga', $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
protected function tearDown(): void
{
$this->scrapingJobRepository->clear();
$this->chapterRepository->clear();
$this->mangaRepository->clear();
$this->sourceRepository->clear();
}
}