diff --git a/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationOnTransitionTest.php b/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationOnTransitionTest.php index fed7f2dce..e2fc30da8 100644 --- a/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationOnTransitionTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Workflow/EventSubscriber/NotificationOnTransitionTest.php @@ -13,6 +13,7 @@ namespace Chill\MainBundle\Tests\Workflow\EventSubscriber; use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\UserGroup; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep; use Chill\MainBundle\Workflow\EventSubscriber\NotificationOnTransition; @@ -20,8 +21,6 @@ use Chill\MainBundle\Workflow\Helper\MetadataExtractor; use Doctrine\ORM\EntityManagerInterface; use PHPUnit\Framework\TestCase; use Prophecy\Argument; -use Prophecy\Call\Call; -use Prophecy\Exception\Prediction\FailedPredictionException; use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Security\Core\Security; use Symfony\Component\Workflow\Event\Event; @@ -57,29 +56,23 @@ final class NotificationOnTransitionTest extends TestCase $id->setValue($entityWorkflow, 1); $step = new EntityWorkflowStep(); + $userGroup = (new UserGroup())->addUser($userInGroup = new User())->addUser($dest); $entityWorkflow->addStep($step); - $step->addDestUser($dest) + $step + ->addDestUser($dest) + ->addDestUserGroup($userGroup) ->setCurrentStep('to_state'); $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->should( - static function ($args) use ($dest) { - /** @var Call[] $args */ - if (1 !== \count($args)) { - throw new FailedPredictionException('no notification sent'); - } - $notification = $args[0]->getArguments()[0]; - - if (!$notification instanceof Notification) { - throw new FailedPredictionException('persist is not a notification'); - } - - if (!$notification->getAddressees()->contains($dest)) { - throw new FailedPredictionException('the dest is not notified'); - } - } - ); + // we check that both notification has been persisted once, + // eliminating doublons + $em->persist(Argument::that( + fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($dest) + ))->shouldBeCalledOnce(); + $em->persist(Argument::that( + fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($userInGroup) + ))->shouldBeCalledOnce(); $engine = $this->prophesize(\Twig\Environment::class); $engine->render(Argument::type('string'), Argument::type('array')) diff --git a/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationOnTransition.php b/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationOnTransition.php index d955d777f..89ae65f4c 100644 --- a/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationOnTransition.php +++ b/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/NotificationOnTransition.php @@ -13,6 +13,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber; use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\UserGroup; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Workflow\Helper\MetadataExtractor; use Doctrine\ORM\EntityManagerInterface; @@ -69,7 +70,18 @@ class NotificationOnTransition implements EventSubscriberInterface // the dests for the current step $entityWorkflow->getCurrentStep()->getDestUser()->toArray(), // the cc users for the current step - $entityWorkflow->getCurrentStep()->getCcUser()->toArray() + $entityWorkflow->getCurrentStep()->getCcUser()->toArray(), + // the users within groups + $entityWorkflow->getCurrentStep()->getDestUserGroups()->reduce( + function (array $accumulator, UserGroup $userGroup) { + foreach ($userGroup->getUsers() as $user) { + $accumulator[] = $user; + } + + return $accumulator; + }, + [] + ), ) as $dest) { $dests[spl_object_hash($dest)] = $dest; }