feat: endpoint pour la création d'un manga directement via l'api
This commit is contained in:
parent
4017cabff2
commit
3dc0a0b406
@@ -6,18 +6,34 @@ use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
|
||||
|
||||
class InMemoryImageProcessor implements ImageProcessorInterface
|
||||
{
|
||||
private const string FULL_IMAGE_PATH = '/images/full';
|
||||
private const string THUMBNAIL_PATH = '/images/thumbnails';
|
||||
private const string FAKE_FULL_IMAGE_PATH = '/images/full/test-image.jpg';
|
||||
private const string FAKE_THUMBNAIL_PATH = '/images/thumbnails/test-image.jpg';
|
||||
|
||||
/** @var array<string, string> */
|
||||
private array $downloadedImages = [];
|
||||
|
||||
/** @var array<string, string> */
|
||||
private array $thumbnails = [];
|
||||
|
||||
public function downloadImage(string $imageUrl): string
|
||||
{
|
||||
$filename = sprintf('%s/%s.jpg', self::FULL_IMAGE_PATH, uniqid());
|
||||
return $filename;
|
||||
$this->downloadedImages[$imageUrl] = self::FAKE_FULL_IMAGE_PATH;
|
||||
return self::FAKE_FULL_IMAGE_PATH;
|
||||
}
|
||||
|
||||
public function createThumbnail(string $originalImagePath): string
|
||||
{
|
||||
$filename = basename($originalImagePath);
|
||||
return sprintf('%s/%s', self::THUMBNAIL_PATH, $filename);
|
||||
$this->thumbnails[$originalImagePath] = self::FAKE_THUMBNAIL_PATH;
|
||||
return self::FAKE_THUMBNAIL_PATH;
|
||||
}
|
||||
|
||||
public function getDownloadedImages(): array
|
||||
{
|
||||
return $this->downloadedImages;
|
||||
}
|
||||
|
||||
public function getThumbnails(): array
|
||||
{
|
||||
return $this->thumbnails;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Command\CreateManga;
|
||||
use App\Domain\Manga\Application\CommandHandler\CreateMangaHandler;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryImageProcessor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CreateMangaHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangaRepository $repository;
|
||||
private InMemoryImageProcessor $imageProcessor;
|
||||
private CreateMangaHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryMangaRepository();
|
||||
$this->imageProcessor = new InMemoryImageProcessor();
|
||||
$this->handler = new CreateMangaHandler(
|
||||
$this->repository,
|
||||
$this->imageProcessor
|
||||
);
|
||||
}
|
||||
|
||||
public function testHandleSuccess(): void
|
||||
{
|
||||
// Arrange
|
||||
$command = new CreateManga(
|
||||
title: 'One Piece',
|
||||
slug: 'one-piece',
|
||||
description: 'Description test',
|
||||
author: 'Eiichiro Oda',
|
||||
publicationYear: 1997,
|
||||
genres: ['action', 'adventure'],
|
||||
status: 'ongoing',
|
||||
externalId: 'external-123',
|
||||
imageUrl: 'http://example.com/image.jpg',
|
||||
rating: 4.5
|
||||
);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$savedManga = $this->repository->findAll()[0];
|
||||
$this->assertEquals('One Piece', $savedManga->getTitle()->getValue());
|
||||
$this->assertEquals('one-piece', $savedManga->getSlug()->getValue());
|
||||
$this->assertEquals('external-123', $savedManga->getExternalId()?->getValue());
|
||||
$this->assertNotNull($savedManga->getImageUrls());
|
||||
$this->assertStringStartsWith('/images/full/', $savedManga->getImageUrls()->getFull());
|
||||
$this->assertStringStartsWith('/images/thumbnails/', $savedManga->getImageUrls()->getThumbnail());
|
||||
}
|
||||
|
||||
public function testHandleWithoutImage(): void
|
||||
{
|
||||
// Arrange
|
||||
$command = new CreateManga(
|
||||
title: 'One Piece',
|
||||
slug: 'one-piece',
|
||||
description: 'Description test',
|
||||
author: 'Eiichiro Oda',
|
||||
publicationYear: 1997,
|
||||
genres: ['action', 'adventure'],
|
||||
status: 'ongoing',
|
||||
externalId: 'external-123',
|
||||
imageUrl: null,
|
||||
rating: 4.5
|
||||
);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$savedManga = $this->repository->findAll()[0];
|
||||
$this->assertEquals('One Piece', $savedManga->getTitle()->getValue());
|
||||
$this->assertNull($savedManga->getImageUrls());
|
||||
}
|
||||
}
|
||||
128
tests/Feature/Manga/CreateMangaDirectlyTest.php
Normal file
128
tests/Feature/Manga/CreateMangaDirectlyTest.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Manga;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
|
||||
use App\Tests\Feature\AbstractApiTestCase;
|
||||
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||
|
||||
class CreateMangaDirectlyTest extends AbstractApiTestCase
|
||||
{
|
||||
use ResetDatabase;
|
||||
|
||||
public function testCreateMangaDirectly(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'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
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
// Verify the manga was created in database
|
||||
$entityManager = static::getContainer()->get('doctrine')->getManager();
|
||||
$manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
|
||||
|
||||
$this->assertNotNull($manga);
|
||||
$this->assertEquals('One Piece', $manga->getTitle());
|
||||
$this->assertEquals('Test description', $manga->getDescription());
|
||||
$this->assertEquals('Eiichiro Oda', $manga->getAuthor());
|
||||
$this->assertEquals(1997, $manga->getPublicationYear());
|
||||
$this->assertEquals(['action', 'adventure'], $manga->getGenres());
|
||||
$this->assertEquals('ongoing', $manga->getStatus());
|
||||
$this->assertEquals('external-123', $manga->getExternalId());
|
||||
$this->assertEquals('/images/full/test-image.jpg', $manga->getImageUrl());
|
||||
$this->assertEquals('/images/thumbnails/test-image.jpg', $manga->getThumbnailUrl());
|
||||
$this->assertEquals(4.5, $manga->getRating());
|
||||
}
|
||||
|
||||
public function testCreateMangaWithoutImage(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => 'One Piece',
|
||||
'slug' => 'one-piece',
|
||||
'description' => 'Test description',
|
||||
'author' => 'Eiichiro Oda',
|
||||
'publicationYear' => 1997,
|
||||
'genres' => ['action', 'adventure'],
|
||||
'status' => 'ongoing',
|
||||
'externalId' => 'external-123',
|
||||
'imageUrl' => null,
|
||||
'rating' => 4.5
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$entityManager = static::getContainer()->get('doctrine')->getManager();
|
||||
$manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
|
||||
|
||||
$this->assertNotNull($manga);
|
||||
$this->assertNull($manga->getImageUrl());
|
||||
$this->assertNull($manga->getThumbnailUrl());
|
||||
}
|
||||
|
||||
public function testCreateMangaWithInvalidData(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => '', // Invalid: empty title
|
||||
'publicationYear' => 'invalid', // Invalid: not a number
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(400);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'The type of the "publicationYear" attribute must be "int", "string" given.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testCreateMangaWithInvalidYear(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => 'One Piece',
|
||||
'slug' => 'one-piece',
|
||||
'description' => 'Test description',
|
||||
'author' => 'Eiichiro Oda',
|
||||
'publicationYear' => 2200, // Année invalide > 2100
|
||||
'genres' => ['action', 'adventure'],
|
||||
'status' => 'ongoing',
|
||||
'externalId' => 'external-123',
|
||||
'imageUrl' => null,
|
||||
'rating' => 4.5
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(422);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'publicationYear: L\'année de publication doit être comprise entre 1900 et 2100',
|
||||
'title' => 'An error occurred'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user