php-censor/tests/PHPCI/Plugin/EmailTest.php

385 lines
10 KiB
PHP
Raw Normal View History

2013-05-31 13:56:35 +02:00
<?php
2013-05-31 13:56:35 +02:00
/**
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 https://www.phptesting.org/
2013-11-25 22:19:48 +01:00
*/
2013-05-31 13:56:35 +02:00
namespace Tests\PHPCI\Plugin;
2013-11-25 22:19:48 +01:00
2013-05-31 13:56:35 +02:00
use PHPCI\Plugin\Email as EmailPlugin;
use PHPCI\Model\Build;
2013-05-31 13:56:35 +02:00
/**
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;
/**
* @var int buildStatus
*/
public $buildStatus;
/**
* @var array $message;
*/
public $message;
/**
* @var bool $mailDelivered
*/
public $mailDelivered;
2013-11-25 22:19:48 +01:00
public function setUp()
{
2016-04-21 06:58:09 +02:00
$this->message = [];
$this->mailDelivered = true;
2016-04-21 06:58:09 +02:00
$self = $this;
$this->mockProject = $this->getMock(
'\PHPCI\Model\Project',
2016-04-21 06:58:09 +02:00
['getTitle'],
[],
"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',
2016-04-21 06:58:09 +02:00
['getLog', 'getStatus', 'getProject', 'getCommitterEmail'],
[],
2013-11-25 22:19:48 +01:00
"mockBuild",
false
);
$this->mockBuild->expects($this->any())
->method('getLog')
->will($this->returnValue("Build Log"));
$this->mockBuild->expects($this->any())
->method('getStatus')
->will($this->returnCallback(function () use ($self) {
return $self->buildStatus;
}));
$this->mockBuild->expects($this->any())
->method('getProject')
->will($this->returnValue($this->mockProject));
$this->mockBuild->expects($this->any())
->method('getCommitterEmail')
->will($this->returnValue('committer-email@example.com'));
2013-11-25 22:19:48 +01:00
$this->mockCiBuilder = $this->getMock(
'\PHPCI\Builder',
2016-04-21 06:58:09 +02:00
[
2013-11-25 22:19:48 +01:00
'getSystemConfig',
'getBuild',
'log'
2016-04-21 06:58:09 +02:00
],
[],
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')
2016-04-21 09:46:38 +02:00
->will($this->returnValue(['email_settings' => ['from_address' => "test-from-address@example.com"]]));
}
2016-04-21 06:58:09 +02:00
protected function loadEmailPluginWithOptions($arrOptions = [], $buildStatus = null, $mailDelivered = true)
{
$this->mailDelivered = $mailDelivered;
if (is_null($buildStatus)) {
$this->buildStatus = Build::STATUS_SUCCESS;
} else {
$this->buildStatus = $buildStatus;
}
// Reset current message.
2016-04-21 06:58:09 +02:00
$this->message = [];
$self = $this;
$this->testedEmailPlugin = $this->getMock(
'\PHPCI\Plugin\Email',
2016-04-21 09:46:38 +02:00
['sendEmail'],
[
$this->mockCiBuilder,
$this->mockBuild,
$arrOptions
2016-04-21 09:46:38 +02:00
]
2013-11-25 22:19:48 +01:00
);
$this->testedEmailPlugin->expects($this->any())
->method('sendEmail')
->will($this->returnCallback(function ($to, $cc, $subject, $body) use ($self) {
$self->message['to'][] = $to;
$self->message['cc'] = $cc;
$self->message['subject'] = $subject;
$self->message['body'] = $body;
2013-11-25 22:19:48 +01:00
return $self->mailDelivered;
2014-11-12 13:37:52 +01:00
}));
2013-11-25 22:19:48 +01:00
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testReturnsFalseWithoutArgs()
2013-11-25 22:19:48 +01:00
{
$this->loadEmailPluginWithOptions();
2013-11-25 22:19:48 +01:00
$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
*/
2014-11-12 13:37:52 +01:00
public function testBuildsBasicEmails()
2013-11-25 22:19:48 +01:00
{
2016-04-21 09:46:38 +02:00
$this->loadEmailPluginWithOptions(['addresses' => ['test-receiver@example.com']], Build::STATUS_SUCCESS);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('test-receiver@example.com', $this->message['to']);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testBuildsDefaultEmails()
{
2016-04-21 09:46:38 +02:00
$this->loadEmailPluginWithOptions(['default_mailto_address' => 'default-mailto-address@example.com'], Build::STATUS_SUCCESS);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('default-mailto-address@example.com', $this->message['to']);
2013-11-25 22:19:48 +01:00
}
/**
* @covers PHPUnit::execute
*/
public function testExecute_UniqueRecipientsFromWithCommitter()
{
2016-04-21 09:46:38 +02:00
$this->loadEmailPluginWithOptions(['addresses' => ['test-receiver@example.com', 'test-receiver2@example.com']]);
$returnValue = $this->testedEmailPlugin->execute();
$this->assertTrue($returnValue);
$this->assertCount(2, $this->message['to']);
$this->assertContains('test-receiver@example.com', $this->message['to']);
$this->assertContains('test-receiver2@example.com', $this->message['to']);
}
/**
* @covers PHPUnit::execute
*/
public function testExecute_UniqueRecipientsWithCommitter()
{
2016-04-21 09:46:38 +02:00
$this->loadEmailPluginWithOptions([
'committer' => true,
'addresses' => ['test-receiver@example.com', 'committer@test.com']
]);
$returnValue = $this->testedEmailPlugin->execute();
$this->assertTrue($returnValue);
$this->assertContains('test-receiver@example.com', $this->message['to']);
$this->assertContains('committer@test.com', $this->message['to']);
}
/**
* @covers PHPUnit::execute
2013-11-25 22:19:48 +01:00
*/
2014-11-12 13:37:52 +01:00
public function testCcDefaultEmails()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'default_mailto_address' => 'default-mailto-address@example.com',
2016-04-21 09:46:38 +02:00
'cc' => [
'cc-email-1@example.com',
'cc-email-2@example.com',
'cc-email-3@example.com',
2016-04-21 09:46:38 +02:00
],
],
Build::STATUS_SUCCESS
);
$this->testedEmailPlugin->execute();
$this->assertEquals(
2016-04-21 09:46:38 +02:00
[
'cc-email-1@example.com',
'cc-email-2@example.com',
'cc-email-3@example.com',
2016-04-21 09:46:38 +02:00
],
$this->message['cc']
);
}
/**
* @covers PHPUnit::execute
2013-11-25 22:19:48 +01:00
*/
2014-11-12 13:37:52 +01:00
public function testBuildsCommitterEmails()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'committer' => true
2016-04-21 09:46:38 +02:00
],
Build::STATUS_SUCCESS
);
$this->testedEmailPlugin->execute();
$this->assertContains('committer-email@example.com', $this->message['to']);
}
/**
* @covers PHPUnit::execute
2013-11-25 22:19:48 +01:00
*/
2014-11-12 13:37:52 +01:00
public function testMailSuccessfulBuildHaveProjectName()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_SUCCESS
);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('Test-Project', $this->message['subject']);
$this->assertContains('Test-Project', $this->message['body']);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testMailFailingBuildHaveProjectName()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_FAILED
);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('Test-Project', $this->message['subject']);
$this->assertContains('Test-Project', $this->message['body']);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testMailSuccessfulBuildHaveStatus()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_SUCCESS
);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('Passing', $this->message['subject']);
$this->assertContains('successful', $this->message['body']);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testMailFailingBuildHaveStatus()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_FAILED
);
2014-11-12 13:51:01 +01:00
$this->testedEmailPlugin->execute();
$this->assertContains('Failing', $this->message['subject']);
$this->assertContains('failed', $this->message['body']);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testMailDeliverySuccess()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_FAILED,
true
);
$returnValue = $this->testedEmailPlugin->execute();
$this->assertEquals(true, $returnValue);
}
/**
* @covers PHPUnit::execute
*/
2014-11-12 13:37:52 +01:00
public function testMailDeliveryFail()
{
$this->loadEmailPluginWithOptions(
2016-04-21 09:46:38 +02:00
[
'addresses' => ['test-receiver@example.com']
],
Build::STATUS_FAILED,
false
);
$returnValue = $this->testedEmailPlugin->execute();
$this->assertEquals(false, $returnValue);
}
}