scraper = new InMemoryScraperAdapter(); $this->repository = new InMemoryScrapingJobRepository(); $this->eventBus = new InMemoryEventBus(); $this->handler = new ScrapeChapterHandler( $this->scraper, $this->repository, $this->eventBus ); } public function testHandleSuccessfully(): void { $command = new ScrapeChapter( mangaId: 1, chapterNumber: 2, sourceId: 3, ); $this->handler->handle($command); $scrapingJobs = $this->repository->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()); $this->repository->clear(); } public function testHandleThrowsException(): void { $command = new ScrapeChapter( mangaId: 1, chapterNumber: 2, sourceId: 3, ); $exception = new \Exception('Scraping failed'); $this->scraper->simulateError($exception); $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(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); } }