Skip to content

Commit

Permalink
Add isAmountAndCurrencyEqualTo()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jan 23, 2020
1 parent b01ccf4 commit 6f3d8ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 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 are equal to those of the given money.
*
* Unlike isEqualTo(), this method only accepts a money, and returns false if the given money is in another
* currency, instead of throwing a MoneyMismatchException.
*
* @param AbstractMoney $that
*
* @return bool
*/
final public function isAmountAndCurrencyEqualTo(AbstractMoney $that) : bool
{
return $this->getAmount()->isEqualTo($that->getAmount())
&& $this->getCurrency()->is($that->getCurrency());
}

/**
* Returns the amount of the given parameter.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,23 @@ public function testIsGreaterThanOrEqualToOtherCurrency() : void
Money::of('1.00', 'EUR')->isGreaterThanOrEqualTo(Money::of('1.00', 'USD'));
}

/**
* @dataProvider providerIsAmountAndCurrencyEqualTo
*/
public function testIsAmountAndCurrencyEqualTo(array $a, array $b, bool $c) : void
{
$this->assertSame($c, Money::of(...$a)->isAmountAndCurrencyEqualTo(Money::of(...$b)));
}

public function providerIsAmountAndCurrencyEqualTo() : \Generator
{
foreach ($this->providerCompare() as [$a, $b, $c]) {
yield [$a, $b, $c === 0];
}

yield [[1, 'EUR'], [1, 'USD'], false];
}

/**
* @return array
*/
Expand Down

0 comments on commit 6f3d8ca

Please sign in to comment.