From 353c4cafdb3a4f49914821cac2082ebd7f488540 Mon Sep 17 00:00:00 2001 From: Adirelle Date: Thu, 8 Jan 2015 16:48:28 +0100 Subject: [PATCH] Remove duplicates from the list of recipients in the email plugin. Closes #731 --- PHPCI/Plugin/Email.php | 2 +- Tests/PHPCI/Plugin/EmailTest.php | 57 ++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index 98b226e3..7c5171d6 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -182,7 +182,7 @@ class Email implements \PHPCI\Plugin $addresses[] = $this->options['default_mailto_address']; return $addresses; } - return $addresses; + return array_unique($addresses); } /** diff --git a/Tests/PHPCI/Plugin/EmailTest.php b/Tests/PHPCI/Plugin/EmailTest.php index 87225772..0fa2d8f0 100644 --- a/Tests/PHPCI/Plugin/EmailTest.php +++ b/Tests/PHPCI/Plugin/EmailTest.php @@ -57,6 +57,10 @@ class EmailTest extends \PHPUnit_Framework_TestCase ->method('getStatus') ->will($this->returnValue(\PHPCI\Model\Build::STATUS_SUCCESS)); + $this->mockBuild->expects($this->any()) + ->method('getCommitterEmail') + ->will($this->returnValue("committer@test.com")); + $this->mockCiBuilder = $this->getMock( '\PHPCI\Builder', array( @@ -151,8 +155,53 @@ class EmailTest extends \PHPUnit_Framework_TestCase ); $this->assertEquals($expectedReturn, $returnValue); + } + /** + * @covers PHPUnit::execute + */ + public function testExecute_UniqueRecipientsFromWithCommitter() + { + $this->loadEmailPluginWithOptions( + array( + 'addresses' => array('test-receiver@example.com', 'test-receiver2@example.com') + ) + ); + $actualMails = []; + $this->catchMailPassedToSend($actualMails); + + $returnValue = $this->testedEmailPlugin->execute(); + $this->assertTrue($returnValue); + + $this->assertCount(2, $actualMails); + + $actualTos = array(key($actualMails[0]->getTo()), key($actualMails[1]->getTo())); + $this->assertContains('test-receiver@example.com', $actualTos); + $this->assertContains('test-receiver2@example.com', $actualTos); + } + + /** + * @covers PHPUnit::execute + */ + public function testExecute_UniqueRecipientsWithCommiter() + { + $this->loadEmailPluginWithOptions( + array( + 'commiter' => true, + 'addresses' => array('test-receiver@example.com', 'committer@test.com') + ) + ); + + $actualMails = []; + $this->catchMailPassedToSend($actualMails); + + $returnValue = $this->testedEmailPlugin->execute(); + $this->assertTrue($returnValue); + + $actualTos = array(key($actualMails[0]->getTo()), key($actualMails[1]->getTo())); + $this->assertContains('test-receiver@example.com', $actualTos); + $this->assertContains('committer@test.com', $actualTos); } /** @@ -215,12 +264,16 @@ class EmailTest extends \PHPUnit_Framework_TestCase */ protected function catchMailPassedToSend(&$actualMail) { - $this->mockMailer->expects($this->once()) + $this->mockMailer->expects(is_array($actualMail) ? $this->atLeast(1) : $this->once()) ->method('send') ->will( $this->returnCallback( function ($passedMail) use (&$actualMail) { - $actualMail = $passedMail; + if(is_array($actualMail)) { + $actualMail[] = $passedMail; + } else { + $actualMail = $passedMail; + } return array(); } )