2010-01-18 01:56:29 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-19 02:32:18 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-01-18 01:56:29 +01:00
|
|
|
class CurrencyFieldTest extends SapphireTest {
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testValidate() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$f = new CurrencyField('TestField');
|
|
|
|
|
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates positive decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates negative decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates positive decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('324511434634');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate(new RequiredFields()),
|
|
|
|
'Validates large integers'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValue() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$f = new CurrencyField('TestField');
|
|
|
|
|
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$123.45',
|
|
|
|
'Prepends dollar sign to positive decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$-123.45',
|
|
|
|
'Prepends dollar sign to negative decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$1');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$1.00',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2.5');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2.50',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2500000.13');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2,500,000.13',
|
|
|
|
'Formats large value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2.50000013');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2.50',
|
|
|
|
'Truncates long decimal portions'
|
|
|
|
);
|
2010-01-18 01:56:29 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataValue() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$f = new CurrencyField('TestField');
|
|
|
|
|
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), 123.45
|
|
|
|
);
|
2010-01-18 01:56:29 +01:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), -123.45
|
|
|
|
);
|
2010-01-18 01:56:29 +01:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), -123.45
|
|
|
|
);
|
2010-01-18 01:56:29 +01:00
|
|
|
}
|
|
|
|
}
|