4 * Class CRM_Utils_RuleTest
7 class CRM_Utils_MoneyTest
extends CiviUnitTestCase
{
9 public function setUp() {
14 * @dataProvider subtractCurrenciesDataProvider
15 * @param string $leftOp
16 * @param string $rightOp
17 * @param string $currency
18 * @param float $expectedResult
20 public function testSubtractCurrencies($leftOp, $rightOp, $currency, $expectedResult) {
21 $this->assertEquals($expectedResult, CRM_Utils_Money
::subtractCurrencies($leftOp, $rightOp, $currency));
24 public function testEquals() {
27 for ($i = 0; $i <= 10; $i++
) {
28 $equalValues = CRM_Utils_Money
::equals($testValue, $testValue +
($i * 0.0001), 'USD');
29 $this->assertTrue($equalValues);
32 $this->assertFalse(CRM_Utils_Money
::equals($testValue, $testValue +
0.001000000001, 'USD'));
38 public function subtractCurrenciesDataProvider() {
40 [number_format(300.00, 2), number_format(299.99, 2), 'USD', number_format(0.01, 2)],
44 [number_format(19.99, 2), number_format(20.00, 2), 'USD', number_format(-0.01, 2)],
45 ['notanumber', 5.00, 'USD', NULL],
50 * Test rounded by currency function.
52 * In practice this only does rounding to 2 since rounding by any other amount is
53 * only place-holder supported.
55 public function testFormatLocaleNumericRoundedByCurrency() {
56 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByCurrency(8950.3678, 'NZD');
57 $this->assertEquals('8,950.37', $result);
61 * Test rounded by currency function.
63 * This should be formatted according to European standards - . thousand separator
64 * and , for decimal. (The Europeans are wrong but they don't know that. We will forgive them
65 * because ... metric).
67 public function testFormatLocaleNumericRoundedByCurrencyEuroThousand() {
68 $this->setCurrencySeparators('.');
69 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByCurrency(8950.3678, 'NZD');
70 $this->assertEquals('8.950,37', $result);
71 $this->setCurrencySeparators(',');
75 * Test that using the space character as a currency works
77 public function testSpaceCurrency() {
78 $this->assertEquals(' 8,950.37', CRM_Utils_Money
::format(8950.37, ' '));
82 * Test that passing an invalid currency throws an error
84 public function testInvalidCurrency() {
85 $this->expectException(\CRM_Core_Exception
::class, 'Invalid currency "NOT_A_CURRENCY"');
86 CRM_Utils_Money
::format(4.00, 'NOT_A_CURRENCY');