silverstripe-framework/tests/model/CompositeDBFieldTest.php

53 lines
1.8 KiB
PHP

<?php
/**
* @package framework
* @subpackage tests
*/
class CompositeDBFieldTest extends SapphireTest {
protected $extraDataObjects = array(
'CompositeDBFieldTest_DataObject',
'SubclassedDBFieldObject',
);
function testHasDatabaseFieldOnDataObject() {
$obj = singleton('CompositeDBFieldTest_DataObject');
$this->assertTrue($obj->hasDatabaseField('MyMoneyAmount'));
$this->assertTrue($obj->hasDatabaseField('MyMoneyCurrency'));
$this->assertFalse($obj->hasDatabaseField('MyMoney'));
}
/**
* Test DataObject::composite_fields() and DataObject::is_composite_field()
*/
function testCompositeFieldMetaDataFunctions() {
$this->assertEquals('Money', DataObject::is_composite_field('CompositeDBFieldTest_DataObject', 'MyMoney'));
$this->assertNull(DataObject::is_composite_field('CompositeDBFieldTest_DataObject', 'Title'));
$this->assertEquals(array('MyMoney' => 'Money'),
DataObject::composite_fields('CompositeDBFieldTest_DataObject'));
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'MyMoney'));
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherMoney'));
$this->assertNull(DataObject::is_composite_field('SubclassedDBFieldObject', 'Title'));
$this->assertNull(DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherField'));
$this->assertEquals(array('MyMoney' => 'Money', 'OtherMoney' => 'Money'),
DataObject::composite_fields('SubclassedDBFieldObject'));
}
}
class CompositeDBFieldTest_DataObject extends DataObject {
static $db = array(
'Title' => 'Text',
'MyMoney' => 'Money',
);
}
class SubclassedDBFieldObject extends CompositeDBFieldTest_DataObject {
static $db = array(
'OtherField' => 'Text',
'OtherMoney' => 'Money',
);
}