mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 01:08:26 +00:00 
			
		
		
		
	Add OpenedEntityWorkflowHelper and tests
Implemented OpenedEntityWorkflowHelper to handle final state checks for EntityWorkflow. This includes methods to determine if a workflow has reached positive or negative final steps. Added corresponding unit tests to ensure proper functionality.
This commit is contained in:
		| @@ -0,0 +1,105 @@ | ||||
| <?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 Chill\MainBundle\Tests\Workflow\Helper; | ||||
|  | ||||
| use Chill\MainBundle\Entity\Workflow\EntityWorkflow; | ||||
| use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore; | ||||
| use Chill\MainBundle\Workflow\Helper\OpenedEntityWorkflowHelper; | ||||
| use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO; | ||||
| use PHPUnit\Framework\TestCase; | ||||
| use Symfony\Component\Workflow\DefinitionBuilder; | ||||
| use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore; | ||||
| use Symfony\Component\Workflow\Registry; | ||||
| use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface; | ||||
| use Symfony\Component\Workflow\Workflow; | ||||
| use Symfony\Component\Workflow\WorkflowInterface; | ||||
|  | ||||
| /** | ||||
|  * @internal | ||||
|  * | ||||
|  * @coversNothing | ||||
|  */ | ||||
| class OpenedEntityWorkflowHelperTest extends TestCase | ||||
| { | ||||
|     private function buildRegistry(): Registry | ||||
|     { | ||||
|         $builder = new DefinitionBuilder(); | ||||
|         $builder | ||||
|             ->setInitialPlaces('initial') | ||||
|             ->addPlaces(['initial', 'final_positive', 'final_negative', 'final_no_data']) | ||||
|             ->setMetadataStore( | ||||
|                 new InMemoryMetadataStore( | ||||
|                     placesMetadata: [ | ||||
|                         'final_positive' => ['isFinalPositive' => true, 'isFinal' => true], | ||||
|                         'final_negative' => ['isFinalPositive' => false, 'isFinal' => true], | ||||
|                         'final_no_data' => ['isFinal' => true], | ||||
|                     ] | ||||
|                 ) | ||||
|             ); | ||||
|  | ||||
|         $workflow = new Workflow($builder->build(), new EntityWorkflowMarkingStore(), name: 'dummy'); | ||||
|         $registry = new Registry(); | ||||
|         $registry->addWorkflow( | ||||
|             $workflow, | ||||
|             new class () implements WorkflowSupportStrategyInterface { | ||||
|                 public function supports(WorkflowInterface $workflow, object $subject): bool | ||||
|                 { | ||||
|                     return true; | ||||
|                 } | ||||
|             } | ||||
|         ); | ||||
|  | ||||
|         return $registry; | ||||
|     } | ||||
|  | ||||
|     public function testIsFinalPositive(): void | ||||
|     { | ||||
|         $entityWorkflow = new EntityWorkflow(); | ||||
|         $entityWorkflow->setWorkflowName('dummy'); | ||||
|         $dto = new WorkflowTransitionContextDTO($entityWorkflow); | ||||
|         $entityWorkflow->setStep('final_positive', $dto, 'fake', new \DateTimeImmutable()); | ||||
|         $entityWorkflow->getCurrentStep()->setIsFinal(true); | ||||
|  | ||||
|         $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); | ||||
|  | ||||
|         self::assertTrue($helper->isFinalPositive($entityWorkflow)); | ||||
|         self::assertFalse($helper->isFinalNegative($entityWorkflow)); | ||||
|     } | ||||
|  | ||||
|     public function testIsFinalNegative(): void | ||||
|     { | ||||
|         $entityWorkflow = new EntityWorkflow(); | ||||
|         $entityWorkflow->setWorkflowName('dummy'); | ||||
|         $dto = new WorkflowTransitionContextDTO($entityWorkflow); | ||||
|         $entityWorkflow->setStep('final_negative', $dto, 'fake', new \DateTimeImmutable()); | ||||
|         $entityWorkflow->getCurrentStep()->setIsFinal(true); | ||||
|  | ||||
|         $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); | ||||
|  | ||||
|         self::assertFalse($helper->isFinalPositive($entityWorkflow)); | ||||
|         self::assertTrue($helper->isFinalNegative($entityWorkflow)); | ||||
|     } | ||||
|  | ||||
|     public function testIsFinalNoInformation(): void | ||||
|     { | ||||
|         $entityWorkflow = new EntityWorkflow(); | ||||
|         $entityWorkflow->setWorkflowName('dummy'); | ||||
|         $dto = new WorkflowTransitionContextDTO($entityWorkflow); | ||||
|         $entityWorkflow->setStep('final_no_data', $dto, 'fake', new \DateTimeImmutable()); | ||||
|         $entityWorkflow->getCurrentStep()->setIsFinal(true); | ||||
|  | ||||
|         $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); | ||||
|  | ||||
|         self::assertFalse($helper->isFinalPositive($entityWorkflow)); | ||||
|         self::assertFalse($helper->isFinalNegative($entityWorkflow)); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,83 @@ | ||||
| <?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 Chill\MainBundle\Workflow\Helper; | ||||
|  | ||||
| use Chill\MainBundle\Entity\Workflow\EntityWorkflow; | ||||
| use Symfony\Component\Workflow\Registry; | ||||
|  | ||||
| /** | ||||
|  * Helps to determine the status of the EntityWorkflow. | ||||
|  * | ||||
|  * A "negative final" step means that the workflow has been canceled, without being formerly finalized | ||||
|  */ | ||||
| class OpenedEntityWorkflowHelper | ||||
| { | ||||
|     public function __construct(private readonly Registry $registry) {} | ||||
|  | ||||
|     /** | ||||
|      * Return true if the entity workflow has reached a final step. | ||||
|      * | ||||
|      * @return bool true if the entityworkflow has reached a final step | ||||
|      */ | ||||
|     public function isFinal(EntityWorkflow $entityWorkflow): bool | ||||
|     { | ||||
|         return $entityWorkflow->isFinal(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * return true if the entity workflow has reached a final and positive step. | ||||
|      * | ||||
|      * If multiple steps, they are all taken into account: return true if at least one step has the metadata | ||||
|      * `isFinalPositive: true` | ||||
|      */ | ||||
|     public function isFinalPositive(EntityWorkflow $entityWorkflow): bool | ||||
|     { | ||||
|         if (!$this->isFinal($entityWorkflow)) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         return $this->findFinalMark($entityWorkflow, true) ?? false; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * return true if the entity workflow has reached a final and negative step. | ||||
|      * | ||||
|      * If multiple steps, they are all taken into account: return true if at least one step has the metadata | ||||
|      * `isFinalPositive: false`. | ||||
|      */ | ||||
|     public function isFinalNegative(EntityWorkflow $entityWorkflow): bool | ||||
|     { | ||||
|         if (!$this->isFinal($entityWorkflow)) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         return $this->findFinalMark($entityWorkflow, false) ?? false; | ||||
|     } | ||||
|  | ||||
|     private function findFinalMark(EntityWorkflow $entityWorkflow, bool $expectedMark): ?bool | ||||
|     { | ||||
|         $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); | ||||
|         $marking = $workflow->getMarkingStore()->getMarking($entityWorkflow); | ||||
|  | ||||
|         foreach ($marking->getPlaces() as $place => $key) { | ||||
|             $metadata = $workflow->getMetadataStore()->getPlaceMetadata($place); | ||||
|  | ||||
|             if (array_key_exists('isFinalPositive', $metadata)) { | ||||
|                 if ($expectedMark === $metadata['isFinalPositive']) { | ||||
|                     return true; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user