Skip to content

Commit

Permalink
Merge pull request #226 from Ocramius/feature/throwable-support
Browse files Browse the repository at this point in the history
Feature: throwable support
  • Loading branch information
henrikbjorn committed Mar 8, 2016
2 parents a77852a + c190940 commit c5a6bbe
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 15 deletions.
39 changes: 29 additions & 10 deletions src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public function resume()
*
* @param Envelope $envelope
* @param Queue $queue
*
* @throws \Exception
* @throws \Throwable
*/
public function invoke(Envelope $envelope, Queue $queue)
{
Expand All @@ -134,16 +137,10 @@ public function invoke(Envelope $envelope, Queue $queue)
$queue->acknowledge($envelope);

$this->dispatcher->dispatch(BernardEvents::ACKNOWLEDGE, new EnvelopeEvent($envelope, $queue));
} catch (\Exception $e) {
// Make sure the exception is not interfering.
// Previously failing jobs handling have been moved to a middleware.
//
// Emit an event to let others log that exception
$this->dispatcher->dispatch(BernardEvents::REJECT, new RejectEnvelopeEvent($envelope, $queue, $e));

if ($this->options['stop-on-error']) {
throw $e;
}
} catch (\Throwable $error) {
$this->rejectDispatch($error, $envelope, $queue);
} catch (\Exception $exception) {
$this->rejectDispatch($exception, $envelope, $queue);

This comment has been minimized.

Copy link
@mnvx

mnvx Apr 3, 2017

Hello. As I see this line it is dead code.

This comment has been minimized.

Copy link
@bendavies

bendavies Apr 21, 2017

Contributor

Throwable was introduced in php 7

}
}

Expand Down Expand Up @@ -178,4 +175,26 @@ protected function bind()
pcntl_signal(SIGCONT, [$this, 'resume']);
}
}

/**
* @param \Throwable|\Exception $exception note that the type-hint is missing due to PHP 5.x compat
*
* @param Envelope $envelope
* @param Queue $queue
*
* @throws \Exception
* @throws \Throwable
*/
private function rejectDispatch($exception, Envelope $envelope, Queue $queue)
{
// Make sure the exception is not interfering.
// Previously failing jobs handling have been moved to a middleware.
//
// Emit an event to let others log that exception
$this->dispatcher->dispatch(BernardEvents::REJECT, new RejectEnvelopeEvent($envelope, $queue, $exception));

if ($this->options['stop-on-error']) {
throw $exception;
}
}
}
10 changes: 5 additions & 5 deletions src/Event/RejectEnvelopeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ class RejectEnvelopeEvent extends EnvelopeEvent
protected $exception;

/**
* @param Envelope $envelope
* @param Queue $queue
* @param \Exception $exception
* @param Envelope $envelope
* @param Queue $queue
* @param \Exception|\Throwable $exception
*/
public function __construct(Envelope $envelope, Queue $queue, \Exception $exception)
public function __construct(Envelope $envelope, Queue $queue, $exception)
{
parent::__construct($envelope, $queue);

$this->exception = $exception;
}

/**
* @return \Exception
* @return \Exception|\Throwable
*/
public function getException()
{
Expand Down
46 changes: 46 additions & 0 deletions tests/ConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@

class ConsumerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SimpleRouter
*/
private $router;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $dispatcher;

/**
* @var Consumer
*/
private $consumer;

public function setUp()
{
$this->router = new SimpleRouter;
Expand Down Expand Up @@ -180,4 +195,35 @@ public function testEnvelopeWillBeInvoked()

$this->assertTrue($service->importUsers);
}

/**
* @requires PHP 7.0
*/
public function testWillRejectDispatchOnThrowableError()
{
$this->router->add('ImportReport', new Fixtures\Service);

$queue = new InMemoryQueue('send-newsletter');
$queue->enqueue(new Envelope(new DefaultMessage('ImportReport')));

$this->dispatcher->expects(self::at(0))->method('dispatch')->with('bernard.ping');
$this->dispatcher->expects(self::at(1))->method('dispatch')->with('bernard.invoke');

$this
->dispatcher
->expects(self::at(2))
->method('dispatch')
->with(
'bernard.reject',
self::callback(function (RejectEnvelopeEvent $rejectEnvelope) {
self::assertInstanceOf('TypeError', $rejectEnvelope->getException());

return true;
})
);

self::setExpectedException('TypeError');

$this->consumer->tick($queue, ['stop-on-error' => true]);
}
}
15 changes: 15 additions & 0 deletions tests/Driver/AbstractDoctrineDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

abstract class AbstractDoctrineDriverTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;

/**
* @var DoctrineDriver
*/
protected $driver;

public function setUp()
{
if (defined('HHVM_VERSION')) {
Expand Down Expand Up @@ -149,4 +159,9 @@ protected function setUpDatabase()

return $connection;
}

/**
* @return \Doctrine\DBAL\Connection
*/
protected abstract function createConnection();
}
21 changes: 21 additions & 0 deletions tests/Event/RejectEnvelopeEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class RejectEnvelopeEventTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Bernard\Envelope|\PHPUnit_Framework_MockObject_MockObject
*/
private $envelope;

/**
* @var \Bernard\Queue|\PHPUnit_Framework_MockObject_MockObject
*/
private $queue;

public function setUp()
{
$this->envelope = $this->getMockBuilder('Bernard\Envelope')
Expand All @@ -27,4 +37,15 @@ public function testRetrieveException()

$this->assertSame($e, $event->getException());
}

/**
* @requires PHP 7.0
*/
public function testCanContainThrowable()
{
$error = new \TypeError();
$event = new RejectEnvelopeEvent($this->envelope, $this->queue, $error);

self::assertSame($error, $event->getException());
}
}
6 changes: 6 additions & 0 deletions tests/Fixtures/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public function createFile()
{
touch(__DIR__ . '/create_file.test');
}

public function importReport(Report $report)
{
// note: the class hinted on this method does not exist on purpose, as calling this method should cause a
// Throwable to be thrown
}
}

0 comments on commit c5a6bbe

Please sign in to comment.