mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
#2323: Fix casting of negative currency values; add CurrencyTest unit testing
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@51145 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
4848d0bdb9
commit
397bb5fcc2
@ -15,15 +15,19 @@ class Currency extends Decimal {
|
||||
|
||||
function Nice() {
|
||||
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
|
||||
return '$' . number_format($this->value, 2);
|
||||
$val = '$' . number_format(abs($this->value), 2);
|
||||
if($this->value < 0) return "($val)";
|
||||
else return $val;
|
||||
}
|
||||
|
||||
function Whole() {
|
||||
return '$' . number_format($this->value, 0);
|
||||
$val = '$' . number_format(abs($this->value), 0);
|
||||
if($this->value < 0) return "($val)";
|
||||
else return $val;
|
||||
}
|
||||
|
||||
function setValue($value) {
|
||||
$this->value = ereg_replace('[^0-9.]+','', $value);
|
||||
$this->value = ereg_replace('[^0-9.\-]+','', $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
24
tests/fieldtypes/CurrencyTest.php
Normal file
24
tests/fieldtypes/CurrencyTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
class CurrencyTest extends SapphireTest {
|
||||
function testNiceFormatting() {
|
||||
// Test a bunch of different data values and results in Nice() and Whole()
|
||||
$tests = array(
|
||||
'$50.00' => array('$50.00', '$50'),
|
||||
'this is -50.29 dollars' => array('($50.29)', '($50)'),
|
||||
'this is -50.79 dollars' => array('($50.79)', '($51)'),
|
||||
'this is 50.79 dollars' => array('$50.79', '$51'),
|
||||
'-1000' => array('($1,000.00)','($1,000)'),
|
||||
'-$2000' => array('($2,000.00)', '($2,000)'),
|
||||
'5000' => array('$5,000.00', '$5,000'),
|
||||
);
|
||||
|
||||
foreach($tests as $value => $niceValues) {
|
||||
$c = new Currency('MyField');
|
||||
$c->setValue($value);
|
||||
$this->assertEquals($niceValues[0], $c->Nice());
|
||||
$this->assertEquals($niceValues[1], $c->Whole());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user