silverstripe-framework/tests/model/DBCompositeTest.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

123 lines
4.2 KiB
PHP

<?php
use SilverStripe\Model\FieldType\DBMoney;
/**
* @package framework
* @subpackage tests
*/
class DBCompositeTest extends SapphireTest {
protected $extraDataObjects = array(
'DBCompositeTest_DataObject',
'SubclassedDBFieldObject',
);
public function testHasDatabaseFieldOnDataObject() {
$obj = singleton('DBCompositeTest_DataObject');
$this->assertTrue($obj->hasDatabaseField('MyMoneyAmount'));
$this->assertTrue($obj->hasDatabaseField('MyMoneyCurrency'));
$this->assertFalse($obj->hasDatabaseField('MyMoney'));
// Check that nested fields are exposed properly
$this->assertTrue($obj->dbObject('MyMoney')->hasField('Amount'));
$this->assertTrue($obj->dbObject('MyMoney')->hasField('Currency'));
// Test getField accessor
$this->assertTrue($obj->MyMoney instanceof DBMoney);
$this->assertTrue($obj->MyMoney->hasField('Amount'));
$obj->MyMoney->Amount = 100.00;
$this->assertEquals(100.00, $obj->MyMoney->Amount);
$this->assertEquals(100.00, $obj->MyMoneyAmount);
// Not strictly correct
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoneyAmount'));
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoneyCurrency'));
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoney'));
}
/**
* Test DataObject::composite_fields() and DataObject::is_composite_field()
*/
public function testCompositeFieldMetaDataFunctions() {
$this->assertEquals('Money', DataObject::is_composite_field('DBCompositeTest_DataObject', 'MyMoney'));
$this->assertFalse(DataObject::is_composite_field('DBCompositeTest_DataObject', 'Title'));
$this->assertEquals(
array(
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
),
DataObject::composite_fields('DBCompositeTest_DataObject')
);
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'MyMoney'));
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherMoney'));
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'Title'));
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherField'));
$this->assertEquals(
array(
'MyMoney' => 'Money',
'OtherMoney' => 'Money',
'OverriddenMoney' => 'Money',
),
DataObject::composite_fields('SubclassedDBFieldObject')
);
}
/**
* Tests that changes to the fields affect the underlying dataobject, and vice versa
*/
public function testFieldBinding() {
$object = new DBCompositeTest_DataObject();
$object->MyMoney->Currency = 'NZD';
$object->MyMoney->Amount = 100.0;
$this->assertEquals('NZD', $object->MyMoneyCurrency);
$this->assertEquals(100.0, $object->MyMoneyAmount);
$object->write();
$object2 = DBCompositeTest_DataObject::get()->byID($object->ID);
$this->assertEquals('NZD', $object2->MyMoney->Currency);
$this->assertEquals(100.0, $object2->MyMoney->Amount);
$object2->MyMoneyCurrency = 'USD';
$this->assertEquals('USD', $object2->MyMoney->Currency);
$object2->MyMoney->setValue(array('Currency' => 'EUR', 'Amount' => 200.0));
$this->assertEquals('EUR', $object2->MyMoneyCurrency);
$this->assertEquals(200.0, $object2->MyMoneyAmount);
}
/**
* Ensures that composite fields are assigned to the correct tables
*/
public function testInheritedTables() {
$object1 = new DBCompositeTest_DataObject();
$object2 = new SubclassedDBFieldObject();
$this->assertEquals('DBCompositeTest_DataObject', $object1->dbObject('MyMoney')->getTable());
$this->assertEquals('DBCompositeTest_DataObject', $object1->dbObject('OverriddenMoney')->getTable());
$this->assertEquals('DBCompositeTest_DataObject', $object2->dbObject('MyMoney')->getTable());
$this->assertEquals('SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable());
$this->assertEquals('SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable());
}
}
class DBCompositeTest_DataObject extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Text',
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
);
}
class SubclassedDBFieldObject extends DBCompositeTest_DataObject {
private static $db = array(
'OtherField' => 'Text',
'OtherMoney' => 'Money',
'OverriddenMoney' => 'Money'
);
}