Skip to content

Commit

Permalink
Added tests for new compiler pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCelavi committed Jun 1, 2017
1 parent aebd896 commit bf7cab2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
Expand Up @@ -31,7 +31,7 @@ public function process(ContainerBuilder $container)
return;
}

if (!$container->hasParameter('runopencode.exchange_rate.notifications.e_mail')) {
if (!$container->hasParameter('runopencode.exchange_rate.notifications.email.recipients')) {
$container->removeDefinition('runopencode.exchange_rate.notifications.email');
return;
}
Expand All @@ -40,14 +40,14 @@ public function process(ContainerBuilder $container)
throw new RuntimeException('Bundle "symfony/swiftmailer-bundle" is required for email notifications.');
}

$recipients = $container->getParameter('runopencode.exchange_rate.notifications.e_mail');
$recipients = $container->getParameter('runopencode.exchange_rate.notifications.email.recipients');

if (0 === count($recipients)) {
throw new RuntimeException('At least one recipient for e-mail notifications must be provided.');
}

$container
->getDefinition('runopencode.exchange_rate.notifications.e_mail')
->addArgument($recipients);
->getDefinition('runopencode.exchange_rate.notifications.email')
->setArgument(2, $recipients);
}
}
Expand Up @@ -367,7 +367,7 @@ protected function configureRateType(array $config, ContainerBuilder $container)
protected function configureNotifications(array $config, ContainerBuilder $container)
{
if (isset($config['notifications']['e_mail']['enabled']) && $config['notifications']['e_mail']['enabled']) {
$container->setParameter('runopencode.exchange_rate.notifications.e_mail', $config['notifications']['e_mail']['recipients']);
$container->setParameter('runopencode.exchange_rate.notifications.email.recipients', $config['notifications']['e_mail']['recipients']);
}

return $this;
Expand Down
@@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Exchange Rate Bundle, an RunOpenCode project.
*
* (c) 2017 RunOpenCode
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RunOpenCode\Bundle\ExchangeRate\Tests\DependencyInjection\CompilerPass;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass\NotificationsCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Class NotificationsCompilerPassTest
*
* @package RunOpenCode\Bundle\ExchangeRate\Tests\DependencyInjection\CompilerPass
*/
class NotificationsCompilerPassTest extends AbstractCompilerPassTestCase
{
/**
* @test
*/
public function itRemovesEmailEventListenerWhenEmailNotificationsAreDisabled()
{
$this->setDefinition('runopencode.exchange_rate.notifications.email', new Definition());

$this->compile();

$this->assertFalse($this->container->has('runopencode.exchange_rate.notifications.email'));
}

/**
* @test
*
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function itThrowsExceptionWhenMailerServiceIsMissing()
{
$this->setParameter('runopencode.exchange_rate.notifications.email.recipients', []);
$this->setDefinition('runopencode.exchange_rate.notifications.email', new Definition());

$this->compile();
}

/**
* @test
*
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function itThrowsExceptionWhenThereAreNoEmailRecipients()
{
$this->setParameter('runopencode.exchange_rate.notifications.email.recipients', []);
$this->setDefinition('runopencode.exchange_rate.notifications.email', new Definition());
$this->setDefinition('mailer', new Definition());

$this->compile();
}

/**
* @test
*/
public function itSetsRecipientsForEmailNotifications()
{
$this->setParameter('runopencode.exchange_rate.notifications.email.recipients', ['test@test.test']);
$this->setDefinition('runopencode.exchange_rate.notifications.email', new Definition(null, [ 'arg1', 'arg2' ]));
$this->setDefinition('mailer', new Definition());

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithArgument('runopencode.exchange_rate.notifications.email', 2, ['test@test.test']);
}

/**
* {@inheritdoc}
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container
->addCompilerPass(new NotificationsCompilerPass());
}
}

0 comments on commit bf7cab2

Please sign in to comment.