From d6c55c830b88a8eb7494b551181952baddc711c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 13 Nov 2024 12:40:53 +0100 Subject: [PATCH] Persist EntityWorkflow and its steps with EntityManager Refactored NotificationToUserGroupsOnTransition to utilize EntityManager for persisting EntityWorkflowStep. This ensures entities have generated IDs, leading to proper email notifications during workflow transitions. --- .../NotificationToUserGroupsOnTransitionTest.php | 12 +++--------- .../NotificationToUserGroupsOnTransition.php | 8 ++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationToUserGroupsOnTransitionTest.php b/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationToUserGroupsOnTransitionTest.php index 329294ecc..ffa102978 100644 --- a/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationToUserGroupsOnTransitionTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationToUserGroupsOnTransitionTest.php @@ -59,9 +59,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase public function testOnCompletedSendNotificationToUserGroupWithEmailAddress(): void { $entityWorkflow = new EntityWorkflow(); - $reflection = new \ReflectionClass($entityWorkflow); - $idProperty = $reflection->getProperty('id'); - $idProperty->setValue($entityWorkflow, 1); + $this->em->persist($entityWorkflow); $entityWorkflow->setWorkflowName('dummy'); $dto = new WorkflowTransitionContextDTO($entityWorkflow); @@ -69,11 +67,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase $ug->setEmail('test@email.com')->setLabel(['fr' => 'test group']); $mailer = $this->prophesize(MailerInterface::class); - $sendMethod = $mailer->send(Argument::that(function (RawMessage $message) use ($entityWorkflow): bool { - // we need to set an id to the current step of the entity workflow - $this->em->persist($entityWorkflow); - $this->em->persist($entityWorkflow->getCurrentStep()); - + $sendMethod = $mailer->send(Argument::that(function (RawMessage $message): bool { if (!$message instanceof TemplatedEmail) { return false; } @@ -140,7 +134,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase } }); - $notificationEventSubscriber = new NotificationToUserGroupsOnTransition($this->twig, $metadataExtractor, $registry, $mailer); + $notificationEventSubscriber = new NotificationToUserGroupsOnTransition($this->twig, $metadataExtractor, $registry, $mailer, $this->em); $eventDispatcher->addSubscriber($notificationEventSubscriber); return $registry; diff --git a/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationToUserGroupsOnTransition.php b/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationToUserGroupsOnTransition.php index 8ac5ab750..63682ec1a 100644 --- a/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationToUserGroupsOnTransition.php +++ b/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationToUserGroupsOnTransition.php @@ -14,6 +14,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber; use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Workflow\Helper\MetadataExtractor; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Mailer\MailerInterface; @@ -27,6 +28,7 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr private MetadataExtractor $metadataExtractor, private Registry $registry, private MailerInterface $mailer, + private EntityManagerInterface $entityManager, ) {} public static function getSubscribedEvents(): array @@ -61,6 +63,12 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()) ); + $currentStep = $entityWorkflow->getCurrentStep(); + if (!$this->entityManager->contains($currentStep)) { + // this is necessary to generate an id for the step + $this->entityManager->persist($currentStep); + } + // send to groups foreach ($entityWorkflow->getCurrentStep()->getDestUserGroups() as $userGroup) { if (!$userGroup->hasEmail()) {