feat: GetManga endpoint + tests

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-10 19:40:47 +01:00
parent e3d380eadd
commit 2f615a4936
10 changed files with 301 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Domain\Manga\Application\Query;
readonly class GetManga
{
public function __construct(
public string $id
) {}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Domain\Manga\Application\QueryHandler;
use App\Domain\Manga\Application\Query\GetManga;
use App\Domain\Manga\Application\Response\MangaResponse;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
readonly class GetMangaHandler
{
public function __construct(
private MangaRepositoryInterface $mangaRepository
) {}
public function handle(GetManga $query): MangaResponse
{
$manga = $this->mangaRepository->findById($query->id);
if (!$manga) {
throw new MangaNotFoundException();
}
return new MangaResponse(
id: $manga->getId()->getValue(),
title: $manga->getTitle()->getValue(),
slug: $manga->getSlug()->getValue(),
description: $manga->getDescription(),
author: $manga->getAuthor(),
publicationYear: $manga->getPublicationYear(),
genres: $manga->getGenres(),
status: $manga->getStatus(),
externalId: $manga->getExternalId()?->getValue(),
imageUrl: $manga->getImageUrl(),
rating: $manga->getRating()
);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Domain\Manga\Application\Response;
readonly class MangaResponse
{
public function __construct(
public string $id,
public string $title,
public string $slug,
public string $description,
public string $author,
public int $publicationYear,
public array $genres,
public string $status,
public ?string $externalId,
public ?string $imageUrl,
public ?float $rating
) {}
}

View File

@@ -2,6 +2,8 @@
namespace App\Domain\Manga\Domain\Exception; namespace App\Domain\Manga\Domain\Exception;
class MangaDomainException extends \DomainException use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class MangaDomainException extends NotFoundHttpException
{ {
} }

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Domain\Manga\Domain\Exception;
class MangaNotFoundException extends MangaDomainException
{
public function __construct()
{
parent::__construct('Manga not found');
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
use ApiPlatform\Metadata\ApiProperty;
readonly class MangaDetail
{
public function __construct(
#[ApiProperty(identifier: true)]
public string $id,
public string $title,
public string $slug,
public string $description,
public string $author,
public int $publicationYear,
public array $genres,
public string $status,
public ?string $externalId,
public ?string $imageUrl,
public ?float $rating
) {}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaDetail;
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\GetMangaStateProvider;
#[ApiResource(
shortName: 'Manga',
operations: [
new Get(
uriTemplate: '/mangas/{id}',
provider: GetMangaStateProvider::class,
output: MangaDetail::class
)
]
)]
class MangaResource
{
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Domain\Manga\Application\Query\GetManga;
use App\Domain\Manga\Application\QueryHandler\GetMangaHandler;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaDetail;
readonly class GetMangaStateProvider implements ProviderInterface
{
public function __construct(
private GetMangaHandler $handler
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MangaDetail
{
$query = new GetManga($uriVariables['id']);
$response = $this->handler->handle($query);
return new MangaDetail(
id: $response->id,
title: $response->title,
slug: $response->slug,
description: $response->description,
author: $response->author,
publicationYear: $response->publicationYear,
genres: $response->genres,
status: $response->status,
externalId: $response->externalId,
imageUrl: $response->imageUrl,
rating: $response->rating
);
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Tests\Domain\Manga\Application\QueryHandler;
use App\Domain\Manga\Application\Query\GetManga;
use App\Domain\Manga\Application\QueryHandler\GetMangaHandler;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
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\InMemoryMangaRepository;
use PHPUnit\Framework\TestCase;
class GetMangaHandlerTest extends TestCase
{
private InMemoryMangaRepository $repository;
private GetMangaHandler $handler;
protected function setUp(): void
{
$this->repository = new InMemoryMangaRepository();
$this->handler = new GetMangaHandler($this->repository);
}
public function testHandleThrowsExceptionWhenMangaNotFound(): void
{
$this->expectException(MangaNotFoundException::class);
$query = new GetManga('non-existent-id');
$this->handler->handle($query);
}
public function testHandleReturnsMangaResponse(): void
{
// Arrange
$manga = new Manga(
new MangaId('123'),
new MangaTitle('One Piece'),
new MangaSlug('one-piece'),
'Description test',
'Eiichiro Oda',
1997,
['action', 'adventure'],
'ongoing',
new ExternalId('external-123'),
'http://example.com/image.jpg',
4.5
);
$this->repository->save($manga);
// Act
$query = new GetManga('123');
$response = $this->handler->handle($query);
// Assert
$this->assertEquals('123', $response->id);
$this->assertEquals('One Piece', $response->title);
$this->assertEquals('one-piece', $response->slug);
$this->assertEquals('Description test', $response->description);
$this->assertEquals('Eiichiro Oda', $response->author);
$this->assertEquals(1997, $response->publicationYear);
$this->assertEquals(['action', 'adventure'], $response->genres);
$this->assertEquals('ongoing', $response->status);
$this->assertEquals('external-123', $response->externalId);
$this->assertEquals('http://example.com/image.jpg', $response->imageUrl);
$this->assertEquals(4.5, $response->rating);
}
protected function tearDown(): void
{
$this->repository->clear();
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Tests\Feature\Manga;
use App\Entity\Manga;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;
class GetMangaTest extends AbstractApiTestCase
{
use ResetDatabase;
public function testGetNonExistentManga(): void
{
// When
$client = static::createClient();
$response = $client->request('GET', '/api/mangas/999');
// Then
$this->assertResponseStatusCodeSame(404);
}
public function testGetExistingManga(): void
{
// Given
$manga = new Manga();
$manga->setTitle('One Piece')
->setSlug('one-piece')
->setDescription('Test description')
->setAuthor('Eiichiro Oda')
->setPublicationYear(1997)
->setGenres(['action', 'adventure'])
->setStatus('ongoing')
->setExternalId('external-123')
->setImageUrl('http://example.com/image.jpg')
->setRating(4.5)
->setMonitored(true);
$entityManager = static::getContainer()->get('doctrine')->getManager();
$entityManager->persist($manga);
$entityManager->flush();
// When
$client = static::createClient();
$response = $client->request('GET', '/api/mangas/' . $manga->getId());
// Then
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'id' => (string) $manga->getId(),
'title' => 'One Piece',
'slug' => 'one-piece',
'description' => 'Test description',
'author' => 'Eiichiro Oda',
'publicationYear' => 1997,
'genres' => ['action', 'adventure'],
'status' => 'ongoing',
'externalId' => 'external-123',
'imageUrl' => 'http://example.com/image.jpg',
'rating' => 4.5
]);
}
}