diff --git a/Randomizer/SmtpRandomizer.php b/Randomizer/SmtpRandomizer.php index 9f5a6b6..849af9b 100644 --- a/Randomizer/SmtpRandomizer.php +++ b/Randomizer/SmtpRandomizer.php @@ -26,6 +26,9 @@ class SmtpRandomizer /** @var array */ private $smtps; + /** @var array */ + private $smtp; + /** * {@inheritdoc} */ @@ -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'))); } @@ -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 * diff --git a/Swiftmailer/Transport/RandomSmtpTransport.php b/Swiftmailer/Transport/RandomSmtpTransport.php index 89cd351..52968a9 100644 --- a/Swiftmailer/Transport/RandomSmtpTransport.php +++ b/Swiftmailer/Transport/RandomSmtpTransport.php @@ -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()); } diff --git a/Tests/Randomizer/SmtpRandomizerTest.php b/Tests/Randomizer/SmtpRandomizerTest.php index 477f60e..1e07add 100644 --- a/Tests/Randomizer/SmtpRandomizerTest.php +++ b/Tests/Randomizer/SmtpRandomizerTest.php @@ -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'; } /** @@ -44,6 +44,8 @@ private function getConfig() 'port' => 3, 'auth_mode' => 4, 'encryption' => 5, + 'fromEmail' => 6, + 'fromName' => 7, ]; } @@ -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); + + } }