feat: endpoint FetchMangaChapters et tests
This commit is contained in:
parent
3dc0a0b406
commit
879b8fa2dc
@@ -6,18 +6,22 @@ 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;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
||||
|
||||
class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
/** @var Manga[] */
|
||||
/** @var array<string, Manga> */
|
||||
private array $mangas = [];
|
||||
|
||||
/** @var array<string, Chapter[]> */
|
||||
/** @var array<string, array<Chapter>> */
|
||||
private array $chapters = [];
|
||||
|
||||
/** @var array<Chapter> */
|
||||
private array $savedChapters = [];
|
||||
|
||||
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
|
||||
{
|
||||
$sortedMangas = $this->mangas;
|
||||
$sortedMangas = array_values($this->mangas);
|
||||
|
||||
usort($sortedMangas, function (Manga $a, Manga $b) use ($sortBy, $sortOrder) {
|
||||
$valueA = $this->getPropertyValue($a, $sortBy);
|
||||
@@ -40,26 +44,17 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
|
||||
public function findById(string $id): ?Manga
|
||||
{
|
||||
foreach ($this->mangas as $manga) {
|
||||
if ($manga->getId()->getValue() === $id) {
|
||||
return $manga;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return $this->mangas[$id] ?? null;
|
||||
}
|
||||
|
||||
public function save(Manga $manga): void
|
||||
{
|
||||
$this->mangas[] = $manga;
|
||||
$this->mangas[$manga->getId()->getValue()] = $manga;
|
||||
}
|
||||
|
||||
public function delete(Manga $manga): void
|
||||
{
|
||||
$this->mangas = array_filter(
|
||||
$this->mangas,
|
||||
fn(Manga $existingManga) => !$existingManga->getId()->equals($manga->getId())
|
||||
);
|
||||
unset($this->mangas[$manga->getId()->getValue()]);
|
||||
}
|
||||
|
||||
private function getPropertyValue(Manga $manga, string $property): mixed
|
||||
@@ -91,7 +86,7 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
|
||||
public function countChapters(string $mangaId): int
|
||||
{
|
||||
return isset($this->chapters[$mangaId]) ? count($this->chapters[$mangaId]) : 0;
|
||||
return count($this->chapters[$mangaId] ?? []);
|
||||
}
|
||||
|
||||
public function addChaptersToManga(string $mangaId, int $count): void
|
||||
@@ -111,9 +106,35 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function findByExternalId(ExternalId $externalId): ?Manga
|
||||
{
|
||||
foreach ($this->mangas as $manga) {
|
||||
if ($manga->getExternalId() && $manga->getExternalId()->getValue() === $externalId->getValue()) {
|
||||
return $manga;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function saveChapter(Chapter $chapter): void
|
||||
{
|
||||
$this->savedChapters[] = $chapter;
|
||||
if (!isset($this->chapters[$chapter->getMangaId()])) {
|
||||
$this->chapters[$chapter->getMangaId()] = [];
|
||||
}
|
||||
$this->chapters[$chapter->getMangaId()][] = $chapter;
|
||||
}
|
||||
|
||||
/** @return array<Chapter> */
|
||||
public function getSavedChapters(): array
|
||||
{
|
||||
return $this->savedChapters;
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
$this->mangas = [];
|
||||
$this->chapters = [];
|
||||
$this->savedChapters = [];
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ class InMemoryMangadexClient implements MangadexClientInterface
|
||||
private array $mangas = [];
|
||||
private array $feeds = [];
|
||||
private array $aggregates = [];
|
||||
private array $mangaFeeds = [];
|
||||
|
||||
public function __construct(
|
||||
array $mangas = [],
|
||||
@@ -61,22 +62,7 @@ class InMemoryMangadexClient implements MangadexClientInterface
|
||||
|
||||
public function getMangaFeed(string $mangaId, int $offset = 0, int $limit = 500, string $order = 'asc'): array
|
||||
{
|
||||
if (!isset($this->feeds[$mangaId])) {
|
||||
return [
|
||||
'data' => [],
|
||||
'total' => 0
|
||||
];
|
||||
}
|
||||
|
||||
$feed = $this->feeds[$mangaId];
|
||||
if ($order === 'desc') {
|
||||
$feed = array_reverse($feed);
|
||||
}
|
||||
|
||||
return [
|
||||
'data' => array_slice($feed, $offset, $limit),
|
||||
'total' => count($feed)
|
||||
];
|
||||
return $this->mangaFeeds[$mangaId] ?? ['data' => []];
|
||||
}
|
||||
|
||||
public function getMangaAggregate(string $mangaId): array
|
||||
@@ -120,4 +106,9 @@ class InMemoryMangadexClient implements MangadexClientInterface
|
||||
{
|
||||
$this->aggregates[$mangaId] = $aggregate;
|
||||
}
|
||||
|
||||
public function setMangaFeed(string $mangaId, array $feed): void
|
||||
{
|
||||
$this->mangaFeeds[$mangaId] = $feed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Command\FetchMangaChapters;
|
||||
use App\Domain\Manga\Application\CommandHandler\FetchMangaChaptersHandler;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
||||
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\InMemoryMangadexClient;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FetchMangaChaptersHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangadexClient $mangadexClient;
|
||||
private InMemoryMangaRepository $mangaRepository;
|
||||
private FetchMangaChaptersHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mangadexClient = new InMemoryMangadexClient();
|
||||
$this->mangaRepository = new InMemoryMangaRepository();
|
||||
$this->handler = new FetchMangaChaptersHandler(
|
||||
$this->mangadexClient,
|
||||
$this->mangaRepository
|
||||
);
|
||||
}
|
||||
|
||||
public function testHandleWithExistingManga(): void
|
||||
{
|
||||
$externalId = 'manga-123';
|
||||
$manga = new Manga(
|
||||
new MangaId('manga-id'),
|
||||
new MangaTitle('Test Manga'),
|
||||
new MangaSlug('test-manga'),
|
||||
'Description',
|
||||
'Author',
|
||||
2024,
|
||||
[],
|
||||
'ongoing',
|
||||
new ExternalId($externalId)
|
||||
);
|
||||
|
||||
$this->mangaRepository->save($manga);
|
||||
|
||||
$this->mangadexClient->setMangaFeed($externalId, [
|
||||
'data' => [
|
||||
[
|
||||
'id' => 'chapter-1',
|
||||
'attributes' => [
|
||||
'chapter' => '1',
|
||||
'title' => 'Chapter 1',
|
||||
'volume' => '1'
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$command = new FetchMangaChapters($externalId);
|
||||
$this->handler->handle($command);
|
||||
|
||||
$this->assertCount(1, $this->mangaRepository->getSavedChapters());
|
||||
}
|
||||
|
||||
public function testHandleWithNonExistingManga(): void
|
||||
{
|
||||
$externalId = 'non-existing-manga';
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('Manga not found');
|
||||
|
||||
$command = new FetchMangaChapters($externalId);
|
||||
$this->handler->handle($command);
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class GetMangaChaptersHandlerTest extends TestCase
|
||||
// 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);
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Scraping\Adapter;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class InMemoryMessageBus implements MessageBusInterface
|
||||
{
|
||||
/** @var array<object> */
|
||||
public static array $messages = [];
|
||||
|
||||
public function dispatch(object $message, array $stamps = []): Envelope
|
||||
{
|
||||
self::$messages[] = $message;
|
||||
return new Envelope($message);
|
||||
}
|
||||
|
||||
public function getDispatchedMessages(): array
|
||||
{
|
||||
return self::$messages;
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
self::$messages = [];
|
||||
}
|
||||
|
||||
public function hasMessageOfType(string $messageClass): bool
|
||||
{
|
||||
foreach (self::$messages as $message) {
|
||||
if ($message instanceof $messageClass) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user