Skip to content

Commit

Permalink
Better event and return value (#30)
Browse files Browse the repository at this point in the history
* Make sure we use the proper event status

* Dont fix return value and make sure failed recipients get added to the event

* We should assertEmpty instead of assertNull
  • Loading branch information
Nyholm committed Jul 12, 2016
1 parent 4e2c690 commit 11fed5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ This document will tell you what changes that has been made between versions.
* Upgraded from Mailgun client 1.x to 2.x.
* Start using PSR-4
* Improved tests and documentation
* Changed namespace of `cspoo\Swiftmailer\MailgunBundle\Services\MailgunTransport` to `cspoo\Swiftmailer\MailgunBundle\Service\MailgunTransport`
* Changed namespace of `cspoo\Swiftmailer\MailgunBundle\Services\MailgunTransport` to `cspoo\Swiftmailer\MailgunBundle\Service\MailgunTransport`
* Catching all exception that may be thrown by the MailGun API.
* The return value of MaingunTransport::sendMessage is no longer fixed to 1. It does now comply with the interface.
9 changes: 7 additions & 2 deletions Service/MailgunTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function stop()
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;

if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
Expand All @@ -103,16 +105,19 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)

$postData = $this->getPostData($message);
$domain = $this->getDomain($message);
$sent = 1;
$sent = count($postData['to']);
try {
$result = $this->mailgun->sendMessage($domain, $postData, $message->toString());
$resultStatus = $result->http_response_code == 200 ? Swift_Events_SendEvent::RESULT_SUCCESS : Swift_Events_SendEvent::RESULT_FAILED;
} catch (\Exception $e) {
$failedRecipients = $postData['to'];
$sent = 0;
$resultStatus = Swift_Events_SendEvent::RESULT_FAILED;
}

if ($evt) {
$evt->setResult($result->http_response_code == 200 ? Swift_Events_SendEvent::RESULT_SUCCESS : Swift_Events_SendEvent::RESULT_FAILED);
$evt->setResult($resultStatus);
$evt->setFailedRecipients($failedRecipients);
$this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}

Expand Down
19 changes: 15 additions & 4 deletions Tests/Service/MailgunTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public function testSendMessageOk()
$failed = null;
$sent = $transport->send($message, $failed);

$this->assertEquals(1, $sent);
$this->assertNull($failed);
$this->assertEquals(3, $sent);
$this->assertEmpty($failed);
}

public function testSendMessageWithException()
Expand Down Expand Up @@ -148,9 +148,20 @@ public function testSendMessageWithException()
private function getTransport()
{
$dispatcher = $this->getMock('Swift_Events_EventDispatcher');
$dispatcher->expects($this->any())
->method('createSendEvent')
->willReturn($this->getMockBuilder('Swift_Events_SendEvent')->disableOriginalConstructor()->getMock());



$mailgun = $this->getMock('Mailgun\Mailgun');
$transport = new MailgunTransport($dispatcher, $mailgun, 'default.com');
$result = new \stdClass();
$result->http_response_code = 200;

$mailgun->expects($this->any())
->method('sendMessage')
->willReturn($result);

return $transport;
return new MailgunTransport($dispatcher, $mailgun, 'default.com');
}
}

0 comments on commit 11fed5b

Please sign in to comment.