Created email senders and integration tests for these senders

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent 2a145bfa3e
commit 54baa4d6ba

@ -26,7 +26,8 @@ class BasicEmailSender implements EmailSender {
$mailer->Password = $heskSettings['smtp_password'];
}
$mailer->FromName = $heskSettings['noreply_name'] ? $heskSettings['noreply_name'] : '';
$mailer->FromName = $heskSettings['noreply_name'] !== null &&
$heskSettings['noreply_name'] !== '' ? $heskSettings['noreply_name'] : '';
$mailer->From = $heskSettings['noreply_mail'];
if ($emailBuilder->to !== null) {
@ -58,6 +59,13 @@ class BasicEmailSender implements EmailSender {
}
$mailer->Timeout = $heskSettings['smtp_timeout'];
if ($emailBuilder->attachments !== null) {
foreach ($emailBuilder->attachments as $attachment) {
$mailer->addAttachment(__DIR__ . '/../../../' . $heskSettings['attach_dir'] . '/' . $attachment->savedName,
$attachment->fileName);
}
}
if ($mailer->send()) {
return true;
}

@ -15,7 +15,7 @@ interface EmailSender {
* @param $heskSettings array
* @param $modsForHeskSettings array
* @param $sendAsHtml bool
* @return bool|string true if message sent successfully, error string otherwise
* @return bool|string|\stdClass true if message sent successfully, string for PHPMail/Smtp error, stdClass for Mailgun error
*/
function sendEmail($emailBuilder, $heskSettings, $modsForHeskSettings, $sendAsHtml);
}

@ -5,25 +5,69 @@ namespace BusinessLogic\Emails;
use BusinessLogic\Tickets\Attachment;
use BusinessLogic\Tickets\Ticket;
use Mailgun\Mailgun;
class MailgunEmailSender implements EmailSender {
/**
* @param $emailBuilder EmailBuilder
* @param $heskSettings array
* @param $modsForHeskSettings array
*/
function sendEmail($emailBuilder, $heskSettings, $modsForHeskSettings) {
// TODO: Implement sendEmail() method.
function sendEmail($emailBuilder, $heskSettings, $modsForHeskSettings, $sendAsHtml) {
$mailgunArray = array();
$mailgunArray['from'] = $heskSettings['noreply_mail']; // Email Address
if ($heskSettings['noreply_name'] !== null && $heskSettings['noreply_name'] !== '') {
$mailgunArray['from'] = "{$heskSettings['noreply_name']} <{$heskSettings['noreply_mail']}>"; // Name and address
}
$mailgunArray['to'] = implode(',', $emailBuilder->to);
if ($emailBuilder->cc !== null) {
$mailgunArray['cc'] = implode(',', $emailBuilder->cc);
}
if ($emailBuilder->bcc !== null) {
$mailgunArray['bcc'] = implode(',', $emailBuilder->bcc);
}
$mailgunArray['subject'] = $emailBuilder->subject;
$mailgunArray['text'] = $emailBuilder->message;
if ($sendAsHtml) {
$mailgunArray['html'] = $emailBuilder->htmlMessage;
}
$mailgunAttachments = array();
if ($emailBuilder->attachments !== null) {
foreach ($emailBuilder->attachments as $attachment) {
$mailgunAttachments[] = array(
'remoteName' => $attachment->fileName,
'filePath' => __DIR__ . '/../../../' . $heskSettings['attach_dir'] . '/' . $attachment->savedName
);
}
}
var_dump($mailgunArray);
$result = $this->sendMessage($mailgunArray, $mailgunAttachments, $modsForHeskSettings);
if (isset($result->http_response_code)
&& $result->http_response_code === 200) {
return true;
}
return $result;
}
/**
* @param $emailBuilder EmailBuilder
* @param $ticket Ticket
* @param $attachments Attachment[]
* @param $heskSettings array
* @param $modsForHeskSettings array
*/
function sendEmailWithTicket($emailBuilder, $ticket, $attachments, $heskSettings, $modsForHeskSettings) {
// TODO: Implement sendEmailWithTicket() method.
private function sendMessage($mailgunArray, $attachments, $modsForHeskSettings) {
$messageClient = new Mailgun($modsForHeskSettings['mailgun_api_key']);
$mailgunAttachments = array();
if (count($attachments) > 0) {
$mailgunAttachments = array(
'attachment' => $attachments
);
}
$result = $messageClient->sendMessage($modsForHeskSettings['mailgun_domain'], $mailgunArray, $mailgunAttachments);
return $result;
}
}

@ -2,9 +2,10 @@
namespace BusinessLogic\Emails;
use PHPUnit\Framework\TestCase;
use BusinessLogic\IntegrationTestCaseBase;
use BusinessLogic\Tickets\Attachment;
class BasicEmailSenderIntegrationTest extends TestCase {
class BasicEmailSenderIntegrationTest extends IntegrationTestCaseBase {
/**
* @var $emailSender BasicEmailSender;
*/
@ -20,9 +21,16 @@ class BasicEmailSenderIntegrationTest extends TestCase {
*/
private $modsForHeskSettings;
/**
* @var $attachmentsToPurge string[]
*/
private $attachmentsToPurge;
protected function setUp() {
global $hesk_settings, $modsForHesk_settings;
$this->skip();
if (!defined('IN_SCRIPT')) {
define('IN_SCRIPT', 1);
}
@ -32,6 +40,13 @@ class BasicEmailSenderIntegrationTest extends TestCase {
$this->emailSender = new BasicEmailSender();
$this->heskSettings = $hesk_settings;
$this->modsForHeskSettings = $modsForHesk_settings;
$this->attachmentsToPurge = array();
}
protected function tearDown() {
foreach ($this->attachmentsToPurge as $file) {
unlink($file);
}
}
function testItCanSendHtmlMail() {
@ -45,6 +60,24 @@ class BasicEmailSenderIntegrationTest extends TestCase {
$emailBuilder->htmlMessage = "Test <b>HTML</b> <i>message</i>";
$emailBuilder->subject = "BasicEmailSenderIntegrationTest";
// Uncomment to test attachments.
$attachment = new Attachment();
$attachment->id = 1;
$attachment->fileName = "file.txt";
$attachment->savedName = "test1.txt";
$filename1 = __DIR__ . '/../../../../' . $this->heskSettings['attach_dir'] . '/' . $attachment->savedName;
file_put_contents($filename1, 'TEST DATA');
$otherAttachment = new Attachment();
$otherAttachment->id = 2;
$otherAttachment->fileName = "file2.txt";
$otherAttachment->savedName = "test2.txt";
$filename2 = __DIR__ . '/../../../../' . $this->heskSettings['attach_dir'] . '/' . $otherAttachment->savedName;
file_put_contents($filename2, 'TEST DATA 2');
$emailBuilder->attachments = array($attachment, $otherAttachment);
$this->attachmentsToPurge = array($filename1, $filename2);
//-- Act
$result = $this->emailSender->sendEmail($emailBuilder, $this->heskSettings, $this->modsForHeskSettings, true);

@ -0,0 +1,109 @@
<?php
namespace BusinessLogic\Emails;
use BusinessLogic\IntegrationTestCaseBase;
use BusinessLogic\Tickets\Attachment;
class MailgunEmailSenderIntegrationTest extends IntegrationTestCaseBase {
/**
* @var $emailSender MailgunEmailSender;
*/
private $emailSender;
/**
* @var $heskSettings array
*/
private $heskSettings;
/**
* @var $modsForHeskSettings array
*/
private $modsForHeskSettings;
/**
* @var $attachmentsToPurge string[]
*/
private $attachmentsToPurge;
protected function setUp() {
global $hesk_settings, $modsForHesk_settings;
$this->skip();
if (!defined('IN_SCRIPT')) {
define('IN_SCRIPT', 1);
}
require(__DIR__ . '/../../../../hesk_settings.inc.php');
require(__DIR__ . '/../../integration_test_mfh_settings.php');
$this->emailSender = new MailgunEmailSender();
$this->heskSettings = $hesk_settings;
$this->modsForHeskSettings = $modsForHesk_settings;
$this->attachmentsToPurge = array();
}
protected function tearDown() {
foreach ($this->attachmentsToPurge as $file) {
unlink($file);
}
}
function testItCanSendMail() {
//-- Arrange
$emailBuilder = new EmailBuilder();
$emailBuilder->to = array('mfh1@mailinator.com');
$emailBuilder->cc = array('mfh2@mailinator.com');
$emailBuilder->bcc = array('mfh3@mailinator.com');
$emailBuilder->message = "Test PLAIN TEXT message";
$emailBuilder->htmlMessage = "Test <b>HTML</b> <i>message</i>";
$emailBuilder->subject = "MailgunEmailSenderIntegrationTest";
// Uncomment to test attachments.
$attachment = new Attachment();
$attachment->id = 1;
$attachment->fileName = "file.txt";
$attachment->savedName = "test1.txt";
$filename1 = __DIR__ . '/../../../../' . $this->heskSettings['attach_dir'] . '/' . $attachment->savedName;
file_put_contents($filename1, 'TEST DATA');
$otherAttachment = new Attachment();
$otherAttachment->id = 2;
$otherAttachment->fileName = "file2.txt";
$otherAttachment->savedName = "test2.txt";
$filename2 = __DIR__ . '/../../../../' . $this->heskSettings['attach_dir'] . '/' . $otherAttachment->savedName;
file_put_contents($filename2, 'TEST DATA 2');
$emailBuilder->attachments = array($attachment, $otherAttachment);
$this->attachmentsToPurge = array($filename1, $filename2);
//-- Act
$result = $this->emailSender->sendEmail($emailBuilder, $this->heskSettings, $this->modsForHeskSettings, true);
//-- Assert
if ($result !== true) {
$this->fail($result);
}
}
function testItCanSendPlaintextMail() {
//-- Arrange
//$hesk_settings['smtp'] = 0 //Uncomment this to use PHPMail
$emailBuilder = new EmailBuilder();
$emailBuilder->to = array('mfh1@mailinator.com');
$emailBuilder->cc = array('mfh2@mailinator.com');
$emailBuilder->bcc = array('mfh3@mailinator.com');
$emailBuilder->message = "Test PLAIN TEXT message";
$emailBuilder->subject = "MailgunEmailSenderIntegrationTest";
//-- Act
$result = $this->emailSender->sendEmail($emailBuilder, $this->heskSettings, $this->modsForHeskSettings, false);
//-- Assert
if ($result !== true) {
$this->fail($result);
}
}
}

@ -0,0 +1,10 @@
<?php
namespace BusinessLogic;
use PHPUnit\Framework\TestCase;
class IntegrationTestCaseBase extends TestCase {
function skip() {
$this->markTestSkipped(sprintf("Skipping Integration Test %s", get_class($this)));
}
}

@ -1,2 +1,3 @@
<?php
require_once(__DIR__ . '/BusinessLogic/IntegrationTestCaseBase.php');
require_once(__DIR__ . '/../bootstrap.php');

@ -11,6 +11,11 @@
],
"require": {
"phpunit/phpunit": "5.7.9",
"phpmailer/phpmailer": "^5.2"
"phpmailer/phpmailer": "^5.2",
"mailgun/mailgun-php": "^2.1",
"php-http/guzzle6-adapter": "^1.1",
"php-http/message": "^1.5",
"php-http/curl-client": "^1.7",
"guzzlehttp/psr7": "^1.3"
}
}

1018
api/composer.lock generated

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save