diff --git a/app/src/Document/TestDocument.php b/app/src/Document/TestDocument.php index 3112e2e79..e6e1558eb 100644 --- a/app/src/Document/TestDocument.php +++ b/app/src/Document/TestDocument.php @@ -19,6 +19,6 @@ class TestDocument implements MessageHolderInterface #[ODM\PrePersist] public function raiseNewEvent(): void { - $this->onHoldMessages[] = new NewTestDocumentMessage(); + $this->onHoldMessages[] = new NewTestDocumentMessage($this); } } diff --git a/app/src/Message/NewTestDocumentMessage.php b/app/src/Message/NewTestDocumentMessage.php index 302b83fe1..36003b1a1 100644 --- a/app/src/Message/NewTestDocumentMessage.php +++ b/app/src/Message/NewTestDocumentMessage.php @@ -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])); + } } diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index 89a866fc3..d0a403a28 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -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' diff --git a/config/packages/test/messenger.yaml b/config/packages/test/messenger.yaml deleted file mode 100644 index 6e45c785c..000000000 --- a/config/packages/test/messenger.yaml +++ /dev/null @@ -1,4 +0,0 @@ -framework: - messenger: - transports: - sync: 'in-memory://' diff --git a/packages/messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListener.php b/packages/messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListener.php index 15f2c6ab1..bcb1a3131 100644 --- a/packages/messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListener.php +++ b/packages/messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListener.php @@ -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; @@ -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 @@ -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( @@ -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; + } } diff --git a/packages/messenger/Tests/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php b/packages/messenger/Tests/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php index 2cd0325b9..9ed715222 100644 --- a/packages/messenger/Tests/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php +++ b/packages/messenger/Tests/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php @@ -17,6 +17,7 @@ class PropertyReferenceEncodingListenerTest extends TestCase protected function setUp(): void { $this->object = new PropertyReferenceEncodingListener( + $this->createMock(ManagerRegistry::class), $this->createMock(ManagerRegistry::class) ); } diff --git a/tests/Controller/Api/TestDocumentControllerTest.php b/tests/Controller/Api/TestDocumentControllerTest.php index de4e39f9a..85ab6c20e 100644 --- a/tests/Controller/Api/TestDocumentControllerTest.php +++ b/tests/Controller/Api/TestDocumentControllerTest.php @@ -16,8 +16,6 @@ public function testCreate(): void ->jsonRequest( 'POST', '/api/test-documents', - [ - ] ); static::assertResponseIsSuccessful(); diff --git a/tests/Messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php b/tests/Messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php index e160b6e9e..7829f19d7 100644 --- a/tests/Messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php +++ b/tests/Messenger/DoctrineEnvelopeEntityReference/EventListener/PropertyReferenceEncodingListenerTest.php @@ -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; @@ -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(); @@ -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(); @@ -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() + ); + } }