From a05e2e2c3b996cfead88c0fb6677684e5174e44f Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 27 Apr 2018 23:07:51 -0400 Subject: [PATCH] Relax Messenger config and fix some bugs --- .../DependencyInjection/Configuration.php | 2 +- .../DependencyInjection/FrameworkExtension.php | 11 +++++++++-- .../DependencyInjection/ConfigurationTest.php | 2 +- .../Fixtures/php/messenger.php | 1 + .../messenger_amqp_transport_no_serializer.php | 12 ++++++++++++ .../Fixtures/php/messenger_transport.php | 1 + .../Fixtures/php/messenger_transports.php | 1 + .../Fixtures/php/serializer_disabled.php | 3 +++ .../Fixtures/xml/messenger.xml | 1 + .../messenger_amqp_transport_no_serializer.xml | 14 ++++++++++++++ .../Fixtures/xml/messenger_transport.xml | 1 + .../Fixtures/xml/messenger_transports.xml | 1 + .../Fixtures/xml/serializer_disabled.xml | 3 +++ .../Fixtures/yml/messenger.yml | 1 + .../messenger_amqp_transport_no_serializer.yml | 5 +++++ .../Fixtures/yml/messenger_transport.yml | 1 + .../Fixtures/yml/messenger_transports.yml | 1 + .../Fixtures/yml/serializer_disabled.yml | 2 ++ .../FrameworkExtensionTest.php | 16 ++++++++++++++-- 19 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_amqp_transport_no_serializer.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_amqp_transport_no_serializer.xml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_amqp_transport_no_serializer.yml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index c0ab2cc766..f46397e57c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -1010,7 +1010,7 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->arrayNode('serializer') - ->canBeDisabled() + ->{!class_exists(FullStack::class) && class_exists(Serializer::class) ? 'canBeDisabled' : 'canBeEnabled'}() ->addDefaultsIfNotSet() ->children() ->scalarNode('format')->defaultValue('json')->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 806d8d729b..b1a1e8eb5d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1449,8 +1449,8 @@ class FrameworkExtension extends Extension $loader->load('messenger.xml'); if ($this->isConfigEnabled($container, $config['serializer'])) { - if (\count($config['transports']) > 0 && !$this->isConfigEnabled($container, $serializerConfig)) { - throw new LogicException('Using the default encoder/decoder, Symfony Messenger requires the Serializer. Enable it or install it by running "composer require symfony/serializer-pack".'); + if (!$this->isConfigEnabled($container, $serializerConfig)) { + throw new LogicException('The default Messenger serializer cannot be enabled as the Serializer support is not available. Try enable it or install it by running "composer require symfony/serializer-pack".'); } $container->getDefinition('messenger.transport.serializer') @@ -1458,6 +1458,9 @@ class FrameworkExtension extends Extension ->replaceArgument(2, $config['serializer']['context']); } else { $container->removeDefinition('messenger.transport.serializer'); + if ('messenger.transport.serializer' === $config['encoder'] || 'messenger.transport.serializer' === $config['decoder']) { + $container->removeDefinition('messenger.transport.amqp.factory'); + } } $container->setAlias('messenger.transport.encoder', $config['encoder']); @@ -1506,6 +1509,10 @@ class FrameworkExtension extends Extension $container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $messageToSenderIdsMapping); foreach ($config['transports'] as $name => $transport) { + if (0 === strpos($transport['dsn'], 'amqp://') && !$container->hasDefinition('messenger.transport.amqp.factory')) { + throw new LogicException('The default AMQP transport is not available. Make sure you have installed and enabled the Serializer component. Try enable it or install it by running "composer require symfony/serializer-pack".'); + } + $senderDefinition = (new Definition(SenderInterface::class)) ->setFactory(array(new Reference('messenger.transport_factory'), 'createSender')) ->setArguments(array($transport['dsn'], $transport['options'])) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 931edb5908..e93c67f448 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -256,7 +256,7 @@ class ConfigurationTest extends TestCase 'routing' => array(), 'transports' => array(), 'serializer' => array( - 'enabled' => true, + 'enabled' => !class_exists(FullStack::class), 'format' => 'json', 'context' => array(), ), diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger.php index 16b311f5cd..0135ee69e0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger.php @@ -5,6 +5,7 @@ use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage; $container->loadFromExtension('framework', array( 'messenger' => array( + 'serializer' => false, 'routing' => array( FooMessage::class => array('sender.bar', 'sender.biz'), BarMessage::class => 'sender.foo', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_amqp_transport_no_serializer.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_amqp_transport_no_serializer.php new file mode 100644 index 0000000000..f2389d5426 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_amqp_transport_no_serializer.php @@ -0,0 +1,12 @@ +loadFromExtension('framework', array( + 'messenger' => array( + 'serializer' => array( + 'enabled' => false, + ), + 'transports' => array( + 'default' => 'amqp://localhost/%2f/messages', + ), + ), +)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transport.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transport.php index ec90a3197b..60c4800e3f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transport.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transport.php @@ -1,6 +1,7 @@ loadFromExtension('framework', array( + 'serializer' => true, 'messenger' => array( 'serializer' => array( 'format' => 'csv', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transports.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transports.php index 3f90bda7f3..1f80286718 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transports.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_transports.php @@ -3,6 +3,7 @@ $container->loadFromExtension('framework', array( 'serializer' => true, 'messenger' => array( + 'serializer' => true, 'transports' => array( 'default' => 'amqp://localhost/%2f/messages', 'customised' => array( diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_disabled.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_disabled.php index dedd090beb..7f96c4e44e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_disabled.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_disabled.php @@ -4,4 +4,7 @@ $container->loadFromExtension('framework', array( 'serializer' => array( 'enabled' => false, ), + 'messenger' => array( + 'serializer' => false, + ), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger.xml index 0a130ce0ba..37c58b6a9d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger.xml @@ -7,6 +7,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_amqp_transport_no_serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_amqp_transport_no_serializer.xml new file mode 100644 index 0000000000..0863ef6820 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_amqp_transport_no_serializer.xml @@ -0,0 +1,14 @@ + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transport.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transport.xml index ca7c597d44..bcca131a53 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transport.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transport.xml @@ -6,6 +6,7 @@ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transports.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transports.xml index 211135570c..b3f9505de1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transports.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_transports.xml @@ -8,6 +8,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_disabled.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_disabled.xml index 73f1dccb1a..4ffd135174 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_disabled.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_disabled.xml @@ -7,5 +7,8 @@ + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger.yml index 7f038af11f..0983f2ef32 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger.yml @@ -1,5 +1,6 @@ framework: messenger: + serializer: false routing: 'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage': ['sender.bar', 'sender.biz'] 'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage': 'sender.foo' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_amqp_transport_no_serializer.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_amqp_transport_no_serializer.yml new file mode 100644 index 0000000000..e31f3275a4 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_amqp_transport_no_serializer.yml @@ -0,0 +1,5 @@ +framework: + messenger: + serializer: false + transports: + default: 'amqp://localhost/%2f/messages' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transport.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transport.yml index af590a5169..7abed80e5d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transport.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transport.yml @@ -1,4 +1,5 @@ framework: + serializer: true messenger: serializer: format: csv diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transports.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transports.yml index 0c38638679..4522212f4d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transports.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_transports.yml @@ -1,6 +1,7 @@ framework: serializer: true messenger: + serializer: true transports: default: 'amqp://localhost/%2f/messages' customised: diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_disabled.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_disabled.yml index 330e19a697..e629f4aea4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_disabled.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_disabled.yml @@ -1,3 +1,5 @@ framework: serializer: enabled: false + messenger: + serializer: false diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 49a87b5869..f6cf5637f5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -526,7 +526,8 @@ abstract class FrameworkExtensionTest extends TestCase public function testMessenger() { $container = $this->createContainerFromFile('messenger'); - $this->assertTrue($container->has('message_bus')); + $this->assertTrue($container->hasAlias('message_bus')); + $this->assertFalse($container->hasDefinition('messenger.transport.amqp.factory')); } public function testMessengerTransports() @@ -556,6 +557,8 @@ abstract class FrameworkExtensionTest extends TestCase $this->assertCount(2, $receiverArguments); $this->assertSame('amqp://localhost/%2f/messages?exchange_name=exchange_name', $receiverArguments[0]); $this->assertSame(array('queue' => array('name' => 'Queue')), $receiverArguments[1]); + + $this->assertTrue($container->hasDefinition('messenger.transport.amqp.factory')); } public function testMessengerRouting() @@ -574,13 +577,22 @@ abstract class FrameworkExtensionTest extends TestCase /** * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException - * @expectedExceptionMessage Using the default encoder/decoder, Symfony Messenger requires the Serializer. Enable it or install it by running "composer require symfony/serializer-pack". + * @expectedExceptionMessage The default Messenger serializer cannot be enabled as the Serializer support is not available. Try enable it or install it by running "composer require symfony/serializer-pack". */ public function testMessengerTransportConfigurationWithoutSerializer() { $this->createContainerFromFile('messenger_transport_no_serializer'); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException + * @expectedExceptionMessage The default AMQP transport is not available. Make sure you have installed and enabled the Serializer component. Try enable it or install it by running "composer require symfony/serializer-pack". + */ + public function testMessengerAMQPTransportConfigurationWithoutSerializer() + { + $this->createContainerFromFile('messenger_amqp_transport_no_serializer'); + } + public function testMessengerTransportConfiguration() { $container = $this->createContainerFromFile('messenger_transport');