public function testTemplateArgumentReplacesTemplateMapConfiguration() { $channel = $this->getMock(NotificationChannelInterface::class); $channel->expects(self::once())->method('publish'); $user = User::create('Ma27', '123456', '*****@*****.**', new PhpPasswordHasher()); $class = 'AppBundle\\Model\\Core\\Handler\\SecretHandler'; $event = new NotificationInput(); $event->setLanguage('en')->addParameter('foo', 'bar')->addUser($user); $notificator = new ChannelDelegatingNotificator([], ['mail' => $channel]); $notificator->publishNotification($class, $event, ['mail'], 'AppBundle:Email/Secret:data'); self::assertSame('AppBundle:Email/Secret:data', $event->getTemplateSource()); }
/** * {@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); }); }
/** * Creates the event and runs the notificator. * * @param mixed[] $parameters * @param \AppBundle\Model\User\User[] $users * @param string[] $channels * @param string $language * @param string|null $template * * @throws \LogicException If the dispatcher is not set. */ public function notify(array $parameters, array $users, array $channels = [], string $language = null, string $template = null) { if (!$this->notificator) { throw new \LogicException('Notification service must be set before running `NotificatableTrait::notify`!'); } $event = new NotificationInput(); if (null !== $language) { $event->setLanguage($language); } array_walk($users, [$event, 'addUser']); // can't flip and walk here since parameter values might be objects // and those must not be flipped. foreach ($parameters as $name => $value) { $event->addParameter($name, $value); } $this->notificator->publishNotification(get_class($this), $event, $channels, $template); }
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); }
/** * Evaluates the locale. * * @param NotificationInput $input * * @return string */ private function getLocale(NotificationInput $input) : string { return null === $input->getLanguage() ? $this->translator->getLocale() : $input->getLanguage(); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Cannot apply parameter locale since this parameter is reserved! */ public function addInvalidParameter() { $event = new NotificationInput(); $event->addParameter('locale', 'de'); }