2008-03-17 03:10:23 +01:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\FieldType\DBCurrency;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBCurrencyTest extends SapphireTest {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNiceFormatting() {
|
2008-03-17 03:10:23 +01:00
|
|
|
// Test a bunch of different data values and results in Nice() and Whole()
|
|
|
|
$tests = array(
|
2008-03-17 04:03:43 +01:00
|
|
|
// Test basic operation
|
2008-03-17 03:10:23 +01:00
|
|
|
'$50.00' => array('$50.00', '$50'),
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
// Test removal of junk text
|
2008-03-17 03:10:23 +01:00
|
|
|
'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'),
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
// Test negative numbers
|
2008-03-17 03:10:23 +01:00
|
|
|
'-1000' => array('($1,000.00)','($1,000)'),
|
2008-03-17 04:03:43 +01:00
|
|
|
'-$2,000' => array('($2,000.00)', '($2,000)'),
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
// Test thousands comma
|
2008-03-17 03:10:23 +01:00
|
|
|
'5000' => array('$5,000.00', '$5,000'),
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
// Test scientific notation
|
|
|
|
'5.68434188608E-14' => array('$0.00', '$0'),
|
|
|
|
'5.68434188608E7' => array('$56,843,418.86', '$56,843,419'),
|
2012-09-26 23:34:00 +02:00
|
|
|
"Sometimes Es are still bad: 51 dollars, even though they\'re used in scientific notation"
|
|
|
|
=> array('$51.00', '$51'),
|
2008-03-17 04:03:43 +01:00
|
|
|
"What about 5.68434188608E7 in the middle of a string" => array('$56,843,418.86', '$56,843,419'),
|
2008-03-17 03:10:23 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 03:10:23 +01:00
|
|
|
foreach($tests as $value => $niceValues) {
|
2015-08-30 07:02:55 +02:00
|
|
|
$c = new DBCurrency('MyField');
|
2008-03-17 03:10:23 +01:00
|
|
|
$c->setValue($value);
|
|
|
|
$this->assertEquals($niceValues[0], $c->Nice());
|
|
|
|
$this->assertEquals($niceValues[1], $c->Whole());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|