Skip to content

Commit

Permalink
Fix MoneyBag::getAmount() throwing exception on custom currency
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed May 31, 2020
1 parent c87752e commit 91f2b5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/MoneyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\Money;

use Brick\Math\BigRational;
use Brick\Money\Tests\CurrencyTest;

/**
* Container for monies in different currencies.
Expand All @@ -29,12 +30,12 @@ final class MoneyBag implements MoneyContainer
*/
public function getAmount($currency) : BigRational
{
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
if (is_int($currency)) {
$currencyCode = (string) Currency::of($currency);
} else {
$currencyCode = (string) $currency;
}

$currencyCode = (string) $currency;

return isset($this->amounts[$currencyCode])
? $this->amounts[$currencyCode]
: BigRational::zero();
Expand Down
14 changes: 13 additions & 1 deletion tests/MoneyBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\Money\Tests;

use Brick\Money\Context\AutoContext;
use Brick\Money\Currency;
use Brick\Money\Money;
use Brick\Money\MoneyBag;
use Brick\Money\RationalMoney;
Expand All @@ -25,7 +26,7 @@ public function testEmptyMoneyBag() : void
}
}

public function testAddSubtractMoney() : void
public function testAddSubtractMoney() : MoneyBag
{
$moneyBag = new MoneyBag();

Expand All @@ -46,5 +47,16 @@ public function testAddSubtractMoney() : void

$moneyBag->add(RationalMoney::of('1/3', 'EUR'));
$this->assertMoneyBagContains(['EUR' => '21284003/60000', 'JPY' => '4.1234'], $moneyBag);

return $moneyBag;
}

/**
* @depends testAddSubtractMoney
*/
public function testAddCustomCurrency(MoneyBag $moneyBag) : void
{
$moneyBag->add(Money::of('0.1234', new Currency('BTC', 0, 'Bitcoin', 8)));
$this->assertMoneyBagContains(['EUR' => '21284003/60000', 'JPY' => '4.1234', 'BTC' => '0.1234'], $moneyBag);
}
}

0 comments on commit 91f2b5b

Please sign in to comment.