Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F74161414
PhortuneCurrencyTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jul 26, 05:22
Size
3 KB
Mime Type
text/x-php
Expires
Sun, Jul 28, 05:22 (2 d)
Engine
blob
Format
Raw Data
Handle
19344815
Attached To
rPH Phabricator
PhortuneCurrencyTestCase.php
View Options
<?php
final
class
PhortuneCurrencyTestCase
extends
PhabricatorTestCase
{
public
function
testCurrencyFormatForDisplay
()
{
$map
=
array
(
'0'
=>
'$0.00 USD'
,
'.01'
=>
'$0.01 USD'
,
'1.00'
=>
'$1.00 USD'
,
'-1.23'
=>
'$-1.23 USD'
,
'50000.00'
=>
'$50000.00 USD'
,
);
foreach
(
$map
as
$input
=>
$expect
)
{
$this
->
assertEqual
(
$expect
,
PhortuneCurrency
::
newFromString
(
$input
,
'USD'
)->
formatForDisplay
(),
"newFromString({$input})->formatForDisplay()"
);
}
}
public
function
testCurrencyFormatBareValue
()
{
// NOTE: The PayPal API depends on the behavior of the bare value format!
$map
=
array
(
'0'
=>
'0.00'
,
'.01'
=>
'0.01'
,
'1.00'
=>
'1.00'
,
'-1.23'
=>
'-1.23'
,
'50000.00'
=>
'50000.00'
,
);
foreach
(
$map
as
$input
=>
$expect
)
{
$this
->
assertEqual
(
$expect
,
PhortuneCurrency
::
newFromString
(
$input
,
'USD'
)->
formatBareValue
(),
"newFromString({$input})->formatBareValue()"
);
}
}
public
function
testCurrencyFromString
()
{
$map
=
array
(
'1.00'
=>
100
,
'1.00 USD'
=>
100
,
'$1.00'
=>
100
,
'$1.00 USD'
=>
100
,
'-$1.00 USD'
=>
-
100
,
'$-1.00 USD'
=>
-
100
,
'1'
=>
100
,
'.99'
=>
99
,
'$.99'
=>
99
,
'-$.99'
=>
-
99
,
'$-.99'
=>
-
99
,
'$.99 USD'
=>
99
,
);
foreach
(
$map
as
$input
=>
$expect
)
{
$this
->
assertEqual
(
$expect
,
PhortuneCurrency
::
newFromString
(
$input
,
'USD'
)->
getValue
(),
"newFromString({$input})->getValue()"
);
}
}
public
function
testInvalidCurrencyFromString
()
{
$map
=
array
(
'--1'
,
'$$1'
,
'1 JPY'
,
'buck fiddy'
,
'1.2.3'
,
'1 dollar'
,
);
foreach
(
$map
as
$input
)
{
$caught
=
null
;
try
{
PhortuneCurrency
::
newFromString
(
$input
,
'USD'
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
,
"{$input}"
);
}
}
public
function
testCurrencyRanges
()
{
$value
=
PhortuneCurrency
::
newFromString
(
'3.00 USD'
);
$value
->
assertInRange
(
'2.00 USD'
,
'4.00 USD'
);
$value
->
assertInRange
(
'2.00 USD'
,
null
);
$value
->
assertInRange
(
null
,
'4.00 USD'
);
$value
->
assertInRange
(
null
,
null
);
$caught
=
null
;
try
{
$value
->
assertInRange
(
'4.00 USD'
,
null
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
);
$caught
=
null
;
try
{
$value
->
assertInRange
(
null
,
'2.00 USD'
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
);
$caught
=
null
;
try
{
// Minimum and maximum are reversed here.
$value
->
assertInRange
(
'4.00 USD'
,
'2.00 USD'
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
);
$credit
=
PhortuneCurrency
::
newFromString
(
'-3.00 USD'
);
$credit
->
assertInRange
(
'-4.00 USD'
,
'-2.00 USD'
);
$credit
->
assertInRange
(
'-4.00 USD'
,
null
);
$credit
->
assertInRange
(
null
,
'-2.00 USD'
);
$credit
->
assertInRange
(
null
,
null
);
$caught
=
null
;
try
{
$credit
->
assertInRange
(
'-2.00 USD'
,
null
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
);
$caught
=
null
;
try
{
$credit
->
assertInRange
(
null
,
'-4.00 USD'
);
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
Exception
);
}
}
Event Timeline
Log In to Comment