Skip to content

Commit

Permalink
Remove messenger transport service conditionally
Browse files Browse the repository at this point in the history
Fixes #1601
  • Loading branch information
gndk authored and ostrolucky committed Dec 30, 2022
1 parent 0421ebc commit af0d82d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
16 changes: 15 additions & 1 deletion DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1030,14 +1031,27 @@ 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;
}

$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
Expand Down
3 changes: 1 addition & 2 deletions Resources/config/messenger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
</service>

<!--
The following service isn't tagged as transport factory because the class may not exist.
The tag is added conditionally in DoctrineExtension.
The following service is removed conditionally in DoctrineExtension, if symfony/doctrine-messenger is not installed.
-->
<service id="messenger.transport.doctrine.factory" class="Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransportFactory" public="false">
<argument type="service" id="doctrine" />
Expand Down
59 changes: 59 additions & 0 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit af0d82d

Please sign in to comment.