-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Deprecate HandleTrait
in favor of a new SingleHandlingTrait
#52952
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\LogicException; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
trait SingleHandlingTrait | ||
{ | ||
private readonly MessageBusInterface $messageBus; | ||
|
||
/** | ||
* Dispatches the given message, expecting to be handled by a single handler | ||
* and returns the result from the handler returned value. | ||
* This behavior is useful for both synchronous command & query buses, | ||
* the last one usually returning the handler result. | ||
* | ||
* @param object|Envelope $message The message or the message pre-wrapped in an envelope | ||
MatTheCat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param StampInterface[] $stamps | ||
*/ | ||
private function handle(object $message, array $stamps = []): mixed | ||
{ | ||
if (!isset($this->messageBus)) { | ||
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)); | ||
} | ||
|
||
$exceptions = []; | ||
|
||
try { | ||
$envelope = $this->messageBus->dispatch($message, $stamps); | ||
} catch (HandlerFailedException $exception) { | ||
$envelope = $exception->getEnvelope(); | ||
$exceptions = $exception->getWrappedExceptions(); | ||
} | ||
|
||
/** @var HandledStamp[] $handledStamps */ | ||
$handledStamps = $envelope->all(HandledStamp::class); | ||
|
||
$handlers = array_merge( | ||
array_map(static fn (HandledStamp $stamp) => $stamp->getHandlerName(), $handledStamps), | ||
array_keys($exceptions), | ||
); | ||
|
||
if (!$handlers) { | ||
throw new LogicException(sprintf('Message of type "%s" was handled zero times. Exactly one handler is expected when using "%s::%s()".', $envelope->getMessage()::class, static::class, __FUNCTION__)); | ||
} | ||
|
||
if (\count($handlers) > 1) { | ||
throw new LogicException(sprintf('Message of type "%s" was handled multiple times. Only one handler is expected when using "%s::%s()", got %d: "%s".', $envelope->getMessage()::class, static::class, __FUNCTION__, \count($handlers), implode('", "', $handlers))); | ||
} | ||
|
||
if ($exceptions) { | ||
throw reset($exceptions); | ||
} | ||
|
||
return $handledStamps[0]->getResult(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Messenger/Tests/Fixtures/TestTracesWithSingleHandlingTraitAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Fixtures; | ||
|
||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\SingleHandlingTrait; | ||
|
||
/** | ||
* @see \Symfony\Component\Messenger\Tests\TraceableMessageBusTest::testItTracesDispatchWhenSingleHandlingTraitIsUsed | ||
*/ | ||
class TestTracesWithSingleHandlingTraitAction | ||
{ | ||
use SingleHandlingTrait; | ||
|
||
public function __construct(MessageBusInterface $messageBus) | ||
{ | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function __invoke($message) | ||
{ | ||
$this->handle($message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/Symfony/Component/Messenger/Tests/SingleHandlingTraitTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\LogicException; | ||
use Symfony\Component\Messenger\MessageBus; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\SingleHandlingTrait; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
|
||
class SingleHandlingTraitTest extends TestCase | ||
{ | ||
public function testItThrowsOnNoMessageBusInstance() | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('You must provide a "Symfony\Component\Messenger\MessageBusInterface" instance in the "Symfony\Component\Messenger\Tests\SingleHandlerBus::$messageBus" property, but that property has not been initialized yet.'); | ||
$singleHandlerBus = new SingleHandlerBus(null); | ||
$message = new DummyMessage('Hello'); | ||
|
||
$singleHandlerBus->dispatch($message); | ||
} | ||
|
||
public function testHandleReturnsHandledStampResult() | ||
{ | ||
$bus = $this->createMock(MessageBus::class); | ||
$singleHandlerBus = new SingleHandlerBus($bus); | ||
|
||
$message = new DummyMessage('Hello'); | ||
$bus->expects($this->once())->method('dispatch')->willReturn( | ||
new Envelope($message, [new HandledStamp('result', 'DummyHandler::__invoke')]) | ||
); | ||
|
||
$this->assertSame('result', $singleHandlerBus->dispatch($message)); | ||
} | ||
|
||
public function testHandleAcceptsEnvelopes() | ||
{ | ||
$bus = $this->createMock(MessageBus::class); | ||
$singleHandlerBus = new SingleHandlerBus($bus); | ||
|
||
$envelope = new Envelope(new DummyMessage('Hello'), [new HandledStamp('result', 'DummyHandler::__invoke')]); | ||
$bus->expects($this->once())->method('dispatch')->willReturn($envelope); | ||
|
||
$this->assertSame('result', $singleHandlerBus->dispatch($envelope)); | ||
} | ||
|
||
public function testHandleThrowsOnNoHandledStamp() | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" was handled zero times. Exactly one handler is expected when using "Symfony\Component\Messenger\Tests\SingleHandlerBus::handle()".'); | ||
$bus = $this->createMock(MessageBus::class); | ||
$singleHandlerBus = new SingleHandlerBus($bus); | ||
|
||
$message = new DummyMessage('Hello'); | ||
$bus->expects($this->once())->method('dispatch')->willReturn(new Envelope($message)); | ||
|
||
$singleHandlerBus->dispatch($message); | ||
} | ||
|
||
public function testHandleThrowsOnMultipleHandledStamps() | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" was handled multiple times. Only one handler is expected when using "Symfony\Component\Messenger\Tests\SingleHandlerBus::handle()", got 2: "FirstDummyHandler::__invoke", "SecondDummyHandler::__invoke".'); | ||
$bus = $this->createMock(MessageBus::class); | ||
$singleHandlerBus = new SingleHandlerBus($bus); | ||
|
||
$message = new DummyMessage('Hello'); | ||
$bus->expects($this->once())->method('dispatch')->willThrowException( | ||
new HandlerFailedException( | ||
new Envelope($message, [new HandledStamp('first_result', 'FirstDummyHandler::__invoke')]), | ||
['SecondDummyHandler::__invoke' => new \RuntimeException('SecondDummyHandler failed.')] | ||
) | ||
); | ||
|
||
$singleHandlerBus->dispatch($message); | ||
} | ||
|
||
public function testHandleThrowsWrappedException() | ||
{ | ||
$bus = $this->createMock(MessageBus::class); | ||
$singleHandlerBus = new SingleHandlerBus($bus); | ||
|
||
$message = new DummyMessage('Hello'); | ||
$wrappedException = new \RuntimeException('Handler failed.'); | ||
$bus->expects($this->once())->method('dispatch')->willThrowException( | ||
new HandlerFailedException( | ||
new Envelope($message), | ||
['DummyHandler::__invoke' => new \RuntimeException('Handler failed.')] | ||
) | ||
); | ||
|
||
$this->expectException($wrappedException::class); | ||
$this->expectExceptionMessage($wrappedException->getMessage()); | ||
|
||
$singleHandlerBus->dispatch($message); | ||
} | ||
} | ||
|
||
class SingleHandlerBus | ||
{ | ||
use SingleHandlingTrait; | ||
|
||
public function __construct(?MessageBusInterface $messageBus) | ||
{ | ||
if ($messageBus) { | ||
$this->messageBus = $messageBus; | ||
} | ||
} | ||
|
||
public function dispatch($message): string | ||
{ | ||
return $this->handle($message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fact is we're querying for a value, hence i prefer naming
QueryBusTrait::query
, but not a blocker :)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t want this trait’s name to imply a single use-case as there can be many (a command bus returning IDs e.g.).
EDIT: heh, just found #29167 (comment) 😁