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;
|
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
class MoneyFieldTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestObject::class,
|
|
|
|
CustomSetter_Object::class,
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveInto() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$o = new TestObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
$m = new DBMoney();
|
2015-05-20 13:18:34 +02:00
|
|
|
$m->setAmount(123456.78);
|
2009-05-26 00:21:02 +02:00
|
|
|
$m->setCurrency('EUR');
|
|
|
|
$f = new MoneyField('MyMoney', 'MyMoney', $m);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$f->saveInto($o);
|
2015-05-20 13:18:34 +02:00
|
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValueAsMoney() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$o = new TestObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$f = new MoneyField('MyMoney', 'MyMoney');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
$m = new DBMoney();
|
2015-05-20 13:18:34 +02:00
|
|
|
$m->setAmount(123456.78);
|
2009-05-26 00:21:02 +02:00
|
|
|
$m->setCurrency('EUR');
|
|
|
|
$f->setValue($m);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$f->saveInto($o);
|
2015-05-20 13:18:34 +02:00
|
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValueAsArray() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$o = new TestObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$f = new MoneyField('MyMoney', 'MyMoney');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-20 13:18:34 +02:00
|
|
|
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$f->saveInto($o);
|
2015-05-20 13:18:34 +02:00
|
|
|
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
2009-06-30 05:16:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This UT tests if saveInto used customised getters/setters correctly.
|
|
|
|
* Saving values for CustomMoney shall go through the setCustomMoney_Test
|
2014-08-15 08:53:05 +02:00
|
|
|
* setter method and double the value.
|
2009-06-30 05:16:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValueViaSetter() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$o = new CustomSetter_Object();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-30 05:16:28 +02:00
|
|
|
$f = new MoneyField('CustomMoney', 'Test Money Field');
|
2015-05-20 13:18:34 +02:00
|
|
|
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-30 05:16:28 +02:00
|
|
|
$f->saveInto($o);
|
2015-05-20 13:18:34 +02:00
|
|
|
$this->assertEquals((2 * 123456.78), $o->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
2009-06-30 05:16:28 +02:00
|
|
|
}
|
2015-05-20 13:18:34 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|