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

Skip to content

Commit 1742f67

Browse files
alexander-schranzOskarStark
authored andcommitted
Add stamps to handle trait
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
1 parent 764fe52 commit 1742f67

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add `SentForRetryStamp` that identifies whether a failed message was sent for retry
99
* Add `Symfony\Component\Messenger\Middleware\DeduplicateMiddleware` and `Symfony\Component\Messenger\Stamp\DeduplicateStamp`
1010
* Add `--class-filter` option to the `messenger:failed:remove` command
11+
* Add `$stamps` parameter to `HandleTrait::handle`
1112

1213
7.2
1314
---

src/Symfony/Component/Messenger/HandleTrait.php

+5-3
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.
@@ -29,15 +30,16 @@ trait HandleTrait
2930
* This behavior is useful for both synchronous command & query buses,
3031
* the last one usually returning the handler result.
3132
*
32-
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
33+
* @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 middleware behavior
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-
8000 $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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface MessageBusInterface
2323
* Dispatches the given message.
2424
*
2525
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
26-
* @param StampInterface[] $stamps
26+
* @param StampInterface[] $stamps Stamps set on the Envelope which are used to control middleware behavior
2727
*
2828
* @throws ExceptionInterface
2929
*/

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Messenger\MessageBus;
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020
use Symfony\Component\Messenger\Stamp\HandledStamp;
21+
use Symfony\Component\Messenger\Stamp\StampInterface;
2122
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2223

2324
class HandleTraitTest extends TestCase
@@ -56,6 +57,20 @@ public function testHandleAcceptsEnvelopes()
5657
$this->assertSame('result', $queryBus->query($envelope));
5758
}
5859

60+
public function testHandleWithStamps()
61+
{
62+
$bus = $this->createMock(MessageBus::class);
63+
$queryBus = new TestQueryBus($bus);
64+
$stamp = $this->createMock(StampInterface::class);
65+
66+
$query = new DummyMessage('Hello');
67+
$bus->expects($this->once())->method('dispatch')->with($query, [$stamp])->willReturn(
68+
new Envelope($query, [new HandledStamp('result', 'DummyHandler::__invoke')])
69+
);
70+
71+
$queryBus->query($query, [$stamp]);
72+
}
73+
5974
public function testHandleThrowsOnNoHandledStamp()
6075
{
6176
$this->expectException(LogicException::class);
@@ -96,8 +111,8 @@ public function __construct(?MessageBusInterface $messageBus)
96111
}
97112
}
98113

99-
public function query($query): string
114+
public function query($query, array $stamps = []): string
100115
{
101-
return $this->handle($query);
116+
return $this->handle($query, $stamps);
102117
}
103118
}

0 commit comments

Comments
 (0)
0