feat: firsts unit tests for ScrapeChapterHandler.php
This commit is contained in:
parent
21fcdd1084
commit
89570ad951
23
tests/Domain/Scraping/Adapter/InMemoryEventBus.php
Normal file
23
tests/Domain/Scraping/Adapter/InMemoryEventBus.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Scraping\Adapter;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class InMemoryEventBus implements MessageBusInterface
|
||||
{
|
||||
private array $dispatchedMessages = [];
|
||||
|
||||
public function dispatch(object $message, array $stamps = []): Envelope
|
||||
{
|
||||
$this->dispatchedMessages[] = $message;
|
||||
|
||||
return new Envelope($message);
|
||||
}
|
||||
|
||||
public function getDispatchedMessages(): array
|
||||
{
|
||||
return $this->dispatchedMessages;
|
||||
}
|
||||
}
|
||||
47
tests/Domain/Scraping/Adapter/InMemoryScraperAdapter.php
Normal file
47
tests/Domain/Scraping/Adapter/InMemoryScraperAdapter.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Scraping\Adapter;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\Service\ScraperInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class InMemoryScraperAdapter implements ScraperInterface
|
||||
{
|
||||
private array $jobs = [];
|
||||
private ?\Exception $shouldThrowException = null;
|
||||
|
||||
public function createScrapingJob(string $mangaId, string $chapterId, string $sourceId): ScrapingJob
|
||||
{
|
||||
if ($this->shouldThrowException) {
|
||||
throw $this->shouldThrowException;
|
||||
}
|
||||
|
||||
$job = new ScrapingJob(Uuid::uuid4(), $mangaId, $chapterId, $sourceId);
|
||||
$this->jobs[] = $job;
|
||||
|
||||
return $job;
|
||||
}
|
||||
|
||||
public function scrape(ScrapingJob $job): void
|
||||
{
|
||||
if ($this->shouldThrowException) {
|
||||
throw $this->shouldThrowException;
|
||||
}
|
||||
}
|
||||
|
||||
public function simulateError(\Exception $exception): void
|
||||
{
|
||||
$this->shouldThrowException = $exception;
|
||||
}
|
||||
|
||||
public function getJobs(): array
|
||||
{
|
||||
return $this->jobs;
|
||||
}
|
||||
|
||||
public function supports(string $sourceType): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Scraping\Adapter;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
|
||||
class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
{
|
||||
/** @var ScrapingJob[] */
|
||||
private array $jobs = [];
|
||||
|
||||
public function save(ScrapingJob $job): void
|
||||
{
|
||||
$this->jobs[] = $job;
|
||||
}
|
||||
|
||||
public function getJobs(): array
|
||||
{
|
||||
return $this->jobs;
|
||||
}
|
||||
|
||||
public function findById(string $id): ?ScrapingJob
|
||||
{
|
||||
foreach ($this->jobs as $job) {
|
||||
if ($job->getId() === $id) {
|
||||
return $job;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findByChapterId(string $chapterId): ?ScrapingJob
|
||||
{
|
||||
foreach ($this->jobs as $job) {
|
||||
if ($job->getChapterId() === $chapterId) {
|
||||
return $job;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user