Skip to content

Commit cf3966c

Browse files
bug #50881 [Messenger] Fix passing options set via tags to handler descriptors (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Messenger] Fix passing options set via tags to handler descriptors | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Spotted while working on #50873 Commits ------- 2a6f72b [Messenger] Fix passing options set via tags to handler descriptors
2 parents a746442 + 2a6f72b commit cf3966c

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/Symfony/Component/Mailer/Bridge/Sendinblue/Tests/Transport/SendinblueApiTransportTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Symfony\Component\Mailer\Header\TagHeader;
2222
use Symfony\Component\Mime\Address;
2323
use Symfony\Component\Mime\Email;
24-
use Symfony\Component\Mime\Part\DataPart;
2524
use Symfony\Contracts\HttpClient\ResponseInterface;
2625

2726
class SendinblueApiTransportTest extends TestCase
@@ -131,7 +130,6 @@ public function testSend()
131130
$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
132131
$transport->setPort(8984);
133132

134-
$dataPart = new DataPart('body');
135133
$mail = new Email();
136134
$mail->subject('Hello!')
137135
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
@@ -141,7 +139,6 @@ public function testSend()
141139
->addCc('foo@bar.fr')
142140
->addBcc('foo@bar.fr')
143141
->addReplyTo('foo@bar.fr')
144-
->attachPart($dataPart)
145142
;
146143

147144
$message = $transport->send($mail);

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,16 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
115115
$options = ['method' => $options];
116116
}
117117

118-
if (!isset($options['from_transport']) && isset($tag['from_transport'])) {
119-
$options['from_transport'] = $tag['from_transport'];
120-
}
121-
122-
$priority = $tag['priority'] ?? $options['priority'] ?? 0;
118+
$options += array_filter($tag);
119+
unset($options['handles']);
120+
$priority = $options['priority'] ?? 0;
123121
$method = $options['method'] ?? '__invoke';
124122

125123
if (isset($options['bus'])) {
126124
if (!\in_array($options['bus'], $busIds)) {
127125
$messageLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : ($r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method));
128126

129-
throw new RuntimeException(sprintf('Invalid configuration "%s" for message "%s": bus "%s" does not exist.', $messageLocation, $message, $options['bus']));
127+
throw new RuntimeException(sprintf('Invalid configuration '.$messageLocation.' for message "%s": bus "%s" does not exist.', $message, $options['bus']));
130128
}
131129

132130
$buses = [$options['bus']];
@@ -135,7 +133,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
135133
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
136134
$messageLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : ($r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method));
137135

138-
throw new RuntimeException(sprintf('Invalid handler service "%s": class or interface "%s" "%s" not found.', $serviceId, $message, $messageLocation));
136+
throw new RuntimeException(sprintf('Invalid handler service "%s": class or interface "%s" '.$messageLocation.' not found.', $serviceId, $message));
139137
}
140138

141139
if (!$r->hasMethod($method)) {

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,15 @@ public function testProcessHandlersByBus()
170170
$container,
171171
$commandBusHandlersLocatorDefinition->getArgument(0),
172172
MultipleBusesMessage::class,
173-
[MultipleBusesMessageHandler::class]
173+
[MultipleBusesMessageHandler::class],
174+
[['bus' => $commandBusId]]
174175
);
175176
$this->assertHandlerDescriptor(
176177
$container,
177178
$commandBusHandlersLocatorDefinition->getArgument(0),
178179
DummyCommand::class,
179-
[DummyCommandHandler::class]
180+
[DummyCommandHandler::class],
181+
[['bus' => $commandBusId]]
180182
);
181183

182184
$queryBusHandlersLocatorDefinition = $container->getDefinition($queryBusId.'.messenger.handlers_locator');
@@ -185,13 +187,15 @@ public function testProcessHandlersByBus()
185187
$container,
186188
$queryBusHandlersLocatorDefinition->getArgument(0),
187189
DummyQuery::class,
188-
[DummyQueryHandler::class]
190+
[DummyQueryHandler::class],
191+
[['bus' => $queryBusId]]
189192
);
190193
$this->assertHandlerDescriptor(
191194
$container,
192195
$queryBusHandlersLocatorDefinition->getArgument(0),
193196
MultipleBusesMessage::class,
194-
[MultipleBusesMessageHandler::class]
197+
[MultipleBusesMessageHandler::class],
198+
[['bus' => $queryBusId]]
195199
);
196200
}
197201

@@ -442,7 +446,7 @@ public function testItRegistersHandlersOnDifferentBuses()
442446
public function testItThrowsAnExceptionOnUnknownBus()
443447
{
444448
$this->expectException(RuntimeException::class);
445-
$this->expectExceptionMessage('Invalid configuration "returned by method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerOnUndefinedBus::getHandledMessages()"" for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage": bus "some_undefined_bus" does not exist.');
449+
$this->expectExceptionMessage('Invalid configuration returned by method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerOnUndefinedBus::getHandledMessages()" for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage": bus "some_undefined_bus" does not exist.');
446450
$container = $this->getContainerBuilder();
447451
$container
448452
->register(HandlerOnUndefinedBus::class, HandlerOnUndefinedBus::class)
@@ -455,7 +459,7 @@ public function testItThrowsAnExceptionOnUnknownBus()
455459
public function testUndefinedMessageClassForHandler()
456460
{
457461
$this->expectException(RuntimeException::class);
458-
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" "used as argument type in method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler::__invoke()"" not found.');
462+
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" used as argument type in method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler::__invoke()" not found.');
459463
$container = $this->getContainerBuilder();
460464
$container
461465
->register(UndefinedMessageHandler::class, UndefinedMessageHandler::class)
@@ -468,7 +472,7 @@ public function testUndefinedMessageClassForHandler()
468472
public function testUndefinedMessageClassForHandlerImplementingMessageHandlerInterface()
469473
{
470474
$this->expectException(RuntimeException::class);
471-
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaHandlerInterface": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" "used as argument type in method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaHandlerInterface::__invoke()"" not found.');
475+
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaHandlerInterface": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" used as argument type in method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaHandlerInterface::__invoke()" not found.');
472476
$container = $this->getContainerBuilder();
473477
$container
474478
->register(UndefinedMessageHandlerViaHandlerInterface::class, UndefinedMessageHandlerViaHandlerInterface::class)
@@ -481,7 +485,7 @@ public function testUndefinedMessageClassForHandlerImplementingMessageHandlerInt
481485
public function testUndefinedMessageClassForHandlerImplementingMessageSubscriberInterface()
482486
{
483487
$this->expectException(RuntimeException::class);
484-
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaSubscriberInterface": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" "returned by method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaSubscriberInterface::getHandledMessages()"" not found.');
488+
$this->expectExceptionMessage('Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaSubscriberInterface": class or interface "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" returned by method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandlerViaSubscriberInterface::getHandledMessages()" not found.');
485489
$container = $this->getContainerBuilder();
486490
$container
487491
->register(UndefinedMessageHandlerViaSubscriberInterface::class, UndefinedMessageHandlerViaSubscriberInterface::class)
@@ -713,12 +717,12 @@ public function testItRegistersTheDebugCommand()
713717

714718
$this->assertEquals([
715719
$commandBusId => [
716-
DummyCommand::class => [[DummyCommandHandler::class, []]],
717-
MultipleBusesMessage::class => [[MultipleBusesMessageHandler::class, []]],
720+
DummyCommand::class => [[DummyCommandHandler::class, ['bus' => $commandBusId]]],
721+
MultipleBusesMessage::class => [[MultipleBusesMessageHandler::class, ['bus' => $commandBusId]]],
718722
],
719723
$queryBusId => [
720-
DummyQuery::class => [[DummyQueryHandler::class, []]],
721-
MultipleBusesMessage::class => [[MultipleBusesMessageHandler::class, []]],
724+
DummyQuery::class => [[DummyQueryHandler::class, ['bus' => $queryBusId]]],
725+
MultipleBusesMessage::class => [[MultipleBusesMessageHandler::class, ['bus' => $queryBusId]]],
722726
],
723727
$emptyBus => [],
724728
], $container->getDefinition('console.command.messenger_debug')->getArgument(0));

0 commit comments

Comments
 (0)