Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Pass envelope to message handler as second __invoke parameter #42005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: Ubuntu-20.04

env:
extensions: amqp,apcu,igbinary,intl,mbstring,memcached,mongodb,redis
extensions: amqp,apcu,igbinary,intl,mbstring,memcached,mongodb,redis-5.3.4

strategy:
matrix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testFromDsnWithTls()
$redis->expects($this->once())
->method('connect')
->with('tls://127.0.0.1', 6379)
->willReturn(null);
->willReturn(true);

Connection::fromDsn('redis://127.0.0.1?tls=1', [], $redis);
}
Expand All @@ -92,7 +92,7 @@ public function testFromDsnWithTlsOption()
$redis->expects($this->once())
->method('connect')
->with('tls://127.0.0.1', 6379)
->willReturn(null);
->willReturn(true);

Connection::fromDsn('redis://127.0.0.1', ['tls' => true], $redis);
}
Expand All @@ -103,7 +103,7 @@ public function testFromDsnWithRedissScheme()
$redis->expects($this->once())
->method('connect')
->with('tls://127.0.0.1', 6379)
->willReturn(null);
->willReturn(true);

Connection::fromDsn('rediss://127.0.0.1?delete_after_ack=true', [], $redis);
}
Expand Down Expand Up @@ -315,7 +315,7 @@ public function testMaxEntries()

$redis->expects($this->exactly(1))->method('xadd')
->with('queue', '*', ['message' => '{"body":"1","headers":[]}'], 20000, true)
->willReturn(1);
->willReturn('1');

$connection = Connection::fromDsn('redis://localhost/queue?stream_max_entries=20000', ['delete_after_ack' => true], $redis);
$connection->add('1', []);
Expand Down Expand Up @@ -365,7 +365,7 @@ public function testLastErrorGetsCleared()
{
$redis = $this->createMock(\Redis::class);

$redis->expects($this->once())->method('xadd')->willReturn(0);
$redis->expects($this->once())->method('xadd')->willReturn('0');
$redis->expects($this->once())->method('xack')->willReturn(0);

$redis->method('getLastError')->willReturnOnConsecutiveCalls('xadd error', 'xack error');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker.
* Add support for resetting container services after each messenger message.
* Add message envelope to handler `__invoke()` calls

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope

try {
$handler = $handlerDescriptor->getHandler();
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message));
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message, $envelope));
$envelope = $envelope->with($handledStamp);
$this->logger->info('Message {class} handled by {handler}', $context + ['handler' => $handledStamp->getHandlerName()]);
} catch (\Throwable $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public function testItCallsTheHandlerAndNextMiddleware()
$middleware->handle($envelope, $this->getStackMock());
}

public function testHandlerWithEnvelopeInConstructor()
{
$message = new DummyMessage('Hey');
$envelope = new Envelope($message);

$handler = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']);

$middleware = new HandleMessageMiddleware(new HandlersLocator([
DummyMessage::class => [$handler],
]));

$handler->expects($this->once())->method('__invoke')->with($message, $envelope);

$middleware->handle($envelope, $this->getStackMock());
}

/**
* @dataProvider itAddsHandledStampsProvider
*/
Expand Down