Skip to content

Commit

Permalink
Add AbstractMoney::matches() method
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Mar 12, 2018
1 parent 46997fd commit 0654902
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/AbstractMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ final public function isGreaterThanOrEqualTo($that) : bool
return $this->getAmount()->isGreaterThanOrEqualTo($this->getAmountOf($that));
}

/**
* Returns whether this money's amount and currency match those of the given money.
*
* This differs from isEqualTo() in that it only accepts a money, and returns false
* if the currencies are not equal, instead of throwing an exception.
*
* @param AbstractMoney $that
*
* @return bool
*/
final public function matches(AbstractMoney $that) : bool
{
return $this->getAmount()->isEqualTo($that->getAmount())
&& $this->getCurrency()->is($that->getCurrency());
}

/**
* Returns the amount of the given parameter.
*
Expand Down
25 changes: 20 additions & 5 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Brick\Money\Tests;

use Brick\Money\Currency;
use Brick\Money\Exception\MoneyMismatchException;
use Brick\Money\Money;
use Brick\Money\Context;
use Brick\Money\Context\CashContext;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Context\AutoContext;
use Brick\Money\Context\CashContext;
use Brick\Money\Context\CustomContext;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Currency;
use Brick\Money\Exception\MoneyMismatchException;
use Brick\Money\Money;
use Brick\Money\RationalMoney;

use Brick\Math\BigDecimal;
use Brick\Math\BigInteger;
Expand Down Expand Up @@ -1004,4 +1005,18 @@ public function testTotalOfDifferentCurrenciesThrowsException()
Money::of('1.00', 'USD')
);
}

public function testMatches()
{
$a = Money::of(3, 'USD');
$b = RationalMoney::of('15/5', 'USD');
$c = RationalMoney::of('15/5', 'EUR');

$this->assertTrue($a->matches($b));
$this->assertTrue($b->matches($a));
$this->assertFalse($a->matches($c));
$this->assertFalse($c->matches($a));
$this->assertFalse($b->matches($c));
$this->assertFalse($c->matches($b));
}
}

0 comments on commit 0654902

Please sign in to comment.