2009-06-02 05:45:38 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
use SilverStripe\Model\FieldType\DBMoney;
|
|
|
|
|
2009-06-02 05:45:38 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-06-02 05:45:38 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBCompositeTest extends SapphireTest {
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2015-08-30 07:02:55 +02:00
|
|
|
'DBCompositeTest_DataObject',
|
2010-04-12 04:03:16 +02:00
|
|
|
'SubclassedDBFieldObject',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasDatabaseFieldOnDataObject() {
|
2015-08-30 07:02:55 +02:00
|
|
|
$obj = singleton('DBCompositeTest_DataObject');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-02 05:45:38 +02:00
|
|
|
$this->assertTrue($obj->hasDatabaseField('MyMoneyAmount'));
|
|
|
|
$this->assertTrue($obj->hasDatabaseField('MyMoneyCurrency'));
|
|
|
|
$this->assertFalse($obj->hasDatabaseField('MyMoney'));
|
2015-09-04 05:49:22 +02:00
|
|
|
|
|
|
|
// Check that nested fields are exposed properly
|
|
|
|
$this->assertTrue($obj->dbObject('MyMoney')->hasField('Amount'));
|
|
|
|
$this->assertTrue($obj->dbObject('MyMoney')->hasField('Currency'));
|
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
// Test getField accessor
|
2015-08-30 07:02:55 +02:00
|
|
|
$this->assertTrue($obj->MyMoney instanceof DBMoney);
|
2015-09-10 05:46:23 +02:00
|
|
|
$this->assertTrue($obj->MyMoney->hasField('Amount'));
|
|
|
|
$obj->MyMoney->Amount = 100.00;
|
|
|
|
$this->assertEquals(100.00, $obj->MyMoney->Amount);
|
|
|
|
$this->assertEquals(100.00, $obj->MyMoneyAmount);
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
// Not strictly correct
|
|
|
|
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoneyAmount'));
|
|
|
|
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoneyCurrency'));
|
|
|
|
$this->assertFalse($obj->dbObject('MyMoney')->hasField('MyMoney'));
|
2009-06-02 05:45:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 10:49:52 +02:00
|
|
|
/**
|
|
|
|
* Test DataObject::composite_fields() and DataObject::is_composite_field()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCompositeFieldMetaDataFunctions() {
|
2015-08-30 07:02:55 +02:00
|
|
|
$this->assertEquals('Money', DataObject::is_composite_field('DBCompositeTest_DataObject', 'MyMoney'));
|
|
|
|
$this->assertFalse(DataObject::is_composite_field('DBCompositeTest_DataObject', 'Title'));
|
2015-09-04 05:49:22 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'MyMoney' => 'Money',
|
|
|
|
'OverriddenMoney' => 'Money'
|
|
|
|
),
|
2015-08-30 07:02:55 +02:00
|
|
|
DataObject::composite_fields('DBCompositeTest_DataObject')
|
2015-09-04 05:49:22 +02:00
|
|
|
);
|
2009-08-11 10:49:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'MyMoney'));
|
|
|
|
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherMoney'));
|
2015-09-10 05:46:23 +02:00
|
|
|
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'Title'));
|
|
|
|
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherField'));
|
2015-09-04 05:49:22 +02:00
|
|
|
$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() {
|
2015-08-30 07:02:55 +02:00
|
|
|
$object = new DBCompositeTest_DataObject();
|
2015-09-04 05:49:22 +02:00
|
|
|
$object->MyMoney->Currency = 'NZD';
|
|
|
|
$object->MyMoney->Amount = 100.0;
|
|
|
|
$this->assertEquals('NZD', $object->MyMoneyCurrency);
|
|
|
|
$this->assertEquals(100.0, $object->MyMoneyAmount);
|
|
|
|
$object->write();
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
$object2 = DBCompositeTest_DataObject::get()->byID($object->ID);
|
2015-09-04 05:49:22 +02:00
|
|
|
$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() {
|
2015-08-30 07:02:55 +02:00
|
|
|
$object1 = new DBCompositeTest_DataObject();
|
2015-09-04 05:49:22 +02:00
|
|
|
$object2 = new SubclassedDBFieldObject();
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
$this->assertEquals('DBCompositeTest_DataObject', $object1->dbObject('MyMoney')->getTable());
|
|
|
|
$this->assertEquals('DBCompositeTest_DataObject', $object1->dbObject('OverriddenMoney')->getTable());
|
|
|
|
$this->assertEquals('DBCompositeTest_DataObject', $object2->dbObject('MyMoney')->getTable());
|
2015-09-04 05:49:22 +02:00
|
|
|
$this->assertEquals('SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable());
|
|
|
|
$this->assertEquals('SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable());
|
2009-08-11 10:49:52 +02:00
|
|
|
}
|
2009-06-02 05:45:38 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBCompositeTest_DataObject extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Title' => 'Text',
|
|
|
|
'MyMoney' => 'Money',
|
2015-09-04 05:49:22 +02:00
|
|
|
'OverriddenMoney' => 'Money'
|
2009-06-02 05:45:38 +02:00
|
|
|
);
|
|
|
|
}
|
2009-08-11 10:49:52 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
class SubclassedDBFieldObject extends DBCompositeTest_DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'OtherField' => 'Text',
|
|
|
|
'OtherMoney' => 'Money',
|
2015-09-04 05:49:22 +02:00
|
|
|
'OverriddenMoney' => 'Money'
|
2009-08-11 10:49:52 +02:00
|
|
|
);
|
|
|
|
}
|