diff --git a/src/MoneyBag.php b/src/MoneyBag.php index 7bce90d..01389bf 100644 --- a/src/MoneyBag.php +++ b/src/MoneyBag.php @@ -5,6 +5,7 @@ namespace Brick\Money; use Brick\Math\BigRational; +use Brick\Money\Tests\CurrencyTest; /** * Container for monies in different currencies. @@ -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(); diff --git a/tests/MoneyBagTest.php b/tests/MoneyBagTest.php index 4fdc7e4..6e1c4b2 100644 --- a/tests/MoneyBagTest.php +++ b/tests/MoneyBagTest.php @@ -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; @@ -25,7 +26,7 @@ public function testEmptyMoneyBag() : void } } - public function testAddSubtractMoney() : void + public function testAddSubtractMoney() : MoneyBag { $moneyBag = new MoneyBag(); @@ -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); } }