diff --git a/DependencyInjection/Compiler/MailerCompilerPass.php b/DependencyInjection/Compiler/MailerCompilerPass.php index 436fcaee..67950571 100644 --- a/DependencyInjection/Compiler/MailerCompilerPass.php +++ b/DependencyInjection/Compiler/MailerCompilerPass.php @@ -5,6 +5,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\Loader; /** @@ -22,11 +23,12 @@ public function process(ContainerBuilder $container) return; } - if (!$container->hasDefinition('mailer')) { + try { + $definition = $container->findDefinition('mailer'); + } catch (ServiceNotFoundException $e) { throw new \InvalidArgumentException('To enable mail reporting you have to install the "swiftmailer/swiftmailer" or "symfony/mailer".'); } - $definition = $container->getDefinition('mailer'); $filename = \Swift_Mailer::class !== $definition->getClass() ? 'symfony_mailer.xml' : 'swift_mailer.xml'; $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config')); diff --git a/Tests/DependencyInjection/Compiler/MailerCompilerPassTest.php b/Tests/DependencyInjection/Compiler/MailerCompilerPassTest.php index 08cbde1b..9a85c234 100644 --- a/Tests/DependencyInjection/Compiler/MailerCompilerPassTest.php +++ b/Tests/DependencyInjection/Compiler/MailerCompilerPassTest.php @@ -65,6 +65,19 @@ public function testSwiftMailer() ); } + public function testSwiftMailerWithAliasDefinition() + { + $this->setParameter('liip_monitor.mailer.enabled', true); + $this->setDefinition('swift.mailer', new Definition(\Swift_Mailer::class)); + $this->container->setAlias('mailer', 'swift.mailer'); + + $this->assertContainerBuilderHasAlias('mailer'); + + $this->compile(); + + $this->assertContainerBuilderHasService('liip_monitor.reporter.swift_mailer', SwiftMailerReporter::class); + } + public function testSymfonyMailer() { $this->setParameter('liip_monitor.mailer.enabled', true); @@ -107,6 +120,19 @@ public function testSymfonyMailer() ); } + public function testSymfonyMailerWithAliasDefinition() + { + $this->setParameter('liip_monitor.mailer.enabled', true); + $this->setDefinition('symfony.mailer', new Definition(MailerInterface::class)); + $this->container->setAlias('mailer', 'symfony.mailer'); + + $this->assertContainerBuilderHasAlias('mailer'); + + $this->compile(); + + $this->assertContainerBuilderHasService('liip_monitor.reporter.symfony_mailer', SymfonyMailerReporter::class); + } + public function testMailerWithoutPackage() { $this->setParameter('liip_monitor.mailer.enabled', true); @@ -117,6 +143,19 @@ public function testMailerWithoutPackage() $this->compile(); } + public function testMailerMissingAliasDefinition() + { + $this->setParameter('liip_monitor.mailer.enabled', true); + $this->setDefinition('swift.mailer', new Definition(\Swift_Mailer::class)); + + $this->assertFalse($this->container->hasAlias('mailer')); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('To enable mail reporting you have to install the "swiftmailer/swiftmailer" or "symfony/mailer".'); + + $this->compile(); + } + protected function registerCompilerPass(ContainerBuilder $container): void { $container->addCompilerPass(new MailerCompilerPass());