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
4 changes: 3 additions & 1 deletion src/CommandBus/SymfonyCommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

use function array_values;

final class SymfonyCommandBus implements CommandBus
{
public function __construct(
Expand All @@ -20,7 +22,7 @@ public function dispatch(object $command): void
try {
$this->messageBus->dispatch($command);
} catch (HandlerFailedException $e) {
throw $e->getWrappedExceptions(null, true)[0] ?? $e;
throw array_values($e->getWrappedExceptions(null, true))[0] ?? $e;
}
}
}
125 changes: 125 additions & 0 deletions tests/Unit/CommandBus/SymfonyCommandBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?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 RuntimeException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
final class SymfonyCommandBusTest extends TestCase
{
public function testDispatch(): void
{
$command = new CreateProfile(CustomId::fromString('1'));

$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();

return $envelope;
}
};
$messageBus = new MessageBus([$middleware]);

$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);

self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}

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

$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();

throw new RuntimeException('test');
}
};
$messageBus = new MessageBus([$middleware]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');

$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);

self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}

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

$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();

throw new HandlerFailedException($envelope, [new RuntimeException('test')]);
}
};
$messageBus = new MessageBus([$middleware]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');

$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);

self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}

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

$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();

throw new HandlerFailedException($envelope, ['controller-class' => new RuntimeException('test')]);
}
};
$messageBus = new MessageBus([$middleware]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');

$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);

self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}
}
84 changes: 0 additions & 84 deletions tests/Unit/CommandBus/SymfonyCommandtBusTest.php

This file was deleted.

Loading