Skip to content

Commit

Permalink
money manager created + tests + doc
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-levan committed Sep 25, 2014
1 parent 6657704 commit 3779dfa
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Money/MoneyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Created by levan on 25/09/2014.
*/

namespace Tbbc\MoneyBundle\Money;


use Money\Currency;
use Money\Money;

class MoneyManager
implements MoneyManagerInterface
{
/** @var int */
protected $decimals;

/** @var string */
protected $referenceCurrencyCode;

public function __construct(
$referenceCurrencyCode,
$decimals = 2
)
{
$this->referenceCurrencyCode = $referenceCurrencyCode;
$this->decimals = $decimals;
}

/**
* @inheritdoc
*/
public function createMoneyFromFloat($floatAmount, $currencyCode = null)
{
if (is_null($currencyCode)) {
$currencyCode = $this->referenceCurrencyCode;
}
$currency = new Currency($currencyCode);
$amountAsInt = $floatAmount * pow(10, $this->decimals);
$amountAsInt = round($amountAsInt);
$amountAsInt = intval($amountAsInt);
$money = new Money($amountAsInt, $currency);
return $money;
}

}
17 changes: 17 additions & 0 deletions Money/MoneyManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Tbbc\MoneyBundle\Money;

use Money\Money;

interface MoneyManagerInterface
{
/**
* convert the float amount into a money object
*
* @param float $floatAmount amount before multiplication. Ex: 32.15
* @param string|null $currencyCode in the list of currencies from config.yml. If null, use the reference currency
* @return Money
*/
public function createMoneyFromFloat($floatAmount, $currencyCode = null);

}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="tbbc_money.pair_manager.class">Tbbc\MoneyBundle\Pair\PairManager</parameter>
<parameter key="tbbc_money.money_manager.class">Tbbc\MoneyBundle\Money\MoneyManager</parameter>
<parameter key="tbbc_money.pair_history_manager.class">Tbbc\MoneyBundle\PairHistory\PairHistoryManager</parameter>
<parameter key="tbbc_money.pair.csv_storage.class">Tbbc\MoneyBundle\Pair\Storage\CsvStorage</parameter>
<parameter key="tbbc_money.pair_manager.ratio_file_name">%kernel.root_dir%/data/tbbc_money/ratio_file_name.csv</parameter>
Expand All @@ -24,6 +25,10 @@
<argument type="service" id="tbbc_money.ratio_provider.rate_exchange" />
</call>
</service>
<service id="tbbc_money.money_manager" class="%tbbc_money.money_manager.class%">
<argument>%tbbc_money.reference_currency%</argument>
<argument>%tbbc_money.decimals%</argument>
</service>
<!-- Storage -->
<service id="tbbc_money.pair.csv_storage" class="%tbbc_money.pair.csv_storage.class%">
<argument>%tbbc_money.pair_manager.ratio_file_name%</argument>
Expand Down
10 changes: 10 additions & 0 deletions Tests/Fonctionnal/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Console\Input\StringInput;
use Tbbc\MoneyBundle\Money\MoneyManager;
use Tbbc\MoneyBundle\Pair\PairManagerInterface;
use Tbbc\MoneyBundle\Twig\CurrencyExtension;
use Tbbc\MoneyBundle\Twig\MoneyExtension;
Expand Down Expand Up @@ -67,6 +68,15 @@ public function testMoneyTwigExtension()
$this->assertEquals(Money::USD(125), $usd);
}

public function testMoneyManager()
{
/** @var MoneyManager $moneyManager */
$moneyManager = $this->client->getContainer()->get("tbbc_money.money_manager");
$money = $moneyManager->createMoneyFromFloat('2.5', 'USD');
$this->assertEquals("USD", $money->getCurrency()->getName());
$this->assertEquals(2500, $money->getAmount()); // note : 3 decimals in config for theses tests
}

public function testHistoryRatio()
{
\Locale::setDefault('en');
Expand Down
42 changes: 42 additions & 0 deletions Tests/Money/MoneyManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Tbbc\MoneyBundle\Tests\Money;

use Money\Money;
use Tbbc\MoneyBundle\Money\MoneyManager;
use Tbbc\MoneyBundle\MoneyException;

/**
* @group moneymanager
*/
class MoneyManagerTest extends \PHPUnit_Framework_TestCase
{
/** @var MoneyManager */
protected $manager;

public function setUp()
{
$this->manager = new MoneyManager("EUR", 2);
}

public function tearDown()
{
}

public function testCreateMoneyFromFloat()
{
$money = $this->manager->createMoneyFromFloat(2.5);
$this->assertEquals("EUR", $money->getCurrency()->getName());
$this->assertEquals(250, $money->getAmount());

$money = $this->manager->createMoneyFromFloat(2.5, 'USD');
$this->assertEquals("USD", $money->getCurrency()->getName());
$this->assertEquals(250, $money->getAmount());

$money = $this->manager->createMoneyFromFloat(2.49999999999999);
$this->assertEquals(250, $money->getAmount());

$money = $this->manager->createMoneyFromFloat(2.529999999999);
$this->assertEquals(253, $money->getAmount());
}

}

0 comments on commit 3779dfa

Please sign in to comment.