|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\EventSourcingBundle\Tests\Unit\CommandBus; |
| 6 | + |
| 7 | +use Patchlevel\EventSourcing\Aggregate\CustomId; |
| 8 | +use Patchlevel\EventSourcingBundle\CommandBus\SymfonyCommandBus; |
| 9 | +use Patchlevel\EventSourcingBundle\Tests\Fixtures\CreateProfile; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use RuntimeException; |
| 12 | +use Symfony\Component\Messenger\Envelope; |
| 13 | +use Symfony\Component\Messenger\Exception\HandlerFailedException; |
| 14 | +use Symfony\Component\Messenger\MessageBus; |
| 15 | +use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
| 16 | +use Symfony\Component\Messenger\Middleware\StackInterface; |
| 17 | + |
| 18 | +/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */ |
| 19 | +final class SymfonyCommandBusTest extends TestCase |
| 20 | +{ |
| 21 | + public function testDispatch(): void |
| 22 | + { |
| 23 | + $command = new CreateProfile(CustomId::fromString('1')); |
| 24 | + |
| 25 | + $middleware = new class implements MiddlewareInterface |
| 26 | + { |
| 27 | + public object|null $command = null; |
| 28 | + |
| 29 | + public function handle(Envelope $envelope, StackInterface $stack): Envelope |
| 30 | + { |
| 31 | + $this->command = $envelope->getMessage(); |
| 32 | + |
| 33 | + return $envelope; |
| 34 | + } |
| 35 | + }; |
| 36 | + $messageBus = new MessageBus([$middleware]); |
| 37 | + |
| 38 | + $commandBus = new SymfonyCommandBus($messageBus); |
| 39 | + $commandBus->dispatch($command); |
| 40 | + |
| 41 | + self::assertNotNull($middleware->command); |
| 42 | + self::assertSame($command, $middleware->command); |
| 43 | + } |
| 44 | + |
| 45 | + public function testException(): void |
| 46 | + { |
| 47 | + $command = new CreateProfile(CustomId::fromString('1')); |
| 48 | + |
| 49 | + $middleware = new class implements MiddlewareInterface |
| 50 | + { |
| 51 | + public object|null $command = null; |
| 52 | + |
| 53 | + public function handle(Envelope $envelope, StackInterface $stack): Envelope |
| 54 | + { |
| 55 | + $this->command = $envelope->getMessage(); |
| 56 | + |
| 57 | + throw new RuntimeException('test'); |
| 58 | + } |
| 59 | + }; |
| 60 | + $messageBus = new MessageBus([$middleware]); |
| 61 | + |
| 62 | + $this->expectException(RuntimeException::class); |
| 63 | + $this->expectExceptionMessageMatches('/^test$/'); |
| 64 | + |
| 65 | + $commandBus = new SymfonyCommandBus($messageBus); |
| 66 | + $commandBus->dispatch($command); |
| 67 | + |
| 68 | + self::assertNotNull($middleware->command); |
| 69 | + self::assertSame($command, $middleware->command); |
| 70 | + } |
| 71 | + |
| 72 | + public function testRecursiveException(): void |
| 73 | + { |
| 74 | + $command = new CreateProfile(CustomId::fromString('1')); |
| 75 | + |
| 76 | + $middleware = new class implements MiddlewareInterface |
| 77 | + { |
| 78 | + public object|null $command = null; |
| 79 | + |
| 80 | + public function handle(Envelope $envelope, StackInterface $stack): Envelope |
| 81 | + { |
| 82 | + $this->command = $envelope->getMessage(); |
| 83 | + |
| 84 | + throw new HandlerFailedException($envelope, [new RuntimeException('test')]); |
| 85 | + } |
| 86 | + }; |
| 87 | + $messageBus = new MessageBus([$middleware]); |
| 88 | + |
| 89 | + $this->expectException(RuntimeException::class); |
| 90 | + $this->expectExceptionMessageMatches('/^test$/'); |
| 91 | + |
| 92 | + $commandBus = new SymfonyCommandBus($messageBus); |
| 93 | + $commandBus->dispatch($command); |
| 94 | + |
| 95 | + self::assertNotNull($middleware->command); |
| 96 | + self::assertSame($command, $middleware->command); |
| 97 | + } |
| 98 | + |
| 99 | + public function testRecursiveExceptionStringKey(): void |
| 100 | + { |
| 101 | + $command = new CreateProfile(CustomId::fromString('1')); |
| 102 | + |
| 103 | + $middleware = new class implements MiddlewareInterface |
| 104 | + { |
| 105 | + public object|null $command = null; |
| 106 | + |
| 107 | + public function handle(Envelope $envelope, StackInterface $stack): Envelope |
| 108 | + { |
| 109 | + $this->command = $envelope->getMessage(); |
| 110 | + |
| 111 | + throw new HandlerFailedException($envelope, ['controller-class' => new RuntimeException('test')]); |
| 112 | + } |
| 113 | + }; |
| 114 | + $messageBus = new MessageBus([$middleware]); |
| 115 | + |
| 116 | + $this->expectException(RuntimeException::class); |
| 117 | + $this->expectExceptionMessageMatches('/^test$/'); |
| 118 | + |
| 119 | + $commandBus = new SymfonyCommandBus($messageBus); |
| 120 | + $commandBus->dispatch($command); |
| 121 | + |
| 122 | + self::assertNotNull($middleware->command); |
| 123 | + self::assertSame($command, $middleware->command); |
| 124 | + } |
| 125 | +} |
0 commit comments