TransportNamesStamp example


        $mySender = $this->createMock(SenderInterface::class);
        $otherSender = $this->createMock(SenderInterface::class);
        $sendersLocator = $this->createContainer([
            'my_sender' => $mySender,
            'other_sender' => $otherSender,
        ]);
        $locator = new SendersLocator([
            DummyMessage::class => ['my_sender'],
        ]$sendersLocator);

        $this->assertSame(['other_sender' => $otherSender]iterator_to_array($locator->getSenders(new Envelope(new DummyMessage('a')[new TransportNamesStamp(['other_sender'])]))));
        $this->assertSame([]iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
    }

    public function testSendersMapWithFallback()
    {
        $firstSender = $this->createMock(SenderInterface::class);
        $secondSender = $this->createMock(SenderInterface::class);
        $sendersLocator = $this->createContainer([
            'first' => $firstSender,
            'second' => $secondSender,
        ]);
        
if (!$event->isQueued()) {
            return;
        }

        $message = $event->getMessage();
        if (!$message instanceof Message || !$message->getHeaders()->has('X-Bus-Transport')) {
            return;
        }

        $names = $message->getHeaders()->get('X-Bus-Transport')->getBody();
        $names = array_map('trim', explode(',', $names));
        $event->addStamp(new TransportNamesStamp($names));
        $message->getHeaders()->remove('X-Bus-Transport');
    }

    public static function getSubscribedEvents(): array
    {
        return [
            MessageEvent::class => 'onMessage',
        ];
    }
}
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer as SymfonySerializer;

class TransportNamesStampTest extends TestCase
{
    public function testGetSenders()
    {
        $configuredSenders = ['first_transport', 'second_transport', 'other_transport'];
        $stamp = new TransportNamesStamp($configuredSenders);
        $stampSenders = $stamp->getTransportNames();
        $this->assertEquals(\count($configuredSenders), \count($stampSenders));

        foreach ($configuredSenders as $key => $sender) {
            $this->assertSame($sender$stampSenders[$key]);
        }
    }

    public function testDeserialization()
    {
        $stamp = new TransportNamesStamp(['foo']);
        
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;

final class RedispatchMessageHandler
{
    public function __construct(
        private MessageBusInterface $bus,
    ) {
    }

    public function __invoke(RedispatchMessage $message): void
    {
        $this->bus->dispatch($message->envelope, [new TransportNamesStamp($message->transportNames)]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.