mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Create test for CancelStaleWorkflowHandler: wip state
This commit is contained in:
parent
dab68fb409
commit
35199b6993
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Services\Workflow;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflow;
|
||||
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflowHandler;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
||||
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\Transition;
|
||||
use Symfony\Component\Workflow\Workflow;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
|
||||
class CancelStaleWorkflowHandlerTest extends TestCase
|
||||
{
|
||||
public function testInvokeWorkflowWithOneStep(): void
|
||||
{
|
||||
$workflow = $this->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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user