Skip to content

Commit

Permalink
Got 100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCelavi committed Jun 2, 2017
1 parent bf7cab2 commit 431275f
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 88 deletions.
Expand Up @@ -48,6 +48,9 @@ public function process(ContainerBuilder $container)

$container
->getDefinition('runopencode.exchange_rate.notifications.email')
->setArgument(2, $recipients);
->setArgument(2, $recipients)
->addTag('kernel.event_listener', [ 'event' => 'runopencode.exchange_rate.fetch.success' ])
->addTag('kernel.event_listener', [ 'event' => 'runopencode.exchange_rate.fetch.error' ])
;
}
}
Expand Up @@ -58,7 +58,6 @@ public function getXsdValidationBasePath()
*/
public function load(array $config, ContainerBuilder $container)
{

$configuration = new TreeConfiguration();
$config = $this->processConfiguration($configuration, $config);

Expand Down
42 changes: 1 addition & 41 deletions src/RunOpenCode/Bundle/ExchangeRate/Event/FetchErrorEvent.php
Expand Up @@ -17,7 +17,7 @@
*
* @package RunOpenCode\Bundle\ExchangeRate\Event
*/
class FetchErrorEvent extends Event implements \IteratorAggregate, \ArrayAccess
class FetchErrorEvent extends Event
{
/**
* @var array
Expand Down Expand Up @@ -54,44 +54,4 @@ public function getDate()
{
return $this->date;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->errors);
}

/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return isset($this->errors[$offset]);
}

/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->errors[$offset];
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
throw new LogicException(sprintf('Method "%s" of class "%s" can not be invoked in this context.', __FUNCTION__, __CLASS__));
}

/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
throw new LogicException(sprintf('Method "%s" of class "%s" can not be invoked in this context.', __FUNCTION__, __CLASS__));
}
}
42 changes: 1 addition & 41 deletions src/RunOpenCode/Bundle/ExchangeRate/Event/FetchSuccessEvent.php
Expand Up @@ -17,7 +17,7 @@
*
* @package RunOpenCode\Bundle\ExchangeRate\Event
*/
class FetchSuccessEvent extends Event implements \IteratorAggregate, \ArrayAccess
class FetchSuccessEvent extends Event
{
/**
* @var array
Expand Down Expand Up @@ -54,44 +54,4 @@ public function getDate()
{
return $this->date;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->rates);
}

/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return isset($this->rates[$offset]);
}

/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->rates[$offset];
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
throw new LogicException(sprintf('Method "%s" of class "%s" can not be invoked in this context.', __FUNCTION__, __CLASS__));
}

/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
throw new LogicException(sprintf('Method "%s" of class "%s" can not be invoked in this context.', __FUNCTION__, __CLASS__));
}
}
Expand Up @@ -112,6 +112,6 @@ private function renderBlock($template, $block, array $context = [])
$template = $this->twig->loadTemplate($template);
$context = $this->twig->mergeGlobals($context);

return $template->renderParentBlock($block, $context);
return $template->renderBlock($block, $context);
}
}
Expand Up @@ -5,7 +5,7 @@

<h1>{{ source }}:</h1>

{% import '@ExchangeRate/mail/_table.html.twig' with { rates: fetched } only %}
{% include '@ExchangeRate/mail/_table.html.twig' with { rates: fetched } only %}

{% endfor %}

Expand Down
4 changes: 2 additions & 2 deletions test/Command/FetchCommandTest.php
Expand Up @@ -226,11 +226,11 @@ public function itNotifiesAboutSuccessAndErrors()

$this->assertEquals(FetchEvents::SUCCESS, $invocations[0]->parameters[0]);
$this->assertEquals(FetchSuccessEvent::class, get_class($invocations[0]->parameters[1]));
$this->assertEquals([$rate], $invocations[0]->parameters[1]['source_1']);
$this->assertEquals([$rate], $invocations[0]->parameters[1]->getRates()['source_1']);

$this->assertEquals(FetchEvents::ERROR, $invocations[1]->parameters[0]);
$this->assertEquals(FetchErrorEvent::class, get_class($invocations[1]->parameters[1]));
$this->assertInstanceOf(\Exception::class, $invocations[1]->parameters[1]['source_2']);
$this->assertInstanceOf(\Exception::class, $invocations[1]->parameters[1]->getErrors()['source_2']);

$this->assertEquals(-1, $returnValue);
}
Expand Down
21 changes: 21 additions & 0 deletions test/DependencyInjection/ExtensionTest.php
Expand Up @@ -259,6 +259,27 @@ public function itConfiguresFormTypes()
]);
}

/**
* @test
*/
public function itConfiguresNotifications()
{
$this->load([
'base_currency' => 'RSD',
'rates' => [
['currency_code' => 'EUR', 'rate_type' => 'median', 'source' => 'national_bank_of_serbia'],
],
'notifications' => [
'email' => [
'enabled' => true,
'recipients' => ['test@test.test']
]
],
]);

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

/**
* {@inheritdoc}
*/
Expand Down
126 changes: 126 additions & 0 deletions test/Listener/FetchEventsEmailNotificationListenerTest.php
@@ -0,0 +1,126 @@
<?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\Listener;

use RunOpenCode\Bundle\ExchangeRate\Event\FetchErrorEvent;
use RunOpenCode\Bundle\ExchangeRate\Event\FetchSuccessEvent;
use RunOpenCode\Bundle\ExchangeRate\Listener\FetchEventsEmailNotificationListener;
use RunOpenCode\ExchangeRate\Model\Rate;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Crawler;

/**
* Class FetchEventsEmailNotificationListenerTest
*
* @package RunOpenCode\Bundle\ExchangeRate\Tests\Listener
*/
class FetchEventsEmailNotificationListenerTest extends WebTestCase
{

public function setUp()
{
self::bootKernel();
}

/**
* @test
*/
public function itSendsSuccessMailNotification()
{
$mailer = $this
->getMockBuilder(\Swift_Mailer::class)
->disableOriginalConstructor()
->getMock();

$mailer
->expects($spy = $this->once())
->method('send');

$listener = new FetchEventsEmailNotificationListener($this->getTwig(), $mailer, ['test@test.com']);

$listener->onFetchSuccess(new FetchSuccessEvent([
'some_source' => [
new Rate('some_source', 10, 'EUR', 'median', new \DateTime(), 'RSD'),
new Rate('some_source', 12, 'CHF', 'median', new \DateTime(), 'RSD'),
],
'some_other_source' => [
new Rate('some_other_source', 8, 'USD', 'median', new \DateTime(), 'RSD'),
],
], new \DateTime('now')));

/**
* @var \PHPUnit_Framework_MockObject_Invocation_Object $invocation
*/
$invocation = $spy->getInvocations()[0];

/**
* @var \Swift_Message $message
*/
$message = $invocation->parameters[0];

$this->assertInstanceOf(\Swift_Message::class, $message);

$this->assertEquals(['test@test.com' => null], $message->getTo());

$crawler = new Crawler($message->getBody());

$this->assertEquals(2, $crawler->filter('table')->count());

$this->assertEquals(2, $crawler->filter('table')->first()->filter('tbody tr')->count());
$this->assertEquals(1, $crawler->filter('table')->last()->filter('tbody tr')->count());
}

/**
* @test
*/
public function itSendsErrorMailNotification()
{
$mailer = $this
->getMockBuilder(\Swift_Mailer::class)
->disableOriginalConstructor()
->getMock();

$mailer
->expects($spy = $this->once())
->method('send');

$listener = new FetchEventsEmailNotificationListener($this->getTwig(), $mailer, ['test@test.com']);

$listener->onFetchError(new FetchErrorEvent([
'some_source' => new \Exception('Error for some_source.'),
'some_other_source' => new \Exception('Error for some_other_source.'),
], new \DateTime('now')));

/**
* @var \PHPUnit_Framework_MockObject_Invocation_Object $invocation
*/
$invocation = $spy->getInvocations()[0];

/**
* @var \Swift_Message $message
*/
$message = $invocation->parameters[0];

$this->assertInstanceOf(\Swift_Message::class, $message);

$this->assertEquals(['test@test.com' => null], $message->getTo());

$this->assertContains('Error for some_source.', $message->getBody());
$this->assertContains('Error for some_other_source.', $message->getBody());
}

/**
* @return \Twig_Environment
*/
private function getTwig()
{
return self::$kernel->getContainer()->get('twig');
}
}

0 comments on commit 431275f

Please sign in to comment.