feat: finalizing Scraping endpoint
This commit is contained in:
parent
0374ab0e46
commit
073439163b
20
tests/Domain/Scraping/Adapter/InMemoryCbzGenerator.php
Normal file
20
tests/Domain/Scraping/Adapter/InMemoryCbzGenerator.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Scraping\Adapter;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\Service\CbzGeneratorInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\CbzPath;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\TempDirectory;
|
||||
|
||||
readonly class InMemoryCbzGenerator implements CbzGeneratorInterface
|
||||
{
|
||||
public function __construct(private string $projectDir)
|
||||
{
|
||||
}
|
||||
|
||||
public function generate(ScrapingJob $job, TempDirectory $tempDirectory): CbzPath
|
||||
{
|
||||
return new CbzPath('test.cbz');
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,15 @@ class InMemoryScraperAdapter implements ScraperInterface
|
||||
{
|
||||
private ?\Exception $shouldThrowException = null;
|
||||
|
||||
public function scrape(ScrapingJob $job): void
|
||||
public function scrape(ScrapingJob $job): ScrapingJob
|
||||
{
|
||||
if ($this->shouldThrowException) {
|
||||
throw $this->shouldThrowException;
|
||||
$job->fail($this->shouldThrowException->getMessage());
|
||||
return $job;
|
||||
}
|
||||
|
||||
$job->complete();
|
||||
return $job;
|
||||
}
|
||||
|
||||
public function simulateError(\Exception $exception): void
|
||||
|
||||
@@ -12,23 +12,17 @@ class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
|
||||
public function save(ScrapingJob $job): void
|
||||
{
|
||||
self::$jobs[] = $job;
|
||||
self::$jobs[$job->getId()] = $job;
|
||||
}
|
||||
|
||||
public function getJobs(): array
|
||||
{
|
||||
return self::$jobs;
|
||||
return array_values(self::$jobs);
|
||||
}
|
||||
|
||||
public function findById(string $id): ?ScrapingJob
|
||||
{
|
||||
foreach (self::$jobs as $job) {
|
||||
if ($job->getId() === $id) {
|
||||
return $job;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return self::$jobs[$id] ?? null;
|
||||
}
|
||||
|
||||
public function findByChapterId(string $chapterId): ?ScrapingJob
|
||||
@@ -46,4 +40,4 @@ class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
{
|
||||
self::$jobs = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ namespace App\Tests\Domain\Scraping\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Scraping\Application\Command\ScrapeChapter;
|
||||
use App\Domain\Scraping\Application\CommandHandler\ScrapeChapterHandler;
|
||||
use App\Domain\Scraping\Domain\Event\ChapterScraped;
|
||||
use App\Domain\Scraping\Domain\Event\ChapterScrapingFailed;
|
||||
use App\Domain\Scraping\Domain\Event\ChapterScrapingStarted;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingStatus;
|
||||
use App\Tests\Domain\Scraping\Adapter\InMemoryEventBus;
|
||||
use App\Tests\Domain\Scraping\Adapter\InMemoryScraperAdapter;
|
||||
use App\Tests\Domain\Scraping\Adapter\InMemoryScrapingJobRepository;
|
||||
@@ -41,17 +43,18 @@ class ScrapeChapterHandlerTest extends TestCase
|
||||
$this->handler->handle($command);
|
||||
|
||||
$scrapingJobs = $this->repository->getJobs();
|
||||
|
||||
|
||||
$this->assertCount(1, $scrapingJobs);
|
||||
$job = $scrapingJobs[0];
|
||||
|
||||
$savedJobs = $this->repository->getJobs();
|
||||
$this->assertCount(1, $savedJobs);
|
||||
$this->assertSame($job, $savedJobs[0]);
|
||||
|
||||
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
||||
$this->assertCount(1, $dispatchedMessages);
|
||||
$this->assertCount(2, $dispatchedMessages);
|
||||
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
||||
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[1]);
|
||||
$this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId());
|
||||
|
||||
$this->repository->clear();
|
||||
}
|
||||
|
||||
public function testHandleThrowsException(): void
|
||||
@@ -65,18 +68,18 @@ class ScrapeChapterHandlerTest extends TestCase
|
||||
$exception = new \Exception('Scraping failed');
|
||||
$this->scraper->simulateError($exception);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Scraping failed');
|
||||
$this->handler->handle($command);
|
||||
|
||||
try {
|
||||
$this->handler->handle($command);
|
||||
} finally {
|
||||
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
||||
$this->assertCount(2, $dispatchedMessages);
|
||||
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
||||
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[1]);
|
||||
$this->assertEquals(2, $dispatchedMessages[1]->getChapterNumber());
|
||||
$this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason());
|
||||
}
|
||||
$dispatchedMessages = $this->eventBus->getDispatchedMessages();
|
||||
$this->assertCount(2, $dispatchedMessages);
|
||||
$this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]);
|
||||
$this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[1]);
|
||||
$this->assertEquals(2, $dispatchedMessages[1]->getChapterNumber());
|
||||
$this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason());
|
||||
|
||||
$jobs = $this->repository->getJobs();
|
||||
$this->assertCount(1, $jobs);
|
||||
$this->assertEquals(ScrapingStatus::FAILED, $jobs[0]->status);
|
||||
$this->assertEquals('Scraping failed', $jobs[0]->failureReason);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user