chill-bundles/src/Bundle/ChillMainBundle/Tests/Services/Workflow/CancelStaleWorkflowHandlerTest.php
2024-09-09 10:40:42 +02:00

140 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
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\WorkflowInterface;
/**
* @internal
*
* @coversNothing
*/
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));
}
}