Skip to content

Commit

Permalink
Merge pull request #50 from einorler/remainingTests
Browse files Browse the repository at this point in the history
Document and Command tests
  • Loading branch information
saimaz committed Mar 29, 2016
2 parents 3a1b7db + cbb7774 commit b2cf501
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Command/UpdateEsRatesCommand.php
Expand Up @@ -11,7 +11,7 @@

namespace ONGR\CurrencyExchangeBundle\Command;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ClientException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -39,7 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$data = $this->getContainer()->get('ongr_currency_exchange.currency_rates_service');
$data->reloadRates();
$output->writeln(sprintf('<info>Currency rates updated</info>'));
} catch (ConnectException $e) {
} catch (ClientException $e) {
$output->writeln(
sprintf(
'<error>Error ocurred during update. </error> <comment>`%s`</comment>',
Expand Down
6 changes: 3 additions & 3 deletions Service/CurrencyRatesService.php
Expand Up @@ -17,6 +17,7 @@
use ONGR\CurrencyExchangeBundle\Driver\CurrencyDriverInterface;
use ONGR\CurrencyExchangeBundle\Exception\RatesNotLoadedException;
use ONGR\ElasticsearchBundle\Result\Result;
use ONGR\ElasticsearchBundle\Collection\Collection;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use ONGR\ElasticsearchBundle\Service\Manager;
Expand Down Expand Up @@ -157,21 +158,20 @@ private function getCachedRates()
*/
public function reloadRates()
{
$esRates = [];
$this->rates = $this->driver->getRates();

/** @var CurrencyDocument $document */
$document = new CurrencyDocument();
$document->setCreatedAt(new \DateTime());
$document->rates = new Collection();

if ($this->rates) {
foreach ($this->rates as $name => $value) {
$ratesObject = new RatesObject();
$ratesObject->setName($name);
$ratesObject->setValue($value);
$esRates[] = $ratesObject;
$document->rates[] = $ratesObject;
}
$document->rates = $esRates;
$this->manager->persist($document);
$this->manager->commit();
$this->updateRatesCache($this->rates);
Expand Down
124 changes: 124 additions & 0 deletions Tests/Functional/Command/UpdateEsRatesCommandTest.php
@@ -0,0 +1,124 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\CurrencyExchangeBundle\Tests\Functional\Command;

use ONGR\CurrencyExchangeBundle\Command\UpdateEsRatesCommand;
use ONGR\CurrencyExchangeBundle\Driver\OpenExchangeRatesDriver;
use ONGR\CurrencyExchangeBundle\Service\CurrencyRatesService;
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;

class UpdateEsRatesCommandTest extends AbstractElasticsearchTestCase
{
/**
* Tests the UpdateEsRates command
*/
public function testUpdateEsRatesCommand()
{
$this->setUpConfiguration(true);
$app = new Application();
$command = new UpdateEsRatesCommand();
$command->setContainer($this->getContainer());
$app->add($command);
$command = $app->find('ongr:currency:update');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
]
);

$this->assertContains(
'Currency rates updated',
$tester->getDisplay()
);
$this->assertEquals(0, $tester->getStatusCode(), 'Status code should be zero.');
}

/**
* Tests the UpdateEsRates command
*/
public function testUpdateEsRatesCommandException()
{
$this->setUpConfiguration(false);
$app = new Application();
$command = new UpdateEsRatesCommand();
$command->setContainer($this->getContainer());
$app->add($command);
$command = $app->find('ongr:currency:update');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
]
);
$this->assertContains(
'Error ocurred during update.',
$tester->getDisplay()
);
}

/**
* mocks http client to set a service to the container
*
* @param bool $loadInfo determines if the request will provide a response
*/
public function setUpConfiguration($loadInfo)
{
$response = [
'disclaimer' => 'https://openexchangerates.org/terms/',
'license' => 'https://openexchangerates.org/license/',
'timestamp' => 1449877801,
'base' => 'USD',
'rates' => [
'EUR' => 0.935485,
'AFN' => 66.809999,
'ALL' => 125.716501,
'AMD' => 484.902502,
'ANG' => 1.788575,
'AOA' => 135.295998,
'ARS' => 9.750101,
'AUD' => 1.390866
]
];
if ($loadInfo) {
$response_json = json_encode($response);
$mock = new MockHandler([
new Response(200, [], $response_json)
]);
} else {
$mock = new MockHandler([new Response(400, [])]);
}
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);

$driver = new OpenExchangeRatesDriver(
$client,
'http://openexchangerates.org/api/latest.json'
);
$item = $this->getMock('Stash\Interfaces\ItemInterface');
$item->expects($this->any())->method('set');
$pool = $this->getMock('Stash\Interfaces\PoolInterface');
$pool->expects($this->any())->method('getItem')->with('ongr_currency')->willReturn($item);
$currencyRateService = new CurrencyRatesService(
$driver,
$this->getManager(),
$pool
);
$this->getContainer()->set('ongr_currency_exchange.currency_rates_service', $currencyRateService);
}
}
32 changes: 32 additions & 0 deletions Tests/Unit/Document/CurrencyDocumentTest.php
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\CurrencyExchangeBundle\Tests\Unit\Document;

use ONGR\CurrencyExchangeBundle\Document\CurrencyDocument;
use ONGR\CurrencyExchangeBundle\Document\RatesObject;

class CurrencyDocumentTest extends \PHPUnit_Framework_TestCase
{
/**
* Test getters and setters
*/
public function testGettersAndSetters()
{
$time = time();
$rates = new RatesObject();
$currency = new CurrencyDocument();
$currency->setCreatedAt($time);
$currency->setRates($rates);
$this->assertEquals($time, $currency->getCreatedAt());
$this->assertEquals($rates, $currency->getRates());
}
}
31 changes: 31 additions & 0 deletions Tests/Unit/Document/RatesObjectTest.php
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\CurrencyExchangeBundle\Tests\Unit\Document;

use ONGR\CurrencyExchangeBundle\Document\RatesObject;

class RatesObjectTest extends \PHPUnit_Framework_TestCase
{
/**
* Test getters and setters
*/
public function testGettersAndSetters()
{
$name = 'USD';
$value = 1.2543;
$rate = new RatesObject();
$rate->setName($name);
$rate->setValue($value);
$this->assertEquals($name, $rate->getName());
$this->assertEquals($value, $rate->getValue());
}
}

0 comments on commit b2cf501

Please sign in to comment.