createMock(EntityWorkflow::class); $workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class)])); $workflow->expects($this->once())->method('getCurrentStep')->willReturn(new EntityWorkflowStep()); $workflowRepository = $this->createMock(EntityWorkflowRepository::class); $workflowRepository->expects($this->once())->method('find')->with($this->identicalTo(1))->willReturn($workflow); $em = $this->createMock(EntityManagerInterface::class); $em->expects($this->exactly(2))->method('remove')->with($this->isInstanceOf(EntityWorkflowStep::class)); $em->expects($this->once())->method('flush'); $registry = $this->createMock(Registry::class); $logger = $this->createMock(LoggerInterface::class); $handler = new CancelStaleWorkflowHandler($workflowRepository, $registry, $em, $logger); $handler(new CancelStaleWorkflow(1)); } public function testInvokeWorkflowWithMultipleStepsAndValidTransition(): void { $workflow = $this->createMock(EntityWorkflow::class); $workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class), $this->createMock(EntityWorkflowStep::class)])); $transition = $this->createMock(Transition::class); $workflowComponent = $this->createMock(WorkflowInterface::class); $registryMock = $this->createMock(Registry::class); $registryMock->method('get') ->willReturn($workflowComponent); $workflowComponent->method('getEnabledTransitions')->willReturn([$transition]); $workflowComponent->expects($this->once())->method('apply')->with($workflow, 'annule'); $metadataStore = $this->createMock(MetadataStore::class); $metadataStore->method('getMetadata')->willReturnMap([ ['isFinal', $transition, true], ['isFinalPositive', $transition, false], ]); $workflowComponent->method('getMetadataStore')->willReturn($metadataStore); $workflowRepository = $this->createMock(EntityWorkflowRepository::class); $workflowRepository->expects($this->once())->method('find')->with($this->identicalTo(1))->willReturn($workflow); $em = $this->createMock(EntityManagerInterface::class); $em->expects($this->never())->method('remove'); $em->expects($this->once())->method('flush'); $registry = $this->createMock(Registry::class); $registry->method('get')->willReturn($workflowComponent); $logger = $this->createMock(LoggerInterface::class); $logger->expects($this->once())->method('info')->with('EntityWorkflow 1 has been transitioned.'); $handler = new CancelStaleWorkflowHandler($workflowRepository, $registry, $em, $logger); $handler(new CancelStaleWorkflow(1)); } public function testInvokeWorkflowWithMultipleStepsAndNoValidTransition(): void { $this->expectException(UnrecoverableMessageHandlingException::class); $this->expectExceptionMessage('No valid transition found for EntityWorkflow 1.'); $workflow = $this->createMock(EntityWorkflow::class); $workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class), $this->createMock(EntityWorkflowStep::class)])); $transition = $this->createMock(Transition::class); $workflowComponent = $this->createMock(WorkflowInterface::class); $registryMock = $this->createMock(Registry::class); $registryMock->method('get') ->willReturn($workflowComponent); $workflowComponent->method('getEnabledTransitions')->willReturn([$transition]); $metadataStore = $this->createMock(MetadataStoreInterface::class); $metadataStore->method('getMetadata')->willReturnMap([ ['isFinal', $transition, false], ['isFinalPositive', $transition, true], ]); $workflowComponent->method('getMetadataStore')->willReturn($metadataStore); $workflowRepository = $this->createMock(EntityWorkflowRepository::class); $workflowRepository->expects($this->once())->method('find')->with($this->identicalTo(1))->willReturn($workflow); $em = $this->createMock(EntityManagerInterface::class); $em->expects($this->never())->method('remove'); $em->expects($this->never())->method('flush'); $registry = $this->createMock(Registry::class); $registry->method('get')->willReturn($workflowComponent); $logger = $this->createMock(LoggerInterface::class); $logger->expects($this->once())->method('error')->with('No valid transition found for EntityWorkflow 1.'); $handler = new CancelStaleWorkflowHandler($workflowRepository, $registry, $em, $logger); $handler(new CancelStaleWorkflow(1)); } }