Files
Mangarr/tests/Feature/Scraping/SetMangaPreferredSourcesTest.php
ext.jeremy.guillot@maxicoffee.domains 5ed303612a feat: migrer vers Symfony 8, PHP 8.4 et les dépendances majeures associées
- PHP 8.3 → 8.4 (Dockerfile + composer.json)
- Symfony 7.0 → 8.0 (tous les composants symfony/*)
- API Platform 3.x → 4.x : migration openapiContext → openapi: new Operation(...)
- Doctrine DBAL 3 → 4 : suppression use_savepoints, replace prepare/executeQuery
- Doctrine ORM 2.x → 3.x : ClassMetadataInfo → ClassMetadata, setParameters → setParameter
- Doctrine Bundle 2.x → 3.x, Fixtures Bundle 3.x → 4.x
- zenstruck/foundry 1.x → 2.x : ModelFactory → PersistentObjectFactory, getDefaults → defaults
- phpmd/phpmd 2.x → 3.x-dev (seule version supportant Symfony 8)
- phparkitect 0.3 → 0.8 : NotDependsOnTheseNamespaces prend un array
- symfony/mercure-bundle 0.3 → 0.4, symfony/monolog-bundle 3 → 4
- Suppression de runtime/frankenphp-symfony (intégré nativement dans symfony/runtime 8)
- worker.Caddyfile : suppression de APP_RUNTIME (détection automatique Symfony 8)
- Routes errors.xml/wdt.xml/profiler.xml → .php (Symfony 8 supprime le XML)
- Types::ARRAY → Types::JSON dans Entity/Manga.php (DBAL 4 retire array type)
- Suppression de src/Schedule.php (doublon vide avec MonitoringSchedule)
- Tests : hydra:Collection → Collection, hydra:member → member (API Platform 4)
2026-03-26 17:55:12 +01:00

182 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Feature\Scraping;
use App\Domain\Scraping\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Entity\ContentSource;
use App\Entity\Manga;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\ResetDatabase;
final class SetMangaPreferredSourcesTest extends AbstractApiTestCase
{
use ResetDatabase;
private int $mangaId;
private int $source1Id;
private int $source2Id;
private MangaRepositoryInterface $mangaRepository;
protected function setUp(): void
{
parent::setUp();
$this->mangaRepository = self::getContainer()->get(MangaRepositoryInterface::class);
// Création des sources de contenu
$source1 = new ContentSource();
$source1->setBaseUrl('https://mangadex.org')
->setChapterUrlFormat('https://mangadex.org/chapter/{id}')
->setScrapingType('html')
->setImageSelector('.chapter-image img')
->setNextPageSelector('.next-page')
->setChapterSelector('.chapter-list a');
$source2 = new ContentSource();
$source2->setBaseUrl('https://mangakakalot.com')
->setChapterUrlFormat('https://mangakakalot.com/chapter/{id}')
->setScrapingType('javascript')
->setImageSelector('.page-image img')
->setNextPageSelector('.next-button')
->setChapterSelector('.chapter-link');
$this->entityManager->persist($source1);
$this->entityManager->persist($source2);
$this->entityManager->flush();
$this->source1Id = $source1->getId();
$this->source2Id = $source2->getId();
// Création d'un manga
$manga = new Manga();
$manga->setTitle('Test Manga')
->setSlug('test-manga')
->setDescription('Description test')
->setAuthor('Author test')
->setPublicationYear(2020)
->setGenres(['action'])
->setStatus('ongoing')
->setRating(4.5)
->setMonitored(false);
$this->entityManager->persist($manga);
$this->entityManager->flush();
$this->mangaId = $manga->getId();
}
public function testItReturnsNotFoundWhenMangaDoesNotExist(): void
{
$response = static::createClient()->request('POST', '/api/mangas/999999/preferred-sources', [
'json' => [
'sourceIds' => [(string) $this->source1Id],
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_INTERNAL_SERVER_ERROR);
$this->assertJsonContains([
'detail' => 'Manga not found with ID: 999999',
]);
}
public function testItReturnsNotFoundWhenSourceDoesNotExist(): void
{
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'sourceIds' => ['999999'],
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_INTERNAL_SERVER_ERROR);
$this->assertJsonContains([
'detail' => 'One or more sources do not exist or are not active',
]);
}
public function testItSetsPreferredSourcesSuccessfully(): void
{
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'sourceIds' => [(string) $this->source1Id, (string) $this->source2Id],
],
]);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
// Vérifier que les sources préférées ont été sauvegardées
$manga = $this->mangaRepository->getById((string) $this->mangaId);
$this->assertNotNull($manga);
// Vérifier que les sources préférées ont été mises à jour
// Note: Le repository du domaine peut avoir une logique différente pour récupérer les sources préférées
// Pour l'instant, on vérifie juste que l'opération s'est bien passée
}
public function testItUpdatesExistingPreferredSources(): void
{
// Définir des sources préférées initiales
$manga = $this->entityManager->find(Manga::class, $this->mangaId);
$source1 = $this->entityManager->find(ContentSource::class, $this->source1Id);
$manga->addPreferredSource($source1);
$this->entityManager->flush();
// Modifier les sources préférées
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'sourceIds' => [(string) $this->source2Id],
],
]);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
// Vérifier que les sources préférées ont été mises à jour
$manga = $this->mangaRepository->getById((string) $this->mangaId);
$this->assertNotNull($manga);
}
public function testItAcceptsEmptySourceIds(): void
{
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'sourceIds' => [],
],
]);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
// Vérifier que les sources préférées ont été supprimées
$manga = $this->mangaRepository->getById((string) $this->mangaId);
$this->assertNotNull($manga);
}
public function testItValidatesSourceIdsFormat(): void
{
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'sourceIds' => ['invalid-id', '123'],
],
]);
// TODO: Corriger le cas où l'ID est invalide
$this->assertResponseStatusCodeSame(Response::HTTP_INTERNAL_SERVER_ERROR);
}
public function testItValidatesRequestFormat(): void
{
$response = static::createClient()->request('POST', "/api/mangas/{$this->mangaId}/preferred-sources", [
'json' => [
'invalidField' => 'value',
],
]);
// TODO: Corriger le cas où le format de la requête est invalide
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
}
}