Skip to content

Commit ed10b36

Browse files
bug #58594 [Messenger] Check for #[AsMessage] attributes on parents (HypeMC)
This PR was merged into the 7.2 branch. Discussion ---------- [Messenger] Check for `#[AsMessage]` attributes on parents | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT I've been testing the `#[AsMessage]` attribute and noticed that the attribute is not being read from parent classes or interfaces. This is different to how the `routing` option works, so currently, attributes can't fully replace the option. Commits ------- a164474fe36 [Messenger] Check for `#[AsMessage]` attributes on parents
2 parents b61b332 + c139768 commit ed10b36

6 files changed

+98
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
use Symfony\Component\Messenger\Attribute\AsMessage;
15+
16+
#[AsMessage(transport: ['first_sender', 'third_sender'])]
17+
interface DummyMessageInterfaceWithAttribute1
18+
{
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
use Symfony\Component\Messenger\Attribute\AsMessage;
15+
16+
#[AsMessage(transport: 'second_sender')]
17+
interface DummyMessageInterfaceWithAttribute2
18+
{
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
class DummyMessageWithInterfaceWithAttribute implements DummyMessageInterfaceWithAttribute1, DummyMessageInterfaceWithAttribute2
15+
{
16+
public function __construct(
17+
private string $message,
18+
) {
19+
}
20+
21+
public function getMessage(): string
22+
{
23+
return $this->message;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
use Symfony\Component\Messenger\Attribute\AsMessage;
15+
16+
#[AsMessage(transport: 'third_sender')]
17+
class DummyMessageWithParentWithAttribute extends DummyMessageWithAttribute
18+
{
19+
}

Tests/Transport/Sender/SendersLocatorTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,26 @@ public function testItReturnsTheSenderBasedOnTransportNamesStamp()
5555
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
5656
}
5757

58-
public function testItReturnsTheSenderBasedOnAsMessageAttribute()
58+
/**
59+
* @testWith ["\\Symfony\\Component\\Messenger\\Tests\\Fixtures\\DummyMessageWithAttribute", ["first_sender", "second_sender"]]
60+
* ["\\Symfony\\Component\\Messenger\\Tests\\Fixtures\\DummyMessageWithParentWithAttribute", ["third_sender", "first_sender", "second_sender"]]
61+
* ["\\Symfony\\Component\\Messenger\\Tests\\Fixtures\\DummyMessageWithInterfaceWithAttribute", ["first_sender", "third_sender", "second_sender"]]
62+
*/
63+
public function testItReturnsTheSenderBasedOnAsMessageAttribute(string $messageClass, array $expectedSenders)
5964
{
6065
$firstSender = $this->createMock(SenderInterface::class);
6166
$secondSender = $this->createMock(SenderInterface::class);
67+
$thirdSender = $this->createMock(SenderInterface::class);
6268
$otherSender = $this->createMock(SenderInterface::class);
6369
$sendersLocator = $this->createContainer([
6470
'first_sender' => $firstSender,
6571
'second_sender' => $secondSender,
72+
'third_sender' => $thirdSender,
6673
'other_sender' => $otherSender,
6774
]);
6875
$locator = new SendersLocator([], $sendersLocator);
6976

70-
$this->assertSame(['first_sender' => $firstSender, 'second_sender' => $secondSender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessageWithAttribute('a')))));
77+
$this->assertSame($expectedSenders, array_keys(iterator_to_array($locator->getSenders(new Envelope(new $messageClass('a'))))));
7178
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
7279
}
7380

Transport/Sender/SendersLocator.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ public function getSenders(Envelope $envelope): iterable
8080
private function getTransportNamesFromAttribute(Envelope $envelope): array
8181
{
8282
$transports = [];
83-
$message = $envelope->getMessage();
83+
$messageClass = $envelope->getMessage()::class;
8484

85-
foreach ((new \ReflectionClass($message))->getAttributes(AsMessage::class, \ReflectionAttribute::IS_INSTANCEOF) as $refAttr) {
86-
$asMessage = $refAttr->newInstance();
85+
foreach ([$messageClass] + class_parents($messageClass) + class_implements($messageClass) as $class) {
86+
foreach ((new \ReflectionClass($class))->getAttributes(AsMessage::class, \ReflectionAttribute::IS_INSTANCEOF) as $refAttr) {
87+
$asMessage = $refAttr->newInstance();
8788

88-
if ($asMessage->transport) {
89-
$transports = \array_merge($transports, (array) $asMessage->transport);
89+
if ($asMessage->transport) {
90+
$transports = array_merge($transports, (array) $asMessage->transport);
91+
}
9092
}
9193
}
9294

0 commit comments

Comments
 (0)