diff --git a/src/CommandBus/SymfonyCommandBus.php b/src/CommandBus/SymfonyCommandBus.php index 958d0f7c..47cc2fe8 100644 --- a/src/CommandBus/SymfonyCommandBus.php +++ b/src/CommandBus/SymfonyCommandBus.php @@ -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( @@ -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; } } } diff --git a/tests/Unit/CommandBus/SymfonyCommandBusTest.php b/tests/Unit/CommandBus/SymfonyCommandBusTest.php new file mode 100644 index 00000000..f93b8b0f --- /dev/null +++ b/tests/Unit/CommandBus/SymfonyCommandBusTest.php @@ -0,0 +1,125 @@ +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); + } +} diff --git a/tests/Unit/CommandBus/SymfonyCommandtBusTest.php b/tests/Unit/CommandBus/SymfonyCommandtBusTest.php deleted file mode 100644 index af671703..00000000 --- a/tests/Unit/CommandBus/SymfonyCommandtBusTest.php +++ /dev/null @@ -1,84 +0,0 @@ -createMock(MessageBusInterface::class); - $messageBus - ->expects($this->once()) - ->method('dispatch') - ->with($command) - ->willReturn($envelope); - - $commandBus = new SymfonyCommandBus($messageBus); - $commandBus->dispatch($command); - } - - public function testException(): void - { - $command = new CreateProfile( - CustomId::fromString('1'), - ); - $internalException = new class extends RuntimeException { - }; - $envelope = new Envelope($command); - - $messageBus = $this->createMock(MessageBusInterface::class); - $messageBus - ->expects($this->once()) - ->method('dispatch') - ->with($command) - ->willThrowException(new HandlerFailedException($envelope, [$internalException])); - - $commandBus = new SymfonyCommandBus($messageBus); - - $this->expectException($internalException::class); - - $commandBus->dispatch($command); - } - - public function testRecursiveException(): void - { - $command = new CreateProfile( - CustomId::fromString('1'), - ); - $internalException = new class extends RuntimeException { - }; - $envelope = new Envelope($command); - - $messageBus = $this->createMock(MessageBusInterface::class); - $messageBus - ->expects($this->once()) - ->method('dispatch') - ->with($command) - ->willThrowException(new HandlerFailedException( - $envelope, - [new HandlerFailedException($envelope, [$internalException])], - )); - - $commandBus = new SymfonyCommandBus($messageBus); - $this->expectException($internalException::class); - - $commandBus->dispatch($command); - } -}