-
Notifications
You must be signed in to change notification settings - Fork 23
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 #73 from tyx/fix/unrecoverable-unpack
- Loading branch information
Showing
2 changed files
with
95 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
namespace Bref\Symfony\Messenger\Test\Unit\Service; | ||
|
||
use Bref\Symfony\Messenger\Service\SimpleBusDriver; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Psr\Log\NullLogger; | ||
use stdClass; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Messenger\Stamp\ReceivedStamp; | ||
use Throwable; | ||
|
||
final class SimpleBusDriverTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $messengerBus; | ||
|
||
/** @before */ | ||
public function prepare() | ||
{ | ||
$this->messengerBus = $this->prophesize(MessageBusInterface::class); | ||
} | ||
|
||
public function test_it_dispatch_message_on_specified_transport_ready_to_be_consumed() | ||
{ | ||
$sut = new SimpleBusDriver(new NullLogger()); | ||
$envelopeToBeConsumed = new Envelope(new stdClass()); | ||
$this->messengerWillSucceedInConsumingOnTransport($envelopeToBeConsumed, 'my_transport'); | ||
|
||
$sut->putEnvelopeOnBus($this->messengerBus->reveal(), $envelopeToBeConsumed, 'my_transport'); | ||
} | ||
|
||
public function test_it_unpack_handler_failed_exception() | ||
{ | ||
$sut = new SimpleBusDriver(new NullLogger()); | ||
$envelopeToBeConsumed = new Envelope(new stdClass()); | ||
$this->messengerWillFailToConsumeOnTransport( | ||
$envelopeToBeConsumed, | ||
'async', | ||
new UnrecoverableMessageHandlingException('boum') | ||
); | ||
$this->expectException(UnrecoverableMessageHandlingException::class); | ||
$sut->putEnvelopeOnBus($this->messengerBus->reveal(), $envelopeToBeConsumed, 'async'); | ||
} | ||
|
||
private function messengerWillSucceedInConsumingOnTransport(Envelope $envelope, string $transport) | ||
{ | ||
$this->messengerBus | ||
->dispatch($envelope->with(new ReceivedStamp($transport), new ConsumedByWorkerStamp)) | ||
->willReturn( | ||
$envelope->with(new HandledStamp('result', 'my.handler')) | ||
) | ||
->shouldBeCalled() | ||
; | ||
} | ||
|
||
private function messengerWillFailToConsumeOnTransport(Envelope $envelope, string $transport, Throwable $failure) | ||
{ | ||
$this->messengerBus | ||
->dispatch($envelope->with(new ReceivedStamp($transport), new ConsumedByWorkerStamp)) | ||
->willThrow( | ||
new HandlerFailedException( | ||
$envelope->with(new HandledStamp('result', 'my.handler')), | ||
[ | ||
$failure | ||
] | ||
) | ||
) | ||
->shouldBeCalled() | ||
; | ||
} | ||
} |