feat: scraping endpoints, job persistence, firsts unit tests, legacy entities usage

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-07 11:56:51 +01:00
parent c55cd62ec7
commit 0374ab0e46
34 changed files with 348 additions and 326 deletions

View File

@@ -8,21 +8,8 @@ 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) {
@@ -35,11 +22,6 @@ class InMemoryScraperAdapter implements ScraperInterface
$this->shouldThrowException = $exception;
}
public function getJobs(): array
{
return $this->jobs;
}
public function supports(string $sourceType): bool
{
return true;

View File

@@ -33,14 +33,14 @@ class ScrapeChapterHandlerTest extends TestCase
public function testHandleSuccessfully(): void
{
$command = new ScrapeChapter(
chapterId: 2,
mangaId: 1,
chapterNumber: 2,
sourceId: 3,
mangaId: 1
);
$this->handler->handle($command);
$scrapingJobs = $this->scraper->getJobs();
$scrapingJobs = $this->repository->getJobs();
$this->assertCount(1, $scrapingJobs);
$job = $scrapingJobs[0];
@@ -57,9 +57,9 @@ class ScrapeChapterHandlerTest extends TestCase
public function testHandleThrowsException(): void
{
$command = new ScrapeChapter(
chapterId: 2,
mangaId: 1,
chapterNumber: 2,
sourceId: 3,
mangaId: 1
);
$exception = new \Exception('Scraping failed');
@@ -72,10 +72,11 @@ class ScrapeChapterHandlerTest extends TestCase
$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());
$this->assertCount(2, $dispatchedMessages);
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[1]);
$this->assertEquals(2, $dispatchedMessages[1]->getChapterNumber());
$this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason());
}
}
}

View File

@@ -21,7 +21,7 @@ class ScrapingStatusTest extends ApiTestCase
parent::setUp();
self::bootKernel();
$this->messageBus = self::getContainer()->get(MessageBusInterface::class);
$this->repository = self::getContainer()->get(ScrapingJobRepositoryInterface::class);
}
@@ -30,11 +30,11 @@ class ScrapingStatusTest extends ApiTestCase
{
// Given
$jobId = Uuid::uuid4()->toString();
$job = new ScrapingJob($jobId, 'manga-123', 'chapter-456', 'source-789');
$job = new ScrapingJob($jobId, 'manga-123', 1, 'source-789');
$job->addPage(new PageNumber(1), new ImageUrl('http://example.com/page1.jpg'));
$job->addPage(new PageNumber(2), new ImageUrl('http://example.com/page2.jpg'));
$this->repository->save($job);
// When
@@ -63,7 +63,7 @@ class ScrapingStatusTest extends ApiTestCase
protected function tearDown(): void
{
parent::tearDown();
self::ensureKernelShutdown();
}
}
}