mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-27 04:56:02 +02:00
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.
103 lines
2.5 KiB
PHP
103 lines
2.5 KiB
PHP
<?php
|
|
|
|
use SilverStripe\Model\FieldType\DBMoney;
|
|
|
|
/**
|
|
* @package framework
|
|
* @subpackage tests
|
|
*/
|
|
class MoneyFieldTest extends SapphireTest {
|
|
|
|
protected $extraDataObjects = array(
|
|
'MoneyFieldTest_Object',
|
|
'MoneyFieldTest_CustomSetter_Object',
|
|
);
|
|
|
|
public function testSaveInto() {
|
|
$o = new MoneyFieldTest_Object();
|
|
|
|
$m = new DBMoney();
|
|
$m->setAmount(123456.78);
|
|
$m->setCurrency('EUR');
|
|
$f = new MoneyField('MyMoney', 'MyMoney', $m);
|
|
|
|
$f->saveInto($o);
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
|
}
|
|
|
|
public function testSetValueAsMoney() {
|
|
$o = new MoneyFieldTest_Object();
|
|
|
|
$f = new MoneyField('MyMoney', 'MyMoney');
|
|
|
|
$m = new DBMoney();
|
|
$m->setAmount(123456.78);
|
|
$m->setCurrency('EUR');
|
|
$f->setValue($m);
|
|
|
|
$f->saveInto($o);
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
|
}
|
|
|
|
public function testSetValueAsArray() {
|
|
$o = new MoneyFieldTest_Object();
|
|
|
|
$f = new MoneyField('MyMoney', 'MyMoney');
|
|
|
|
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
|
|
|
|
$f->saveInto($o);
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
|
}
|
|
|
|
/**
|
|
* This UT tests if saveInto used customised getters/setters correctly.
|
|
* Saving values for CustomMoney shall go through the setCustomMoney_Test
|
|
* setter method and double the value.
|
|
*/
|
|
public function testSetValueViaSetter() {
|
|
$o = new MoneyFieldTest_CustomSetter_Object();
|
|
|
|
$f = new MoneyField('CustomMoney', 'Test Money Field');
|
|
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
|
|
|
|
$f->saveInto($o);
|
|
$this->assertEquals((2 * 123456.78), $o->MyMoney->getAmount());
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
|
}
|
|
|
|
}
|
|
|
|
class MoneyFieldTest_Object extends DataObject implements TestOnly {
|
|
private static $db = array(
|
|
'MyMoney' => 'Money',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Customised class, implementing custom getter and setter methods for
|
|
* MyMoney.
|
|
*/
|
|
class MoneyFieldTest_CustomSetter_Object extends DataObject implements TestOnly {
|
|
private static $db = array(
|
|
'MyMoney' => 'Money',
|
|
);
|
|
|
|
public function getCustomMoney() {
|
|
return $this->MyMoney->getValue();
|
|
}
|
|
|
|
public function setCustomMoney($value) {
|
|
|
|
$newAmount = $value->getAmount() * 2;
|
|
$this->MyMoney->setAmount($newAmount);
|
|
|
|
$newAmount = $value->getAmount() * 2;
|
|
$this->MyMoney->setCurrency($value->getCurrency());
|
|
|
|
}
|
|
}
|