Skip to content

Commit

Permalink
fix: Set messenger.transports if config has not empty transports value
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonymous committed Aug 12, 2024
1 parent abe91d4 commit a9000a0
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/DependencyInjection/BrefMessengerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ public function load(array $configs, ContainerBuilder $container): void

public function prepend(ContainerBuilder $container): void
{
$configs = $container->getExtensionConfig('framework');
$frameworkConfig = $container->getExtensionConfig('framework');
$messengerTransports = $this->getMessengerTransports($frameworkConfig);

foreach (array_reverse($configs) as $config) {
if (array_key_exists('messenger', $config)) {
$container->setParameter('messenger.transports', $config['messenger']['transports']);
}
if (! empty($messengerTransports)) {
$container->setParameter('messenger.transports', $messengerTransports);
}
}

private function getMessengerTransports(array $frameworkConfig): array
{
$transportConfigs = array_column($frameworkConfig, 'messenger.transports');
$transportConfigs = array_filter($transportConfigs);

if (empty($transportConfigs)) {
return [];
}

return array_merge_recursive(...$transportConfigs);
}
}
239 changes: 239 additions & 0 deletions tests/Unit/DependencyInjection/BrefMessengerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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' => [],
],
],
];
}
}

0 comments on commit a9000a0

Please sign in to comment.