Skip to content

Commit 21f5165

Browse files
Merge pull request moneyphp#35 from pamil/master
Base currency and counter currency in CurrencyPair named correctly.
2 parents 7ad2118 + 0624fae commit 21f5165

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

lib/Money/CurrencyPair.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
class CurrencyPair
1515
{
1616
/** @var Currency */
17-
private $counterCurrency;
17+
private $baseCurrency;
1818

1919
/** @var Currency */
20-
private $baseCurrency;
20+
private $counterCurrency;
2121

2222
/** @var float */
2323
private $ratio;
2424

2525
/**
26-
* @param \Money\Currency $counterCurrency
2726
* @param \Money\Currency $baseCurrency
27+
* @param \Money\Currency $counterCurrency
2828
* @param float $ratio
2929
* @throws \Money\InvalidArgumentException
3030
*/
31-
public function __construct(Currency $counterCurrency, Currency $baseCurrency, $ratio)
31+
public function __construct(Currency $baseCurrency, Currency $counterCurrency, $ratio)
3232
{
3333
if(!is_numeric($ratio)) {
3434
throw new InvalidArgumentException("Ratio must be numeric");
@@ -70,12 +70,12 @@ public static function createFromIso($iso)
7070
*/
7171
public function convert(Money $money)
7272
{
73-
if (!$money->getCurrency()->equals($this->counterCurrency)) {
73+
if (!$money->getCurrency()->equals($this->baseCurrency)) {
7474
throw new InvalidArgumentException("The Money has the wrong currency");
7575
}
7676

7777
// @todo add rounding mode?
78-
return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency);
78+
return new Money((int) round($money->getAmount() * $this->ratio), $this->counterCurrency);
7979
}
8080

8181
/** @return \Money\Currency */

tests/Money/Tests/CurrencyPairTest.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,64 @@ public function ParsesIso()
4040
}
4141

4242
/**
43-
* @test
4443
* @expectedException \Money\InvalidArgumentException
4544
* @expectedExceptionMessage Can't create currency pair from ISO string '1.2500', format of string is invalid
4645
*/
4746
public function ParsesIsoWithException()
4847
{
4948
CurrencyPair::createFromIso('1.2500');
5049
}
50+
51+
/**
52+
* @expectedException \Money\InvalidArgumentException
53+
* @expectedExceptionMessage Ratio must be numeric
54+
* @dataProvider dataProviderNonNumericRatio
55+
*/
56+
public function testConstructorWithNonNumericRatio($nonNumericRatio)
57+
{
58+
new CurrencyPair(new Currency('EUR'), new Currency('USD'), $nonNumericRatio);
59+
}
60+
61+
public function testGetRatio()
62+
{
63+
$ratio = 1.2500;
64+
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), $ratio);
65+
66+
$this->assertEquals($ratio, $pair->getRatio());
67+
}
68+
69+
public function testGetBaseCurrency()
70+
{
71+
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
72+
73+
$this->assertEquals(new Currency('EUR'), $pair->getBaseCurrency());
74+
}
75+
76+
public function testGetCounterCurrency()
77+
{
78+
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
79+
80+
$this->assertEquals(new Currency('USD'), $pair->getCounterCurrency());
81+
}
82+
83+
/**
84+
* @expectedException \Money\InvalidArgumentException
85+
* @expectedExceptionMessage The Money has the wrong currency
86+
*/
87+
public function testConvertWithInvalidCurrency()
88+
{
89+
$money = new Money(100, new Currency('JPY'));
90+
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
91+
92+
$pair->convert($money);
93+
}
94+
95+
public function dataProviderNonNumericRatio()
96+
{
97+
return array(
98+
array('NonNumericRatio'),
99+
array('16AlsoIncorrect'),
100+
array('10.00ThisIsToo')
101+
);
102+
}
51103
}

0 commit comments

Comments
 (0)