Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/CommandBus/SymfonyCommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcingBundle\CommandBus;

use Patchlevel\EventSourcing\CommandBus\CommandBus;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

final class SymfonyCommandBus implements CommandBus
Expand All @@ -16,6 +17,10 @@ public function __construct(

public function dispatch(object $command): void
{
$this->messageBus->dispatch($command);
try {
$this->messageBus->dispatch($command);
} catch (HandlerFailedException $e) {
throw $e->getWrappedExceptions(null, true)[0] ?? $e;
}
}
}
88 changes: 88 additions & 0 deletions tests/Unit/CommandBus/SymfonyCommandtBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingBundle\Tests\Unit\CommandBus;

use Patchlevel\EventSourcing\Aggregate\CustomId;
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyCommandBus;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\CreateProfile;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use RuntimeException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
final class SymfonyCommandtBusTest extends TestCase
{
use ProphecyTrait;

public function testDispatch(): void
{
$command = new CreateProfile(
CustomId::fromString('1'),
);

$symfony = $this->prophesize(MessageBusInterface::class);

$envelope = new Envelope($command);

$symfony->dispatch($command)->willReturn($envelope)->shouldBeCalled();

$commandBus = new SymfonyCommandBus($symfony->reveal());
$commandBus->dispatch($command);
}

public function testException(): void
{
$command = new CreateProfile(
CustomId::fromString('1'),
);

$symfony = $this->prophesize(MessageBusInterface::class);

$internalException = new class extends RuntimeException {
};

$envelope = new Envelope($command);

$symfony
->dispatch($command)
->willThrow(new HandlerFailedException($envelope, [$internalException]))
->shouldBeCalled();

$commandBus = new SymfonyCommandBus($symfony->reveal());

$this->expectException($internalException::class);

$commandBus->dispatch($command);
}


public function testRecursiveException(): void
{
$command = new CreateProfile(
CustomId::fromString('1'),
);

$symfony = $this->prophesize(MessageBusInterface::class);

$internalException = new class extends RuntimeException {
};

$envelope = new Envelope($command);

$symfony
->dispatch($command)
->willThrow(new HandlerFailedException($envelope, [new HandlerFailedException($envelope, [$internalException])]))
->shouldBeCalled();

$commandBus = new SymfonyCommandBus($symfony->reveal());

$this->expectException($internalException::class);

$commandBus->dispatch($command);
}
}