8000 Add stamps to handle trait · symfony/symfony@c82413c · GitHub
[go: up one dir, main page]

Skip to content

Commit c82413c

Browse files
Add stamps to handle trait
1 parent 8b680f0 commit c82413c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/Symfony/Component/Messenger/HandleTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Exception\LogicException;
1515
use Symfony\Component\Messenger\Stamp\HandledStamp;
16+
use Symfony\Component\Messenger\Stamp\StampInterface;
1617

1718
/**
1819
* Leverages a message bus to expect a single, synchronous message handling and return its result.
@@ -30,14 +31,15 @@ trait HandleTrait
3031
* the last one usually returning the handler result.
3132
*
3233
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
34+
* @param StampInterface[] $stamps Stamps to be set on the Envelope which are used to control middlewares behaviours
3335
*/
34-
private function handle(object $message): mixed
36+
private function handle(object $message, array $stamps = []): mixed
3537
{
3638
if (!isset($this->messageBus)) {
3739
throw new LogicException(sprintf('You must provide a "%s" instance in the "%s::$messageBus" property, but that property has not been initialized yet.', MessageBusInterface::class, static::class));
3840
}
3941

40-
$envelope = $this->messageBus->dispatch($message);
42+
$envelope = $this->messageBus->dispatch($message, $stamps);
4143
/** @var HandledStamp[] $handledStamps */
4244
$handledStamps = $envelope->all(HandledStamp::class);
4345

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface MessageBusInterface
2222
* Dispatches the given message.
2323
*
2424
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
25-
* @param StampInterface[] $stamps
25+
* @param StampInterface[] $stamps Stamps set on the Envelope which are used to control middlewares behaviours
2626
*/
2727
public function dispatch(object $message, array $stamps = []): Envelope;
2828
}

src/Symfony/Component/Messenger/Tests/HandleTraitTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Messenger\MessageBus;
1010
use Symfony\Component\Messenger\MessageBusInterface;
1111
use Symfony\Component\Messenger\Stamp\HandledStamp;
12+
use Symfony\Component\Messenger\Stamp\StampInterface;
1213
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1314

1415
class HandleTraitTest extends TestCase
@@ -47,6 +48,20 @@ public function testHandleAcceptsEnvelopes()
4748
$this->assertSame('result', $queryBus->query($envelope));
4849
}
4950

51+
public function testHandleWithStamps()
52+
{
53+
$bus = $this->createMock(MessageBus::class);
54+
$queryBus = new TestQueryBus($bus);
55+
$stamp = $this->createMock(StampInterface::class);
56+
57+
$query = new DummyMessage('Hello');
58+
$bus->expects($this->once())->method('dispatch')->with($query, [$stamp])->willReturn(
59+
new Envelope($query, [new HandledStamp('result', 'DummyHandler::__invoke')])
60+
);
61+
62+
$queryBus->query($query, [$stamp]);
63+
}
64+
5065
public function testHandleThrowsOnNoHandledStamp()
5166
{
5267
$this->expectException(LogicException::class);
@@ -87,8 +102,8 @@ public function __construct(?MessageBusInterface $messageBus)
87102
}
88103
}
89104

90-
public function query($query): string
105+
public function query($query, array $stamps = []): string
91106
{
92-
return $this->handle($query);
107+
return $this->handle($query, $stamps);
93108
}
94109
}

0 commit comments

Comments
 (0)
0