|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CurrencyField; |
|
6
|
|
|
use SilverStripe\ORM\FieldType\DBCurrency; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
|
|
9
|
|
|
class DBCurrencyTest extends SapphireTest |
|
10
|
|
|
{ |
|
11
|
|
|
public function testNiceFormatting() |
|
12
|
|
|
{ |
|
13
|
|
|
// Test a bunch of different data values and results in Nice() and Whole() |
|
14
|
|
|
$tests = array( |
|
15
|
|
|
// Test basic operation |
|
16
|
|
|
'$50.00' => array('$50.00', '$50'), |
|
17
|
|
|
|
|
18
|
|
|
// Test removal of junk text |
|
19
|
|
|
'this is -50.29 dollars' => array('($50.29)', '($50)'), |
|
20
|
|
|
'this is -50.79 dollars' => array('($50.79)', '($51)'), |
|
21
|
|
|
'this is 50.79 dollars' => array('$50.79', '$51'), |
|
22
|
|
|
|
|
23
|
|
|
// Test negative numbers |
|
24
|
|
|
'-1000' => array('($1,000.00)','($1,000)'), |
|
25
|
|
|
'-$2,000' => array('($2,000.00)', '($2,000)'), |
|
26
|
|
|
|
|
27
|
|
|
// Test thousands comma |
|
28
|
|
|
'5000' => array('$5,000.00', '$5,000'), |
|
29
|
|
|
|
|
30
|
|
|
// Test scientific notation |
|
31
|
|
|
'5.68434188608E-14' => array('$0.00', '$0'), |
|
32
|
|
|
'5.68434188608E7' => array('$56,843,418.86', '$56,843,419'), |
|
33
|
|
|
"Sometimes Es are still bad: 51 dollars, even though they\'re used in scientific notation" |
|
34
|
|
|
=> array('$51.00', '$51'), |
|
35
|
|
|
"What about 5.68434188608E7 in the middle of a string" => array('$56,843,418.86', '$56,843,419'), |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($tests as $value => $niceValues) { |
|
39
|
|
|
$c = new DBCurrency('MyField'); |
|
40
|
|
|
$c->setValue($value); |
|
41
|
|
|
$this->assertEquals($niceValues[0], $c->Nice()); |
|
42
|
|
|
$this->assertEquals($niceValues[1], $c->Whole()); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testScaffoldedField() |
|
47
|
|
|
{ |
|
48
|
|
|
// Test DBCurrency scaffolds a CurrencyField |
|
49
|
|
|
$currencyDbField = DBCurrency::create('Currency'); |
|
50
|
|
|
$scaffoldedField = $currencyDbField->scaffoldFormField(); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertInstanceOf(CurrencyField::class, $scaffoldedField); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|