phpci/Tests/PHPCI/Plugin/EmailTest.php

292 lines
8 KiB
PHP
Raw Normal View History

2013-05-31 13:56:35 +02:00
<?php
/**
2013-11-25 22:19:48 +01:00
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
2013-05-31 13:56:35 +02:00
namespace PHPCI\Plugin\Tests;
2013-11-25 22:19:48 +01:00
2013-05-31 13:56:35 +02:00
use PHPCI\Plugin\Email as EmailPlugin;
/**
2013-11-25 22:19:48 +01:00
* Unit test for the PHPUnit plugin.
* @author meadsteve
*/
class EmailTest extends \PHPUnit_Framework_TestCase
2013-05-31 13:56:35 +02:00
{
2013-11-25 22:19:48 +01:00
/**
* @var EmailPlugin $testedPhpUnit
*/
protected $testedEmailPlugin;
2013-05-31 13:56:35 +02:00
2013-11-25 22:19:48 +01:00
/**
* @var \PHPUnit_Framework_MockObject_MockObject $mockCiBuilder
*/
protected $mockCiBuilder;
2013-05-31 13:56:35 +02:00
/**
* @var \PHPUnit_Framework_MockObject_MockObject $mockBuild
*/
protected $mockBuild;
/**
* @var \PHPUnit_Framework_MockObject_MockObject $mockProject
*/
protected $mockProject;
2013-11-25 22:19:48 +01:00
public function setUp()
{
$this->mockProject = $this->getMock(
'\PHPCI\Model\Project',
array('getTitle'),
array(),
"mockProject",
false
);
$this->mockProject->expects($this->any())
->method('getTitle')
->will($this->returnValue("Test project"));
$this->mockBuild = $this->getMock(
2013-11-25 22:19:48 +01:00
'\PHPCI\Model\Build',
array('getLog', 'getStatus', 'getProject'),
2013-11-25 22:19:48 +01:00
array(),
"mockBuild",
false
);
$this->mockBuild->expects($this->any())
->method('getLog')
->will($this->returnValue("Build Log"));
$this->mockBuild->expects($this->any())
->method('getStatus')
->will($this->returnValue(\PHPCI\Model\Build::STATUS_SUCCESS));
$this->mockBuild->expects($this->any())
->method('getProject')
->will($this->returnValue($this->mockProject));
2013-11-25 22:19:48 +01:00
$this->mockCiBuilder = $this->getMock(
'\PHPCI\Builder',
array(
'getSystemConfig',
'getBuildProjectTitle',
'getBuild',
'log'
),
array(),
2013-11-25 22:30:10 +01:00
"mockBuilder_email",
2013-11-25 22:19:48 +01:00
false
);
$this->mockCiBuilder->buildPath = "/";
$this->mockCiBuilder->expects($this->any())
->method('getSystemConfig')
->with('phpci')
2013-11-25 22:19:48 +01:00
->will(
$this->returnValue(
array(
'email_settings' => array(
'from_address' => "test-from-address@example.com"
)
)
)
2013-11-25 22:19:48 +01:00
);
$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(
2013-11-25 22:19:48 +01:00
'\Swift_Mailer',
array('send'),
array(),
"mockMailer",
false
);
$this->loadEmailPluginWithOptions();
}
protected function loadEmailPluginWithOptions($arrOptions = array())
{
$this->testedEmailPlugin = new EmailPlugin(
2013-05-31 13:56:35 +02:00
$this->mockCiBuilder,
$this->mockBuild,
$arrOptions
2013-05-31 13:56:35 +02:00
);
2013-11-25 22:19:48 +01:00
}
/**
* @covers PHPUnit::execute
*/
public function testExecute_ReturnsFalseWithoutArgs()
{
$returnValue = $this->testedEmailPlugin->execute();
// As no addresses will have been mailed as non are configured.
2013-11-25 22:19:48 +01:00
$expectedReturn = false;
2013-05-31 13:56:35 +02:00
2013-11-25 22:19:48 +01:00
$this->assertEquals($expectedReturn, $returnValue);
}
2013-11-25 22:19:48 +01:00
/**
* @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);
2013-11-25 22:19:48 +01:00
$returnValue = $this->testedEmailPlugin->execute();
$expectedReturn = true;
$this->assertSystemMail(
'test-receiver@example.com',
'test-from-address@example.com',
"Log Output: <br><pre>Build Log</pre>",
"PHPCI - Test-Project - Passing Build",
$actualMail
);
2013-11-25 22:19:48 +01:00
$this->assertEquals($expectedReturn, $returnValue);
2013-11-25 22:19:48 +01:00
}
/**
2013-11-25 22:19:48 +01:00
* @covers PHPUnit::sendEmail
*/
public function testSendEmail_CallsMailerSend()
{
$this->mockMailer->expects($this->once())
->method('send');
$this->testedEmailPlugin->sendEmail("test@email.com", array(), "hello", "body");
}
/**
2013-11-25 22:19:48 +01:00
* @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, array(), $subject, $body);
}
/**
2013-11-25 22:19:48 +01:00
* @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, array(), $subject, $body);
$this->assertSystemMail(
$toAddress,
'test-from-address@example.com',
$body,
$subject,
$actualMail
);
}
/**
2013-11-25 22:19:48 +01:00
* @param \Swift_Message $actualMail passed by ref and populated with
* the message object the mock mailer
* receives.
*/
protected function catchMailPassedToSend(&$actualMail)
{
$this->mockMailer->expects(is_array($actualMail) ? $this->atLeast(1) : $this->once())
->method('send')
->will(
$this->returnCallback(
function ($passedMail) use (&$actualMail) {
if(is_array($actualMail)) {
$actualMail[] = $passedMail;
} else {
$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
*/
2013-11-25 22:19:48 +01:00
protected function assertSystemMail(
$expectedToAddress,
$expectedFromAddress,
$expectedBody,
$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()
);
$this->assertEquals(
array($expectedToAddress => null),
$actualMail->getTo()
);
$this->assertEquals(
$expectedBody,
$actualMail->getBody()
);
$this->assertEquals(
$expectedSubject,
$actualMail->getSubject()
);
}
}