scraper = new InMemoryScraperAdapter(); $this->imageDownloader = new InMemoryImageDownloader(); $this->cbzGenerator = new InMemoryCbzGenerator('/test/project/dir'); $this->scrapingJobRepository = new InMemoryScrapingJobRepository(); $this->chapterRepository = new InMemoryChapterRepository(); $this->mangaRepository = new InMemoryMangaRepository(); $this->sourceRepository = new InMemorySourceRepository(); $this->eventBus = new InMemoryEventBus(); $this->chapterRepository->save(new Chapter( id: '1', mangaId: 'test-manga', chapterNumber: 2, volumeNumber: 1, cbzPath: null, )); $this->handler = new ScrapeChapterHandler( $this->scraper, $this->imageDownloader, $this->cbzGenerator, $this->scrapingJobRepository, $this->chapterRepository, $this->mangaRepository, $this->sourceRepository, $this->eventBus ); } public function testHandleSuccessfully(): void { $command = new ScrapeChapter( mangaId: 'test-manga', chapterNumber: '2', sourceId: 'test-source' ); $this->handler->handle($command); $scrapingJobs = $this->scrapingJobRepository->getJobs(); $this->assertCount(1, $scrapingJobs); $job = $scrapingJobs[0]; $dispatchedMessages = $this->eventBus->getDispatchedMessages(); $this->assertCount(2, $dispatchedMessages); $this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]); $this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[1]); $this->assertEquals($job->getId(), $dispatchedMessages[0]->getJobId()); $chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('test-manga', 2); $this->assertNotNull($chapter->cbzPath); } public function testHandleThrowsException(): void { $command = new ScrapeChapter( mangaId: 'test-manga', chapterNumber: '2', sourceId: 'test-source' ); $exception = new \Exception('Scraping failed'); $this->scraper->simulateError($exception); $this->expectException(\Exception::class); $this->expectExceptionMessage('Scraping failed'); $this->handler->handle($command); $dispatchedMessages = $this->eventBus->getDispatchedMessages(); $this->assertCount(2, $dispatchedMessages); $this->assertInstanceOf(ChapterScrapingStarted::class, $dispatchedMessages[0]); $this->assertInstanceOf(ChapterScrapingFailed::class, $dispatchedMessages[1]); $this->assertEquals('test-manga', $dispatchedMessages[1]->getMangaId()); $this->assertEquals('2', $dispatchedMessages[1]->getChapterNumber()); $this->assertEquals('Scraping failed', $dispatchedMessages[1]->getReason()); $jobs = $this->scrapingJobRepository->getJobs(); $this->assertCount(1, $jobs); $this->assertEquals(ScrapingStatus::FAILED, $jobs[0]->status); $this->assertEquals('Scraping failed', $jobs[0]->failureReason); } protected function tearDown(): void { $this->scrapingJobRepository->clear(); $this->chapterRepository->clear(); $this->mangaRepository->clear(); $this->sourceRepository->clear(); } }