From 85dc9bdb2fb6a66c5f98ba8f7423bbd2bcb8ffb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Oct 2024 23:35:13 +0200 Subject: [PATCH] Add `getSuggestedUsers` method in `EntityWorkflowManager` Implemented the method to retrieve a list of suggested users for an entity workflow, filtering out duplicates. Added corresponding unit tests to verify the method's functionality and ensure its correctness in various scenarios. --- .../Workflow/EntityWorkflowManagerTest.php | 49 +++++++++++++++++++ .../Workflow/EntityWorkflowManager.php | 27 ++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Tests/Workflow/EntityWorkflowManagerTest.php diff --git a/src/Bundle/ChillMainBundle/Tests/Workflow/EntityWorkflowManagerTest.php b/src/Bundle/ChillMainBundle/Tests/Workflow/EntityWorkflowManagerTest.php new file mode 100644 index 000000000..4570d86b8 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Workflow/EntityWorkflowManagerTest.php @@ -0,0 +1,49 @@ +createMock(EntityWorkflow::class); + $entityWorkflow->method('getUsersInvolved')->willReturn([$user1, $user2]); + + $user3 = new User(); + $handler = $this->createMock(EntityWorkflowHandlerInterface::class); + $handler->method('getSuggestedUsers')->willReturn([$user1, $user3]); + $handler->method('supports')->willReturn(true); + + $manager = new EntityWorkflowManager([$handler], new Registry()); + + $users = $manager->getSuggestedUsers($entityWorkflow); + + self::assertcount(3, $users); + self::assertContains($user1, $users); + self::assertContains($user2, $users); + self::assertContains($user3, $users); + } +} diff --git a/src/Bundle/ChillMainBundle/Workflow/EntityWorkflowManager.php b/src/Bundle/ChillMainBundle/Workflow/EntityWorkflowManager.php index f1c87fd4e..16cfd66b7 100644 --- a/src/Bundle/ChillMainBundle/Workflow/EntityWorkflowManager.php +++ b/src/Bundle/ChillMainBundle/Workflow/EntityWorkflowManager.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Workflow; use Chill\DocStoreBundle\Entity\StoredObject; +use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Entity\Workflow\EntityWorkflowSend; use Chill\MainBundle\Workflow\Exception\HandlerNotFoundException; @@ -93,4 +94,30 @@ class EntityWorkflowManager throw new HandlerWithPublicViewNotFoundException(); } + + /** + * @return list + */ + public function getSuggestedUsers(EntityWorkflow $entityWorkflow, bool $addUsersInvolved = true): array + { + $users = []; + if ($addUsersInvolved) { + foreach ($entityWorkflow->getUsersInvolved() as $user) { + $users[] = $user; + } + } + + foreach ($this->getHandler($entityWorkflow)->getSuggestedUsers($entityWorkflow) as $user) { + $users[] = $user; + } + + return array_values( + // filter objects to remove duplicates + array_filter( + $users, + fn ($o, $k) => array_search($o, $users, true) === $k, + ARRAY_FILTER_USE_BOTH + ) + ); + } }