Commit 21f5165a authored by Mathias Verraes's avatar Mathias Verraes
Browse files

Merge pull request #35 from pamil/master

Base currency and counter currency in CurrencyPair named correctly.
parents 7ad21182 0624fae2
......@@ -14,21 +14,21 @@ namespace Money;
class CurrencyPair
{
/** @var Currency */
private $counterCurrency;
private $baseCurrency;
/** @var Currency */
private $baseCurrency;
private $counterCurrency;
/** @var float */
private $ratio;
/**
* @param \Money\Currency $counterCurrency
* @param \Money\Currency $baseCurrency
* @param \Money\Currency $counterCurrency
* @param float $ratio
* @throws \Money\InvalidArgumentException
*/
public function __construct(Currency $counterCurrency, Currency $baseCurrency, $ratio)
public function __construct(Currency $baseCurrency, Currency $counterCurrency, $ratio)
{
if(!is_numeric($ratio)) {
throw new InvalidArgumentException("Ratio must be numeric");
......@@ -70,12 +70,12 @@ class CurrencyPair
*/
public function convert(Money $money)
{
if (!$money->getCurrency()->equals($this->counterCurrency)) {
if (!$money->getCurrency()->equals($this->baseCurrency)) {
throw new InvalidArgumentException("The Money has the wrong currency");
}
// @todo add rounding mode?
return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency);
return new Money((int) round($money->getAmount() * $this->ratio), $this->counterCurrency);
}
/** @return \Money\Currency */
......
......@@ -40,7 +40,6 @@ class CurrencyPairTest extends PHPUnit_Framework_TestCase
}
/**
* @test
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage Can't create currency pair from ISO string '1.2500', format of string is invalid
*/
......@@ -48,4 +47,57 @@ class CurrencyPairTest extends PHPUnit_Framework_TestCase
{
CurrencyPair::createFromIso('1.2500');
}
/**
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage Ratio must be numeric
* @dataProvider dataProviderNonNumericRatio
*/
public function testConstructorWithNonNumericRatio($nonNumericRatio)
{
new CurrencyPair(new Currency('EUR'), new Currency('USD'), $nonNumericRatio);
}
public function testGetRatio()
{
$ratio = 1.2500;
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), $ratio);
$this->assertEquals($ratio, $pair->getRatio());
}
public function testGetBaseCurrency()
{
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$this->assertEquals(new Currency('EUR'), $pair->getBaseCurrency());
}
public function testGetCounterCurrency()
{
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$this->assertEquals(new Currency('USD'), $pair->getCounterCurrency());
}
/**
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage The Money has the wrong currency
*/
public function testConvertWithInvalidCurrency()
{
$money = new Money(100, new Currency('JPY'));
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$pair->convert($money);
}
public function dataProviderNonNumericRatio()
{
return array(
array('NonNumericRatio'),
array('16AlsoIncorrect'),
array('10.00ThisIsToo')
);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment