/**
  * @dataProvider getHtmlValues
  *
  * @param string|null $htmlValue
  */
 public function testSendWithMailRendered($htmlValue)
 {
     $message = $this->getMockBuilder(\Swift_Message::class)->disableOriginalConstructor()->getMock();
     /* @var MailRenderedInterface|\PHPUnit_Framework_MockObject_MockObject $mail */
     $mail = $this->getMockBuilder(MailRenderedInterface::class)->getMock();
     $mail->expects($this->once())->method('getSubject')->will($this->returnValue('Subject'));
     $mail->expects($this->once())->method('getHTMLBody')->will($this->returnValue($htmlValue));
     $mail->expects($this->once())->method('getBody')->will($this->returnValue('Body'));
     $this->swiftMailer->expects($this->at(0))->method('send')->with($message)->will($this->returnValue(1));
     $this->assertTrue($this->transport->send($message, $mail));
 }
 /**
  * Tests the sendEmail method.
  */
 public function testSendEmail()
 {
     $message = new Message();
     $message->setBody(array('subject' => 'subject', 'from' => array('email' => '*****@*****.**', 'name' => 'nameFrom'), 'to' => array('*****@*****.**', '*****@*****.**' => 'nameTo2'), 'replyTo' => array('*****@*****.**', '*****@*****.**' => 'nameReplyTo2'), 'message' => array('text' => 'message text', 'html' => 'message html')));
     $mail = $this->getMockBuilder('Swift_Message')->disableOriginalConstructor()->getMock();
     $mail->expects($this->once())->method('setSubject')->with($this->equalTo('subject'))->willReturnSelf();
     $mail->expects($this->once())->method('setFrom')->with($this->equalTo(array('*****@*****.**' => 'nameFrom')))->willReturnSelf();
     $mail->expects($this->once())->method('setTo')->with($this->equalTo(array('*****@*****.**', '*****@*****.**' => 'nameTo2')))->willReturnSelf();
     $mail->expects($this->once())->method('setReplyTo')->with($this->equalTo(array('*****@*****.**', '*****@*****.**' => 'nameReplyTo2')))->willReturnSelf();
     $mail->expects($this->exactly(2))->method('addPart')->withConsecutive(array($this->equalTo('message text'), $this->equalTo('text/plain')), array($this->equalTo('message html'), $this->equalTo('text/html')))->willReturnSelf();
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($mail));
     $method = new \ReflectionMethod($this->consumer, 'sendEmail');
     $method->setAccessible(true);
     $method->invoke($this->consumer, $message);
 }
 public function testMessageSubjectFormatting()
 {
     // Wire Mailer to expect a specific Swift_Message with a customized Subject
     $messageTemplate = new \Swift_Message();
     $messageTemplate->setSubject('Alert: %level_name% %message%');
     $receivedMessage = null;
     $this->mailer->expects($this->once())->method('send')->with($this->callback(function ($value) use(&$receivedMessage) {
         $receivedMessage = $value;
         return true;
     }));
     $handler = new SwiftMailerHandler($this->mailer, $messageTemplate);
     $records = array($this->getRecord(Logger::EMERGENCY));
     $handler->handleBatch($records);
     $this->assertEquals('Alert: EMERGENCY test', $receivedMessage->getSubject());
 }
 public function testNotifyWithEmptyAuthor()
 {
     $ticketUniqueId = UniqueId::generate();
     $reporter = new UserAdapter(1, UserAdapter::TYPE_DIAMANTE);
     $assignee = new User();
     $assignee->setId(2);
     $assignee->setEmail('*****@*****.**');
     $author = null;
     $branch = new Branch('KEY', 'Name', 'Description');
     $ticket = new Ticket($ticketUniqueId, new TicketSequenceNumber(1), 'Subject', 'Description', $branch, $reporter, $assignee, new Source(Source::WEB), new Priority(Priority::PRIORITY_MEDIUM), new Status(Status::NEW_ONE));
     $notification = new TicketNotification((string) $ticketUniqueId, $author, 'Header', 'Subject', new \ArrayIterator(array('key' => 'value')), array('file.ext'));
     $message = new \Swift_Message();
     $this->watchersService->expects($this->once())->method('getWatchers')->will($this->returnValue([new WatcherList($ticket, 'diamante_1')]));
     $this->diamanteUserRepository->expects($this->exactly(2))->method('get')->with(1)->will($this->returnValue($this->diamanteUser));
     $this->configManager->expects($this->once())->method('get')->will($this->returnValue('*****@*****.**'));
     $this->nameFormatter->expects($this->once())->method('format')->with($this->diamanteUser)->will($this->returnValue('First Last'));
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->ticketRepository->expects($this->once())->method('getByUniqueId')->with($ticketUniqueId)->will($this->returnValue($ticket));
     $this->templateResolver->expects($this->any())->method('resolve')->will($this->returnValueMap(array(array($notification, TemplateResolver::TYPE_TXT, 'txt.template.html'), array($notification, TemplateResolver::TYPE_HTML, 'html.template.html'))));
     $optionsConstraint = $this->logicalAnd($this->arrayHasKey('changes'), $this->arrayHasKey('attachments'), $this->arrayHasKey('user'), $this->arrayHasKey('header'), $this->contains($notification->getChangeList()), $this->contains($notification->getAttachments()), $this->contains('First Last'), $this->contains($notification->getHeaderText()));
     $this->twig->expects($this->at(0))->method('render')->with('txt.template.html', $optionsConstraint)->will($this->returnValue('Rendered TXT template'));
     $this->twig->expects($this->at(1))->method('render')->with('html.template.html', $optionsConstraint)->will($this->returnValue('Rendered HTML template'));
     $this->mailer->expects($this->once())->method('send')->with($this->logicalAnd($this->isInstanceOf('\\Swift_Message'), $this->callback(function (\Swift_Message $other) use($notification) {
         $to = $other->getTo();
         return false !== strpos($other->getSubject(), $notification->getSubject()) && false !== strpos($other->getSubject(), 'KEY-1') && false !== strpos($other->getBody(), 'Rendered TXT template') && array_key_exists('*****@*****.**', $to) && $other->getHeaders()->has('References') && false !== strpos($other->getHeaders()->get('References'), '*****@*****.**') && false !== strpos($other->getHeaders()->get('References'), '*****@*****.**');
     })));
     $this->messageReferenceRepository->expects($this->once())->method('findAllByTicket')->with($ticket)->will($this->returnValue(array(new MessageReference('*****@*****.**', $ticket), new MessageReference('*****@*****.**', $ticket))));
     $this->messageReferenceRepository->expects($this->once())->method('store')->with($this->logicalAnd($this->isInstanceOf('\\Diamante\\DeskBundle\\Model\\Ticket\\EmailProcessing\\MessageReference')));
     $notifier = new EmailNotifier($this->container, $this->twig, $this->mailer, $this->templateResolver, $this->ticketRepository, $this->messageReferenceRepository, $this->userService, $this->nameFormatter, $this->diamanteUserRepository, $this->configManager, $this->oroUserManager, $this->watchersService, $this->senderHost);
     $notifier->notify($notification);
 }
 public function testMessageCanBeCustomizedGivenLoggedData()
 {
     // Wire Mailer to expect a specific Swift_Message with a customized Subject
     $expectedMessage = new \Swift_Message();
     $this->mailer->expects($this->once())->method('send')->with($this->callback(function ($value) use($expectedMessage) {
         return $value instanceof \Swift_Message && $value->getSubject() === 'Emergency' && $value === $expectedMessage;
     }));
     // Callback dynamically changes subject based on number of logged records
     $callback = function ($content, array $records) use($expectedMessage) {
         $subject = count($records) > 0 ? 'Emergency' : 'Normal';
         $expectedMessage->setSubject($subject);
         return $expectedMessage;
     };
     $handler = new SwiftMailerHandler($this->mailer, $callback);
     // Logging 1 record makes this an Emergency
     $records = array($this->getRecord(Logger::EMERGENCY));
     $handler->handleBatch($records);
 }
 public function testSendConfirmationEmail()
 {
     $email = '*****@*****.**';
     $activationHash = md5(time());
     $fromEmail = '*****@*****.**';
     $htmlTemplate = 'html.tpl';
     $txtTemplate = 'txt.tpl';
     $registrationMailer = new RegistrationMailer($this->twig, $this->mailer, $fromEmail, $htmlTemplate, $txtTemplate);
     $confirmation = new \Swift_Message();
     $renderedHtmlTpl = 'rendered html';
     $renderedTxtTpl = 'rendered txt';
     $this->twig->expects($this->at(0))->method('render')->with($txtTemplate)->will($this->returnValue($renderedTxtTpl));
     $this->twig->expects($this->at(1))->method('render')->with($htmlTemplate)->will($this->returnValue($renderedHtmlTpl));
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($confirmation));
     $this->mailer->expects($this->once())->method('send')->with($this->logicalAnd($this->isInstanceOf('\\Swift_Message'), $this->callback(function (\Swift_Message $other) use($email, $renderedTxtTpl, $fromEmail) {
         return $other->getSubject() == 'Confirmation' && $other->getBody() == $renderedTxtTpl && array_key_exists($email, $other->getTo()) && array_key_exists($fromEmail, $other->getReplyTo());
     })));
     $registrationMailer->sendConfirmationEmail($email, $activationHash);
 }
 public function testNotify()
 {
     $author = new ApiUser('*****@*****.**', 'password');
     $notification = new UserNotification($author, 'Header');
     $format = '%prefix% %first_name% %middle_name% %last_name% %suffix%';
     $message = new \Swift_Message();
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->configManager->expects($this->once())->method('get')->will($this->returnValue('Mike The Bot'));
     $this->nameFormatter->expects($this->any())->method('format')->with($this->diamanteUser)->will($this->returnValue('First Last'));
     $this->userService->expects($this->once())->method('verifyDiamanteUserExists')->with($this->equalTo($author->getEmail()))->will($this->returnValue(1));
     $this->userService->expects($this->once())->method('getByUser')->with($this->equalTo(new UserAdapter(1, UserAdapter::TYPE_DIAMANTE)))->will($this->returnValue($this->diamanteUser));
     $this->nameFormatter->expects($this->once())->method('getNameFormat')->will($this->returnValue($format));
     $this->templateResolver->expects($this->any())->method('resolve')->will($this->returnValueMap(array(array($notification, TemplateResolver::TYPE_TXT, 'txt.template.html'), array($notification, TemplateResolver::TYPE_HTML, 'html.template.html'))));
     $optionsConstraint = $this->logicalAnd($this->arrayHasKey('user'), $this->arrayHasKey('header'), $this->contains('First  Last'), $this->contains($notification->getHeaderText()));
     $this->twig->expects($this->at(0))->method('render')->with('txt.template.html', $optionsConstraint)->will($this->returnValue('Rendered TXT template'));
     $this->twig->expects($this->at(1))->method('render')->with('html.template.html', $optionsConstraint)->will($this->returnValue('Rendered HTML template'));
     $this->mailer->expects($this->once())->method('send')->with($this->logicalAnd($this->isInstanceOf('\\Swift_Message'), $this->callback(function (\Swift_Message $other) use($notification) {
         $to = $other->getTo();
         return false !== strpos($other->getBody(), 'Rendered TXT template') && array_key_exists('*****@*****.**', $to);
     })));
     $notifier = new EmailNotifier($this->twig, $this->mailer, $this->templateResolver, $this->userService, $this->nameFormatter, $this->configManager, $this->senderEmail);
     $notifier->notify($notification);
 }
예제 #8
0
 /**
  * Tests the sendMessage method with no body_html block.
  */
 public function testSendMessageWithNoBodyHtmlBlock()
 {
     $this->twig->expects($this->once())->method('loadTemplate')->will($this->returnValue($this->template));
     $contact = new Contact();
     $this->template->expects($this->exactly(3))->method('renderBlock')->withConsecutive(array($this->equalTo('subject'), $this->equalTo(array('contact' => $contact))), array($this->equalTo('body_text'), $this->equalTo(array('contact' => $contact))), array($this->equalTo('body_html'), $this->equalTo(array('contact' => $contact))))->willReturnOnConsecutiveCalls('', 'Body text', '');
     $message = $this->getMockBuilder('\\Swift_Message')->disableOriginalConstructor()->getMock();
     $message->expects($this->once())->method('setSubject')->willReturnSelf();
     $message->expects($this->once())->method('setFrom')->willReturnSelf();
     $message->expects($this->once())->method('setTo')->willReturnSelf();
     $message->expects($this->once())->method('setBody')->with($this->equalTo('Body text'));
     $message->expects($this->never())->method('addPart');
     $this->serviceMailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->mailer->expects($this->once())->method('send')->with($this->equalTo($message));
     $this->serviceMailer->sendMessage($contact);
 }
예제 #9
0
 /**
  * @param string         $templateName
  * @param array          $templateParams
  * @param \Swift_Message $expectedMessage
  * @param string         $emailType
  */
 protected function assertSendCalled($templateName, array $templateParams, \Swift_Message $expectedMessage, $emailType = 'txt')
 {
     $this->emailTemplate->expects($this->once())->method('getType')->willReturn($emailType);
     $this->objectRepository->expects($this->once())->method('findOneBy')->with(['name' => $templateName])->willReturn($this->emailTemplate);
     $this->renderer->expects($this->once())->method('compileMessage')->with($this->emailTemplate, $templateParams)->willReturn([$expectedMessage->getSubject(), $expectedMessage->getBody()]);
     $to = $expectedMessage->getTo();
     $toKeys = array_keys($to);
     $this->emailHolderHelper->expects($this->once())->method('getEmail')->with($this->isInstanceOf('Oro\\Bundle\\UserBundle\\Entity\\UserInterface'))->willReturn(array_shift($toKeys));
     $this->mailer->expects($this->once())->method('send')->with($this->callback(function (\Swift_Message $actualMessage) use($expectedMessage) {
         $this->assertEquals($expectedMessage->getSubject(), $actualMessage->getSubject());
         $this->assertEquals($expectedMessage->getFrom(), $actualMessage->getFrom());
         $this->assertEquals($expectedMessage->getTo(), $actualMessage->getTo());
         $this->assertEquals($expectedMessage->getBody(), $actualMessage->getBody());
         $this->assertEquals($expectedMessage->getContentType(), $actualMessage->getContentType());
         return true;
     }));
 }