From 1ac5212ab8ce1bd1cb09398b4dd506d9e69e260b Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Wed, 23 Oct 2013 14:48:33 -0500 Subject: [PATCH 1/6] Add test for equality comparisons --- tests/Money/Tests/CurrencyPairTest.php | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Money/Tests/CurrencyPairTest.php b/tests/Money/Tests/CurrencyPairTest.php index 2b23c8c7..84d895ae 100644 --- a/tests/Money/Tests/CurrencyPairTest.php +++ b/tests/Money/Tests/CurrencyPairTest.php @@ -91,6 +91,35 @@ public function testConvertWithInvalidCurrency() $pair->convert($money); } + + /** + * @dataProvider provideEqualityComparisonPairs + */ + public function testEqualityComparisons($pair1, $pair2, $equal) + { + $this->assertSame($equal, $pair1->equals($pair2)); + } + + public function provideEqualityComparisonPairs() + { + return array( + array( + new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), + new CurrencyPair(new Currency('USD'), new Currency('EUR'), 0.8000), + false + ), + array( + new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), + new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.5000), + false + ), + array( + new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), + new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), + true + ), + ); + } public function provideNonNumericRatio() { From c07df321177a2b041fb5ea93a20e4a7e40620c16 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Wed, 23 Oct 2013 14:57:08 -0500 Subject: [PATCH 2/6] Add `equals()` comparison method --- lib/Money/CurrencyPair.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Money/CurrencyPair.php b/lib/Money/CurrencyPair.php index 26a731e0..f75356cc 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 === $other->baseCurrency + && $this->counterCurrency === $other->counterCurrency + && $this->ratio === $other->ratio + ); + } } From d3e4a54368c03f088a191aee3618d26570ca7b28 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Wed, 23 Oct 2013 16:27:17 -0500 Subject: [PATCH 3/6] Update equality comparisons --- lib/Money/CurrencyPair.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Money/CurrencyPair.php b/lib/Money/CurrencyPair.php index f75356cc..4f9c20d7 100644 --- a/lib/Money/CurrencyPair.php +++ b/lib/Money/CurrencyPair.php @@ -103,8 +103,8 @@ public function getRatio() public function equals(CurrencyPair $other) { return ( - $this->baseCurrency === $other->baseCurrency - && $this->counterCurrency === $other->counterCurrency + $this->baseCurrency == $other->baseCurrency + && $this->counterCurrency == $other->counterCurrency && $this->ratio === $other->ratio ); } From 73dd3039617cbd1e170276d88763727796942800 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Wed, 23 Oct 2013 16:32:54 -0500 Subject: [PATCH 4/6] Add better equality comparisons --- tests/Money/Tests/CurrencyPairTest.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/Money/Tests/CurrencyPairTest.php b/tests/Money/Tests/CurrencyPairTest.php index 84d895ae..7db484f6 100644 --- a/tests/Money/Tests/CurrencyPairTest.php +++ b/tests/Money/Tests/CurrencyPairTest.php @@ -102,20 +102,29 @@ public function testEqualityComparisons($pair1, $pair2, $equal) public function provideEqualityComparisonPairs() { + $usd = new Currency('USD'); + $eur = new Currency('EUR'); + $gbp = new Currency('GBP'); + return array( array( - new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), - new CurrencyPair(new Currency('USD'), new Currency('EUR'), 0.8000), + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($gbp, $usd, 1.2500), false ), array( - new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), - new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.5000), + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $gbp, 1.2500), false ), array( - new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), - new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500), + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $usd, 1.5000), + false + ), + array( + new CurrencyPair($eur, $usd, 1.2500), + new CurrencyPair($eur, $usd, 1.2500), true ), ); From fe58b7aa79147ec3144f5884e404b2e2a66ea5dc Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Sun, 27 Oct 2013 14:06:53 -0500 Subject: [PATCH 5/6] Refine Currency comparison --- lib/Money/CurrencyPair.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Money/CurrencyPair.php b/lib/Money/CurrencyPair.php index 4f9c20d7..61fe0fab 100644 --- a/lib/Money/CurrencyPair.php +++ b/lib/Money/CurrencyPair.php @@ -103,9 +103,9 @@ public function getRatio() public function equals(CurrencyPair $other) { return ( - $this->baseCurrency == $other->baseCurrency - && $this->counterCurrency == $other->counterCurrency - && $this->ratio === $other->ratio + $this->baseCurrency->equals($other->baseCurrency) + && $this->counterCurrency->equals($other->counterCurrency) + && $this->ratio === $other->ratio ); } } From b717d1e72fface074bd9462f6a10201b0f7e6d94 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Mon, 28 Oct 2013 09:25:41 -0500 Subject: [PATCH 6/6] Add descriptive keys to comparison provider --- tests/Money/Tests/CurrencyPairTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Money/Tests/CurrencyPairTest.php b/tests/Money/Tests/CurrencyPairTest.php index 7db484f6..8b91b32e 100644 --- a/tests/Money/Tests/CurrencyPairTest.php +++ b/tests/Money/Tests/CurrencyPairTest.php @@ -107,22 +107,22 @@ public function provideEqualityComparisonPairs() $gbp = new Currency('GBP'); return array( - array( + 'Base Mismatch EUR != GBP' => array( new CurrencyPair($eur, $usd, 1.2500), new CurrencyPair($gbp, $usd, 1.2500), false ), - array( + 'Counter Mismatch USD != GBP' => array( new CurrencyPair($eur, $usd, 1.2500), new CurrencyPair($eur, $gbp, 1.2500), false ), - array( + 'Ratio Mismatch 1.2500 != 1.5000' => array( new CurrencyPair($eur, $usd, 1.2500), new CurrencyPair($eur, $usd, 1.5000), false ), - array( + 'Full Equality EUR/USD 1.2500' => array( new CurrencyPair($eur, $usd, 1.2500), new CurrencyPair($eur, $usd, 1.2500), true