diff --git a/lib/Money/CurrencyPair.php b/lib/Money/CurrencyPair.php index 26a731e0..61fe0fab 100644 --- a/lib/Money/CurrencyPair.php +++ b/lib/Money/CurrencyPair.php @@ -95,4 +95,17 @@ public function getRatio() { return $this->ratio; } + + /** + * @param \Money\CurrencyPair $other the currency pair to compare + * @return boolean + */ + public function equals(CurrencyPair $other) + { + return ( + $this->baseCurrency->equals($other->baseCurrency) + && $this->counterCurrency->equals($other->counterCurrency) + && $this->ratio === $other->ratio + ); + } } diff --git a/tests/Money/Tests/CurrencyPairTest.php b/tests/Money/Tests/CurrencyPairTest.php index 2b23c8c7..8b91b32e 100644 --- a/tests/Money/Tests/CurrencyPairTest.php +++ b/tests/Money/Tests/CurrencyPairTest.php @@ -91,6 +91,44 @@ public function testConvertWithInvalidCurrency() $pair->convert($money); } + + /** + * @dataProvider provideEqualityComparisonPairs + */ + public function testEqualityComparisons($pair1, $pair2, $equal) + { + $this->assertSame($equal, $pair1->equals($pair2)); + } + + public function provideEqualityComparisonPairs() + { + $usd = new Currency('USD'); + $eur = new Currency('EUR'); + $gbp = new Currency('GBP'); + + return array( + 'Base Mismatch EUR != GBP' => array( + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($gbp, $usd, 1.2500), + false + ), + 'Counter Mismatch USD != GBP' => array( + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $gbp, 1.2500), + false + ), + 'Ratio Mismatch 1.2500 != 1.5000' => array( + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $usd, 1.5000), + false + ), + 'Full Equality EUR/USD 1.2500' => array( + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $usd, 1.2500), + true + ), + ); + } public function provideNonNumericRatio() {