refactor(manga): merge ChapterRepositoryInterface into MangaRepositoryInterface + pagesDirectory
- Supprime ChapterRepositoryInterface du domaine Manga (et ses implémentations LegacyChapterRepository et InMemoryChapterRepository) - Déplace toutes les méthodes chapter vers MangaRepositoryInterface avec nommage explicite (findChapterById, findVisibleChapterById, updateChapter, deleteChapter, etc.) - Remplace cbzPath par pagesDirectory + pageCount dans le modèle Chapter (transition : toChapterDomain conserve un fallback cbzPath pour les données existantes, updateChapter synchronise les deux colonnes jusqu'à la Phase 4) - Ajoute la migration Doctrine (pages_directory, page_count sur la table chapter) - Met à jour tous les handlers, listeners, query handlers et state providers du domaine Manga pour injecter uniquement MangaRepositoryInterface - Adapte les tests unitaires et InMemoryMangaRepository avec les nouvelles méthodes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
dae215dd3d
commit
c50f1638ee
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Adapter;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Repository\ChapterRepositoryInterface;
|
||||
use App\Domain\Manga\Domain\Model\Chapter;
|
||||
|
||||
class InMemoryChapterRepository implements ChapterRepositoryInterface
|
||||
{
|
||||
/** @var array<string, Chapter> */
|
||||
private array $chapters = [];
|
||||
|
||||
public function findById(string $id): ?Chapter
|
||||
{
|
||||
return $this->chapters[$id] ?? null;
|
||||
}
|
||||
|
||||
public function findVisibleById(string $id): ?Chapter
|
||||
{
|
||||
$chapter = $this->chapters[$id] ?? null;
|
||||
if ($chapter && $chapter->isVisible()) {
|
||||
return $chapter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findByMangaIdAndChapterNumber(string $mangaId, float $chapterNumber): ?Chapter
|
||||
{
|
||||
foreach ($this->chapters as $chapter) {
|
||||
if ($chapter->getMangaId() === $mangaId && $chapter->getNumber() === $chapterNumber) {
|
||||
return $chapter;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save(Chapter $chapter): void
|
||||
{
|
||||
$this->chapters[$chapter->getId()] = $chapter;
|
||||
}
|
||||
|
||||
public function delete(Chapter $chapter): void
|
||||
{
|
||||
unset($this->chapters[$chapter->getId()]);
|
||||
}
|
||||
|
||||
public function findByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_filter(
|
||||
$this->chapters,
|
||||
fn (Chapter $chapter) => $chapter->getMangaId() === $mangaId && $chapter->getVolume() === $volume
|
||||
);
|
||||
}
|
||||
|
||||
public function findVisibleByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_filter(
|
||||
$this->chapters,
|
||||
fn (Chapter $chapter) =>
|
||||
$chapter->getMangaId() === $mangaId &&
|
||||
$chapter->getVolume() === $volume &&
|
||||
$chapter->isVisible()
|
||||
);
|
||||
}
|
||||
|
||||
public function findVisibleWithCbzByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_filter(
|
||||
$this->chapters,
|
||||
fn (Chapter $chapter) =>
|
||||
$chapter->getMangaId() === $mangaId &&
|
||||
$chapter->getVolume() === $volume &&
|
||||
$chapter->isVisible() &&
|
||||
$chapter->isAvailable()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all chapters
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
return array_values($this->chapters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all chapters
|
||||
*/
|
||||
public function clear(): void
|
||||
{
|
||||
$this->chapters = [];
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,9 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
/** @var array<string, array<Chapter>> */
|
||||
private array $chapters = [];
|
||||
|
||||
/** @var array<string, Chapter> */
|
||||
private array $chaptersById = [];
|
||||
|
||||
/** @var array<Chapter> */
|
||||
private array $savedChapters = [];
|
||||
|
||||
@@ -101,12 +104,106 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
return count($this->chapters[$mangaId] ?? []);
|
||||
}
|
||||
|
||||
public function findChapterById(string $id): ?Chapter
|
||||
{
|
||||
return $this->chaptersById[$id] ?? null;
|
||||
}
|
||||
|
||||
public function findVisibleChapterById(string $id): ?Chapter
|
||||
{
|
||||
$chapter = $this->chaptersById[$id] ?? null;
|
||||
if ($chapter && $chapter->isVisible()) {
|
||||
return $chapter;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findChapterByMangaIdAndNumber(string $mangaId, float $chapterNumber): ?Chapter
|
||||
{
|
||||
foreach ($this->chaptersById as $chapter) {
|
||||
if ($chapter->getMangaId() === $mangaId && $chapter->getNumber() === $chapterNumber) {
|
||||
return $chapter;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function saveChapter(Chapter $chapter): ChapterId
|
||||
{
|
||||
$this->savedChapters[] = $chapter;
|
||||
if (!isset($this->chapters[$chapter->getMangaId()])) {
|
||||
$this->chapters[$chapter->getMangaId()] = [];
|
||||
}
|
||||
$this->chapters[$chapter->getMangaId()][] = $chapter;
|
||||
$this->chaptersById[$chapter->getId()] = $chapter;
|
||||
return new ChapterId($chapter->getId());
|
||||
}
|
||||
|
||||
public function updateChapter(Chapter $chapter): void
|
||||
{
|
||||
$this->chaptersById[$chapter->getId()] = $chapter;
|
||||
|
||||
if (isset($this->chapters[$chapter->getMangaId()])) {
|
||||
foreach ($this->chapters[$chapter->getMangaId()] as $key => $existing) {
|
||||
if ($existing->getId() === $chapter->getId()) {
|
||||
$this->chapters[$chapter->getMangaId()][$key] = $chapter;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteChapter(Chapter $chapter): void
|
||||
{
|
||||
unset($this->chaptersById[$chapter->getId()]);
|
||||
|
||||
if (isset($this->chapters[$chapter->getMangaId()])) {
|
||||
$this->chapters[$chapter->getMangaId()] = array_values(
|
||||
array_filter(
|
||||
$this->chapters[$chapter->getMangaId()],
|
||||
fn (Chapter $c) => $c->getId() !== $chapter->getId()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function findChaptersByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->chaptersById,
|
||||
fn (Chapter $chapter) => $chapter->getMangaId() === $mangaId && $chapter->getVolume() === $volume
|
||||
));
|
||||
}
|
||||
|
||||
public function findVisibleChaptersByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->chaptersById,
|
||||
fn (Chapter $chapter) =>
|
||||
$chapter->getMangaId() === $mangaId &&
|
||||
$chapter->getVolume() === $volume &&
|
||||
$chapter->isVisible()
|
||||
));
|
||||
}
|
||||
|
||||
public function findVisibleChaptersWithPagesByMangaIdAndVolume(string $mangaId, int $volume): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->chaptersById,
|
||||
fn (Chapter $chapter) =>
|
||||
$chapter->getMangaId() === $mangaId &&
|
||||
$chapter->getVolume() === $volume &&
|
||||
$chapter->isVisible() &&
|
||||
$chapter->isAvailable()
|
||||
));
|
||||
}
|
||||
|
||||
public function addChaptersToManga(string $mangaId, int $count): void
|
||||
{
|
||||
$this->chapters[$mangaId] = [];
|
||||
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->chapters[$mangaId][] = new Chapter(
|
||||
$chapter = new Chapter(
|
||||
id: new ChapterId((string)$i),
|
||||
mangaId: $mangaId,
|
||||
number: (float)$i,
|
||||
@@ -115,6 +212,8 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
isVisible: true,
|
||||
createdAt: new \DateTimeImmutable()
|
||||
);
|
||||
$this->chapters[$mangaId][] = $chapter;
|
||||
$this->chaptersById[$chapter->getId()] = $chapter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,16 +227,6 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
return null;
|
||||
}
|
||||
|
||||
public function saveChapter(Chapter $chapter): ChapterId
|
||||
{
|
||||
$this->savedChapters[] = $chapter;
|
||||
if (!isset($this->chapters[$chapter->getMangaId()])) {
|
||||
$this->chapters[$chapter->getMangaId()] = [];
|
||||
}
|
||||
$this->chapters[$chapter->getMangaId()][] = $chapter;
|
||||
return new ChapterId($chapter->getId());
|
||||
}
|
||||
|
||||
/** @return array<Chapter> */
|
||||
public function getSavedChapters(): array
|
||||
{
|
||||
@@ -148,6 +237,7 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
$this->mangas = [];
|
||||
$this->chapters = [];
|
||||
$this->chaptersById = [];
|
||||
$this->savedChapters = [];
|
||||
}
|
||||
|
||||
@@ -161,7 +251,6 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
$manga->getDescription()
|
||||
];
|
||||
|
||||
// Ajouter les slugs alternatifs aux champs de recherche
|
||||
foreach ($manga->getAlternativeSlugs() as $altSlug) {
|
||||
$searchableFields[] = $altSlug;
|
||||
}
|
||||
@@ -186,6 +275,7 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
return count($this->search($query, 1, PHP_INT_MAX));
|
||||
}
|
||||
|
||||
public function findExistingChaptersByNumbers(string $mangaId, array $chapterNumbers): array
|
||||
{
|
||||
if (!isset($this->chapters[$mangaId])) {
|
||||
@@ -203,12 +293,10 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
return array_filter(
|
||||
array_values($this->mangas),
|
||||
function (Manga $manga) use ($criteria) {
|
||||
// Vérifier si le monitoring est activé selon le critère
|
||||
if ($manga->getMonitoringStatus()->isEnabled() !== $criteria->enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Vérifier la date de dernière vérification si spécifiée
|
||||
if ($criteria->lastCheckBefore !== null) {
|
||||
$lastCheck = $manga->getLastMonitoringCheck();
|
||||
if ($lastCheck === null || $lastCheck >= $criteria->lastCheckBefore) {
|
||||
|
||||
@@ -12,7 +12,6 @@ use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryChapterRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryPathManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -20,18 +19,15 @@ use PHPUnit\Framework\TestCase;
|
||||
class ImportChapterHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangaRepository $mangaRepository;
|
||||
private InMemoryChapterRepository $chapterRepository;
|
||||
private InMemoryPathManager $pathManager;
|
||||
private ImportChapterHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mangaRepository = new InMemoryMangaRepository();
|
||||
$this->chapterRepository = new InMemoryChapterRepository();
|
||||
$this->pathManager = new InMemoryPathManager();
|
||||
$this->handler = new ImportChapterHandler(
|
||||
$this->mangaRepository,
|
||||
$this->chapterRepository,
|
||||
$this->pathManager
|
||||
);
|
||||
}
|
||||
@@ -66,7 +62,7 @@ class ImportChapterHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
}
|
||||
|
||||
public function test_it_updates_existing_chapter_with_new_cbz(): void
|
||||
public function test_it_updates_existing_chapter_with_new_path(): void
|
||||
{
|
||||
// Arrange
|
||||
$mangaId = 'manga-123';
|
||||
@@ -82,7 +78,7 @@ class ImportChapterHandlerTest extends TestCase
|
||||
);
|
||||
$this->mangaRepository->save($manga);
|
||||
|
||||
// Create an existing chapter without CBZ
|
||||
// Create an existing chapter without pages
|
||||
$existingChapter = new Chapter(
|
||||
new ChapterId('chapter-123'),
|
||||
$mangaId,
|
||||
@@ -92,7 +88,7 @@ class ImportChapterHandlerTest extends TestCase
|
||||
true,
|
||||
null
|
||||
);
|
||||
$this->chapterRepository->save($existingChapter);
|
||||
$this->mangaRepository->saveChapter($existingChapter);
|
||||
|
||||
// Import the same chapter with CBZ
|
||||
$cbzBinary = $this->createValidCbzBinary();
|
||||
@@ -106,18 +102,16 @@ class ImportChapterHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$chapters = $this->chapterRepository->getAll();
|
||||
$this->assertCount(1, $chapters); // Still only one chapter
|
||||
|
||||
$updatedChapter = $chapters[0];
|
||||
$updatedChapter = $this->mangaRepository->findChapterById('chapter-123');
|
||||
$this->assertNotNull($updatedChapter);
|
||||
$this->assertEquals('chapter-123', $updatedChapter->getId());
|
||||
$this->assertEquals($mangaId, $updatedChapter->getMangaId());
|
||||
$this->assertEquals(1.5, $updatedChapter->getNumber());
|
||||
$this->assertEquals('Chapter 1.5', $updatedChapter->getTitle()); // Title preserved
|
||||
$this->assertEquals(1, $updatedChapter->getVolume()); // Volume preserved
|
||||
$this->assertEquals('Chapter 1.5', $updatedChapter->getTitle());
|
||||
$this->assertEquals(1, $updatedChapter->getVolume());
|
||||
$this->assertTrue($updatedChapter->isVisible());
|
||||
$this->assertTrue($updatedChapter->isAvailable()); // Now has CBZ
|
||||
$this->assertStringContainsString('_vol1_ch1.5.cbz', $updatedChapter->getCbzPath());
|
||||
$this->assertTrue($updatedChapter->isAvailable());
|
||||
$this->assertNotNull($updatedChapter->getPagesDirectory());
|
||||
}
|
||||
|
||||
public function test_it_throws_exception_when_manga_not_found(): void
|
||||
@@ -168,24 +162,16 @@ class ImportChapterHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a minimal valid CBZ (ZIP) binary for testing
|
||||
*/
|
||||
private function createValidCbzBinary(): string
|
||||
{
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'cbz');
|
||||
|
||||
// Delete the empty file created by tempnam
|
||||
unlink($tmpFile);
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
|
||||
// Create a new ZIP archive (avoid opening empty file)
|
||||
if ($zip->open($tmpFile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
|
||||
throw new \RuntimeException('Cannot create test CBZ file');
|
||||
}
|
||||
|
||||
// Add a dummy image file to the ZIP
|
||||
$zip->addFromString('image1.jpg', 'fake-image-data');
|
||||
$zip->close();
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryChapterRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryPathManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -19,18 +18,15 @@ use PHPUnit\Framework\TestCase;
|
||||
class ImportVolumeHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangaRepository $mangaRepository;
|
||||
private InMemoryChapterRepository $chapterRepository;
|
||||
private InMemoryPathManager $pathManager;
|
||||
private ImportVolumeHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mangaRepository = new InMemoryMangaRepository();
|
||||
$this->chapterRepository = new InMemoryChapterRepository();
|
||||
$this->pathManager = new InMemoryPathManager();
|
||||
$this->handler = new ImportVolumeHandler(
|
||||
$this->mangaRepository,
|
||||
$this->chapterRepository,
|
||||
$this->pathManager
|
||||
);
|
||||
}
|
||||
@@ -63,7 +59,7 @@ class ImportVolumeHandlerTest extends TestCase
|
||||
true,
|
||||
null
|
||||
);
|
||||
$this->chapterRepository->save($chapter);
|
||||
$this->mangaRepository->saveChapter($chapter);
|
||||
}
|
||||
|
||||
$cbzBinary = $this->createValidCbzBinary();
|
||||
@@ -77,12 +73,12 @@ class ImportVolumeHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$chapters = $this->chapterRepository->findByMangaIdAndVolume($mangaId, $volumeNumber);
|
||||
$chapters = $this->mangaRepository->findChaptersByMangaIdAndVolume($mangaId, $volumeNumber);
|
||||
$this->assertCount(3, $chapters);
|
||||
|
||||
foreach ($chapters as $chapter) {
|
||||
$this->assertTrue($chapter->isAvailable());
|
||||
$this->assertStringContainsString('_vol' . $volumeNumber . '.cbz', $chapter->getCbzPath());
|
||||
$this->assertNotNull($chapter->getPagesDirectory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +149,7 @@ class ImportVolumeHandlerTest extends TestCase
|
||||
$cbzBinary = $this->createValidCbzBinary();
|
||||
$command = new ImportVolume(
|
||||
mangaId: $mangaId,
|
||||
volumeNumber: 999, // Non-existent volume
|
||||
volumeNumber: 999,
|
||||
fileBinary: $cbzBinary
|
||||
);
|
||||
|
||||
@@ -165,9 +161,6 @@ class ImportVolumeHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a minimal valid CBZ (ZIP) binary for testing
|
||||
*/
|
||||
private function createValidCbzBinary(): string
|
||||
{
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'cbz_');
|
||||
@@ -187,7 +180,3 @@ class ImportVolumeHandlerTest extends TestCase
|
||||
return $binaryContent;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user