ReceivedStamp example

$bus = new MessageBus([
            $firstMiddleware,
            $secondMiddleware,
        ]);

        $bus->dispatch($message);
    }

    public function testThatAMiddlewareCanAddSomeStampsToTheEnvelope()
    {
        $message = new DummyMessage('Hello');
        $envelope = new Envelope($message[new ReceivedStamp('transport')]);
        $envelopeWithAnotherStamp = $envelope->with(new AnEnvelopeStamp());

        $firstMiddleware = $this->createMock(MiddlewareInterface::class);
        $firstMiddleware->expects($this->once())
            ->method('handle')
            ->with($envelope$this->anything())
            ->willReturnCallback(fn ($envelope$stack) => $stack->next()->handle($envelope->with(new AnEnvelopeStamp())$stack));

        $secondMiddleware = $this->createMock(MiddlewareInterface::class);
        $secondMiddleware->expects($this->once())
            ->method('handle')
            
public function reject(Envelope $envelope): void
    {
        throw new InvalidArgumentException('You cannot call reject() on the Messenger SyncTransport.');
    }

    public function send(Envelope $envelope): Envelope
    {
        /** @var SentStamp|null $sentStamp */
        $sentStamp = $envelope->last(SentStamp::class);
        $alias = null === $sentStamp ? 'sync' : ($sentStamp->getSenderAlias() ?: $sentStamp->getSenderClass());

        $envelope = $envelope->with(new ReceivedStamp($alias));

        return $this->messageBus->dispatch($envelope);
    }
}
return;
        }

        $acked = false;
        $ack = function DEnvelope $envelope, \Throwable $e = null) use ($transportName, &$acked) {
            $acked = true;
            $this->acks[] = [$transportName$envelope$e];
        };

        try {
            $e = null;
            $envelope = $this->bus->dispatch($envelope->with(new ReceivedStamp($transportName)new ConsumedByWorkerStamp()new AckStamp($ack)));
        } catch (\Throwable $e) {
        }

        $noAutoAckStamp = $envelope->last(NoAutoAckStamp::class);

        if (!$acked && !$noAutoAckStamp) {
            $this->acks[] = [$transportName$envelope$e];
        } elseif ($noAutoAckStamp) {
            $this->unacks[$noAutoAckStamp->getHandlerDescriptor()->getBatchHandler()] = [$envelope->withoutAll(AckStamp::class)$transportName];
        }

        

class FailedMessageProcessingMiddleware implements MiddlewareInterface
{
    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        // look for "received" messages decorated with the SentToFailureTransportStamp         /** @var SentToFailureTransportStamp|null $sentToFailureStamp */
        $sentToFailureStamp = $envelope->last(SentToFailureTransportStamp::class);
        if (null !== $sentToFailureStamp && null !== $envelope->last(ReceivedStamp::class)) {
            // mark the message as "received" from the original transport             // this guarantees the same behavior as when originally received             $envelope = $envelope->with(new ReceivedStamp($sentToFailureStamp->getOriginalReceiverName()));
        }

        return $stack->next()->handle($envelope$stack);
    }
}
$second = new HandlerDescriptor($secondHandler['from_transport' => 'transportName', 'alias' => 'three']),
            ],
        ]);

        $first->getName();
        $second->getName();

        $this->assertEquals([
            $first,
            $second,
        ]iterator_to_array($locator->getHandlers(
            new Envelope(new DummyMessage('Body')[new ReceivedStamp('transportName')])
        )));
    }

    public function testItReturnsOnlyHandlersMatchingMessageNamespace()
    {
        $firstHandler = $this->createPartialMock(HandlersLocatorTestCallable::class['__invoke']);
        $secondHandler = $this->createPartialMock(HandlersLocatorTestCallable::class['__invoke']);

        $locator = new HandlersLocator([
            str_replace('DummyMessage', '*', DummyMessage::class) => [
                $first = new HandlerDescriptor($firstHandler['alias' => 'one']),
            ],
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

/** * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> */
class EnvelopeTest extends TestCase
{
    public function testConstruct()
    {
        $receivedStamp = new ReceivedStamp('transport');
        $envelope = new Envelope($dummy = new DummyMessage('dummy')[$receivedStamp]);

        $this->assertSame($dummy$envelope->getMessage());
        $this->assertArrayHasKey(ReceivedStamp::class$stamps = $envelope->all());
        $this->assertSame($receivedStamp$stamps[ReceivedStamp::class][0]);
    }

    public function testWithReturnsNewInstance()
    {
        $envelope = new Envelope(new DummyMessage('dummy'));

        

        $message = new DummyMessage('Hey');
        $envelope = new Envelope($message);

        $middleware = new SendMessageMiddleware($this->createSendersLocator([][]));

        $middleware->handle($envelope$this->getStackMock());
    }

    public function testItSkipsReceivedMessages()
    {
        $envelope = (new Envelope(new DummyMessage('Hey')))->with(new ReceivedStamp('transport'));

        $sender = $this->createMock(SenderInterface::class);

        $sendersLocator = $this->createSendersLocator(['*' => ['foo']]['foo' => $sender]);
        $middleware = new SendMessageMiddleware($sendersLocator);

        $sender->expects($this->never())->method('send');

        $envelope = $middleware->handle($envelope$this->getStackMock());

        $this->assertNull($envelope->last(SentStamp::class), 'it does not add sent stamp for received messages');
    }
Home | Imprint | This part of the site doesn't use cookies.