This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2322 from zazabe/cm-mail-concurrency
Send mail via SMTP with concurrency support
- Loading branch information
Showing
3 changed files
with
126 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,27 @@ public function testSend() { | |
$this->assertSame([], $failedRecipients); | ||
} | ||
|
||
public function testSendWithHeaders() { | ||
$transport = $this->mockInterface('Swift_Transport')->newInstance(); | ||
$transport->mockMethod('isStarted')->set(true); | ||
$message = new CM_Mail_Message('foo', 'content'); | ||
$message->setTo('[email protected]'); | ||
$message->setCc('[email protected]', 'bar'); | ||
$client = new CM_Mail_Mailer($transport, ['X-foo' => 'bar']); | ||
|
||
$sendMethod = $transport->mockMethod('send')->set(function (CM_Mail_Message $message) { | ||
$this->assertSame(['X-foo' => ['bar']], $message->getCustomHeaders()); | ||
return 2; | ||
}); | ||
$failedRecipients = []; | ||
$this->assertSame([], $message->getCustomHeaders()); | ||
$numSent = $client->send($message, $failedRecipients); | ||
$this->assertSame([], $message->getCustomHeaders()); | ||
$this->assertSame(1, $sendMethod->getCallCount()); | ||
$this->assertSame(2, $numSent); | ||
$this->assertSame([], $failedRecipients); | ||
} | ||
|
||
public function testSendNoRecipient() { | ||
$transport = $this->mockInterface('Swift_Transport')->newInstance(); | ||
$transport->mockMethod('isStarted')->set(true); | ||
|
@@ -37,24 +58,63 @@ public function testSendNoRecipient() { | |
$this->assertSame(0, $sendMethod->getCallCount()); | ||
} | ||
|
||
public function testSendThrows() { | ||
$serviceManager = new CM_Service_Manager(); | ||
$logger = $this->mockObject('CM_Log_Logger'); | ||
$serviceManager->registerInstance('logger', $logger); | ||
$transport = $this->mockInterface('Swift_Transport')->newInstance(); | ||
$transport->mockMethod('isStarted')->set(true); | ||
$message = new Swift_Message('foo', 'content'); | ||
$message->setFrom('[email protected]', 'Foobar'); | ||
$message->setTo('[email protected]'); | ||
$message->setCc('[email protected]', 'bar'); | ||
|
||
$client = new CM_Mail_Mailer($transport); | ||
$client->setServiceManager($serviceManager); | ||
|
||
$sendMethod = $transport->mockMethod('send')->set(function () { | ||
throw new Exception('Failed'); | ||
}); | ||
$errorMethod = $logger->mockMethod('error')->set(function ($message, CM_Log_Context $context = null) { | ||
$exception = $context->getException(); | ||
$this->assertSame('Failed to send email', $message); | ||
$this->assertSame([ | ||
'message' => [ | ||
'subject' => 'foo', | ||
'from' => ['[email protected]' => 'Foobar'], | ||
'to' => ['[email protected]' => null], | ||
'cc' => ['[email protected]' => 'bar'], | ||
'bcc' => null, | ||
], | ||
'failedRecipients' => [], | ||
], $context->getExtra()); | ||
$this->assertInstanceOf('Exception', $exception); | ||
$this->assertSame('Failed', $exception->getMessage()); | ||
}); | ||
|
||
$client->send($message); | ||
/** @var CM_Exception_Invalid $exception */ | ||
$this->assertSame(1, $sendMethod->getCallCount()); | ||
$this->assertSame(1, $errorMethod->getCallCount()); | ||
} | ||
|
||
public function testSendFailed() { | ||
$serviceManager = new CM_Service_Manager(); | ||
$logger = $this->mockObject('CM_Log_Logger'); | ||
$serviceManager->registerInstance('logger', $logger); | ||
$transport = $this->mockInterface('Swift_Transport')->newInstance(); | ||
$transport->mockMethod('isStarted')->set(true); | ||
$message = new Swift_Message('foo', 'content'); | ||
$message->setSender('[email protected]', 'Foobar'); | ||
$message->setFrom('[email protected]', 'Foobar'); | ||
$message->setTo('[email protected]'); | ||
$message->setCc('[email protected]', 'bar'); | ||
|
||
$client = new CM_Mail_Mailer($transport); | ||
$client->setServiceManager($serviceManager); | ||
|
||
$sendMethod = $transport->mockMethod('send')->set(0); | ||
$warningMethod = $logger->mockMethod('warning'); | ||
$errorMethod = $logger->mockMethod('error')->set(function ($message, CM_Log_Context $context = null) { | ||
$this->assertSame('Failed to send email to all recipients', $message); | ||
$this->assertSame('Failed to send email', $message); | ||
$this->assertSame([ | ||
'message' => [ | ||
'subject' => 'foo', | ||
|
@@ -72,7 +132,6 @@ public function testSendFailed() { | |
|
||
/** @var CM_Exception_Invalid $exception */ | ||
$this->assertSame(1, $sendMethod->getCallCount()); | ||
$this->assertSame(0, $warningMethod->getCallCount()); | ||
$this->assertSame(1, $errorMethod->getCallCount()); | ||
} | ||
} |