Skip to content

Commit

Permalink
Catch any protocol exception while processing queue, fixes #22 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored Aug 31, 2017
1 parent 66e63f3 commit 6617885
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Queue/QueueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Genkgo\Mail\Queue;

use Genkgo\Mail\Exception\AbstractProtocolException;
use Genkgo\Mail\Exception\ConnectionRefusedException;
use Genkgo\Mail\Exception\EmptyQueueException;
use Genkgo\Mail\TransportInterface;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function process()
while ($message = $queue->fetch()) {
try {
$this->transport->send($message);
} catch (ConnectionRefusedException $e) {
} catch (AbstractProtocolException $e) {
$queue->store($message);

// do not continue transporting messages
Expand Down
29 changes: 29 additions & 0 deletions test/Unit/Queue/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Genkgo\TestMail\Unit\Queue;

use Genkgo\Mail\Exception\AbstractProtocolException;
use Genkgo\TestMail\AbstractTestCase;;
use Genkgo\Mail\Exception\ConnectionRefusedException;
use Genkgo\Mail\GenericMessage;
Expand Down Expand Up @@ -81,6 +82,34 @@ public function it_will_readd_failed_messages_to_queue() {
$this->assertCount(3, $storage);
}

/**
* @test
*/
public function it_will_readd_messages_resulting_in_any_protocol_exception() {
$storage = new \ArrayObject();

$queue = new ArrayObjectQueue($storage);
$queue->store($this->newMessage('Test 1'));
$queue->store($this->newMessage('Test 2'));
$queue->store($this->newMessage('Test 3'));

$transport = $this->createMock(TransportInterface::class);
$transport
->expects($this->once())
->method('send')
->with($this->callback(function (GenericMessage $message) {
$this->assertEquals('Test 1', $message->getHeader('subject')[0]->getValue());
return true;
}))
->willThrowException(new class extends AbstractProtocolException{})
;

$processor = new QueueProcessor($transport, [$queue]);
$processor->process();

$this->assertCount(3, $storage);
}

/**
* @param string $subject
* @return MessageInterface
Expand Down

0 comments on commit 6617885

Please sign in to comment.