feat: firsts endpoints and firsts tests
This commit is contained in:
parent
89570ad951
commit
6bc3696190
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