From af0d82dc25c23af3fc2a7310fe410180dd395055 Mon Sep 17 00:00:00 2001 From: gndk <25748293+gndk@users.noreply.github.com> Date: Thu, 29 Dec 2022 14:26:55 +0100 Subject: [PATCH] Remove messenger transport service conditionally Fixes #1601 --- DependencyInjection/DoctrineExtension.php | 16 ++++- Resources/config/messenger.xml | 3 +- .../DoctrineExtensionTest.php | 59 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index 41fc9e6eb..81949edb8 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -48,6 +48,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransportFactory; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; @@ -1030,7 +1031,8 @@ protected function getMetadataDriverClass(string $driverType): string private function loadMessengerServices(ContainerBuilder $container): void { - // If the Messenger component is installed and the doctrine transaction middleware is available, wire it: + // If the Messenger component is installed, wire it: + /** @psalm-suppress UndefinedClass Optional dependency */ if (! interface_exists(MessageBusInterface::class)) { return; @@ -1038,6 +1040,18 @@ private function loadMessengerServices(ContainerBuilder $container): void $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('messenger.xml'); + + /** + * The Doctrine transport component (symfony/doctrine-messenger) is optional. + * Remove service definition, if it is not available + * + * @psalm-suppress UndefinedClass Optional dependency + */ + if (class_exists(DoctrineTransportFactory::class)) { + return; + } + + $container->removeDefinition('messenger.transport.doctrine.factory'); } private function createArrayAdapterCachePool(ContainerBuilder $container, string $objectManagerName, string $cacheName): string diff --git a/Resources/config/messenger.xml b/Resources/config/messenger.xml index a36e9ccb5..a4bd5f07a 100644 --- a/Resources/config/messenger.xml +++ b/Resources/config/messenger.xml @@ -39,8 +39,7 @@ diff --git a/Tests/DependencyInjection/DoctrineExtensionTest.php b/Tests/DependencyInjection/DoctrineExtensionTest.php index 69ba316a5..1d1d182fd 100644 --- a/Tests/DependencyInjection/DoctrineExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -49,6 +49,7 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransportFactory; use Symfony\Component\Messenger\MessageBusInterface; use function array_values; @@ -941,6 +942,64 @@ public function testMessengerIntegration(): void } } + public function testMessengerIntegrationWithDoctrineTransport(): void + { + /** @psalm-suppress UndefinedClass */ + if (! interface_exists(MessageBusInterface::class)) { + $this->markTestSkipped('Symfony Messenger component is not installed'); + } + + /** @psalm-suppress UndefinedClass */ + if (! class_exists(DoctrineTransportFactory::class)) { + $this->markTestSkipped('This test requires Symfony Messenger Doctrine transport to be installed'); + } + + $container = $this->getContainer(); + $extension = new DoctrineExtension(); + + $config = BundleConfigurationBuilder::createBuilder() + ->addBaseConnection() + ->build(); + $extension->load([$config], $container); + + $this->assertTrue($container->hasDefinition('messenger.transport.doctrine.factory')); + + $messengerTransportDoctrineFactory = $container->getDefinition('messenger.transport.doctrine.factory'); + + $this->assertCount(1, $messengerTransportDoctrineFactory->getArguments()); + $this->assertSame('doctrine', (string) $messengerTransportDoctrineFactory->getArgument(0)); + + /** @psalm-suppress UndefinedClass */ + $this->assertSame(DoctrineTransportFactory::class, $messengerTransportDoctrineFactory->getClass()); + + $this->assertTrue($messengerTransportDoctrineFactory->hasTag('messenger.transport_factory')); + $this->assertContains('messenger.transport_factory', $container->findTags()); + } + + public function testMessengerIntegrationWithoutDoctrineTransport(): void + { + /** @psalm-suppress UndefinedClass */ + if (! interface_exists(MessageBusInterface::class)) { + $this->markTestSkipped('Symfony Messenger component is not installed'); + } + + /** @psalm-suppress UndefinedClass */ + if (class_exists(DoctrineTransportFactory::class)) { + $this->markTestSkipped('This test requires Symfony Messenger Doctrine transport to not be installed'); + } + + $container = $this->getContainer(); + $extension = new DoctrineExtension(); + + $config = BundleConfigurationBuilder::createBuilder() + ->addBaseConnection() + ->build(); + $extension->load([$config], $container); + + $this->assertFalse($container->hasDefinition('messenger.transport.doctrine.factory')); + $this->assertNotContains('messenger.transport_factory', $container->findTags()); + } + /** @group legacy */ public function testInvalidCacheConfiguration(): void {