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.0005), 'USD');
29 $this->assertTrue($equalValues, 'Currency - USD' . $testValue . ' is equal to USD' . ($testValue +
($i * 0.0005)));
32 $this->assertFalse(CRM_Utils_Money
::equals($testValue +
0.004, $testValue +
0.006, 'USD'), 'Currency - USD' . ($testValue +
0.004) . ' is different to USD' . ($testValue +
0.006));
38 public function subtractCurrenciesDataProvider() {
40 [number_format(300.00, 2), number_format(299.99, 2), 'USD', number_format(0.01, 2)],
44 [269.565217391, 1, 'USD', 268.57],
45 [number_format(19.99, 2), number_format(20.00, 2), 'USD', number_format(-0.01, 2)],
46 ['notanumber', 5.00, 'USD', NULL],
51 * Test rounded by currency function.
53 * In practice this only does rounding to 2 since rounding by any other amount is
54 * only place-holder supported.
56 public function testFormatLocaleNumericRoundedByCurrency() {
57 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByCurrency(8950.3678, 'NZD');
58 $this->assertEquals('8,950.37', $result);
62 * Test rounded by currency function.
64 * This should be formatted according to European standards - . thousand separator
65 * and , for decimal. (The Europeans are wrong but they don't know that. We will forgive them
66 * because ... metric).
68 public function testFormatLocaleNumericRoundedByCurrencyEuroThousand() {
69 $this->setCurrencySeparators('.');
70 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByCurrency(8950.3678, 'NZD');
71 $this->assertEquals('8.950,37', $result);
72 $this->setCurrencySeparators(',');
76 * Test rounded by currency function with specified precision.
78 * @param string $thousandSeparator
80 * @dataProvider getThousandSeparators
82 public function testFormatLocaleNumericRoundedByPrecision($thousandSeparator) {
83 $this->setCurrencySeparators($thousandSeparator);
84 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByPrecision(8950.3678, 3);
85 $expected = ($thousandSeparator === ',') ?
'8,950.368' : '8.950,368';
86 $this->assertEquals($expected, $result);
90 * Test rounded by currency function with specified precision but without padding to reach it.
92 * @param string $thousandSeparator
94 * @dataProvider getThousandSeparators
96 public function testFormatLocaleNumericRoundedByOptionalPrecision($thousandSeparator) {
97 $this->setCurrencySeparators($thousandSeparator);
98 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByOptionalPrecision(8950.3678, 8);
99 $expected = ($thousandSeparator === ',') ?
'8,950.3678' : '8.950,3678';
100 $this->assertEquals($expected, $result);
102 $result = CRM_Utils_Money
::formatLocaleNumericRoundedByOptionalPrecision(123456789.987654321, 9);
103 $expected = ($thousandSeparator === ',') ?
'123,456,789.98765' : '123.456.789,98765';
104 $this->assertEquals($result, $expected);
108 * Test that using the space character as a currency works
110 public function testSpaceCurrency() {
111 $this->assertEquals(' 8,950.37', CRM_Utils_Money
::format(8950.37, ' '));
115 * Test that passing an invalid currency throws an error
117 public function testInvalidCurrency() {
118 $this->expectException(\CRM_Core_Exception
::class, 'Invalid currency "NOT_A_CURRENCY"');
119 CRM_Utils_Money
::format(4.00, 'NOT_A_CURRENCY');