silverstripe-framework/tests/forms/MoneyFieldTest.php
Damian Mooyman 8331171f2c Merge remote-tracking branch 'origin/3.1' into 3
Conflicts:
	.scrutinizer.yml
	admin/javascript/LeftAndMain.Panel.js
	core/startup/ParameterConfirmationToken.php
	dev/Debug.php
	dev/FixtureBlueprint.php
	docs/en/00_Getting_Started/05_Coding_Conventions.md
	docs/en/00_Getting_Started/index.md
	docs/en/02_Developer_Guides/01_Templates/01_Syntax.md
	filesystem/File.php
	filesystem/Folder.php
	forms/FieldList.php
	forms/LabelField.php
	forms/MoneyField.php
	forms/TextField.php
	forms/TreeDropdownField.php
	forms/Validator.php
	forms/gridfield/GridField.php
	forms/gridfield/GridFieldExportButton.php
	lang/de.yml
	lang/fi.yml
	model/DataObject.php
	model/SQLQuery.php
	parsers/ShortcodeParser.php
	security/ChangePasswordForm.php
	security/Security.php
	tests/control/DirectorTest.php
	tests/core/startup/ParameterConfirmationTokenTest.php
	tests/dev/FixtureBlueprintTest.php
	tests/forms/FieldListTest.php
	tests/forms/MoneyFieldTest.php
	tests/model/SQLQueryTest.php
	tests/security/SecurityTest.php
2015-06-02 19:13:38 +12:00

100 lines
2.4 KiB
PHP

<?php
/**
* @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 Money();
$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 Money();
$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());
}
}