feat: ajout de la gestion des commandes pour la suppression des fichiers CBZ et des chapitres, avec création des gestionnaires et des ressources API correspondantes

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-06-29 18:33:33 +02:00
parent 7fe4ac0d3b
commit 37e1b202c2
42 changed files with 1413 additions and 21 deletions

View File

@@ -44,6 +44,23 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
$this->mangas[$manga->getId()] = $manga;
}
public function updatePreferredSources(string $mangaId, array $sourceIds): void
{
if (isset($this->mangas[$mangaId])) {
$manga = $this->mangas[$mangaId];
$updatedManga = new Manga(
$manga->getId(),
$manga->getTitle(),
$manga->getSlug(),
$manga->getDescription(),
$manga->getAuthor(),
$manga->getPublicationYear(),
$sourceIds // Mise à jour des sources préférées
);
$this->mangas[$mangaId] = $updatedManga;
}
}
public function clear(): void
{
$this->mangas = [];

View File

@@ -50,6 +50,42 @@ class InMemorySourceRepository implements SourceRepositoryInterface
$this->sources[$source->getId()->getValue()] = $source;
}
public function validateSourcesExist(array $sourceIds): bool
{
foreach ($sourceIds as $sourceId) {
$source = $this->sources[$sourceId] ?? null;
if (!$source || !$source->isActive()) {
return false;
}
}
return true;
}
/**
* @return Source[]
*/
public function getByIds(array $sourceIds): array
{
$sources = [];
foreach ($sourceIds as $sourceId) {
if (isset($this->sources[$sourceId])) {
$sources[] = $this->sources[$sourceId];
}
}
return $sources;
}
/**
* @return Source[]
*/
public function getAllActive(): array
{
return array_filter(
array_values($this->sources),
fn(Source $source) => $source->isActive()
);
}
public function clear(): void
{
$this->sources = [];

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Tests\Feature\Manga;
use App\Entity\Chapter;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
class DeleteCbzTest extends AbstractApiTestCase
{
use ResetDatabase, Factories;
public function test_it_deletes_chapter_cbz_file(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Chapter 1',
'visible' => true,
'cbzPath' => '/path/to/test.cbz'
]);
$this->entityManager->flush();
$chapterId = $chapter->getId();
// Act
static::createClient()->request('DELETE', "/api/manga/chapters/{$chapterId}/cbz");
// Then
$this->assertResponseStatusCodeSame(204);
// Verify the chapter CBZ was removed
$freshChapter = $this->entityManager->find(Chapter::class, $chapterId);
$this->assertEmpty($freshChapter->getCbzPath());
}
public function test_it_returns_404_for_non_existent_chapter(): void
{
// When
static::createClient()->request('DELETE', '/api/manga/chapters/999999/cbz');
// Then
$this->assertResponseStatusCodeSame(404);
}
public function test_it_returns_404_for_chapter_without_cbz(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'Test Manga',
'slug' => 'test-manga'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Test Chapter',
'visible' => true,
'cbzPath' => null // No CBZ file
]);
$this->entityManager->flush();
$chapterId = $chapter->getId();
// When
static::createClient()->request('DELETE', "/api/manga/chapters/{$chapterId}/cbz");
// Then
$this->assertResponseStatusCodeSame(404);
}
public function test_it_returns_404_for_invisible_chapter(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'Test Manga',
'slug' => 'test-manga'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Test Chapter',
'visible' => false, // Invisible chapter
'cbzPath' => '/path/to/test.cbz'
]);
$this->entityManager->flush();
$chapterId = $chapter->getId();
// When
static::createClient()->request('DELETE', "/api/manga/chapters/{$chapterId}/cbz");
// Then
$this->assertResponseStatusCodeSame(404);
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Tests\Feature\Manga;
use App\Entity\Chapter;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
class DeleteChapterTest extends AbstractApiTestCase
{
use ResetDatabase, Factories;
public function test_it_soft_deletes_chapter(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Chapter 1',
'visible' => true
]);
$chapterId = $chapter->getId();
// Act
static::createClient()->request('DELETE', "/api/manga/chapters/{$chapterId}");
// Then
$this->assertResponseStatusCodeSame(204);
// Verify the chapter was soft deleted (visible = false)
$freshChapter = $this->entityManager->find(Chapter::class, $chapterId);
$this->assertFalse($freshChapter->isVisible());
}
public function test_it_returns_404_for_non_existent_chapter(): void
{
// When
static::createClient()->request('DELETE', '/api/manga/chapters/999999');
// Then
$this->assertResponseStatusCodeSame(404);
}
public function test_it_returns_404_for_already_soft_deleted_chapter(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'Test Manga',
'slug' => 'test-manga'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Test Chapter',
'visible' => false // Already soft deleted
]);
$this->entityManager->flush();
$chapterId = $chapter->getId();
// When
static::createClient()->request('DELETE', "/api/manga/chapters/{$chapterId}");
// Then
$this->assertResponseStatusCodeSame(404);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Tests\Feature\Manga;
use App\Entity\Chapter;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
class DownloadCbzTest extends AbstractApiTestCase
{
use ResetDatabase, Factories;
public function test_it_downloads_chapter_cbz(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Chapter 1',
'visible' => true,
'cbzPath' => '/app/tests/Shared/Files/test-chapter.cbz'
]);
$chapterId = $chapter->getId();
// Act
static::createClient()->request('GET', "/api/manga/chapters/{$chapterId}/download");
// Then
$this->assertResponseIsSuccessful();
$response = static::getClient()->getResponse();
$this->assertEquals('application/x-cbz', $response->headers->get('Content-Type'));
$this->assertStringContainsString('attachment; filename=', $response->headers->get('Content-Disposition'));
$this->assertStringContainsString('test-chapter.cbz', $response->headers->get('Content-Disposition'));
}
public function test_it_returns_404_for_non_existent_chapter(): void
{
// When
static::createClient()->request('GET', '/api/manga/chapters/999999/download');
// Then
$this->assertResponseStatusCodeSame(404);
}
public function test_it_returns_404_for_chapter_without_cbz(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'Test Manga',
'slug' => 'test-manga'
]);
$chapter = ChapterFactory::createOne([
'manga' => $manga,
'number' => 1.0,
'title' => 'Test Chapter',
'visible' => true,
'cbzPath' => null // No CBZ file
]);
$this->entityManager->flush();
$chapterId = $chapter->getId();
// When
static::createClient()->request('GET', "/api/manga/chapters/{$chapterId}/download");
// Then
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace App\Tests\Feature\Manga;
use App\Entity\Chapter;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
class DownloadVolumeTest extends AbstractApiTestCase
{
use ResetDatabase, Factories;
public function test_it_downloads_volume_cbz(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
// Create chapters for volume 1
ChapterFactory::createMany(3, [
'manga' => $manga,
'volume' => 1,
'visible' => true,
'cbzPath' => '/app/tests/Shared/Files/test-chapter.cbz'
]);
$mangaId = $manga->getId();
// Act
static::createClient()->request('GET', "/api/mangas/{$mangaId}/volumes/1/download");
// Assert
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('Content-Type', 'application/x-cbz');
$contentDisposition = static::getClient()->getResponse()->headers->get('Content-Disposition');
$this->assertStringContainsString('attachment; filename=', $contentDisposition);
$this->assertStringContainsString('one-piece-volume-1.cbz', $contentDisposition);
}
public function test_it_returns_404_when_manga_not_found(): void
{
// Act
static::createClient()->request('GET', '/api/mangas/999999/volumes/1/download');
// Assert
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
public function test_it_returns_404_when_volume_not_found(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
$mangaId = $manga->getId();
// Act
static::createClient()->request('GET', "/api/mangas/{$mangaId}/volumes/999/download");
// Assert
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
public function test_it_returns_404_when_no_available_chapters_in_volume(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
// Create chapters for volume 1 but all without CBZ files
ChapterFactory::createMany(3, [
'manga' => $manga,
'volume' => 1,
'visible' => true,
'cbzPath' => null // No CBZ files
]);
$mangaId = $manga->getId();
// Act
static::createClient()->request('GET', "/api/mangas/{$mangaId}/volumes/1/download");
// Assert
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
public function test_it_only_includes_visible_chapters_with_cbz(): void
{
// Arrange
$manga = MangaFactory::createOne([
'title' => 'One Piece',
'slug' => 'one-piece'
]);
// Create a mix of chapters
ChapterFactory::createOne([
'manga' => $manga,
'volume' => 1,
'number' => 1.0,
'visible' => true,
'cbzPath' => '/app/tests/Shared/Files/test-chapter.cbz'
]);
ChapterFactory::createOne([
'manga' => $manga,
'volume' => 1,
'number' => 2.0,
'visible' => false, // Soft deleted
'cbzPath' => '/app/tests/Shared/Files/test-chapter.cbz'
]);
ChapterFactory::createOne([
'manga' => $manga,
'volume' => 1,
'number' => 3.0,
'visible' => true,
'cbzPath' => null // No CBZ
]);
ChapterFactory::createOne([
'manga' => $manga,
'volume' => 1,
'number' => 4.0,
'visible' => true,
'cbzPath' => '/app/tests/Shared/Files/test-chapter.cbz'
]);
$mangaId = $manga->getId();
// Act
static::createClient()->request('GET', "/api/mangas/{$mangaId}/volumes/1/download");
// Assert - Should succeed with only 2 chapters (1 and 4)
$this->assertResponseIsSuccessful();
}
}

Binary file not shown.