silverstripe-framework/tests/forms/CurrencyFieldTest.php

108 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* @package framework
* @subpackage tests
*/
class CurrencyFieldTest extends SapphireTest {
public function testValidate() {
$f = new CurrencyField('TestField');
2014-08-15 08:53:05 +02:00
$f->setValue('123.45');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates positive decimals'
);
2014-08-15 08:53:05 +02:00
$f->setValue('-123.45');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates negative decimals'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$123.45');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates positive decimals with sign'
);
2014-08-15 08:53:05 +02:00
$f->setValue('-$123.45');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates negative decimals with sign'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$-123.45');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates negative decimals with sign'
);
2014-08-15 08:53:05 +02:00
$f->setValue('324511434634');
$this->assertTrue(
$f->validate(new RequiredFields()),
'Validates large integers'
);
}
2014-08-15 08:53:05 +02:00
public function testSetValue() {
$f = new CurrencyField('TestField');
2014-08-15 08:53:05 +02:00
$f->setValue('123.45');
$this->assertEquals(
$f->value, '$123.45',
'Prepends dollar sign to positive decimal'
);
2014-08-15 08:53:05 +02:00
$f->setValue('-123.45');
$this->assertEquals(
$f->value, '$-123.45',
'Prepends dollar sign to negative decimal'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$1');
$this->assertEquals(
$f->value, '$1.00',
'Formats small value'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$2.5');
$this->assertEquals(
$f->value, '$2.50',
'Formats small value'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$2500000.13');
$this->assertEquals(
$f->value, '$2,500,000.13',
'Formats large value'
);
2014-08-15 08:53:05 +02:00
$f->setValue('$2.50000013');
$this->assertEquals(
$f->value, '$2.50',
'Truncates long decimal portions'
);
}
2014-08-15 08:53:05 +02:00
public function testDataValue() {
$f = new CurrencyField('TestField');
2014-08-15 08:53:05 +02:00
$f->setValue('$123.45');
$this->assertEquals(
$f->dataValue(), 123.45
);
2014-08-15 08:53:05 +02:00
$f->setValue('-$123.45');
$this->assertEquals(
$f->dataValue(), -123.45
);
2014-08-15 08:53:05 +02:00
$f->setValue('$-123.45');
$this->assertEquals(
$f->dataValue(), -123.45
);
}
}