From 3779dfaa3a004edf906b26c20736f843c12afc51 Mon Sep 17 00:00:00 2001 From: "Philippe Le Van (@plv)" Date: Thu, 25 Sep 2014 10:49:02 +0200 Subject: [PATCH] money manager created + tests + doc --- Money/MoneyManager.php | 46 ++++++++++++++++++++++++++++++++ Money/MoneyManagerInterface.php | 17 ++++++++++++ Resources/config/services.xml | 5 ++++ Tests/Fonctionnal/ConfigTest.php | 10 +++++++ Tests/Money/MoneyManagerTest.php | 42 +++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 Money/MoneyManager.php create mode 100644 Money/MoneyManagerInterface.php create mode 100644 Tests/Money/MoneyManagerTest.php diff --git a/Money/MoneyManager.php b/Money/MoneyManager.php new file mode 100644 index 00000000..b4b5d222 --- /dev/null +++ b/Money/MoneyManager.php @@ -0,0 +1,46 @@ +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; + } + +} \ No newline at end of file diff --git a/Money/MoneyManagerInterface.php b/Money/MoneyManagerInterface.php new file mode 100644 index 00000000..94860f3a --- /dev/null +++ b/Money/MoneyManagerInterface.php @@ -0,0 +1,17 @@ + Tbbc\MoneyBundle\Pair\PairManager + Tbbc\MoneyBundle\Money\MoneyManager Tbbc\MoneyBundle\PairHistory\PairHistoryManager Tbbc\MoneyBundle\Pair\Storage\CsvStorage %kernel.root_dir%/data/tbbc_money/ratio_file_name.csv @@ -24,6 +25,10 @@ + + %tbbc_money.reference_currency% + %tbbc_money.decimals% + %tbbc_money.pair_manager.ratio_file_name% diff --git a/Tests/Fonctionnal/ConfigTest.php b/Tests/Fonctionnal/ConfigTest.php index 3b8c1425..183175e1 100644 --- a/Tests/Fonctionnal/ConfigTest.php +++ b/Tests/Fonctionnal/ConfigTest.php @@ -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; @@ -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'); diff --git a/Tests/Money/MoneyManagerTest.php b/Tests/Money/MoneyManagerTest.php new file mode 100644 index 00000000..8279ae8f --- /dev/null +++ b/Tests/Money/MoneyManagerTest.php @@ -0,0 +1,42 @@ +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()); + } + +} \ No newline at end of file