feat: Reader beginning

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-16 16:15:42 +01:00
parent e90c0a140e
commit 55945adc53
37 changed files with 1057 additions and 47 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Tests\Domain\Scraping\Adapter;
use App\Domain\Scraping\Domain\Contract\Repository\ChapterRepositoryInterface;
use App\Domain\Scraping\Domain\Model\Chapter;
class InMemoryChapterRepository implements ChapterRepositoryInterface
{
private array $chapters = [];
public function getByMangaIdAndChapterNumber(string $mangaId, int $chapterNumber): Chapter
{
foreach ($this->chapters as $chapter) {
if ($chapter->mangaId === $mangaId && $chapter->chapterNumber === $chapterNumber) {
return $chapter;
}
}
throw new \RuntimeException('Chapter not found');
}
public function save(Chapter $chapter): void
{
$this->chapters[$chapter->id] = $chapter;
}
public function clear(): void
{
$this->chapters = [];
}
}

View File

@@ -4,6 +4,7 @@ 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;
class InMemoryScraperAdapter implements ScraperInterface
@@ -18,6 +19,7 @@ class InMemoryScraperAdapter implements ScraperInterface
}
$job->complete();
$job->cbzPath = new CbzPath('/path/to/test.cbz');
return $job;
}

View File

@@ -28,7 +28,7 @@ class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
public function findByChapterId(string $chapterId): ?ScrapingJob
{
foreach (self::$jobs as $job) {
if ($job->getChapterId() === $chapterId) {
if ($job->getId() === $chapterId) {
return $job;
}
}

View File

@@ -7,7 +7,9 @@ use App\Domain\Scraping\Application\CommandHandler\ScrapeChapterHandler;
use App\Domain\Scraping\Domain\Event\ChapterScraped;
use App\Domain\Scraping\Domain\Event\ChapterScrapingFailed;
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\InMemoryEventBus;
use App\Tests\Domain\Scraping\Adapter\InMemoryScraperAdapter;
use App\Tests\Domain\Scraping\Adapter\InMemoryScrapingJobRepository;
@@ -16,18 +18,30 @@ use PHPUnit\Framework\TestCase;
class ScrapeChapterHandlerTest extends TestCase
{
private InMemoryScraperAdapter $scraper;
private InMemoryScrapingJobRepository $repository;
private InMemoryScrapingJobRepository $scrapingJobRepository;
private InMemoryChapterRepository $chapterRepository;
private InMemoryEventBus $eventBus;
private ScrapeChapterHandler $handler;
protected function setUp(): void
{
$this->scraper = new InMemoryScraperAdapter();
$this->repository = new InMemoryScrapingJobRepository();
$this->scrapingJobRepository = new InMemoryScrapingJobRepository();
$this->chapterRepository = new InMemoryChapterRepository();
$this->chapterRepository->save(new Chapter(
id: '1',
mangaId: '1',
chapterNumber: '2',
volumeNumber: 1,
cbzPath: null,
));
$this->eventBus = new InMemoryEventBus();
$this->handler = new ScrapeChapterHandler(
$this->scraper,
$this->repository,
$this->scrapingJobRepository,
$this->chapterRepository,
$this->eventBus
);
}
@@ -35,16 +49,14 @@ class ScrapeChapterHandlerTest extends TestCase
public function testHandleSuccessfully(): void
{
$command = new ScrapeChapter(
mangaId: 1,
chapterNumber: 2,
sourceId: 3,
mangaId: '1',
chapterNumber: '2',
sourceId: '3',
);
$this->handler->handle($command);
$scrapingJobs = $this->repository->getJobs();
$scrapingJobs = $this->scrapingJobRepository->getJobs();
$this->assertCount(1, $scrapingJobs);
$job = $scrapingJobs[0];
@@ -54,15 +66,16 @@ class ScrapeChapterHandlerTest extends TestCase
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[1]);
$this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId());
$this->repository->clear();
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('1', '2');
$this->assertNotNull($chapter->cbzPath);
}
public function testHandleThrowsException(): void
{
$command = new ScrapeChapter(
mangaId: 1,
chapterNumber: 2,
sourceId: 3,
mangaId: '1',
chapterNumber: '2',
sourceId: '3',
);
$exception = new \Exception('Scraping failed');
@@ -71,15 +84,24 @@ class ScrapeChapterHandlerTest extends TestCase
$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(2, $dispatchedMessages[1]->getChapterNumber());
$this->assertEquals('1', $dispatchedMessages[1]->getMangaId());
$this->assertEquals('2', $dispatchedMessages[1]->getChapterNumber());
$this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason());
$jobs = $this->repository->getJobs();
$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
{
$this->scrapingJobRepository->clear();
$this->chapterRepository->clear();
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace App\Tests\Feature\Reader;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;
final class GetChapterContextTest extends AbstractApiTestCase
{
use ResetDatabase;
public function testItReturnsChapterContext(): void
{
// Arrange
$manga = MangaFactory::createOne([
'slug' => 'manga-1',
]);
$chapter1 = ChapterFactory::createOne([
'manga' => $manga,
'title' => 'Chapter 1',
'number' => 1,
]);
$chapter2 = ChapterFactory::createOne([
'manga' => $manga,
'title' => 'Chapter 2',
'number' => 2,
]);
$chapter3 = ChapterFactory::createOne([
'manga' => $manga,
'title' => 'Chapter 3',
'number' => 3,
]);
// Act
static::createClient()->request('GET', '/api/reader/chapter/' . $chapter1->getId());
// Assert
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'id' => (string)$chapter1->getId(),
'title' => 'Chapter 1',
'number' => 1,
'totalPages' => 0,
'navigation' => [
'hydra:member' => [
null, (string)$chapter2->getId(),
],
]
]);
}
public function testItReturns404ForNonExistentChapter(): void
{
// Act
static::createClient()->request('GET', '/api/reader/chapter/0');
// Assert
$this->assertResponseStatusCodeSame(404);
$this->assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
$this->assertJsonContains([
'hydra:title' => 'An error occurred',
'hydra:description' => 'Le chapitre 0 n\'existe pas',
]);
}
}

View File

@@ -23,7 +23,7 @@ class ScrapeChapterTest extends AbstractApiTestCase
{
// Given
$payload = [
'chapterId' => 'chapter-123',
'chapterNumber' => 'chapter-123',
'sourceId' => 'source-456',
'mangaId' => 'manga-789',
];
@@ -49,7 +49,7 @@ class ScrapeChapterTest extends AbstractApiTestCase
{
// Given
$payload = [
'chapterId' => '',
'chapterNumber' => '',
'sourceId' => 'source-456',
'mangaId' => 'manga-789',
];
@@ -65,7 +65,7 @@ class ScrapeChapterTest extends AbstractApiTestCase
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'chapterId',
'propertyPath' => 'chapterNumber',
'message' => 'This value should not be blank.',
],
],