Skip to content

Commit

Permalink
fix history or ratio was saved more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-levan committed Jul 16, 2014
1 parent a36af8c commit 2401b6d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
16 changes: 7 additions & 9 deletions Pair/PairManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ public function saveRatio($currencyCode, $ratio)
$this->storage->saveRatioList($ratioList);

$savedAt = new \DateTime();
foreach($ratioList as $currencyCode => $ratio) {
$event = new SaveRatioEvent(
$this->getReferenceCurrencyCode(),
$currencyCode,
$ratio,
$savedAt
);
$this->dispatcher->dispatch(TbbcMoneyEvents::AFTER_RATIO_SAVE, $event);
}
$event = new SaveRatioEvent(
$this->getReferenceCurrencyCode(),
$currencyCode,
$ratio,
$savedAt
);
$this->dispatcher->dispatch(TbbcMoneyEvents::AFTER_RATIO_SAVE, $event);
}

/**
Expand Down
32 changes: 26 additions & 6 deletions Tests/Console/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@
use Tbbc\MoneyBundle\Pair\PairManagerInterface;
use Tbbc\MoneyBundle\Tests\TestUtil\CommandTestCase;

/**
* @group functionnal
*/
class ConsoleTest
extends CommandTestCase
{
/** @var \Symfony\Bundle\FrameworkBundle\Client */
private $client;
public function setUp()
{
parent::setUp();
/** @var \Symfony\Bundle\FrameworkBundle\Client client */
$this->client = static::createClient();

$this->runCommand($this->client,'doctrine:database:create');
$this->runCommand($this->client,'doctrine:schema:update --force');
}

public function testRunSaveRatio()
{
$client = self::createClient();
$client = $this->client;


$output = $this->runCommand($client, "tbbc:money:ratio-save USD 1.265");

Expand All @@ -19,24 +35,28 @@ public function testRunSaveRatio()
}
public function testRunRatioList()
{
$client = self::createClient();
$client = $this->client;
$output = $this->runCommand($client, "tbbc:money:ratio-save USD 1.265");
$output = $this->runCommand($client, "tbbc:money:ratio-save CAD 1.1");

$output = $this->runCommand($client, "tbbc:money:ratio-list");

$this->assertEquals("Ratio list\nEUR;1\nUSD;1.265\n\n", $output);
$this->assertEquals("Ratio list\nEUR;1\nUSD;1.265\nCAD;1.1\n\n", $output);
}

public function testRunRatioFetch()
{
$client = self::createClient();
$client = $this->client;
$output = $this->runCommand($client, "tbbc:money:ratio-fetch");
$this->assertNotContains("ERR", $output);

$output = $this->runCommand($client, "tbbc:money:ratio-list");
$res = file_get_contents("http://rate-exchange.appspot.com/currency?from=EUR&to=USD");
$res = json_decode($res, true);
$ratio = $res["rate"];
$this->assertEquals("Ratio list\nEUR;1\nUSD;$ratio\n\n", $output);
$ratioUsd = $res["rate"];
$res = file_get_contents("http://rate-exchange.appspot.com/currency?from=EUR&to=CAD");
$res = json_decode($res, true);
$ratioCad = $res["rate"];
$this->assertEquals("Ratio list\nEUR;1\nUSD;$ratioUsd\nCAD;$ratioCad\n\n", $output);
}
}
15 changes: 12 additions & 3 deletions Tests/Fonctionnal/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function runCommand($command)
public function testConfigParsing()
{
$currencies = $this->client->getContainer()->getParameter('tbbc_money.currencies');
$this->assertEquals(array("USD", "EUR"), $currencies);
$this->assertEquals(array("USD", "EUR", 'CAD'), $currencies);

$referenceCurrency = $this->client->getContainer()->getParameter('tbbc_money.reference_currency');
$this->assertEquals("EUR", $referenceCurrency);
Expand Down Expand Up @@ -92,11 +92,20 @@ public function testHistoryRatio()
$em = $this->client->getContainer()->get("doctrine.orm.entity_manager");
$repo = $em->getRepository('\Tbbc\MoneyBundle\Entity\RatioHistory');
$list = $repo->findAll();
// 4 because of 2 reference currencies and 2 USD
$this->assertEquals(4, count($list));
$this->assertEquals(2, count($list));

}

public function testHistoryOfFetchedRatio()
{
$this->runCommand('tbbc:money:ratio-fetch');
$em = $this->client->getContainer()->get("doctrine.orm.entity_manager");
$repo = $em->getRepository('\Tbbc\MoneyBundle\Entity\RatioHistory');
$list = $repo->findAll();

$this->assertEquals(2, count($list));
}

public function testCurrencyTwigExtension()
{
\Locale::setDefault('en');
Expand Down
3 changes: 3 additions & 0 deletions Tests/PairHistory/PairHistoryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public function testGetRatio()
$this->assertEquals(1.75, $ratio);
$ratio = $this->pairHistoryManager->getRatioAtDate('USD', new \DateTime('2011-07-10 12:30:00'));
$this->assertEquals(null, $ratio);

$ratio = $this->pairHistoryManager->getRatioAtDate('EUR', new \DateTime('2011-07-10 12:30:00'));
$this->assertEquals(1, $ratio);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tbbc_money:
currencies: ["USD", "EUR"]
currencies: ["USD", "EUR", "CAD"]
reference_currency: "EUR"
decimals: 3
enable_pair_history: true
Expand Down

0 comments on commit 2401b6d

Please sign in to comment.