8000 Allow stamps to be passed directly to MessageBusInterface::dispatch() · symfony/symfony@a5d19c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5d19c3

Browse files
committed
Allow stamps to be passed directly to MessageBusInterface::dispatch()
1 parent 76260e7 commit a5d19c3

8 files changed

+37
-10
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* [BC BREAK]: The `MessageBusInterface::dispatch()` signature changed:
8+
a second argument `StampInterface ...$stamps` was added.
79
* New classes: `RoutableMessageBus`, `AddBusNameStampMiddleware`
810
and `BusNameStamp` were added, which allow you to add a bus identifier
911
to the `Envelope` then find the correct bus when receiving from

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1515
use Symfony\Component\Messenger\Middleware\StackMiddleware;
16+
use Symfony\Component\Messenger\Stamp\StampInterface;
1617

1718
/**
1819
* @author Samuel Roze <samuel.roze@gmail.com>
@@ -52,12 +53,13 @@ public function getIterator()
5253
/**
5354
* {@inheritdoc}
5455
*/
55-
public function dispatch($message): Envelope
56+
public function dispatch($message, StampInterface ...$stamps): Envelope
5657
{
5758
if (!\is_object($message)) {
5859
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
5960
}
6061
$envelope = $message instanceof Envelope ? $message : new Envelope($message);
62+
$envelope = $envelope->with(...$stamps);
6163
$middlewareIterator = $this->middlewareAggregate->getIterator();
6264

6365
while ($middlewareIterator instanceof \IteratorAggregate) {

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Messenger;
1313

14+
use Symfony\Component\Messenger\Stamp\StampInterface;
15+
1416
/**
1517
* @author Samuel Roze <samuel.roze@gmail.com>
1618
*
@@ -23,5 +25,5 @@ interface MessageBusInterface
2325
*
2426
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
2527
*/
26-
public function dispatch($message): Envelope;
28+
public function dispatch($message, StampInterface ...$stamps): Envelope;
2729
}

src/Symfony/Component/Messenger/RoutableMessageBus.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1616
use Symfony\Component\Messenger\Stamp\BusNameStamp;
17+
use Symfony\Component\Messenger\Stamp\StampInterface;
1718

1819
/**
1920
* Bus of buses that is routable using a BusNameStamp.
@@ -37,7 +38,7 @@ public function __construct(ContainerInterface $busLocator)
3738
$this->busLoca 6D40 tor = $busLocator;
3839
}
3940

40-
public function dispatch($envelope): Envelope
41+
public function dispatch($envelope, StampInterface ...$stamps): Envelope
4142
{
4243
if (!$envelope instanceof Envelope) {
4344
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope');
@@ -53,6 +54,6 @@ public function dispatch($envelope): Envelope
5354
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStamp->getBusName()));
5455
}
5556

56-
return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope);
57+
return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope, ...$stamps);
5758
}
5859
}

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

Lines changed: 14 additions & 0 deletions
Original file l 9E88 ine numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\Messenger\MessageBus;
1717
use Symfony\Component\Messenger\MessageBusInterface;
1818
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
19+
use Symfony\Component\Messenger\Stamp\BusNameStamp;
20+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1921
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2022
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
2123
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
@@ -134,4 +136,16 @@ public function testThatAMiddlewareCanUpdateTheMessageWhileKeepingTheEnvelopeSta
134136

135137
$bus->dispatch($envelope);
136138
}
139+
140+
public function testItAddsTheStamps()
141+
{
142+
$finalEnvelope = (new MessageBus())->dispatch(new \stdClass(), new DelayStamp(5), new BusNameStamp('bar'));
143+
$this->assertCount(2, $finalEnvelope->all());
144+
}
145+
146+
public function testItAddsTheStampsToEnvelop()
147+
{
148+
$finalEnvelope = (new MessageBus())->dispatch(new Envelope(new \stdClass()), new DelayStamp(5), new BusNameStamp('bar'));
149+
$this->assertCount(2, $finalEnvelope->all());
150+
}
137151
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Messenger\MessageBusInterface;
1919
use Symfony\Component\Messenger\RoutableMessageBus;
2020
use Symfony\Component\Messenger\Stamp\BusNameStamp;
21+
use Symfony\Component\Messenger\Stamp\DelayStamp;
2122

2223
class RoutableMessageBusTest extends TestCase
2324
{
@@ -32,11 +33,12 @@ public function testItRoutesToTheCorrectBus()
3233
$container->expects($this->once())->method('has')->with('foo_bus')->willReturn(true);
3334
$container->expects($this->once())->method('get')->will($this->returnValue($bus2));
3435

36+
$stamp = new DelayStamp(5);
3537
$bus1->expects($this->never())->method('dispatch');
36-
$bus2->expects($this->once())->method('dispatch')->with($envelope)->willReturn($envelope);
38+
$bus2->expects($this->once())->method('dispatch')->with($envelope, $stamp)->willReturn($envelope);
3739

3840
$routableBus = new RoutableMessageBus($container);
39-
$this->assertSame($envelope, $routableBus->dispatch($envelope));
41+
$this->assertSame($envelope, $routableBus->dispatch($envelope, $stamp));
4042
}
4143

4244
public function testItExceptionOnMissingStamp()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\MessageBusInterface;
17+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1718
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
1819
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1920
use Symfony\Component\Messenger\TraceableMessageBus;
@@ -24,12 +25,13 @@ public function testItTracesDispatch()
2425
{
2526
$message = new DummyMessage('Hello');
2627

28+
$stamp = new DelayStamp(5);
2729
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
28-
$bus->expects($this->once())->method('dispatch')->with($message)->willReturn(new Envelope($message));
30+
$bus->expects($this->once())->method('dispatch')->with($message, $stamp)->willReturn(new Envelope($message));
2931

3032
$traceableBus = new TraceableMessageBus($bus);
3133
$line = __LINE__ + 1;
32-
$traceableBus->dispatch($message);
34+
$traceableBus->dispatch($message, $stamp);
3335
$this->assertCount(1, $tracedMessages = $traceableBus->getDispatchedMessages());
3436
$this->assertArraySubset([
3537
'message' => $message,

src/Symfony/Component/Messenger/TraceableMessageBus.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Messenger;
1313

14+
use Symfony\Component\Messenger\Stamp\StampInterface;
15+
1416
/**
1517
* @author Samuel Roze <samuel.roze@gmail.com>
1618
*
@@ -29,7 +31,7 @@ public function __construct(MessageBusInterface $decoratedBus)
2931
/**
3032
* {@inheritdoc}
3133
*/
32-
public function dispatch($message): Envelope
34+
public function dispatch($message, StampInterface ...$stamps): Envelope
3335
{
3436
$envelope = $message instanceof Envelope ? $message : new Envelope($message);
3537
$context = [
@@ -40,7 +42,7 @@ public function dispatch($message): Envelope
4042
];
4143

4244
try {
43-
return $this->decoratedBus->dispatch($message);
45+
return $this->decoratedBus->dispatch($message, ...$stamps);
4446
} catch (\Throwable $e) {
4547
$context['exception'] = $e;
4648

0 commit comments

Comments
 (0)
0