silverstripe-framework/tests/php/Forms/MoneyFieldTest.php

75 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Forms\Tests;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object;
use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject;
use SilverStripe\ORM\FieldType\DBMoney;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\MoneyField;
class MoneyFieldTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
protected $extraDataObjects = array(
2016-10-14 03:30:05 +02:00
TestObject::class,
CustomSetter_Object::class,
);
public function testSaveInto() {
2016-10-14 03:30:05 +02:00
$o = new TestObject();
2014-08-15 08:53:05 +02:00
$m = new DBMoney();
$m->setAmount(123456.78);
$m->setCurrency('EUR');
$f = new MoneyField('MyMoney', 'MyMoney', $m);
2014-08-15 08:53:05 +02:00
$f->saveInto($o);
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
}
2014-08-15 08:53:05 +02:00
public function testSetValueAsMoney() {
2016-10-14 03:30:05 +02:00
$o = new TestObject();
2014-08-15 08:53:05 +02:00
$f = new MoneyField('MyMoney', 'MyMoney');
2014-08-15 08:53:05 +02:00
$m = new DBMoney();
$m->setAmount(123456.78);
$m->setCurrency('EUR');
$f->setValue($m);
2014-08-15 08:53:05 +02:00
$f->saveInto($o);
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
}
2014-08-15 08:53:05 +02:00
public function testSetValueAsArray() {
2016-10-14 03:30:05 +02:00
$o = new TestObject();
2014-08-15 08:53:05 +02:00
$f = new MoneyField('MyMoney', 'MyMoney');
2014-08-15 08:53:05 +02:00
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
2014-08-15 08:53:05 +02:00
$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
2014-08-15 08:53:05 +02:00
* setter method and double the value.
*/
public function testSetValueViaSetter() {
2016-10-14 03:30:05 +02:00
$o = new CustomSetter_Object();
2014-08-15 08:53:05 +02:00
$f = new MoneyField('CustomMoney', 'Test Money Field');
$f->setValue(array('Currency'=>'EUR','Amount'=>123456.78));
2014-08-15 08:53:05 +02:00
$f->saveInto($o);
$this->assertEquals((2 * 123456.78), $o->MyMoney->getAmount());
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
}
}