Skip to content

Commit

Permalink
Fix mailer compiler pass (#230)
Browse files Browse the repository at this point in the history
* fix mailer compiler pass

* add mailer compiler test scenarios for aliases definitions
  • Loading branch information
ixarlie authored and kbond committed Jan 16, 2020
1 parent 4d32cd9 commit a1e2df0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DependencyInjection/Compiler/MailerCompilerPass.php
Expand Up @@ -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;

/**
Expand All @@ -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'));
Expand Down
39 changes: 39 additions & 0 deletions Tests/DependencyInjection/Compiler/MailerCompilerPassTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down

0 comments on commit a1e2df0

Please sign in to comment.