diff --git a/src/DependencyInjection/BrefMessengerExtension.php b/src/DependencyInjection/BrefMessengerExtension.php index 2c75500..341e614 100644 --- a/src/DependencyInjection/BrefMessengerExtension.php +++ b/src/DependencyInjection/BrefMessengerExtension.php @@ -20,10 +20,11 @@ public function prepend(ContainerBuilder $container): void { $configs = $container->getExtensionConfig('framework'); - foreach (array_reverse($configs) as $config) { - if (array_key_exists('messenger', $config)) { - $container->setParameter('messenger.transports', $config['messenger']['transports']); - } + $transportConfigs = array_filter(array_column($configs, 'messenger.transports')); + + if (! empty($transportConfigs)) { + $mergedTransports = array_merge_recursive(...$transportConfigs); + $container->setParameter('messenger.transports', $mergedTransports); } } } diff --git a/tests/Unit/DependencyInjection/BrefMessengerExtensionTest.php b/tests/Unit/DependencyInjection/BrefMessengerExtensionTest.php index 24d7109..e7902a8 100644 --- a/tests/Unit/DependencyInjection/BrefMessengerExtensionTest.php +++ b/tests/Unit/DependencyInjection/BrefMessengerExtensionTest.php @@ -5,6 +5,7 @@ use Bref\Symfony\Messenger\DependencyInjection\BrefMessengerExtension; use Bref\Symfony\Messenger\Service\SimpleBusDriver; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; +use Symfony\Component\DependencyInjection\ContainerBuilder; class BrefMessengerExtensionTest extends AbstractExtensionTestCase { @@ -19,4 +20,242 @@ public function testNoConfigIsValid() $this->assertContainerBuilderHasService(SimpleBusDriver::class); } + + /** + * @dataProvider providePrependSetsMessengerTransportsParameterCases + */ + public function testPrependSetsMessengerTransportsParameter( + array $existConfig, + array $expectedTransportsParameter, + ): void { + $container = self::createMock(ContainerBuilder::class); + $container->method('getExtensionConfig') + ->with('framework') + ->willReturn($existConfig); + + $container->expects(self::atMost(2)) + ->method('setParameter') + ->with( + 'messenger.transports', + $expectedTransportsParameter + ); + + $extension = new BrefMessengerExtension; + $extension->prepend($container); + } + + public function providePrependSetsMessengerTransportsParameterCases(): iterable + { + yield 'single messenger config' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async://', + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'async' => 'async://', + ], + ]; + + yield 'multiple messenger configs' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'sync' => 'sync://', + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'async' => 'async://', + 'sync' => 'sync://', + ], + ]; + + yield 'multiple messenger configs with same transport' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async_overridden://', + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'async' => 'async_overridden://', + ], + ]; + + yield 'multiple messenger configs with different transports' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'sync' => 'sync://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => 'async_overridden://', + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'async' => 'async_overridden://', + 'sync' => 'sync://', + ], + ]; + + yield 'multiple messenger configs with different transports order' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'sync' => 'sync://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => [ + 'dsn' => 'async://', + ], + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => [ + 'dsn' => 'async_overridden://', + ], + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'sync' => 'sync://', + 'async' => [ + 'dsn' => 'async_overridden://', + ], + ], + ]; + + yield 'multiple messenger configs with different transports order and extra keys' => [ + 'existConfig' => [ + [ + 'messenger' => [ + 'transports' => [ + 'sync' => 'sync://', + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => [ + 'dsn' => 'async://', + 'options' => [ + 'queue' => 'queue', + ], + ], + ], + ], + ], + [ + 'messenger' => [ + 'transports' => [ + 'async' => [ + 'dsn' => 'async_overridden://', + ], + ], + ], + ], + ], + 'expectedTransportsParameter' => [ + 'sync' => 'sync://', + 'async' => [ + 'dsn' => 'async_overridden://', + ], + ], + ]; + } + + /** + * @dataProvider provideDoesNotSetMessengerTransportsParameterCases + */ + public function testPrependDoesNotSetMessengerTransportsParameterWhenNoMessengerConfigExists( + array $config, + ): void { + $container = self::createMock(ContainerBuilder::class); + $container->method('getExtensionConfig') + ->with('framework') + ->willReturn($config); + + $container->expects(self::never())->method('setParameter'); + + $extension = new BrefMessengerExtension; + $extension->prepend($container); + } + + public function provideDoesNotSetMessengerTransportsParameterCases(): iterable + { + yield 'empty config' => [ + 'config' => [], + ]; + + yield 'empty messenger config' => [ + 'config' => [ + 'messenger' => [], + ], + ]; + + yield 'not empty messenger config without transports key' => [ + 'config' => [ + 'messenger' => [ + 'busses' => [], + ], + ], + ]; + + yield 'not empty messenger config with empty transports key' => [ + 'config' => [ + 'messenger' => [ + 'transports' => [], + 'busses' => [], + ], + ], + ]; + } }