mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Adjust test to work with actual workflow + minor fix of handler logic
This commit is contained in:
parent
cb446edd18
commit
2e69d2df90
@ -38,7 +38,7 @@ class EntityWorkflow implements TrackCreationInterface, TrackUpdateInterface
|
|||||||
/**
|
/**
|
||||||
* @var Collection<int, \Chill\MainBundle\Entity\Workflow\EntityWorkflowComment>
|
* @var Collection<int, \Chill\MainBundle\Entity\Workflow\EntityWorkflowComment>
|
||||||
*/
|
*/
|
||||||
#[ORM\OneToMany(targetEntity: EntityWorkflowComment::class, mappedBy: 'entityWorkflow', orphanRemoval: true)]
|
#[ORM\OneToMany(mappedBy: 'entityWorkflow', targetEntity: EntityWorkflowComment::class, orphanRemoval: true)]
|
||||||
private Collection $comments;
|
private Collection $comments;
|
||||||
|
|
||||||
#[ORM\Id]
|
#[ORM\Id]
|
||||||
|
@ -35,7 +35,7 @@ class CancelStaleWorkflowHandler
|
|||||||
|
|
||||||
$workflow = $this->workflowRepository->find($workflowId);
|
$workflow = $this->workflowRepository->find($workflowId);
|
||||||
|
|
||||||
if (in_array($workflow, $staleWorkflows, true)) {
|
if (!in_array($workflow, $staleWorkflows, true)) {
|
||||||
$this->logger->alert('Workflow has transitioned in the meantime.', [$workflowId]);
|
$this->logger->alert('Workflow has transitioned in the meantime.', [$workflowId]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ class CancelStaleWorkflowHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$transitionApplied) {
|
if (!$transitionApplied) {
|
||||||
$this->logger->error('No valid transition found for EntityWorkflow %d.', [$workflowId]);
|
$this->logger->error('No valid transition found for EntityWorkflow.', [$workflowId]);
|
||||||
throw new UnrecoverableMessageHandlingException(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
|
throw new UnrecoverableMessageHandlingException(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,139 +2,117 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
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;
|
namespace Services\Workflow;
|
||||||
|
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
||||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||||
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflow;
|
|
||||||
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflowHandler;
|
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflowHandler;
|
||||||
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflowMessage;
|
use Chill\MainBundle\Service\Workflow\CancelStaleWorkflowMessage;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
|
use Symfony\Component\Clock\ClockInterface;
|
||||||
use Symfony\Component\Workflow\Registry;
|
use Symfony\Component\Workflow\Registry;
|
||||||
use Symfony\Component\Workflow\Transition;
|
|
||||||
use Symfony\Component\Workflow\WorkflowInterface;
|
use Symfony\Component\Workflow\WorkflowInterface;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
/**
|
class CancelStaleWorkflowHandlerTest extends KernelTestCase
|
||||||
* @internal
|
|
||||||
*
|
|
||||||
* @coversNothing
|
|
||||||
*/
|
|
||||||
class CancelStaleWorkflowHandlerTest extends TestCase
|
|
||||||
{
|
{
|
||||||
public function testInvokeWorkflowWithOneStep(): void
|
private EntityManagerInterface $em;
|
||||||
|
private Registry $registry;
|
||||||
|
private LoggerInterface $logger;
|
||||||
|
private EntityWorkflowRepository $workflowRepository;
|
||||||
|
private ClockInterface $clock;
|
||||||
|
private string $workflowName;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$workflow = $this->createMock(EntityWorkflow::class);
|
// Boot the Symfony kernel
|
||||||
$workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class)]));
|
self::bootKernel();
|
||||||
$workflow->expects($this->once())->method('getCurrentStep')->willReturn(new EntityWorkflowStep());
|
|
||||||
|
|
||||||
$workflowRepository = $this->createMock(EntityWorkflowRepository::class);
|
// Get the actual services from the container
|
||||||
$workflowRepository->expects($this->once())->method('find')->with($this->identicalTo(1))->willReturn($workflow);
|
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
||||||
|
$this->registry = self::getContainer()->get(Registry::class);
|
||||||
|
$this->logger = self::getContainer()->get(LoggerInterface::class);
|
||||||
|
$this->clock = self::getContainer()->get(ClockInterface::class);
|
||||||
|
$this->workflowRepository = $this->createMock(EntityWorkflowRepository::class);
|
||||||
|
|
||||||
$em = $this->createMock(EntityManagerInterface::class);
|
// Retrieve the workflow configuration dynamically
|
||||||
$em->expects($this->exactly(2))->method('remove')->with($this->isInstanceOf(EntityWorkflowStep::class));
|
$configPath = self::$kernel->getProjectDir() . '/config/packages/workflow.yaml'; // Adjust the path if needed
|
||||||
$em->expects($this->once())->method('flush');
|
$config = Yaml::parseFile($configPath);
|
||||||
|
|
||||||
$registry = $this->createMock(Registry::class);
|
// Extract the workflow name from the configuration
|
||||||
$logger = $this->createMock(LoggerInterface::class);
|
$this->workflowName = array_key_first($config['framework']['workflows']);
|
||||||
|
|
||||||
$handler = new CancelStaleWorkflowHandler($workflowRepository, $registry, $em, $logger);
|
|
||||||
|
|
||||||
$handler(new CancelStaleWorkflowMessage(1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvokeWorkflowWithMultipleStepsAndValidTransition(): void
|
public function testWorkflowWithOneStepOlderThan90DaysIsDeleted(): void
|
||||||
{
|
{
|
||||||
$workflow = $this->createMock(EntityWorkflow::class);
|
$workflow = new EntityWorkflow();
|
||||||
$workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class), $this->createMock(EntityWorkflowStep::class)]));
|
$initialStep = new EntityWorkflowStep();
|
||||||
|
$initialStep->setTransitionAt(new DateTimeImmutable('-93 days'));
|
||||||
|
$workflow->addStep($initialStep);
|
||||||
|
|
||||||
$transition = $this->createMock(Transition::class);
|
$this->em->persist($workflow);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
$workflowComponent = $this->createMock(WorkflowInterface::class);
|
$this->handleStaleWorkflow($workflow);
|
||||||
$registryMock = $this->createMock(Registry::class);
|
|
||||||
$registryMock->method('get')
|
|
||||||
->willReturn($workflowComponent);
|
|
||||||
|
|
||||||
$workflowComponent->method('getEnabledTransitions')->willReturn([$transition]);
|
$deletedWorkflow = $this->workflowRepository->find($workflow->getId());
|
||||||
$workflowComponent->expects($this->once())->method('apply')->with($workflow, 'annule');
|
$this->assertNull($deletedWorkflow, 'The workflow should be deleted.');
|
||||||
|
|
||||||
$metadataStore = $this->createMock(MetadataStore::class);
|
$this->assertNull($this->em->getRepository(EntityWorkflowStep::class)->find($initialStep->getId()), 'The workflow step should be deleted.');
|
||||||
$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 CancelStaleWorkflowMessage(1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvokeWorkflowWithMultipleStepsAndNoValidTransition(): void
|
public function testWorkflowWithMultipleStepsAndNoRecentTransitionsIsCanceled(): void
|
||||||
{
|
{
|
||||||
$this->expectException(UnrecoverableMessageHandlingException::class);
|
$workflow = new EntityWorkflow();
|
||||||
$this->expectExceptionMessage('No valid transition found for EntityWorkflow 1.');
|
$step1 = new EntityWorkflowStep();
|
||||||
|
$step2 = new EntityWorkflowStep();
|
||||||
|
|
||||||
$workflow = $this->createMock(EntityWorkflow::class);
|
$step1->setTransitionAt(new DateTimeImmutable('-92 days'));
|
||||||
$workflow->method('getSteps')->willReturn(new ArrayCollection([$this->createMock(EntityWorkflowStep::class), $this->createMock(EntityWorkflowStep::class)]));
|
$step2->setTransitionAt(new DateTimeImmutable('-91 days'));
|
||||||
|
|
||||||
$transition = $this->createMock(Transition::class);
|
$workflow->addStep($step1);
|
||||||
|
$workflow->addStep($step2);
|
||||||
|
|
||||||
$workflowComponent = $this->createMock(WorkflowInterface::class);
|
$this->em->persist($workflow);
|
||||||
$registryMock = $this->createMock(Registry::class);
|
$this->em->flush();
|
||||||
$registryMock->method('get')
|
|
||||||
->willReturn($workflowComponent);
|
|
||||||
$workflowComponent->method('getEnabledTransitions')->willReturn([$transition]);
|
|
||||||
|
|
||||||
$metadataStore = $this->createMock(MetadataStoreInterface::class);
|
/** @var WorkflowInterface $workflowComponent */
|
||||||
$metadataStore->method('getMetadata')->willReturnMap([
|
$workflowComponent = $this->registry->get($workflow, $this->workflowName);
|
||||||
['isFinal', $transition, false],
|
|
||||||
['isFinalPositive', $transition, true],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$workflowComponent->method('getMetadataStore')->willReturn($metadataStore);
|
$transitions = $workflowComponent->getEnabledTransitions($workflow);
|
||||||
|
$metadataStore = $workflowComponent->getMetadataStore();
|
||||||
|
|
||||||
$workflowRepository = $this->createMock(EntityWorkflowRepository::class);
|
$expectedTransition = null;
|
||||||
$workflowRepository->expects($this->once())->method('find')->with($this->identicalTo(1))->willReturn($workflow);
|
|
||||||
|
|
||||||
$em = $this->createMock(EntityManagerInterface::class);
|
// Find the transition that was expected to be applied by the handler
|
||||||
$em->expects($this->never())->method('remove');
|
foreach ($transitions as $transition) {
|
||||||
$em->expects($this->never())->method('flush');
|
$isFinal = $metadataStore->getMetadata('isFinal', $transition);
|
||||||
|
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
|
||||||
|
|
||||||
$registry = $this->createMock(Registry::class);
|
if ($isFinal === true && $isFinalPositive === false) {
|
||||||
$registry->method('get')->willReturn($workflowComponent);
|
$expectedTransition = $transition;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$logger = $this->createMock(LoggerInterface::class);
|
$this->assertNotNull($expectedTransition, 'Expected to find a valid transition with isFinal = true and isFinalPositive = false.');
|
||||||
$logger->expects($this->once())->method('error')->with('No valid transition found for EntityWorkflow 1.');
|
|
||||||
|
|
||||||
$handler = new CancelStaleWorkflowHandler($workflowRepository, $registry, $em, $logger);
|
$this->handleStaleWorkflow($workflow);
|
||||||
|
$updatedWorkflow = $this->workflowRepository->find($workflow->getId());
|
||||||
|
|
||||||
|
$this->assertEquals($expectedTransition->getName(), $updatedWorkflow->getCurrentStep());
|
||||||
|
|
||||||
$handler(new CancelStaleWorkflowMessage(1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handleStaleWorkflow($workflow): void
|
||||||
|
{
|
||||||
|
$handler = new CancelStaleWorkflowHandler($this->workflowRepository, $this->registry, $this->em, $this->logger, $this->clock);
|
||||||
|
$handler(new CancelStaleWorkflowMessage($workflow->getId()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user