Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Improvement plus new test
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmany committed Apr 2, 2019
1 parent 15cd611 commit 356ddf3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
21 changes: 18 additions & 3 deletions Randomizer/SmtpRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class SmtpRandomizer
/** @var array */
private $smtps;

/** @var array */
private $smtp;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -56,9 +59,7 @@ public function __construct(IntegrationHelper $integrationHelper)
*/
public function randomize(RandomSmtpTransport $randomSmtpTransport, \Swift_Mime_Message &$message = null)
{
$smtps = $this->smtps;
shuffle($smtps);
$smtp = end($smtps);
$smtp = $this->getRandomSmtp();
if (!$host = ArrayHelper::getValue($this->getConfigParamter('host'), $smtp)) {
throw new HostNotExistinCsvRowExpection('Can\'t find host on column possition '.sprintf('"%s"', $this->getConfigParamter('host')));
}
Expand All @@ -76,6 +77,20 @@ public function randomize(RandomSmtpTransport $randomSmtpTransport, \Swift_Mime_
}
}

/**
* @return array
*/
private function getRandomSmtp()
{
if (!$this->smtp) {
$smtps = $this->smtps;
shuffle($smtps);
$this->smtp = end($smtps);
}

return $this->smtp;
}

/**
* @param $key
*
Expand Down
2 changes: 1 addition & 1 deletion Swiftmailer/Transport/RandomSmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function send(\Swift_Mime_Message $message, &$failedRecipients = null)
private function setRandomSmtpServer(\Swift_Mime_Message &$message = null)
{
try {
$this->logger->info(sprintf('Send by random SMTP server: %s with username %s and sender email %s', $this->getHost(), $this->getUsername(), implode(',', $message ? array_keys($message->getFrom()) : [])));
$this->smtpRandomizer->randomize($this, $message);
$this->logger->info(sprintf('Send by random SMTP server: %s with username %s and sender email %s', $this->getHost(), $this->getUsername(), implode(',', $message ? array_keys($message->getFrom()) : [])));
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
}
Expand Down
48 changes: 47 additions & 1 deletion Tests/Randomizer/SmtpRandomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SmtpRandomizerTest extends \PHPUnit_Framework_TestCase
*/
private function generateCsvFromArray()
{
return 'host,username,password,port'."\r\n".'host2,username2,password2,port2'."\r\n".'host3,username3,password3,port3';
return 'host,username,password,port,auth_mode,encryption,fromEmail,fromName'."\r\n".'host2,username,password,port,auth_mode,encryption,fromEmail,fromName'."\r\n".'host3,username,password,port,auth_mode,encryption,fromEmail,fromName';
}

/**
Expand All @@ -44,6 +44,8 @@ private function getConfig()
'port' => 3,
'auth_mode' => 4,
'encryption' => 5,
'fromEmail' => 6,
'fromName' => 7,
];
}

Expand Down Expand Up @@ -170,4 +172,48 @@ function ($host) use (&$results) {
}
$this->assertGreaterThan(1, $uniqueResultsCount);
}


public function testAllColumnsFind()
{
$integrationMock = $this->createMock(Integration::class);
$integrationMock->method('getIsPublished')->willReturn(true);

$smtpRandomizerIntegration = $this->createMock(RandomSmtpIntegration::class);
$smtpRandomizerIntegration->method('getIntegrationSettings')->willReturn($integrationMock);


$smtpRandomizerIntegration->method('mergeConfigToFeatureSettings')->willReturn(
array_merge(['smtps'=> $this->generateCsvFromArray()], $this->getConfig())
);

$integrationHelperMock = $this->createMock(IntegrationHelper::class);
$integrationHelperMock->method('getIntegrationObject')->willReturn($smtpRandomizerIntegration);
$randomSmtpTransportMock = $this->createMock(RandomSmtpTransport::class);

$randomSmtpTransportMock->expects($this->once())->method('setHost')->willReturnCallback(
function ($host) {
$this->assertTrue(isset($host));
});

$randomSmtpTransportMock->expects($this->once())->method('setPort')->willReturnCallback(
function ($return) {
$this->assertTrue(isset($return));
});


$randomSmtpTransportMock->expects($this->once())->method('setEncryption')->willReturnCallback(
function ($return) {
$this->assertTrue(isset($return));
});

$messageMock = $this->createMock(\Swift_Mime_Message::class);
$messageMock->expects($this->once())->method('setFrom')->willReturnCallback(
function ($return) {
$this->assertTrue(isset($return));
});

(new SmtpRandomizer($integrationHelperMock))->randomize($randomSmtpTransportMock, $messageMock);

}
}

0 comments on commit 356ddf3

Please sign in to comment.