|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenSolid\Core\Tests\Integration\Bus; |
| 6 | + |
| 7 | +use OpenSolid\Core\Application\Command\Message\Command; |
| 8 | +use OpenSolid\Core\Domain\Envelop\Attribute\Envelope as EnvelopeAttribute; |
| 9 | +use OpenSolid\Core\Domain\Envelop\Stamp\TransportStamp; |
| 10 | +use OpenSolid\Core\Infrastructure\Bus\Command\Error\NoHandlerForCommand; |
| 11 | +use OpenSolid\Core\Infrastructure\Bus\Command\SymfonyCommandBus; |
| 12 | +use OpenSolid\Core\Infrastructure\Bus\Envelop\Stamp\Transformer\ChainStampTransformer; |
| 13 | +use OpenSolid\Core\Infrastructure\Bus\Envelop\Stamp\Transformer\DefaultStampTransformer; |
| 14 | +use PHPUnit\Framework\Attributes\Test; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Messenger\Envelope; |
| 17 | +use Symfony\Component\Messenger\Exception\HandlerFailedException; |
| 18 | +use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; |
| 19 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 20 | +use Symfony\Component\Messenger\Stamp\HandledStamp; |
| 21 | +use Symfony\Component\Messenger\Stamp\SentStamp; |
| 22 | +use Symfony\Component\Messenger\Stamp\TransportNamesStamp; |
| 23 | + |
| 24 | +class SymfonyCommandBusTest extends TestCase |
| 25 | +{ |
| 26 | + private ChainStampTransformer $stampTransformer; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->stampTransformer = new ChainStampTransformer([ |
| 31 | + new DefaultStampTransformer(), |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + #[Test] |
| 36 | + public function executesCommandAndReturnsResult(): void |
| 37 | + { |
| 38 | + $command = new TestIntegrationCommand(); |
| 39 | + $expectedResult = 'command-executed'; |
| 40 | + |
| 41 | + $envelope = new Envelope($command, [ |
| 42 | + new HandledStamp($expectedResult, 'TestHandler::__invoke'), |
| 43 | + ]); |
| 44 | + |
| 45 | + $messageBus = $this->createMock(MessageBusInterface::class); |
| 46 | + $messageBus->expects($this->once()) |
| 47 | + ->method('dispatch') |
| 48 | + ->willReturn($envelope); |
| 49 | + |
| 50 | + $bus = new SymfonyCommandBus($messageBus, $this->stampTransformer); |
| 51 | + $result = $bus->execute($command); |
| 52 | + |
| 53 | + $this->assertSame($expectedResult, $result); |
| 54 | + } |
| 55 | + |
| 56 | + #[Test] |
| 57 | + public function executesCommandWithStampsFromEnvelopeAttribute(): void |
| 58 | + { |
| 59 | + $command = new TestAsyncCommand(); |
| 60 | + |
| 61 | + $capturedEnvelope = null; |
| 62 | + $messageBus = $this->createMock(MessageBusInterface::class); |
| 63 | + $messageBus->expects($this->once()) |
| 64 | + ->method('dispatch') |
| 65 | + ->willReturnCallback(function (Envelope $envelope) use (&$capturedEnvelope) { |
| 66 | + $capturedEnvelope = $envelope; |
| 67 | + return $envelope->with( |
| 68 | + new HandledStamp('result', 'Handler'), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + $bus = new SymfonyCommandBus($messageBus, $this->stampTransformer); |
| 73 | + $bus->execute($command); |
| 74 | + |
| 75 | + $this->assertNotNull($capturedEnvelope); |
| 76 | + $stamps = $capturedEnvelope->all(TransportNamesStamp::class); |
| 77 | + $this->assertCount(1, $stamps); |
| 78 | + $this->assertSame(['async'], $stamps[0]->getTransportNames()); |
| 79 | + } |
| 80 | + |
| 81 | + #[Test] |
| 82 | + public function throwsNoHandlerForCommandOnNoHandler(): void |
| 83 | + { |
| 84 | + $command = new TestIntegrationCommand(); |
| 85 | + |
| 86 | + $messageBus = $this->createStub(MessageBusInterface::class); |
| 87 | + $messageBus->method('dispatch') |
| 88 | + ->willThrowException(new NoHandlerForMessageException()); |
| 89 | + |
| 90 | + $bus = new SymfonyCommandBus($messageBus, $this->stampTransformer); |
| 91 | + |
| 92 | + $this->expectException(NoHandlerForCommand::class); |
| 93 | + |
| 94 | + $bus->execute($command); |
| 95 | + } |
| 96 | + |
| 97 | + #[Test] |
| 98 | + public function unwrapsHandlerFailedException(): void |
| 99 | + { |
| 100 | + $command = new TestIntegrationCommand(); |
| 101 | + $originalException = new \InvalidArgumentException('Invalid input'); |
| 102 | + $handlerFailedException = new HandlerFailedException( |
| 103 | + new Envelope($command), |
| 104 | + [$originalException] |
| 105 | + ); |
| 106 | + |
| 107 | + $messageBus = $this->createStub(MessageBusInterface::class); |
| 108 | + $messageBus->method('dispatch') |
| 109 | + ->willThrowException($handlerFailedException); |
| 110 | + |
| 111 | + $bus = new SymfonyCommandBus($messageBus, $this->stampTransformer); |
| 112 | + |
| 113 | + $this->expectException(\InvalidArgumentException::class); |
| 114 | + $this->expectExceptionMessage('Invalid input'); |
| 115 | + |
| 116 | + $bus->execute($command); |
| 117 | + } |
| 118 | + |
| 119 | + #[Test] |
| 120 | + public function returnsNullForAsyncHandling(): void |
| 121 | + { |
| 122 | + $command = new TestIntegrationCommand(); |
| 123 | + |
| 124 | + $envelope = new Envelope($command, [ |
| 125 | + new SentStamp('async', 'AsyncSender'), |
| 126 | + ]); |
| 127 | + |
| 128 | + $messageBus = $this->createStub(MessageBusInterface::class); |
| 129 | + $messageBus->method('dispatch') |
| 130 | + ->willReturn($envelope); |
| 131 | + |
| 132 | + $bus = new SymfonyCommandBus($messageBus, $this->stampTransformer); |
| 133 | + $result = $bus->execute($command); |
| 134 | + |
| 135 | + $this->assertNull($result); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +final readonly class TestIntegrationCommand extends Command |
| 140 | +{ |
| 141 | +} |
| 142 | + |
| 143 | +#[EnvelopeAttribute([new TransportStamp('async')])] |
| 144 | +final readonly class TestAsyncCommand extends Command |
| 145 | +{ |
| 146 | +} |
0 commit comments