Skip to content

Commit

Permalink
[Messenger] PropertyReferenceEncodingListener support Doctrine ODM
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 3, 2024
1 parent 76d54fb commit ccb1595
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/src/Document/TestDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class TestDocument implements MessageHolderInterface
#[ODM\PrePersist]
public function raiseNewEvent(): void
{
$this->onHoldMessages[] = new NewTestDocumentMessage();
$this->onHoldMessages[] = new NewTestDocumentMessage($this);
}
}
34 changes: 33 additions & 1 deletion app/src/Message/NewTestDocumentMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

namespace App\Message;

class NewTestDocumentMessage
use App\Document\TestDocument;
use Draw\Component\Messenger\AutoStamp\Message\StampingAwareInterface;
use Draw\Component\Messenger\DoctrineEnvelopeEntityReference\Message\DoctrineReferenceAwareInterface;
use Draw\Component\Messenger\Searchable\Stamp\SearchableTagStamp;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;

class NewTestDocumentMessage implements DoctrineReferenceAwareInterface, StampingAwareInterface
{
private ?TestDocument $testDocument;

public function __construct(?TestDocument $testDocument = null)
{
$this->testDocument = $testDocument;
}

public function getTestDocument(): TestDocument
{
if (null === $this->testDocument) {
throw new UnrecoverableMessageHandlingException('testDocument is null');
}

return $this->testDocument;
}

public function getPropertiesWithDoctrineObject(): array
{
return ['testDocument'];
}

public function stamp(Envelope $envelope): Envelope
{
return $envelope->with(new SearchableTagStamp([$this->testDocument->id]));
}
}
31 changes: 19 additions & 12 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
framework:
when@test:
framework:
messenger:
failure_transport: 'failed'
transports:
sync: 'in-memory://'

framework:
messenger:
failure_transport: 'failed'

transports:
async: 'draw://default?queue_name=default'
async_high_priority: 'draw://default?queue_name=async_high_priority'
async_low_priority: 'draw://default?queue_name=async_low_priority'
failed: 'draw://default?queue_name=failed'
sync: 'sync://'
transports:
async: 'draw://default?queue_name=default'
async_high_priority: 'draw://default?queue_name=async_high_priority'
async_low_priority: 'draw://default?queue_name=async_low_priority'
failed: 'draw://default?queue_name=failed'
sync: 'sync://'

routing:
Draw\Component\Messenger\ManualTrigger\Message\ManuallyTriggeredInterface: 'async'
Draw\Bundle\UserBundle\Message\NewUserLockMessage: 'sync'
Draw\Bundle\UserBundle\Message\UserLockDelayedActivationMessage: 'sync'
routing:
App\Message\NewTestDocumentMessage: ['sync', 'async']
Draw\Component\Messenger\ManualTrigger\Message\ManuallyTriggeredInterface: 'async'
Draw\Bundle\UserBundle\Message\NewUserLockMessage: 'sync'
Draw\Bundle\UserBundle\Message\UserLockDelayedActivationMessage: 'sync'
4 changes: 0 additions & 4 deletions config/packages/test/messenger.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Component\Messenger\DoctrineEnvelopeEntityReference\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Draw\Component\Core\Reflection\ReflectionAccessor;
use Draw\Component\Messenger\DoctrineEnvelopeEntityReference\Message\DoctrineReferenceAwareInterface;
use Draw\Component\Messenger\DoctrineEnvelopeEntityReference\Stamp\PropertyReferenceStamp;
Expand All @@ -24,8 +25,10 @@ public static function getSubscribedEvents(): array
];
}

public function __construct(private ManagerRegistry $managerRegistry)
{
public function __construct(
private ?ManagerRegistry $ormManagerRegistry,
private ?ManagerRegistry $odmManagerRegistry
) {
}

public function createPropertyReferenceStamps(PreEncodeEvent $event): void
Expand Down Expand Up @@ -62,8 +65,7 @@ public function createPropertyReferenceStamps(PreEncodeEvent $event): void
null
);

$metadata = $this->managerRegistry
->getManagerForClass($object::class)
$metadata = $this->getManagerForClass($object::class)
->getClassMetadata($object::class);

$stamps[] = new PropertyReferenceStamp(
Expand All @@ -90,10 +92,22 @@ public function restoreDoctrineObjects(BaseSerializerEvent $event): void
ReflectionAccessor::setPropertyValue(
$message,
$stamp->getPropertyName(),
$this->managerRegistry
->getManagerForClass($stamp->getClass())
$this->getManagerForClass($stamp->getClass())
->find($stamp->getClass(), $stamp->getIdentifiers())
);
}
}

private function getManagerForClass(string $class): ObjectManager
{
$objectManager =
$this->ormManagerRegistry?->getManagerForClass($class)
?? $this->odmManagerRegistry?->getManagerForClass($class);

if (!$objectManager) {
throw new \RuntimeException(sprintf('No manager found for class "%s"', $class));
}

return $objectManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PropertyReferenceEncodingListenerTest extends TestCase
protected function setUp(): void
{
$this->object = new PropertyReferenceEncodingListener(
$this->createMock(ManagerRegistry::class),
$this->createMock(ManagerRegistry::class)
);
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Controller/Api/TestDocumentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public function testCreate(): void
->jsonRequest(
'POST',
'/api/test-documents',
[
]
);

static::assertResponseIsSuccessful();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Tests\Messenger\DoctrineEnvelopeEntityReference\EventListener;

use App\Document\TestDocument;
use App\Entity\User;
use App\Message\NewTestDocumentMessage;
use App\Message\NewUserMessage;
use App\Tests\TestCase;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -20,6 +22,13 @@ class PropertyReferenceEncodingListenerTest extends TestCase

private static string $email;

private EnvelopeFinderInterface $envelopeFinder;

protected function setUp(): void
{
$this->envelopeFinder = static::getContainer()->get(EnvelopeFinderInterface::class);
}

public function testSend(): void
{
$container = static::getContainer();
Expand Down Expand Up @@ -87,9 +96,7 @@ function (PostEncodeEvent $event) use ($user): void {
*/
public function testLoad(): void
{
$envelope = static::getContainer()
->get(EnvelopeFinderInterface::class)
->findByTags([self::$email])[0];
$envelope = $this->envelopeFinder->findByTags([self::$email])[0];

$message = $envelope->getMessage();

Expand All @@ -100,4 +107,26 @@ public function testLoad(): void
$message->getUser()->getEmail()
);
}

public function testODM(): void
{
$testDocument = new TestDocument();

$manager = static::getContainer()->get('doctrine_mongodb')->getManager();

$manager->persist($testDocument);

$manager->flush();

$envelope = $this->envelopeFinder->findByTags([$testDocument->id])[0];

$message = $envelope->getMessage();

static::assertInstanceOf(NewTestDocumentMessage::class, $message);

static::assertSame(
$testDocument,
$message->getTestDocument()
);
}
}

0 comments on commit ccb1595

Please sign in to comment.