feat: firsts endpoints and firsts tests
This commit is contained in:
parent
89570ad951
commit
6bc3696190
38
tests/Domain/Scraping/Adapter/InMemoryMessageBus.php
Normal file
38
tests/Domain/Scraping/Adapter/InMemoryMessageBus.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,21 @@ use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
{
|
||||
/** @var ScrapingJob[] */
|
||||
private array $jobs = [];
|
||||
private static array $jobs = [];
|
||||
|
||||
public function save(ScrapingJob $job): void
|
||||
{
|
||||
$this->jobs[] = $job;
|
||||
self::$jobs[] = $job;
|
||||
}
|
||||
|
||||
public function getJobs(): array
|
||||
{
|
||||
return $this->jobs;
|
||||
return self::$jobs;
|
||||
}
|
||||
|
||||
public function findById(string $id): ?ScrapingJob
|
||||
{
|
||||
foreach ($this->jobs as $job) {
|
||||
foreach (self::$jobs as $job) {
|
||||
if ($job->getId() === $id) {
|
||||
return $job;
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
|
||||
public function findByChapterId(string $chapterId): ?ScrapingJob
|
||||
{
|
||||
foreach ($this->jobs as $job) {
|
||||
foreach (self::$jobs as $job) {
|
||||
if ($job->getChapterId() === $chapterId) {
|
||||
return $job;
|
||||
}
|
||||
@@ -41,4 +41,9 @@ class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
self::$jobs = [];
|
||||
}
|
||||
}
|
||||
@@ -40,17 +40,14 @@ class ScrapeChapterHandlerTest extends TestCase
|
||||
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Vérifier que le job a été créé
|
||||
$scrapingJobs = $this->scraper->getJobs();
|
||||
$this->assertCount(1, $scrapingJobs);
|
||||
$job = $scrapingJobs[0];
|
||||
|
||||
// Vérifier que le job a été sauvegardé
|
||||
$savedJobs = $this->repository->getJobs();
|
||||
$this->assertCount(1, $savedJobs);
|
||||
$this->assertSame($job, $savedJobs[0]);
|
||||
|
||||
// Vérifier que l'événement a été dispatché
|
||||
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
||||
$this->assertCount(1, $dispatchedMessages);
|
||||
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
||||
@@ -74,7 +71,6 @@ class ScrapeChapterHandlerTest extends TestCase
|
||||
try {
|
||||
$this->handler->handle($command);
|
||||
} finally {
|
||||
// Vérifier que l'événement d'échec a été dispatché
|
||||
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
||||
$this->assertCount(1, $dispatchedMessages);
|
||||
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[0]);
|
||||
|
||||
18
tests/Feature/Scraping/AbstractApiTestCase.php
Normal file
18
tests/Feature/Scraping/AbstractApiTestCase.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Scraping;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
|
||||
abstract class AbstractApiTestCase extends ApiTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
51
tests/Feature/Scraping/Factory/ScrapingJobFactory.php
Normal file
51
tests/Feature/Scraping/Factory/ScrapingJobFactory.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Scraping\Factory;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingStatus;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\ImageUrl;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\PageNumber;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class ScrapingJobFactory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ScrapingJobRepositoryInterface $repository
|
||||
) {}
|
||||
|
||||
public function createJob(array $attributes = []): ScrapingJob
|
||||
{
|
||||
$job = new ScrapingJob(
|
||||
$attributes['id'] ?? Uuid::uuid4()->toString(),
|
||||
$attributes['mangaId'] ?? 'manga-'.Uuid::uuid4()->toString(),
|
||||
$attributes['chapterId'] ?? 'chapter-'.Uuid::uuid4()->toString(),
|
||||
$attributes['sourceId'] ?? 'source-'.Uuid::uuid4()->toString()
|
||||
);
|
||||
|
||||
if (isset($attributes['status'])) {
|
||||
$this->setJobStatus($job, $attributes['status']);
|
||||
}
|
||||
|
||||
if (isset($attributes['pages'])) {
|
||||
foreach ($attributes['pages'] as $index => $page) {
|
||||
$job->addPage(new PageNumber($index + 1), new ImageUrl($page));
|
||||
}
|
||||
}
|
||||
|
||||
$this->repository->save($job);
|
||||
|
||||
return $job;
|
||||
}
|
||||
|
||||
private function setJobStatus(ScrapingJob $job, ScrapingStatus $status): void
|
||||
{
|
||||
// Cette méthode nécessite peut-être d'ajouter des méthodes protégées dans ScrapingJob
|
||||
// pour permettre la modification du statut dans les tests
|
||||
// Ou utiliser de la réflexion si nécessaire
|
||||
$reflection = new \ReflectionProperty($job, 'status');
|
||||
$reflection->setAccessible(true);
|
||||
$reflection->setValue($job, $status);
|
||||
}
|
||||
}
|
||||
71
tests/Feature/Scraping/ScrapeChapterTest.php
Normal file
71
tests/Feature/Scraping/ScrapeChapterTest.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Scraping;
|
||||
|
||||
use App\Domain\Scraping\Application\Command\ScrapeChapter;
|
||||
use App\Tests\Domain\Scraping\Adapter\InMemoryMessageBus;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class ScrapeChapterTest extends AbstractApiTestCase
|
||||
{
|
||||
private MessageBusInterface|InMemoryMessageBus $messageBus;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->messageBus = self::getContainer()->get(MessageBusInterface::class);
|
||||
}
|
||||
|
||||
public function testInitiateChapterScraping(): void
|
||||
{
|
||||
// Given
|
||||
$payload = [
|
||||
'chapterId' => 'chapter-123',
|
||||
'sourceId' => 'source-456',
|
||||
'mangaId' => 'manga-789',
|
||||
];
|
||||
|
||||
// When
|
||||
$response = static::createClient()->request('POST', '/api/scraping/chapters', [
|
||||
'json' => $payload,
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(202);
|
||||
|
||||
$messages = $this->messageBus::$messages;
|
||||
$this->assertCount(1, $messages, 'Un message devrait être dispatché');
|
||||
|
||||
/** @var ScrapeChapter $message */
|
||||
$message = $messages[0];
|
||||
$this->assertInstanceOf(ScrapeChapter::class, $message);
|
||||
}
|
||||
|
||||
public function testInitiateChapterScrapingWithInvalidPayload(): void
|
||||
{
|
||||
// Given
|
||||
$payload = [
|
||||
'chapterId' => '',
|
||||
'sourceId' => 'source-456',
|
||||
'mangaId' => 'manga-789',
|
||||
];
|
||||
|
||||
// When
|
||||
$response = static::createClient()->request('POST', '/api/scraping/chapters', [
|
||||
'json' => $payload,
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(422);
|
||||
$this->assertJsonContains([
|
||||
'violations' => [
|
||||
[
|
||||
'propertyPath' => 'chapterId',
|
||||
'message' => 'This value should not be blank.',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
69
tests/Feature/Scraping/ScrapingStatusTest.php
Normal file
69
tests/Feature/Scraping/ScrapingStatusTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Scraping;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingStatus;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\ImageUrl;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\PageNumber;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
|
||||
|
||||
class ScrapingStatusTest extends ApiTestCase
|
||||
{
|
||||
private MessageBusInterface $messageBus;
|
||||
private ScrapingJobRepositoryInterface $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
self::bootKernel();
|
||||
|
||||
$this->messageBus = self::getContainer()->get(MessageBusInterface::class);
|
||||
$this->repository = self::getContainer()->get(ScrapingJobRepositoryInterface::class);
|
||||
}
|
||||
|
||||
public function testGetScrapingStatus(): void
|
||||
{
|
||||
// Given
|
||||
$jobId = Uuid::uuid4()->toString();
|
||||
$job = new ScrapingJob($jobId, 'manga-123', 'chapter-456', 'source-789');
|
||||
|
||||
$job->addPage(new PageNumber(1), new ImageUrl('http://example.com/page1.jpg'));
|
||||
$job->addPage(new PageNumber(2), new ImageUrl('http://example.com/page2.jpg'));
|
||||
|
||||
$this->repository->save($job);
|
||||
|
||||
// When
|
||||
$response = static::createClient()->request('GET', '/api/scraping/jobs/'.$jobId.'/status');
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertJsonContains([
|
||||
'jobId' => $jobId,
|
||||
'status' => ScrapingStatus::IN_PROGRESS->value,
|
||||
'progress' => 0
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGetScrapingStatusForNonExistentJob(): void
|
||||
{
|
||||
// When
|
||||
$response = static::createClient()->request('GET', '/api/scraping/jobs/non-existent-id/status', [
|
||||
'headers' => ['Accept' => 'application/json']
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
self::ensureKernelShutdown();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user