feat: GetChapters endpoint + tests
This commit is contained in:
parent
2f615a4936
commit
6667cc224b
@@ -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 = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Application\QueryHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Query\GetMangaChapters;
|
||||
use App\Domain\Manga\Application\QueryHandler\GetMangaChaptersHandler;
|
||||
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
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\InMemoryMangaRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GetMangaChaptersHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangaRepository $repository;
|
||||
private GetMangaChaptersHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryMangaRepository();
|
||||
$this->handler = new GetMangaChaptersHandler($this->repository);
|
||||
}
|
||||
|
||||
public function testHandleThrowsExceptionWhenMangaNotFound(): void
|
||||
{
|
||||
$this->expectException(MangaNotFoundException::class);
|
||||
|
||||
$query = new GetMangaChapters('non-existent-id');
|
||||
$this->handler->handle($query);
|
||||
}
|
||||
|
||||
public function testHandleReturnsEmptyListWhenNoChapters(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->givenMangaExists('123');
|
||||
|
||||
// Act
|
||||
$query = new GetMangaChapters('123');
|
||||
$response = $this->handler->handle($query);
|
||||
|
||||
// Assert
|
||||
$this->assertEmpty($response->chapters);
|
||||
$this->assertEquals(0, $response->total);
|
||||
$this->assertEquals(1, $response->page);
|
||||
$this->assertEquals(20, $response->limit);
|
||||
$this->assertFalse($response->hasNextPage());
|
||||
$this->assertFalse($response->hasPreviousPage());
|
||||
}
|
||||
|
||||
public function testHandleReturnsPaginatedChapters(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->givenMangaExistsWithChapters('123', 25);
|
||||
|
||||
// Act
|
||||
$query = new GetMangaChapters('123', page: 2, limit: 10);
|
||||
$response = $this->handler->handle($query);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(10, $response->chapters);
|
||||
$this->assertEquals(25, $response->total);
|
||||
$this->assertEquals(2, $response->page);
|
||||
$this->assertEquals(10, $response->limit);
|
||||
$this->assertTrue($response->hasNextPage());
|
||||
$this->assertTrue($response->hasPreviousPage());
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->repository->clear();
|
||||
}
|
||||
|
||||
private function givenMangaExists(string $id): void
|
||||
{
|
||||
$manga = $this->createManga($id);
|
||||
$this->repository->save($manga);
|
||||
}
|
||||
|
||||
private function givenMangaExistsWithChapters(string $id, int $chapterCount): void
|
||||
{
|
||||
$this->givenMangaExists($id);
|
||||
// Note: We'll need to implement this in InMemoryMangaRepository
|
||||
$this->repository->addChaptersToManga($id, $chapterCount);
|
||||
}
|
||||
|
||||
private function createManga(string $id): Manga
|
||||
{
|
||||
return new Manga(
|
||||
id: new MangaId($id),
|
||||
title: new MangaTitle('Test Manga'),
|
||||
slug: new MangaSlug('test-manga'),
|
||||
description: 'This is a test manga',
|
||||
author: 'Test Author',
|
||||
publicationYear: 2024,
|
||||
genres: ['Action', 'Adventure'],
|
||||
status: 'Ongoing',
|
||||
externalId: null,
|
||||
imageUrl: null,
|
||||
rating: null
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user