/** * {@inheritdoc} * * @throws \LogicException If the name is invalid. */ public function publishNotification(string $name, NotificationInput $event, array $channels, string $template = null) { if (null === $template) { if (!array_key_exists($name, $this->templateMap)) { throw new \LogicException(sprintf('Cannot generate template name for notification with name "%s"!', $name)); } $template = $this->templateMap[$name]; } $event->setTemplateSource($template); $enabledChannels = array_filter($this->channels, function (string $name) use($channels) : bool { return in_array($name, $channels, true); }, ARRAY_FILTER_USE_KEY); array_walk($enabledChannels, function (NotificationChannelInterface $channel) use($event) { $channel->publish($event); }); }
public function testSendMail() { $defaultEmail = '*****@*****.**'; $input = new NotificationInput(); $input->addUser(User::create('Ma27', '123456', '*****@*****.**', new PhpPasswordHasher())); $input->addUser(User::create('benbieler', '123456', '*****@*****.**', new PhpPasswordHasher())); $input->setTemplateSource('AppBundle:emails:test'); $mailer = $this->getMockWithoutInvokingTheOriginalConstructor(\Swift_Mailer::class); $mailer->expects(self::once())->method('send')->willReturnCallback(function (\Swift_Message $message) { self::assertSame($message->getTo(), ['*****@*****.**' => 'Ma27', '*****@*****.**' => 'benbieler']); self::assertSame($message->getFrom(), ['*****@*****.**' => 'Sententiaregum']); }); $templatingEngine = $this->getMockWithoutInvokingTheOriginalConstructor(EngineInterface::class); $templatingEngine->expects(self::at(0))->method('render')->with('AppBundle:emails:test.txt.twig', ['locale' => 'en'])->willReturn('Notifications'); $templatingEngine->expects(self::at(1))->method('render')->with('AppBundle:emails:test.html.twig', ['locale' => 'en'])->willReturn('<b>Notifications</b>'); $translator = $this->getMock(TranslatorInterface::class); $translator->expects(self::once())->method('trans')->with('NOTIFICATIONS_SUBJECT', [], 'notifications')->willReturn('Sententiaregum Notifications'); $translator->expects(self::once())->method('getLocale')->willReturn('en'); $listener = new MailingChannel($mailer, $translator, $templatingEngine, $defaultEmail); $listener->publish($input); }
public function testSetTemplateSource() { $event = new NotificationInput(); $event->setTemplateSource('@AppBundle/Resources/views/Email/notification.html.twig'); $this->assertSame('@AppBundle/Resources/views/Email/notification.html.twig', $event->getTemplateSource()); }