From 0c1c3d60effd570a55e09c795a799e5458e945d8 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Fri, 31 May 2013 12:36:12 +0100 Subject: [PATCH 01/13] Adding v5.0 of SwiftMailer to composer. --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7b8c37b3..d034e3c5 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "phpspec/phpspec" : "2.*", "symfony/yaml" : "2.2.x-dev", "symfony/console" : "2.2.*", - "fabpot/php-cs-fixer" : "0.3.*@dev" + "fabpot/php-cs-fixer" : "0.3.*@dev", + "swiftmailer/swiftmailer": "v5.0.0" } } From f76a2a75ccb06143bfec0559707b8d67cf0a2111 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Fri, 31 May 2013 12:56:35 +0100 Subject: [PATCH 02/13] Empty Email plugin and test committed. --- PHPCI/Plugin/Email.php | 46 ++++++++++++++++++++++++++ Tests/PHPCI/Plugin/Email.php | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 PHPCI/Plugin/Email.php create mode 100644 Tests/PHPCI/Plugin/Email.php diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php new file mode 100644 index 00000000..6cff58f9 --- /dev/null +++ b/PHPCI/Plugin/Email.php @@ -0,0 +1,46 @@ + +* @package PHPCI +* @subpackage Plugins +*/ +class Email implements \PHPCI\Plugin +{ + + /** + * @var \PHPCI\Builder + */ + protected $phpci; + + /** + * @var array + */ + protected $options; + + public function __construct(\PHPCI\Builder $phpci, array $options = array()) + { + $this->phpci = $phpci; + $this->options = $options; + } + + /** + * Connects to MySQL and runs a specified set of queries. + */ + public function execute() + { + + return true; + } +} \ No newline at end of file diff --git a/Tests/PHPCI/Plugin/Email.php b/Tests/PHPCI/Plugin/Email.php new file mode 100644 index 00000000..f1e97ffe --- /dev/null +++ b/Tests/PHPCI/Plugin/Email.php @@ -0,0 +1,64 @@ +mockCiBuilder = $this->getMock( + '\PHPCI\Builder', + array(), + array(), + "mockBuilder", + false + ); + $this->mockCiBuilder->buildPath = "/"; + + $this->loadEmailPluginWithOptions(); + } + + protected function loadEmailPluginWithOptions($arrOptions = array()) + { + $this->testedEmailPlugin = new EmailPlugin( + $this->mockCiBuilder, + $arrOptions + ); + } + + /** + * @covers PHPUnit::execute + */ + public function testExecute_ReturnsTrueWithoutArgs() + { + $returnValue = $this->testedEmailPlugin->execute(); + $expectedReturn = true; + + $this->assertEquals($expectedReturn, $returnValue); + } +} \ No newline at end of file From f0a5ba50ca1672bdbd5e1b225fb348cdccfa1ca6 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 09:40:05 +0100 Subject: [PATCH 03/13] 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 --- PHPCI/Plugin/Email.php | 73 ++++++++++++++++++- Tests/PHPCI/Plugin/Email.php | 132 ++++++++++++++++++++++++++++++++++- 2 files changed, 202 insertions(+), 3 deletions(-) diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index 6cff58f9..a0b88312 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -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 ""; + } + } + } } \ No newline at end of file diff --git a/Tests/PHPCI/Plugin/Email.php b/Tests/PHPCI/Plugin/Email.php index f1e97ffe..9e8052be 100644 --- a/Tests/PHPCI/Plugin/Email.php +++ b/Tests/PHPCI/Plugin/Email.php @@ -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() + ); + } } \ No newline at end of file From 7761d9fc085293863e7c310cc2cc9d214524ea96 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:28:42 +0100 Subject: [PATCH 04/13] Adding Builder::getSystemConfig() to provide easy access to config.yml. --- PHPCI/Builder.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index a7b60bc1..1f98cf44 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -116,6 +116,16 @@ class Builder return isset($this->config[$key]) ? $this->config[$key] : null; } + /** + * Access a variable from the config.yml + * @param $key + * @return mixed + */ + public function getSystemConfig($key) + { + return \b8\Registry::getInstance()->get($key); + } + /** * Access the build. * @param Build From 76e5c66b3837c388e757c73daab9cbfa458e67c2 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:29:59 +0100 Subject: [PATCH 05/13] Fixing the email plugin so that it retrieves the system config correctly. --- PHPCI/Plugin/Email.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index a0b88312..09dd4ebd 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -43,9 +43,10 @@ class Email implements \PHPCI\Plugin array $options = array(), \Swift_Mailer $mailer = null) { + $phpCiSettings = $phpci->getSystemConfig('phpci'); $this->phpci = $phpci; $this->options = $options; - $this->emailConfig = $phpci->getConfig('email_settings'); + $this->emailConfig = isset($phpCiSettings['email_settings']) ? $phpCiSettings['email_settings'] : array(); // Either a mailer will have been passed in or we load from the // config. @@ -99,12 +100,16 @@ class Email implements \PHPCI\Plugin protected function getMailConfig($configName) { - if (isset($this->emailConfig[$configName])) { + if (isset($this->emailConfig[$configName]) + && $this->emailConfig[$configName] != "") + { return $this->emailConfig[$configName]; } // Check defaults else { switch($configName) { + case 'smtp_address': + return "localhost"; case 'smtp_port': return '25'; case 'from_address': From 196a1b82b5b7fbb3cca49a417361fe18a540e7cb Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:30:34 +0100 Subject: [PATCH 06/13] Adding email settings to the install command. --- PHPCI/Command/InstallCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index 35459aec..8e06d7ed 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -48,6 +48,12 @@ class InstallCommand extends Command $conf['phpci']['github']['id'] = $this->ask('(Optional) Github Application ID: ', true); $conf['phpci']['github']['secret'] = $this->ask('(Optional) Github Application Secret: ', true); + $conf['phpci']['email_settings']['smtp_address'] = $this->ask('(Optional) Smtp server address: ', true); + $conf['phpci']['email_settings']['smtp_port'] = $this->ask('(Optional) Smtp port: ', true); + $conf['phpci']['email_settings']['smtp_username'] = $this->ask('(Optional) Smtp Username: ', true); + $conf['phpci']['email_settings']['smtp_password'] = $this->ask('(Optional) Smtp Password: ', true); + $conf['phpci']['email_settings']['from_address'] = $this->ask('(Optional) Email address to send from: ', true); + $dbUser = $conf['b8']['database']['username']; $dbPass = $conf['b8']['database']['password']; $dbHost = $conf['b8']['database']['servers']['write']; From d2c408f5897fd90b793e234cfbcc1f29731ca7b3 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:31:15 +0100 Subject: [PATCH 07/13] Updating the email plugin tests so that getSystemConfig is mocked correctly. --- Tests/PHPCI/Plugin/Email.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Tests/PHPCI/Plugin/Email.php b/Tests/PHPCI/Plugin/Email.php index 9e8052be..16588671 100644 --- a/Tests/PHPCI/Plugin/Email.php +++ b/Tests/PHPCI/Plugin/Email.php @@ -38,17 +38,19 @@ class EmailTest extends \PHPUnit_Framework_TestCase { $this->mockCiBuilder = $this->getMock( '\PHPCI\Builder', - array('getConfig'), + array('getSystemConfig'), array(), "mockBuilder", false ); $this->mockCiBuilder->buildPath = "/"; $this->mockCiBuilder->expects($this->any()) - ->method('getConfig') - ->with('email_settings') + ->method('getSystemConfig') + ->with('phpci') ->will($this->returnValue(array( - 'from_address' => "test-from-address@example.com" + 'email_settings' => array( + 'from_address' => "test-from-address@example.com" + ) ))); $this->mockMailer = $this->getMock( From a4c051e024ed2f2c3838017b9c932b549deffd54 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:56:09 +0100 Subject: [PATCH 08/13] Adding Getter for builder's success property so that plugins can find out if the the build has succeeded or not. --- PHPCI/Builder.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 1f98cf44..b1f7c489 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -135,6 +135,15 @@ class Builder return $this->build; } + /** + * Indicates if the build has passed or failed. + * @return bool + */ + public function getSuccessStatus() + { + return $this->success; + } + /** * Run the active build. */ From f8407e39c856d7b5aa91b38e67392a650584de6c Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 1 Jun 2013 13:57:39 +0100 Subject: [PATCH 09/13] Email plugin now looks in the yaml file for an addresses setting. All these addresses will be mailed with a pass or fail message. --- PHPCI/Plugin/Email.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index 09dd4ebd..eec4cda8 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -63,8 +63,34 @@ class Email implements \PHPCI\Plugin */ public function execute() { + // Without some email addresses in the yml file then we + // can't do anything. + if (!isset($this->options['addresses'])) { + return false; + } - return true; + $addresses = $this->options['addresses']; + $sendFailures = array(); + + if($this->phpci->getSuccessStatus()) { + $body = ""; + $sendFailures = $this->sendSeparateEmails( + $addresses, + "PASSED", + $body + ); + } + else { + $body = ""; + $sendFailures = $this->sendSeparateEmails( + $addresses, + "FAILED", + $body + ); + } + + // This is a success if we've not failed to send anything. + return (count($sendFailures) == 0); } /** @@ -85,6 +111,18 @@ class Email implements \PHPCI\Plugin return $failedAddresses; } + public function sendSeparateEmails(array $toAddresses, $subject, $body) + { + $failures = array(); + foreach($toAddresses as $address) { + $newFailures = $this->sendEmail($address, $subject, $body); + foreach($newFailures as $failure) { + $failures[] = $failure; + } + } + return $failures; + } + protected function loadSwiftMailerFromConfig() { /** @var \Swift_SmtpTransport $transport */ From eaac43e0e878f93052b9ed9cf3398662ebc82b95 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Tue, 4 Jun 2013 20:13:15 +0100 Subject: [PATCH 10/13] Updating unit test to reflect that the test will fail without any addresses defined. --- Tests/PHPCI/Plugin/Email.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/PHPCI/Plugin/Email.php b/Tests/PHPCI/Plugin/Email.php index 16588671..fdcb0a05 100644 --- a/Tests/PHPCI/Plugin/Email.php +++ b/Tests/PHPCI/Plugin/Email.php @@ -76,10 +76,11 @@ class EmailTest extends \PHPUnit_Framework_TestCase /** * @covers PHPUnit::execute */ - public function testExecute_ReturnsTrueWithoutArgs() + public function testExecute_ReturnsFalseWithoutArgs() { $returnValue = $this->testedEmailPlugin->execute(); - $expectedReturn = true; + // As no addresses will have been mailed as non are configured. + $expectedReturn = false; $this->assertEquals($expectedReturn, $returnValue); } From 33b840a82d6bd318de505aec1278cfbe43be7298 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Tue, 4 Jun 2013 21:09:16 +0100 Subject: [PATCH 11/13] Adding a default address that is always mailed to by notifications. --- PHPCI/Command/InstallCommand.php | 1 + PHPCI/Plugin/Email.php | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index 8e06d7ed..ad9cbeb7 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -53,6 +53,7 @@ class InstallCommand extends Command $conf['phpci']['email_settings']['smtp_username'] = $this->ask('(Optional) Smtp Username: ', true); $conf['phpci']['email_settings']['smtp_password'] = $this->ask('(Optional) Smtp Password: ', true); $conf['phpci']['email_settings']['from_address'] = $this->ask('(Optional) Email address to send from: ', true); + $conf['phpci']['email_settings']['default_mailto_address'] = $this->ask('(Optional) Default address to email notifications to: ', true); $dbUser = $conf['b8']['database']['username']; $dbPass = $conf['b8']['database']['password']; diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index eec4cda8..d4758ddc 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -63,13 +63,14 @@ class Email implements \PHPCI\Plugin */ public function execute() { + $addresses = $this->getEmailAddresses(); + // Without some email addresses in the yml file then we // can't do anything. - if (!isset($this->options['addresses'])) { + if (count($addresses) == 0) { return false; } - $addresses = $this->options['addresses']; $sendFailures = array(); if($this->phpci->getSuccessStatus()) { @@ -90,6 +91,14 @@ class Email implements \PHPCI\Plugin } // This is a success if we've not failed to send anything. + $this->phpci->log(sprintf( + "%d emails sent", + (count($addresses) - count($sendFailures))) + ); + $this->phpci->log(sprintf( + "%d emails failed to send", + count($sendFailures)) + ); return (count($sendFailures) == 0); } @@ -148,6 +157,8 @@ class Email implements \PHPCI\Plugin switch($configName) { case 'smtp_address': return "localhost"; + case 'default_mailto_address': + return null; case 'smtp_port': return '25'; case 'from_address': @@ -157,4 +168,21 @@ class Email implements \PHPCI\Plugin } } } + + protected function getEmailAddresses() + { + $addresses = array(); + + if (isset($this->options['addresses'])) { + foreach ($this->options['addresses'] as $address) { + $addresses[] = $address; + } + } + + if (isset($this->options['default_mailto_address'])) { + $addresses[] = $this->options['default_mailto_address']; + return $addresses; + } + return $addresses; + } } \ No newline at end of file From 84370038aab354c86f8f25b089c779c5d204533a Mon Sep 17 00:00:00 2001 From: meadsteve Date: Tue, 4 Jun 2013 21:47:45 +0100 Subject: [PATCH 12/13] Added basic body and title for the email notifications. --- PHPCI/Builder.php | 7 ++++ PHPCI/Plugin/Email.php | 17 ++++++---- Tests/PHPCI/Plugin/Email.php | 65 ++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index b1f7c489..f943a092 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -135,6 +135,13 @@ class Builder return $this->build; } + /** + * @return string The title of the project being built. + */ + public function getBuildProjectTitle() { + return $this->getBuild()->getProject()->getTitle(); + } + /** * Indicates if the build has passed or failed. * @return bool diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index d4758ddc..ae7ced05 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -73,20 +73,22 @@ class Email implements \PHPCI\Plugin $sendFailures = array(); + $subjectTemplate = "PHPCI - %s - %s"; + $projectName = $this->phpci->getBuildProjectTitle(); + $logText = $this->phpci->getBuild()->getLog(); + if($this->phpci->getSuccessStatus()) { - $body = ""; $sendFailures = $this->sendSeparateEmails( $addresses, - "PASSED", - $body + sprintf($subjectTemplate, $projectName, "Passing Build"), + sprintf("Log Output:
%s
", $logText) ); } else { - $body = ""; $sendFailures = $this->sendSeparateEmails( $addresses, - "FAILED", - $body + sprintf($subjectTemplate, $projectName, "Failing Build"), + sprintf("Log Output:
%s
", $logText) ); } @@ -113,7 +115,8 @@ class Email implements \PHPCI\Plugin $message = \Swift_Message::newInstance($subject) ->setFrom($this->getMailConfig('from_address')) ->setTo($toAddresses) - ->setBody($body); + ->setBody($body) + ->setContentType("text/html"); $failedAddresses = array(); $this->mailer->send($message, $failedAddresses); diff --git a/Tests/PHPCI/Plugin/Email.php b/Tests/PHPCI/Plugin/Email.php index fdcb0a05..8000da7e 100644 --- a/Tests/PHPCI/Plugin/Email.php +++ b/Tests/PHPCI/Plugin/Email.php @@ -34,16 +34,38 @@ class EmailTest extends \PHPUnit_Framework_TestCase */ protected $mockMailer; + /** + * @var \PHPUnit_Framework_MockObject_MockObject $mockMailer + */ + protected $mockBuild; + public function setUp() { + $this->mockBuild = $this->getMock( + '\PHPCI\Model\Build', + array('getLog'), + array(), + "mockBuild", + false + ); + + $this->mockBuild->expects($this->any()) + ->method('getLog') + ->will($this->returnValue("Build Log")); + $this->mockCiBuilder = $this->getMock( '\PHPCI\Builder', - array('getSystemConfig'), + array('getSystemConfig', + 'getBuildProjectTitle', + 'getBuild', + 'log'), array(), "mockBuilder", false ); - $this->mockCiBuilder->buildPath = "/"; + + $this->mockCiBuilder->buildPath = "/"; + $this->mockCiBuilder->expects($this->any()) ->method('getSystemConfig') ->with('phpci') @@ -52,6 +74,12 @@ class EmailTest extends \PHPUnit_Framework_TestCase 'from_address' => "test-from-address@example.com" ) ))); + $this->mockCiBuilder->expects($this->any()) + ->method('getBuildProjectTitle') + ->will($this->returnValue('Test-Project')); + $this->mockCiBuilder->expects($this->any()) + ->method('getBuild') + ->will($this->returnValue($this->mockBuild)); $this->mockMailer = $this->getMock( '\Swift_Mailer', @@ -85,6 +113,35 @@ class EmailTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedReturn, $returnValue); } + /** + * @covers PHPUnit::execute + */ + public function testExecute_BuildsBasicEmails() + { + $this->loadEmailPluginWithOptions(array( + 'addresses' => array('test-receiver@example.com') + )); + + /** @var \Swift_Message $actualMail */ + $actualMail = null; + $this->catchMailPassedToSend($actualMail); + + $returnValue = $this->testedEmailPlugin->execute(); + $expectedReturn = true; + + $this->assertSystemMail( + 'test-receiver@example.com', + 'test-from-address@example.com', + "Log Output:
Build Log
", + "PHPCI - Test-Project - Passing Build", + $actualMail + ); + + $this->assertEquals($expectedReturn, $returnValue); + + + } + /** * @covers PHPUnit::sendEmail */ @@ -172,6 +229,10 @@ class EmailTest extends \PHPUnit_Framework_TestCase $expectedSubject, $actualMail) { + if (! ($actualMail instanceof \Swift_Message)) { + $type = is_object($actualMail) ? get_class($actualMail) : gettype($actualMail); + throw new \Exception("Expected Swift_Message got " . $type); + } $this->assertEquals( array($expectedFromAddress => null), $actualMail->getFrom() From ab81c75e95ba86f1ecd43501c1c66a4aaee6072f Mon Sep 17 00:00:00 2001 From: Steve B Date: Wed, 5 Jun 2013 09:53:32 +0200 Subject: [PATCH 13/13] Update to composer.json to correct indentation. swiftmailer was added automatically by PHPStorm and the indentation didn't match the rest of the file. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d034e3c5..a4424830 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,6 @@ "symfony/yaml" : "2.2.x-dev", "symfony/console" : "2.2.*", "fabpot/php-cs-fixer" : "0.3.*@dev", - "swiftmailer/swiftmailer": "v5.0.0" + "swiftmailer/swiftmailer" : "v5.0.0" } }