2009-05-05 10:10:51 +02:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Partially based on Zend_CurrencyTest.
|
2014-08-15 08:53:05 +02:00
|
|
|
|
*
|
2009-05-05 10:10:51 +02:00
|
|
|
|
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
|
|
|
* @version $Id: CurrencyTest.php 14644 2009-04-04 18:59:08Z thomas $
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
|
* @package framework
|
2009-05-05 10:10:51 +02:00
|
|
|
|
* @subpackage tests
|
|
|
|
|
*/
|
|
|
|
|
class MoneyTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
|
protected static $fixture_file = 'MoneyTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
|
'MoneyTest_DataObject',
|
2013-04-22 01:50:57 +02:00
|
|
|
|
'MoneyTest_SubClass',
|
2010-04-12 04:03:16 +02:00
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testMoneyFieldsReturnedAsObjects() {
|
2009-08-19 06:59:40 +02:00
|
|
|
|
$obj = $this->objFromFixture('MoneyTest_DataObject', 'test1');
|
2012-05-09 12:43:22 +02:00
|
|
|
|
$this->assertInstanceOf('Money', $obj->MyMoney);
|
2009-08-19 06:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testLoadFromFixture() {
|
2009-05-22 06:45:07 +02:00
|
|
|
|
$obj = $this->objFromFixture('MoneyTest_DataObject', 'test1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-05-09 12:43:22 +02:00
|
|
|
|
$this->assertInstanceOf('Money', $obj->MyMoney);
|
2009-05-22 06:45:07 +02:00
|
|
|
|
$this->assertEquals($obj->MyMoney->getCurrency(), 'EUR');
|
|
|
|
|
$this->assertEquals($obj->MyMoney->getAmount(), 1.23);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testDataObjectChangedFields() {
|
2009-05-23 07:32:16 +02:00
|
|
|
|
$obj = $this->objFromFixture('MoneyTest_DataObject', 'test1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
|
// Without changes
|
|
|
|
|
$curr = $obj->obj('MyMoney');
|
|
|
|
|
$changed = $obj->getChangedFields();
|
|
|
|
|
$this->assertNotContains('MyMoney', array_keys($changed));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
|
// With changes
|
2012-05-09 12:43:22 +02:00
|
|
|
|
$this->assertInstanceOf('Money', $obj->MyMoney);
|
2009-05-23 07:32:16 +02:00
|
|
|
|
$obj->MyMoney->setAmount(99);
|
|
|
|
|
$changed = $obj->getChangedFields();
|
2011-10-07 17:35:25 +02:00
|
|
|
|
$this->assertContains('MyMoney', array_keys($changed), 'Field is detected as changed');
|
|
|
|
|
$this->assertEquals(2, $changed['MyMoney']['level'], 'Correct change level');
|
2009-05-23 07:32:16 +02:00
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testCanOverwriteSettersWithNull() {
|
2009-05-21 06:46:58 +02:00
|
|
|
|
$obj = new MoneyTest_DataObject();
|
|
|
|
|
|
|
|
|
|
$m1 = new Money();
|
|
|
|
|
$m1->setAmount(987.65);
|
|
|
|
|
$m1->setCurrency('USD');
|
|
|
|
|
$obj->MyMoney = $m1;
|
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-21 06:46:58 +02:00
|
|
|
|
$m2 = new Money();
|
|
|
|
|
$m2->setAmount(null);
|
|
|
|
|
$m2->setCurrency(null);
|
|
|
|
|
$obj->MyMoney = $m2;
|
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
|
|
$moneyTest = DataObject::get_by_id('MoneyTest_DataObject',$obj->ID);
|
|
|
|
|
$this->assertTrue($moneyTest instanceof MoneyTest_DataObject);
|
|
|
|
|
$this->assertEquals('', $moneyTest->MyMoneyCurrency);
|
|
|
|
|
$this->assertEquals(0.0000, $moneyTest->MyMoneyAmount);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
/**
|
2012-12-08 12:20:20 +01:00
|
|
|
|
* Write a Money object to the database, then re-read it to ensure it
|
|
|
|
|
* is re-read properly.
|
|
|
|
|
*/
|
|
|
|
|
public function testGettingWrittenDataObject() {
|
|
|
|
|
$local = i18n::get_locale();
|
|
|
|
|
//make sure that the $ amount is not prefixed by US$, as it would be in non-US locale
|
2014-08-15 08:53:05 +02:00
|
|
|
|
i18n::set_locale('en_US');
|
2010-10-19 00:33:41 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$obj = new MoneyTest_DataObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setAmount(987.65);
|
|
|
|
|
$m->setCurrency('USD');
|
|
|
|
|
$obj->MyMoney = $m;
|
|
|
|
|
$this->assertEquals("$987.65", $obj->MyMoney->Nice(),
|
|
|
|
|
"Money field not added to data object properly when read prior to first writing the record."
|
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$objID = $obj->write();
|
|
|
|
|
|
|
|
|
|
$moneyTest = DataObject::get_by_id('MoneyTest_DataObject',$objID);
|
|
|
|
|
$this->assertTrue($moneyTest instanceof MoneyTest_DataObject);
|
|
|
|
|
$this->assertEquals('USD', $moneyTest->MyMoneyCurrency);
|
|
|
|
|
$this->assertEquals(987.65, $moneyTest->MyMoneyAmount);
|
|
|
|
|
$this->assertEquals("$987.65", $moneyTest->MyMoney->Nice(),
|
|
|
|
|
"Money field not added to data object properly when read."
|
|
|
|
|
);
|
2010-10-19 00:33:41 +02:00
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
|
i18n::set_locale($local);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
public function testToCurrency() {
|
|
|
|
|
$USD = new Money();
|
|
|
|
|
$USD->setLocale('en_US');
|
|
|
|
|
$USD->setAmount(53292.18);
|
|
|
|
|
$this->assertSame('$53,292.18', $USD->Nice());
|
|
|
|
|
$this->assertSame('$ 53.292,18', $USD->Nice(array('format' => 'de_AT')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetSign() {
|
2011-05-30 11:33:27 +02:00
|
|
|
|
$SKR = new Money();
|
|
|
|
|
$SKR->setValue(array(
|
|
|
|
|
'Currency' => 'SKR',
|
2009-05-05 10:10:51 +02:00
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
|
2011-05-30 11:33:27 +02:00
|
|
|
|
$this->assertSame('€', $SKR->getSymbol('EUR','de_AT'));
|
|
|
|
|
$this->assertSame(null, $SKR->getSymbol());
|
2009-05-05 10:10:51 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2011-05-30 11:33:27 +02:00
|
|
|
|
$SKR->getSymbol('EGP', 'de_XX');
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$this->setExpectedException("Exception");
|
|
|
|
|
} catch(Exception $e) {
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$EUR = new Money();
|
|
|
|
|
$EUR->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
$EUR->setLocale('de_DE');
|
|
|
|
|
$this->assertSame('€', $EUR->getSymbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetName()
|
|
|
|
|
{
|
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
$m->setLocale('ar_EG');
|
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
|
$this->assertSame('Estnische Krone', $m->getCurrencyName('EEK','de_AT'));
|
|
|
|
|
$this->assertSame('يورو', $m->getCurrencyName());
|
2009-05-05 10:10:51 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2015-09-04 05:49:22 +02:00
|
|
|
|
$m->getCurrencyName('EGP', 'xy_XY');
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$this->setExpectedException("Exception");
|
|
|
|
|
} catch(Exception $e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetShortName() {
|
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
$m->setLocale('de_AT');
|
|
|
|
|
|
|
|
|
|
$this->assertSame('EUR', $m->getShortName('Euro', 'de_AT'));
|
|
|
|
|
$this->assertSame('USD', $m->getShortName('US-Dollar','de_AT'));
|
|
|
|
|
//$this->assertSame('EUR', $m->getShortName(null, 'de_AT'));
|
|
|
|
|
$this->assertSame('EUR', $m->getShortName());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$m->getShortName('EUR', 'xy_ZT');
|
|
|
|
|
$this->setExpectedException("Exception");
|
|
|
|
|
} catch(Exception $e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testSetValueAsArray() {
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$m->getCurrency(),
|
|
|
|
|
'EUR'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$m->getAmount(),
|
|
|
|
|
3.44
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testSetValueAsMoney() {
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m1 = new Money();
|
|
|
|
|
$m1->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
|
|
|
|
$m2 = new Money();
|
|
|
|
|
$m2->setValue($m1);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$m2->getCurrency(),
|
|
|
|
|
'EUR'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$m2->getAmount(),
|
|
|
|
|
3.44
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testExists() {
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m1 = new Money();
|
2012-04-11 04:48:06 +02:00
|
|
|
|
$this->assertFalse($m1->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m2 = new Money();
|
|
|
|
|
$m2->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 3.44
|
|
|
|
|
));
|
2012-04-11 04:48:06 +02:00
|
|
|
|
$this->assertTrue($m2->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m3 = new Money();
|
|
|
|
|
$m3->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 0
|
|
|
|
|
));
|
2012-04-11 04:48:06 +02:00
|
|
|
|
$this->assertTrue($m3->exists());
|
2009-05-05 10:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testLoadIntoDataObject() {
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$obj = new MoneyTest_DataObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-05-09 12:43:22 +02:00
|
|
|
|
$this->assertInstanceOf('Money', $obj->obj('MyMoney'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 1.23
|
|
|
|
|
));
|
|
|
|
|
$obj->MyMoney = $m;
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($obj->MyMoney->getCurrency(), 'EUR');
|
|
|
|
|
$this->assertEquals($obj->MyMoney->getAmount(), 1.23);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testWriteToDataObject() {
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$obj = new MoneyTest_DataObject();
|
|
|
|
|
$m = new Money();
|
|
|
|
|
$m->setValue(array(
|
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
|
'Amount' => 1.23
|
|
|
|
|
));
|
|
|
|
|
$obj->MyMoney = $m;
|
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'EUR',
|
|
|
|
|
DB::query(sprintf(
|
|
|
|
|
'SELECT "MyMoneyCurrency" FROM "MoneyTest_DataObject" WHERE "ID" = %d',
|
|
|
|
|
$obj->ID
|
|
|
|
|
))->value()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'1.23',
|
|
|
|
|
DB::query(sprintf(
|
|
|
|
|
'SELECT "MyMoneyAmount" FROM "MoneyTest_DataObject" WHERE "ID" = %d',
|
|
|
|
|
$obj->ID
|
|
|
|
|
))->value()
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-04-22 01:50:57 +02:00
|
|
|
|
|
|
|
|
|
public function testMoneyLazyLoading() {
|
|
|
|
|
// Get the object, ensuring that MyOtherMoney will be lazy loaded
|
|
|
|
|
$id = $this->idFromFixture('MoneyTest_SubClass', 'test2');
|
|
|
|
|
$obj = MoneyTest_DataObject::get()->byID($id);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('£2.46', $obj->obj('MyOtherMoney')->Nice());
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
public function testHasAmount() {
|
|
|
|
|
$obj = new MoneyTest_DataObject();
|
|
|
|
|
$m = new Money();
|
|
|
|
|
$obj->MyMoney = $m;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
|
$m->setValue(array('Amount' => 1));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertTrue($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 1.00));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertTrue($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 1.01));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertTrue($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 0.99));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertTrue($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 0.01));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertTrue($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 0));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertFalse($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 0.0));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertFalse($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-09-13 02:21:21 +02:00
|
|
|
|
$m->setValue(array('Amount' => 0.00));
|
2013-09-13 02:27:37 +02:00
|
|
|
|
$this->assertFalse($obj->MyMoney->hasAmount());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
}
|
2013-04-22 01:50:57 +02:00
|
|
|
|
|
2009-05-05 10:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 11:05:03 +02:00
|
|
|
|
/**
|
|
|
|
|
* @package framework
|
|
|
|
|
* @subpackage tests
|
|
|
|
|
*/
|
2009-05-05 10:10:51 +02:00
|
|
|
|
class MoneyTest_DataObject 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',
|
|
|
|
|
//'MyOtherMoney' => 'Money',
|
2009-05-05 10:10:51 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2013-05-30 11:05:03 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @package framework
|
|
|
|
|
* @subpackage tests
|
|
|
|
|
*/
|
2013-04-22 01:50:57 +02:00
|
|
|
|
class MoneyTest_SubClass extends MoneyTest_DataObject implements TestOnly {
|
2013-05-30 11:05:03 +02:00
|
|
|
|
|
|
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
'MyOtherMoney' => 'Money',
|
2013-04-22 01:50:57 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
}
|