8000 minor #27841 [Messenger] Envelope-aware middleware is never called wi… · symfony/symfony@4b92b96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b92b96

Browse files
committed
minor #27841 [Messenger] Envelope-aware middleware is never called with a message (Cydonia7)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Envelope-aware middleware is never called with a message | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | yes <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | no Messenger components middlewares implementing `Symfony\Component\Messenger\EnvelopeAwareInterface` receive in their handle method an instance of `Symfony\Component\Messenger\Envelope`, not a message. To better reflect the expected usage, I've updated the unit tests for the message middleware and fixed the code accordingly. Commits ------- 9488e2a [Messenger] Envelope-aware middleware is never called with a message
2 parents c1f23c8 + 9488e2a commit 4b92b96

File tree

6 files changed

+41
-30
lines changed

6 files changed

+41
-30
lines changed

UPGRADE-4.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ FrameworkBundle
8383
this will generate 404s for non-UTF-8 URLs, which are incompatible with you app anyway,
8484
and will allow dumping optimized routers and using Unicode classes in requirements.
8585

86+
Messenger
87+
---------
88+
89+
* The `handle` method of the `Symfony\Component\Messenger\Middleware\ValidationMiddleware` and `Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware` middlewares now requires an `Envelope` object to be given (because they implement the `EnvelopeAwareInterface`). When using these middleware with the provided `MessageBus`, you will not have to do anything. If you use the middlewares any other way, you can use `Envelope::wrap($message)` to create an envelope for your message.
90+
8691
Security
8792
--------
8893

src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3434
}
3535

3636
/**
37+
* @param Envelope $envelope
38+
*
3739
* {@inheritdoc}
3840
*/
39-
public function handle($message, callable $next)
41+
public function handle($envelope, callable $next)
4042
{
41-
$envelope = Envelope::wrap($message);
4243
if ($envelope->get(ReceivedMessage::class)) {
4344
// It's a received message. Do not send it back:
44-
return $next($message);
45+
return $next($envelope);
4546
}
4647

4748
$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage());
@@ -54,7 +55,7 @@ public function handle($message, callable $next)
5455
}
5556
}
5657

57-
return $next($message);
58+
return $next($envelope);
5859
}
5960

6061
private function mustSendAndHandle($message): bool

src/Symfony/Component/Messenger/Middleware/Enhancers/ActivationMiddlewareDecorator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3737
/**
3838
* @param Envelope $message
3939
*/
40-
public function handle($message, callable $next)
40+
public function handle($envelope, callable $next)
4141
{
42-
if (\is_callable($this->activated) ? ($this->activated)($message) : $this->activated) {
43-
return $this->inner->handle($message->getMessageFor($this->inner), $next);
42+
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
43+
return $this->inner->handle($envelope->getMessageFor($this->inner), $next);
4444
}
4545

46-
return $next($message);
46+
return $next($envelope);
4747
}
4848
}

src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Messenger\Middleware;
1313

14-
use Symfony\Component\Messenger\Envelope;
1514
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1615
use Symfony\Component\Messenger\Exception\ValidationFailedException;
1716
use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration;
@@ -29,21 +28,20 @@ public function __construct(ValidatorInterface $validator)
2928
$this->validator = $validator;
3029
}
3130

32-
public function handle($message, callable $next)
31+
public function handle($envelope, callable $next)
3332
{
34-
$envelope = Envelope::wrap($message);
35-
$subject = $envelope->getMessage();
33+
$message = $envelope->getMessage();
3634
$groups = null;
3735
/** @var ValidationConfiguration|null $validationConfig */
3836
if ($validationConfig = $envelope->get(ValidationConfiguration::class)) {
3937
$groups = $validationConfig->getGroups();
4038
}
4139

42-
$violations = $this->validator->validate($subject, null, $groups);
40+
$violations = $this->validator->validate($message, null, $groups);
4341
if (\count($violations)) {
44-
throw new ValidationFailedException($subject, $violations);
42+
throw new ValidationFailedException($message, $violations);
4543
}
4644

47-
return $next($message);
45+
return $next($envelope);
4846
}
4947
}

src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ class SendMessageMiddlewareTest extends TestCase
2626
public function testItSendsTheMessageToAssignedSender()
2727
{
2828
$message = new DummyMessage('Hey');
29+
$envelope = Envelope::wrap($message);
2930
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
3031
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
3132

3233
$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender));
3334

34-
$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
35+
$sender->expects($this->once())->method('send')->with($envelope);
3536
$next->expects($this->never())->method($this->anything());
3637

37-
$middleware->handle($message, $next);
38+
$middleware->handle($envelope, $next);
3839
}
3940

4041
public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()
@@ -54,77 +55,82 @@ public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()
5455
public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageClass()
5556
{
5657
$message = new DummyMessage('Hey');
58+
$envelope = Envelope::wrap($message);
5759
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
5860
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
5961

6062
$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
6163
DummyMessage::class => true,
6264
));
6365

64-
$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
66+
$sender->expects($this->once())->method('send')->with($envelope);
6567
$next->expects($this->once())->method($this->anything());
6668

67-
$middleware->handle($message, $next);
69+
$middleware->handle($envelope, $next);
6870
}
6971

7072
public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageParentClass()
7173
{
7274
$message = new ChildDummyMessage('Hey');
75+
$envelope = Envelope::wrap($message);
7376
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
7477
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
7578

7679
$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
7780
DummyMessage::class => true,
7881
));
7982

80-
$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
83+
$sender->expects($this->once())->method('send')->with($envelope);
8184
$next->expects($this->once())->method($this->anything());
8285

83-
$middleware->handle($message, $next);
86+
$middleware->handle($envelope, $next);
8487
}
8588

8689
public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageInterface()
8790
{
8891
$message = new DummyMessage('Hey');
92+
$envelope = Envelope::wrap($message);
8993
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
9094
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
9195

9296
$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
9397
DummyMessageInterface::class => true,
9498
));
9599

96-
$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
100+
$sender->expects($this->once())->method('send')->with($envelope);
97101
$next->expects($this->once())->method($this->anything());
98102

99-
$middleware->handle($message, $next);
103+
$middleware->handle($envelope, $next);
100104
}
101105

102106
public function testItAlsoCallsTheNextMiddlewareBasedOnWildcard()
103107
{
104108
$message = new DummyMessage('Hey');
109+
$envelope = Envelope::wrap($message);
105110
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
106111
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
107112

108113
$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
109114
'*' => true,
110115
));
111116

112-
$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
117+
$sender->expects($this->once())->method('send')->with($envelope);
113118
$next->expects($this->once())->method($this->anything());
114119

115-
$middleware->handle($message, $next);
120+
$middleware->handle($envelope, $next);
116121
}
117122

118123
public function testItCallsTheNextMiddlewareWhenNoSenderForThisMessage()
119124
{
120125
$message = new DummyMessage('Hey');
126+
$envelope = Envelope::wrap($message);
121127
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
122128

123129
$middleware = new SendMessageMiddleware(new InMemorySenderLocator(null));
124130

125131
$next->expects($this->once())->method($this->anything());
126132

127-
$middleware->handle($message, $next);
133+
$middleware->handle($envelope, $next);
128134
}
129135

130136
public function testItSkipsReceivedMessages()

src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ValidationMiddlewareTest extends TestCase
2424
public function testValidateAndNextMiddleware()
2525
{
2626
$message = new DummyMessage('Hey');
27+
$envelope = Envelope::wrap($message);
2728

2829
$validator = $this->createMock(ValidatorInterface::class);
2930
$validator
@@ -36,19 +37,18 @@ public function testValidateAndNextMiddleware()
3637
$next
3738
->expects($this->once())
3839
->method('__invoke')
39-
->with($message)
40+
->with($envelope)
4041
->willReturn('Hello')
4142
;
4243

43-
$result = (new ValidationMiddleware($validator))->handle($message, $next);
44+
$result = (new ValidationMiddleware($validator))->handle($envelope, $next);
4445

4546
$this->assertSame('Hello', $result);
4647
}
4748

4849
public function testValidateWithConfigurationAndNextMiddleware()
4950
{
5051
$envelope = Envelope::wrap($message = new DummyMessage('Hey'))->with(new ValidationConfiguration($groups = array('Default', 'Extra')));
51-
5252
$validator = $this->createMock(ValidatorInterface::class);
5353
$validator
5454
->expects($this->once())
@@ -76,6 +76,7 @@ public function testValidateWithConfigurationAndNextMiddleware()
7676
public function testValidationFailedException()
7777
{
7878
$message = new DummyMessage('Hey');
79+
$envelope = Envelope::wrap($message);
7980

8081
$violationList = $this->createMock(ConstraintViolationListInterface::class);
8182
$violationList
@@ -96,6 +97,6 @@ public function testValidationFailedException()
9697
->method('__invoke')
9798
;
9899

99-
(new ValidationMiddleware($validator))->handle($message, $next);
100+
(new ValidationMiddleware($validator))->handle($envelope, $next);
100101
}
101102
}

0 commit comments

Comments
 (0)
0