8000 [Messenger] Do not return fallback senders when other senders were al… · symfony/symfony@cd43ccd · GitHub
[go: up one dir, main page]

Skip to content

Commit cd43ccd

Browse files
committed
[Messenger] Do not return fallback senders when other senders were already found
1 parent c7f82de commit cd43ccd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Sender/SendersLocatorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public function testItReturnsTheSenderBasedOnTransportNamesStamp()
5252
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
5353
}
5454

55+
public function testSendersMapWithFallback()
56+
{
57+
$firstSender = $this->createMock(SenderInterface::class);
58+
$secondSender = $this->createMock(SenderInterface::class);
59+
$sendersLocator = $this->createContainer([
60+
'first' => $firstSender,
61+
'second' => $secondSender,
62+
]);
63+
$locator = new SendersLocator([
64+
DummyMessage::class => ['first'],
65+
'*' => ['second'],
66+
], $sendersLocator);
67+
68+
$this->assertSame(['first' => $firstSender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessage('a')))), 'Unexpected senders for configured message');
69+
$this->assertSame(['second' => $secondSender], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))), 'Unexpected senders for unconfigured message');
70+
}
71+
5572
private function createContainer(array $senders): ContainerInterface
5673
{
5774
$container = $this->createMock(ContainerInterface::class);

src/Symfony/Component/Messenger/Transport/Sender/SendersLocator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function getSenders(Envelope $envelope): iterable
5151

5252
foreach (HandlersLocator::listTypes($envelope) as $type) {
5353
foreach ($this->sendersMap[$type] ?? [] as $senderAlias) {
54+
if ('*' === $type && $seen) {
55+
// the '*' acts as a fallback, if other senders already matched
56+
// with previous types, skip the senders bound to the fallback
57+
continue;
58+
}
59+
5460
if (!\in_array($senderAlias, $seen, true)) {
5561
$seen[] = $senderAlias;
5662

0 commit comments

Comments
 (0)
0