MINOR: Add a test that 0 is falser on int, decimal, currency

Validates that https://github.com/silverstripe/silverstripe-framework/issues/3473 has been fixed

The bug was fixed in #8448
This commit is contained in:
Sam Minnee 2018-11-10 12:25:16 +13:00
parent 45e1fcaf30
commit 2625cea5e3
4 changed files with 64 additions and 10 deletions

View File

@ -1932,10 +1932,36 @@ class DataObjectTest extends SapphireTest
{
$obj = new DataObjectTest\Fixture();
$obj->write();
$this->assertInternalType("int", $obj->ID);
}
/**
* Tests that zero values are returned with the correct types
*/
public function testZeroIsFalse()
{
$obj = new DataObjectTest\Fixture();
$obj->MyInt = 0;
$obj->MyDecimal = 0.00;
$obj->MyCurrency = 0.00;
$obj->write();
$this->assertEquals(0, $obj->MyInt, 'DBInt fields should be integer on first assignment');
$this->assertEquals(0.00, $obj->MyDecimal, 'DBDecimal fields should be float on first assignment');
$this->assertEquals(0.00, $obj->MyCurrency, 'DBCurrency fields should be float on first assignment');
$obj2 = DataObjectTest\Fixture::get()->byId($obj->ID);
$this->assertEquals(0, $obj2->MyInt, 'DBInt fields should be integer');
$this->assertEquals(0.00, $obj2->MyDecimal, 'DBDecimal fields should be float');
$this->assertEquals(0.00, $obj2->MyCurrency, 'DBCurrency fields should be float');
$this->assertFalse((bool)$obj2->MyInt, 'DBInt zero fields should be falsey on fetch from DB');
$this->assertFalse((bool)$obj2->MyDecimal, 'DBDecimal zero fields should be falsey on fetch from DB');
$this->assertFalse((bool)$obj2->MyCurrency, 'DBCurrency zero fields should be falsey on fetch from DB');
}
public function testTwoSubclassesWithTheSameFieldNameWork()
{
// Create two objects of different subclasses, setting the values of fields that are

View File

@ -20,7 +20,11 @@ class Fixture extends DataObject implements TestOnly
'DatetimeField' => 'Datetime',
'MyFieldWithDefault' => 'Varchar',
'MyFieldWithAltDefault' => 'Varchar'
'MyFieldWithAltDefault' => 'Varchar',
'MyInt' => 'Int',
'MyCurrency' => 'Currency',
'MyDecimal'=> 'Decimal',
);
private static $defaults = array(

View File

@ -194,6 +194,9 @@ class DatabaseTest extends SapphireTest
$obj->MyField = "value";
$obj->MyInt = 5;
$obj->MyFloat = 6.0;
// Note: in non-PDO SQLite, whole numbers of a decimal field will be returned as integers rather than floats
$obj->MyDecimal = 7.1;
$obj->MyBoolean = true;
$obj->write();
@ -203,20 +206,40 @@ class DatabaseTest extends SapphireTest
)->record();
// IDs and ints are returned as ints
$this->assertInternalType('int', $record['ID']);
$this->assertInternalType('int', $record['MyInt']);
$this->assertInternalType('int', $record['ID'], 'Primary key should be integer');
$this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer');
$this->assertInternalType('float', $record['MyFloat']);
$this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float');
$this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float');
// Booleans are returned as ints  we follow MySQL's lead
$this->assertInternalType('int', $record['MyBoolean']);
$this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int');
// Strings and enums are returned as strings
$this->assertInternalType('string', $record['MyField']);
$this->assertInternalType('string', $record['ClassName']);
$this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string');
$this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string');
// Dates are returned as strings
$this->assertInternalType('string', $record['Created']);
$this->assertInternalType('string', $record['LastEdited']);
$this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string');
// Ensure that the same is true when using non-prepared statements
$record = DB::query('SELECT * FROM "DatabaseTest_MyObject" WHERE "ID" = ' . (int)$obj->ID)->record();
// IDs and ints are returned as ints
$this->assertInternalType('int', $record['ID'], 'Primary key should be integer');
$this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer');
$this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float');
$this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float');
// Booleans are returned as ints  we follow MySQL's lead
$this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int');
// Strings and enums are returned as strings
$this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string');
$this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string');
// Dates are returned as strings
$this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string');
}
}

View File

@ -16,6 +16,7 @@ class MyObject extends DataObject implements TestOnly
'MyField' => 'Varchar',
'MyInt' => 'Int',
'MyFloat' => 'Float',
'MyDecimal' => 'Decimal',
'MyBoolean' => 'Boolean',
);
}