feat: GetChapters endpoint + tests

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-10 20:07:24 +01:00
parent 2f615a4936
commit 6667cc224b
15 changed files with 601 additions and 0 deletions

View File

@@ -3,12 +3,17 @@
namespace App\Tests\Domain\Manga\Adapter;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\Manga;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
class InMemoryMangaRepository implements MangaRepositoryInterface
{
/** @var Manga[] */
private array $mangas = [];
/** @var array<string, Chapter[]> */
private array $chapters = [];
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
{
@@ -66,8 +71,49 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
};
}
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array
{
if (!isset($this->chapters[$mangaId])) {
return [];
}
$chapters = $this->chapters[$mangaId];
usort($chapters, function (Chapter $a, Chapter $b) use ($sortOrder) {
return $sortOrder === 'desc'
? $b->getNumber() <=> $a->getNumber()
: $a->getNumber() <=> $b->getNumber();
});
$offset = ($page - 1) * $limit;
return array_slice($chapters, $offset, $limit);
}
public function countChapters(string $mangaId): int
{
return isset($this->chapters[$mangaId]) ? count($this->chapters[$mangaId]) : 0;
}
public function addChaptersToManga(string $mangaId, int $count): void
{
$this->chapters[$mangaId] = [];
for ($i = 1; $i <= $count; $i++) {
$this->chapters[$mangaId][] = new Chapter(
id: new ChapterId((string)$i),
mangaId: $mangaId,
number: (float)$i,
title: "Chapter $i",
volume: (int)ceil($i / 10),
isVisible: true,
createdAt: new \DateTimeImmutable()
);
}
}
public function clear(): void
{
$this->mangas = [];
$this->chapters = [];
}
}