-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge][Messenger] Add MessageRecordingEntity functionality #34310
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad28854
Added MessageRecordingEntity functionality
vudaltsov acfc5ba
Removed strict types in MessageRecordingEntity fixture
vudaltsov 438599d
Added a standard phpdoc header to the MessageRecordingEntity fixture
vudaltsov a704101
Renamed the subscriber
vudaltsov 4a289b5
Added a hook before the message is dispatched to allow to modify the …
vudaltsov 109fec3
Fixed the DispatchEntityMessagesDoctrineSubscriberTest
vudaltsov e70edbf
Clear messages before calling the dispatcher
vudaltsov 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
67 changes: 67 additions & 0 deletions
67
src/Symfony/Bridge/Doctrine/Messenger/DispatchEntityMessagesDoctrineSubscriber.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,67 @@ | ||
<?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\Bridge\Doctrine\Messenger; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\PostFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> | ||
*/ | ||
final class DispatchEntityMessagesDoctrineSubscriber implements EventSubscriber | ||
{ | ||
private $bus; | ||
private $dispatcher; | ||
|
||
public function __construct(MessageBusInterface $bus, EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->bus = $bus; | ||
$this->dispatcher = $dispatcher; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
Events::postFlush, | ||
]; | ||
} | ||
|
||
public function postFlush(PostFlushEventArgs $args): void | ||
{ | ||
foreach ($args->getEntityManager()->getUnitOfWork()->getIdentityMap() as $entities) { | ||
foreach ($entities as $entity) { | ||
if (!$entity instanceof MessageRecordingEntityInterface) { | ||
continue; | ||
} | ||
|
||
$entity->dispatchMessages(function (array $messages) use ($entity): void { | ||
foreach ($messages as $message) { | ||
$envelope = Envelope::wrap($message, [new DispatchAfterCurrentBusStamp()]); | ||
$event = new EntityMessagePreDispatchEvent($entity, $envelope); | ||
$this->dispatcher->dispatch($event); | ||
$this->bus->dispatch($event->getEnvelope()); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bridge/Doctrine/Messenger/EntityMessagePreDispatchEvent.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,41 @@ | ||
<?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\Bridge\Doctrine\Messenger; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
|
||
final class EntityMessagePreDispatchEvent | ||
{ | ||
private $entity; | ||
private $envelope; | ||
|
||
public function __construct(object $entity, Envelope $envelope) | ||
{ | ||
$this->entity = $entity; | ||
$this->envelope = $envelope; | ||
} | ||
|
||
public function getEntity(): object | ||
{ | ||
return $this->entity; | ||
} | ||
|
||
public function getEnvelope(): Envelope | ||
{ | ||
return $this->envelope; | ||
} | ||
|
||
public function setEnvelope(Envelope $envelope): void | ||
{ | ||
$this->envelope = $envelope; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Bridge/Doctrine/Messenger/MessageRecordingEntityInterface.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,25 @@ | ||
<?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\Bridge\Doctrine\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> | ||
*/ | ||
interface MessageRecordingEntityInterface | ||
{ | ||
/** | ||
* @param callable $dispatcher callable(object[] $messages): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably need some phpdoc explanation on the purpose / usage of this method. |
||
*/ | ||
public function dispatchMessages(callable $dispatcher): void; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Bridge/Doctrine/Messenger/MessageRecordingEntityTrait.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,39 @@ | ||
<?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\Bridge\Doctrine\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> | ||
*/ | ||
trait MessageRecordingEntityTrait | ||
{ | ||
/** | ||
* @var object[] | ||
vudaltsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private $messages = []; | ||
|
||
/** | ||
* @see MessageRecordingEntityInterface::dispatchMessages() | ||
*/ | ||
final public function dispatchMessages(callable $dispatcher): void | ||
{ | ||
[$messages, $this->messages] = [$this->messages, []]; | ||
$dispatcher($messages); | ||
} | ||
|
||
final protected function recordMessage(object $message): void | ||
{ | ||
$this->messages[] = $message; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Bridge/Doctrine/Tests/Fixtures/MessageRecordingEntity.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,25 @@ | ||
<?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\Bridge\Doctrine\Tests\Fixtures; | ||
|
||
use Symfony\Bridge\Doctrine\Messenger\MessageRecordingEntityInterface; | ||
use Symfony\Bridge\Doctrine\Messenger\MessageRecordingEntityTrait; | ||
|
||
final class MessageRecordingEntity implements MessageRecordingEntityInterface | ||
{ | ||
use MessageRecordingEntityTrait; | ||
|
||
public function doRecordMessage(object $message): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a public method (by default) in the entity? |
||
{ | ||
$this->recordMessage($message); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/Symfony/Bridge/Doctrine/Tests/Messenger/DispatchEntityMessagesDoctrineSubscriberTest.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,98 @@ | ||
<?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\Bridge\Doctrine\Tests\Messenger; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Event\PostFlushEventArgs; | ||
use Doctrine\ORM\UnitOfWork; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Messenger\DispatchEntityMessagesDoctrineSubscriber; | ||
use Symfony\Bridge\Doctrine\Messenger\EntityMessagePreDispatchEvent; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\MessageRecordingEntity; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
class DispatchEntityMessagesDoctrineSubscriberTest extends TestCase | ||
{ | ||
public function testMessagesAreDispatched(): void | ||
{ | ||
$entity = new MessageRecordingEntity(); | ||
$message1 = new \stdClass(); | ||
$message2 = new \stdClass(); | ||
$message2->a = 1; | ||
$entity->doRecordMessage($message1); | ||
$entity->doRecordMessage($message2); | ||
|
||
$bus = $this->createMock(MessageBusInterface::class); | ||
$bus | ||
->expects(static::exactly(2)) | ||
->method('dispatch') | ||
->withConsecutive( | ||
[Envelope::wrap($message1, [new DispatchAfterCurrentBusStamp()])], | ||
[Envelope::wrap($message2, [new DispatchAfterCurrentBusStamp()])] | ||
) | ||
->willReturn(new Envelope(new \stdClass())) | ||
; | ||
|
||
$dispatcher = $this->createMock(EventDispatcherInterface::class); | ||
|
||
$args = $this->createPostFlushArgs([$entity]); | ||
|
||
$subscriber = new DispatchEntityMessagesDoctrineSubscriber($bus, $dispatcher); | ||
$subscriber->postFlush($args); | ||
} | ||
|
||
public function testEventIsDispatched(): void | ||
{ | ||
$entity = new MessageRecordingEntity(); | ||
$message = new \stdClass(); | ||
$entity->doRecordMessage($message); | ||
|
||
$bus = $this->createMock(MessageBusInterface::class); | ||
$bus->method('dispatch')->willReturn(new Envelope(new \stdClass())); | ||
|
||
$dispatcher = $this->createMock(EventDispatcherInterface::class); | ||
$dispatcher | ||
->expects(static::once()) | ||
->method('dispatch') | ||
->with(new EntityMessagePreDispatchEvent($entity, Envelope::wrap($message, [ | ||
new DispatchAfterCurrentBusStamp(), | ||
]))) | ||
; | ||
|
||
$args = $this->createPostFlushArgs([$entity]); | ||
|
||
$subscriber = new DispatchEntityMessagesDoctrineSubscriber($bus, $dispatcher); | ||
$subscriber->postFlush($args); | ||
} | ||
|
||
private function createPostFlushArgs(array $entities): PostFlushEventArgs | ||
{ | ||
$uow = $this->createMock(UnitOfWork::class); | ||
$uow | ||
->expects(static::once()) | ||
->method('getIdentityMap') | ||
->willReturn([$entities]) | ||
; | ||
|
||
$em = $this->createMock(EntityManagerInterface::class); | ||
$em | ||
->expects(static::once()) | ||
->method('getUnitOfWork') | ||
->willReturn($uow) | ||
; | ||
|
||
return new PostFlushEventArgs($em); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Symfony/Bridge/Doctrine/Tests/Messenger/MessageRecordingEntityTraitTest.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,60 @@ | ||
<?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\Bridge\Doctrine\Tests\Messenger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\MessageRecordingEntity; | ||
|
||
class MessageRecordingEntityTraitTest extends TestCase | ||
{ | ||
public function testDispatch(): void | ||
{ | ||
$entity = new MessageRecordingEntity(); | ||
$message = new \stdClass(); | ||
|
||
$entity->doRecordMessage($message); | ||
|
||
$entity->dispatchMessages(static function (array $messages) use ($message): void { | ||
static::assertSame([$message], $messages); | ||
}); | ||
} | ||
|
||
public function testMessagesClearedAfterDispatch(): void | ||
{ | ||
$entity = new MessageRecordingEntity(); | ||
$entity->doRecordMessage(new \stdClass()); | ||
|
||
$entity->dispatchMessages(static function (): void { | ||
}); | ||
|
||
$entity->dispatchMessages(static function (array $messages): void { | ||
static::assertCount(0, $messages); | ||
}); | ||
} | ||
|
||
public function testMessagesClearedOnDispatchFailure(): void | ||
{ | ||
$entity = new MessageRecordingEntity(); | ||
$entity->doRecordMessage(new \stdClass()); | ||
|
||
try { | ||
$entity->dispatchMessages(static function (): void { | ||
throw new \Exception(); | ||
}); | ||
} catch (\Exception $exception) { | ||
} | ||
|
||
$entity->dispatchMessages(static function (array $messages): void { | ||
static::assertCount(0, $messages); | ||
}); | ||
} | ||
} |
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.
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 quite understand the naming of this class. Can you explain? This seems to me like a ...
EntityMessageEvent
- but I don't understand the "Pre" part - it happenspostFlush()
.