createMock(LoggerInterface::class); $cronJob = new CancelStaleWorkflowCronJob($this->createMock(EntityWorkflowRepository::class), $clock, $this->buildMessageBus(), $logger); self::assertEquals($expected, $cronJob->canRun($cronJobExecution)); } /** * @throws \DateMalformedStringException * @throws \DateInvalidTimeZoneException * @throws \Exception|Exception */ public function testRun(): void { $clock = new MockClock((new \DateTimeImmutable('now', new \DateTimeZone('+00:00')))->add(new \DateInterval('P120D'))); $workflowRepository = $this->createMock(EntityWorkflowRepository::class); $workflowRepository->method('findWorkflowsWithoutFinalStepAndOlderThan')->willReturn([1, 3, 2]); $messageBus = $this->buildMessageBus(true); $cronJob = new CancelStaleWorkflowCronJob($workflowRepository, $clock, $messageBus, new NullLogger()); $results = $cronJob->run([]); // Assert the result has the last canceled workflow ID self::assertArrayHasKey('last-canceled-workflow-id', $results); self::assertEquals(3, $results['last-canceled-workflow-id']); } /** * @throws \Exception */ public static function buildTestCanRunData(): iterable { yield [ (new CronJobExecution('last-canceled-workflow-id'))->setLastEnd(new \DateTimeImmutable('2023-12-31 00:00:00', new \DateTimeZone('+00:00'))), true, ]; yield [ (new CronJobExecution('last-canceled-workflow-id'))->setLastEnd(new \DateTimeImmutable('2023-12-30 23:59:59', new \DateTimeZone('+00:00'))), true, ]; yield [ (new CronJobExecution('last-canceled-workflow-id'))->setLastEnd(new \DateTimeImmutable('2023-12-31 00:00:01', new \DateTimeZone('+00:00'))), false, ]; } private function buildMessageBus(bool $expectDispatchAtLeastOnce = false): MessageBusInterface { $messageBus = $this->createMock(MessageBusInterface::class); $methodDispatch = match ($expectDispatchAtLeastOnce) { true => $messageBus->expects($this->atLeastOnce())->method('dispatch')->with($this->isInstanceOf(CancelStaleWorkflowMessage::class)), false => $messageBus->method('dispatch'), }; $methodDispatch->willReturnCallback(fn (CancelStaleWorkflowMessage $message) => new Envelope($message)); return $messageBus; } }