Filled out the Plugin\Email::sendEmail(). Pulls in the following settings from the phpci config under the heading email_settings:
smtp_address smtp_port smtp_username smtp_password and from_address
This commit is contained in:
parent
f76a2a75cc
commit
f0a5ba50ca
2 changed files with 202 additions and 3 deletions
|
|
@ -29,10 +29,32 @@ class Email implements \PHPCI\Plugin
|
|||
*/
|
||||
protected $options;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci, array $options = array())
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $emailConfig;
|
||||
|
||||
/**
|
||||
* @var \Swift_Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci,
|
||||
array $options = array(),
|
||||
\Swift_Mailer $mailer = null)
|
||||
{
|
||||
$this->phpci = $phpci;
|
||||
$this->options = $options;
|
||||
$this->emailConfig = $phpci->getConfig('email_settings');
|
||||
|
||||
// Either a mailer will have been passed in or we load from the
|
||||
// config.
|
||||
if ($mailer === null) {
|
||||
$this->loadSwiftMailerFromConfig();
|
||||
}
|
||||
else {
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -43,4 +65,53 @@ class Email implements \PHPCI\Plugin
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $toAddresses Array or single address to send to
|
||||
* @param string $subject Email subject
|
||||
* @param string $body Email body
|
||||
* @return array Array of failed addresses
|
||||
*/
|
||||
public function sendEmail($toAddresses, $subject, $body)
|
||||
{
|
||||
$message = \Swift_Message::newInstance($subject)
|
||||
->setFrom($this->getMailConfig('from_address'))
|
||||
->setTo($toAddresses)
|
||||
->setBody($body);
|
||||
$failedAddresses = array();
|
||||
$this->mailer->send($message, $failedAddresses);
|
||||
|
||||
return $failedAddresses;
|
||||
}
|
||||
|
||||
protected function loadSwiftMailerFromConfig()
|
||||
{
|
||||
/** @var \Swift_SmtpTransport $transport */
|
||||
$transport = \Swift_SmtpTransport::newInstance(
|
||||
$this->getMailConfig('smtp_address'),
|
||||
$this->getMailConfig('smtp_port')
|
||||
);
|
||||
$transport->setUsername($this->getMailConfig('smtp_username'));
|
||||
$transport->setPassword($this->getMailConfig('smtp_password'));
|
||||
|
||||
$this->mailer = \Swift_Mailer::newInstance($transport);
|
||||
}
|
||||
|
||||
protected function getMailConfig($configName)
|
||||
{
|
||||
if (isset($this->emailConfig[$configName])) {
|
||||
return $this->emailConfig[$configName];
|
||||
}
|
||||
// Check defaults
|
||||
else {
|
||||
switch($configName) {
|
||||
case 'smtp_port':
|
||||
return '25';
|
||||
case 'from_address':
|
||||
return "notifications-ci@phptesting.org";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,16 +29,35 @@ class EmailTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
protected $mockCiBuilder;
|
||||
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject $mockMailer
|
||||
*/
|
||||
protected $mockMailer;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mockCiBuilder = $this->getMock(
|
||||
'\PHPCI\Builder',
|
||||
array(),
|
||||
array('getConfig'),
|
||||
array(),
|
||||
"mockBuilder",
|
||||
false
|
||||
);
|
||||
$this->mockCiBuilder->buildPath = "/";
|
||||
$this->mockCiBuilder->expects($this->any())
|
||||
->method('getConfig')
|
||||
->with('email_settings')
|
||||
->will($this->returnValue(array(
|
||||
'from_address' => "test-from-address@example.com"
|
||||
)));
|
||||
|
||||
$this->mockMailer = $this->getMock(
|
||||
'\Swift_Mailer',
|
||||
array('send'),
|
||||
array(),
|
||||
"mockMailer",
|
||||
false
|
||||
);
|
||||
|
||||
$this->loadEmailPluginWithOptions();
|
||||
}
|
||||
|
|
@ -47,7 +66,8 @@ class EmailTest extends \PHPUnit_Framework_TestCase
|
|||
{
|
||||
$this->testedEmailPlugin = new EmailPlugin(
|
||||
$this->mockCiBuilder,
|
||||
$arrOptions
|
||||
$arrOptions,
|
||||
$this->mockMailer
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -61,4 +81,112 @@ class EmailTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::sendEmail
|
||||
*/
|
||||
public function testSendEmail_CallsMailerSend()
|
||||
{
|
||||
$this->mockMailer->expects($this->once())
|
||||
->method('send');
|
||||
$this->testedEmailPlugin->sendEmail("test@email.com", "hello", "body");
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::sendEmail
|
||||
*/
|
||||
public function testSendEmail_BuildsAMessageObject()
|
||||
{
|
||||
$subject = "Test mail";
|
||||
$body = "Message Body";
|
||||
$toAddress = "test@example.com";
|
||||
|
||||
$this->mockMailer->expects($this->once())
|
||||
->method('send')
|
||||
->with($this->isInstanceOf('\Swift_Message'), $this->anything());
|
||||
$this->testedEmailPlugin->sendEmail($toAddress, $subject, $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::sendEmail
|
||||
*/
|
||||
public function testSendEmail_BuildsExpectedMessage()
|
||||
{
|
||||
$subject = "Test mail";
|
||||
$body = "Message Body";
|
||||
$toAddress = "test@example.com";
|
||||
$expectedMessage = \Swift_Message::newInstance($subject)
|
||||
->setFrom('test-from-address@example.com')
|
||||
->setTo($toAddress)
|
||||
->setBody($body);
|
||||
|
||||
/** @var \Swift_Message $actualMail */
|
||||
$actualMail = null;
|
||||
$this->catchMailPassedToSend($actualMail);
|
||||
|
||||
$this->testedEmailPlugin->sendEmail($toAddress, $subject, $body);
|
||||
|
||||
$this->assertSystemMail(
|
||||
$toAddress,
|
||||
'test-from-address@example.com',
|
||||
$body,
|
||||
$subject,
|
||||
$actualMail
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Swift_Message $actualMail passed by ref and populated with
|
||||
* the message object the mock mailer
|
||||
* receives.
|
||||
*/
|
||||
protected function catchMailPassedToSend(&$actualMail)
|
||||
{
|
||||
$this->mockMailer->expects($this->once())
|
||||
->method('send')
|
||||
->will(
|
||||
$this->returnCallback(
|
||||
function ($passedMail) use (&$actualMail) {
|
||||
$actualMail = $passedMail;
|
||||
return array();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the actual mail object is populated as expected.
|
||||
*
|
||||
* @param string $expectedToAddress
|
||||
* @param $expectedFromAddress
|
||||
* @param string $expectedBody
|
||||
* @param string $expectedSubject
|
||||
* @param \Swift_Message $actualMail
|
||||
*/
|
||||
protected function assertSystemMail($expectedToAddress,
|
||||
$expectedFromAddress,
|
||||
$expectedBody,
|
||||
$expectedSubject,
|
||||
$actualMail)
|
||||
{
|
||||
$this->assertEquals(
|
||||
array($expectedFromAddress => null),
|
||||
$actualMail->getFrom()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array($expectedToAddress => null),
|
||||
$actualMail->getTo()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expectedBody,
|
||||
$actualMail->getBody()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expectedSubject,
|
||||
$actualMail->getSubject()
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue