Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
package
money
Commits
537f61c8
Commit
537f61c8
authored
Oct 06, 2013
by
Kamil Kokot
Browse files
Solves #34, adds 100% code coverage for CurrencyPair
parent
7ad21182
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/Money/CurrencyPair.php
View file @
537f61c8
...
...
@@ -14,21 +14,21 @@ namespace Money;
class
CurrencyPair
{
/** @var Currency */
private
$
counter
Currency
;
private
$
base
Currency
;
/** @var Currency */
private
$
base
Currency
;
private
$
counter
Currency
;
/** @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
$
counter
Currency
,
Currency
$
base
Currency
,
$ratio
)
public
function
__construct
(
Currency
$
base
Currency
,
Currency
$
counter
Currency
,
$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
->
counter
Currency
))
{
if
(
!
$money
->
getCurrency
()
->
equals
(
$this
->
base
Currency
))
{
throw
new
InvalidArgumentException
(
"The Money has the wrong currency"
);
}
// @todo add rounding mode?
return
new
Money
((
int
)
round
(
$money
->
getAmount
()
*
$this
->
ratio
),
$this
->
base
Currency
);
return
new
Money
((
int
)
round
(
$money
->
getAmount
()
*
$this
->
ratio
),
$this
->
counter
Currency
);
}
/** @return \Money\Currency */
...
...
tests/Money/Tests/CurrencyPairTest.php
View file @
537f61c8
...
...
@@ -48,4 +48,68 @@ class CurrencyPairTest extends PHPUnit_Framework_TestCase
{
CurrencyPair
::
createFromIso
(
'1.2500'
);
}
/**
* @test
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage Ratio must be numeric
* @dataProvider dataProviderNonNumericRatio
*/
public
function
testConstructorWithNonNumericRatio
(
$nonNumericRatio
)
{
new
CurrencyPair
(
new
Currency
(
'EUR'
),
new
Currency
(
'USD'
),
$nonNumericRatio
);
}
/**
* @test
*/
public
function
testGetRatio
()
{
$ratio
=
1.2500
;
$pair
=
new
CurrencyPair
(
new
Currency
(
'EUR'
),
new
Currency
(
'USD'
),
$ratio
);
$this
->
assertEquals
(
$ratio
,
$pair
->
getRatio
());
}
/**
* @test
*/
public
function
testGetBaseCurrency
()
{
$pair
=
new
CurrencyPair
(
new
Currency
(
'EUR'
),
new
Currency
(
'USD'
),
1.2500
);
$this
->
assertEquals
(
new
Currency
(
'EUR'
),
$pair
->
getBaseCurrency
());
}
/**
* @test
*/
public
function
testGetCounterCurrency
()
{
$pair
=
new
CurrencyPair
(
new
Currency
(
'EUR'
),
new
Currency
(
'USD'
),
1.2500
);
$this
->
assertEquals
(
new
Currency
(
'USD'
),
$pair
->
getCounterCurrency
());
}
/**
* @test
* @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'
)
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment