2009-05-26 00:21:02 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object;
|
|
|
|
use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBMoney;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\MoneyField;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MoneyFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
TestObject::class,
|
|
|
|
CustomSetter_Object::class,
|
|
|
|
);
|
|
|
|
|
|
|
|
public function testSaveInto()
|
|
|
|
{
|
|
|
|
$o = new TestObject();
|
|
|
|
|
|
|
|
$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 TestObject();
|
|
|
|
|
|
|
|
$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 TestObject();
|
|
|
|
|
|
|
|
$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 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());
|
|
|
|
}
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|