2009-05-26 00:21:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-05-26 00:21:02 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MoneyFieldTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'MoneyFieldTest_Object',
|
|
|
|
'MoneyFieldTest_CustomSetter_Object',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveInto() {
|
2009-05-26 00:21:02 +02:00
|
|
|
$o = new MoneyFieldTest_Object();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$m = new Money();
|
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() {
|
2009-05-26 00:21:02 +02:00
|
|
|
$o = new MoneyFieldTest_Object();
|
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
|
|
|
|
2009-05-26 00:21:02 +02:00
|
|
|
$m = new Money();
|
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() {
|
2009-05-26 00:21:02 +02:00
|
|
|
$o = new MoneyFieldTest_Object();
|
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() {
|
2009-06-30 05:16:28 +02:00
|
|
|
$o = new MoneyFieldTest_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
|
|
|
}
|
|
|
|
|
|
|
|
class MoneyFieldTest_Object extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'MyMoney' => 'Money',
|
2009-05-26 00:21:02 +02:00
|
|
|
);
|
|
|
|
}
|
2009-06-30 05:16:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Customised class, implementing custom getter and setter methods for
|
|
|
|
* MyMoney.
|
|
|
|
*/
|
|
|
|
class MoneyFieldTest_CustomSetter_Object extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'MyMoney' => 'Money',
|
2009-06-30 05:16:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCustomMoney() {
|
2009-06-30 05:16:28 +02:00
|
|
|
return $this->MyMoney->getValue();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCustomMoney($value) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-30 05:16:28 +02:00
|
|
|
$newAmount = $value->getAmount() * 2;
|
|
|
|
$this->MyMoney->setAmount($newAmount);
|
|
|
|
|
|
|
|
$newAmount = $value->getAmount() * 2;
|
|
|
|
$this->MyMoney->setCurrency($value->getCurrency());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|