mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE Decimal now allows setting a default value properly
MINOR Unit tests for Decimal field type default value through new test class DecimalTest git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@98056 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
61e06bc57e
commit
2116c46d11
@ -582,7 +582,12 @@ class MySQLDatabase extends SS_Database {
|
||||
$precision = $values['precision'];
|
||||
}
|
||||
|
||||
return 'decimal(' . $precision . ') not null';
|
||||
$defaultValue = '';
|
||||
if(isset($values['default']) && is_numeric($values['default'])) {
|
||||
$defaultValue = ' default ' . $values['default'];
|
||||
}
|
||||
|
||||
return 'decimal(' . $precision . ') not null' . $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,14 +5,15 @@
|
||||
* @subpackage model
|
||||
*/
|
||||
class Decimal extends DBField {
|
||||
protected $wholeSize, $decimalSize;
|
||||
protected $wholeSize, $decimalSize, $defaultValue;
|
||||
|
||||
/**
|
||||
* Create a new Decimal field.
|
||||
*/
|
||||
function __construct($name, $wholeSize = 9, $decimalSize = 2) {
|
||||
function __construct($name, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0) {
|
||||
$this->wholeSize = isset($wholeSize) ? $wholeSize : 9;
|
||||
$this->decimalSize = isset($decimalSize) ? $decimalSize : 2;
|
||||
$this->defaultValue = $defaultValue;
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
@ -25,7 +26,7 @@ class Decimal extends DBField {
|
||||
}
|
||||
|
||||
function requireField() {
|
||||
$parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize", 'arrayValue'=>$this->arrayValue);
|
||||
$parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize", 'default'=>$this->defaultValue, 'arrayValue'=>$this->arrayValue);
|
||||
$values=Array('type'=>'decimal', 'parts'=>$parts);
|
||||
DB::requireField($this->tableName, $this->name, $values);
|
||||
}
|
||||
|
57
tests/fieldtypes/DecimalTest.php
Normal file
57
tests/fieldtypes/DecimalTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class DecimalTest extends SapphireTest {
|
||||
|
||||
public static $fixture_file = 'sapphire/tests/fieldtypes/DecimalTest.yml';
|
||||
|
||||
protected $testDataObject;
|
||||
|
||||
protected $extraDataObjects = array(
|
||||
'DecimalTest_DataObject'
|
||||
);
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->testDataObject = $this->objFromFixture('DecimalTest_DataObject', 'test-dataobject');
|
||||
}
|
||||
|
||||
public function testDefaultValue() {
|
||||
$this->assertEquals($this->testDataObject->MyDecimal1, 0, 'Database default for Decimal type is 0');
|
||||
}
|
||||
|
||||
public function testSpecifiedDefaultValue() {
|
||||
$this->assertEquals($this->testDataObject->MyDecimal2, 2.5, 'Default value for Decimal type is set to 2.5');
|
||||
}
|
||||
|
||||
public function testInvalidSpecifiedDefaultValue() {
|
||||
$this->assertEquals($this->testDataObject->MyDecimal3, 0, 'Invalid default value for Decimal type is casted to 0');
|
||||
}
|
||||
|
||||
public function testSpecifiedDefaultValueInDefaultsArray() {
|
||||
$this->assertEquals($this->testDataObject->MyDecimal4, 4, 'Default value for Decimal type is set to 4');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class DecimalTest_DataObject extends DataObject implements TestOnly {
|
||||
|
||||
public static $db = array(
|
||||
'Name' => 'Varchar',
|
||||
'MyDecimal1' => 'Decimal',
|
||||
'MyDecimal2' => 'Decimal(5,3,2.5)',
|
||||
'MyDecimal3' => 'Decimal(4,2,"Invalid default value")',
|
||||
'MyDecimal4' => 'Decimal'
|
||||
);
|
||||
|
||||
public static $defaults = array(
|
||||
'MyDecimal4' => 4
|
||||
);
|
||||
|
||||
}
|
3
tests/fieldtypes/DecimalTest.yml
Normal file
3
tests/fieldtypes/DecimalTest.yml
Normal file
@ -0,0 +1,3 @@
|
||||
DecimalTest_DataObject:
|
||||
test-dataobject:
|
||||
Name: test
|
Loading…
x
Reference in New Issue
Block a user