Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / tests / phpunit / CRM / Utils / MoneyTest.php
CommitLineData
c10c4749
EL
1<?php
2
3/**
4 * Class CRM_Utils_RuleTest
5 * @group headless
6 */
7class CRM_Utils_MoneyTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13 /**
14 * @dataProvider subtractCurrenciesDataProvider
b8b8e604 15 * @param string $leftOp
16 * @param string $rightOp
17 * @param string $currency
18 * @param float $expectedResult
c10c4749
EL
19 */
20 public function testSubtractCurrencies($leftOp, $rightOp, $currency, $expectedResult) {
21 $this->assertEquals($expectedResult, CRM_Utils_Money::subtractCurrencies($leftOp, $rightOp, $currency));
22 }
23
c16c6ad8
CR
24 public function testEquals() {
25 $testValue = 0.01;
26
3d5246e2
FW
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)));
c16c6ad8
CR
30 }
31
3d5246e2 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));
c16c6ad8
CR
33 }
34
c10c4749
EL
35 /**
36 * @return array
37 */
38 public function subtractCurrenciesDataProvider() {
9099cab3
CW
39 return [
40 [number_format(300.00, 2), number_format(299.99, 2), 'USD', number_format(0.01, 2)],
41 [2, 1, 'USD', 1],
42 [0, 0, 'USD', 0],
43 [1, 2, 'USD', -1],
44 [number_format(19.99, 2), number_format(20.00, 2), 'USD', number_format(-0.01, 2)],
45 ['notanumber', 5.00, 'USD', NULL],
46 ];
c10c4749
EL
47 }
48
b8b8e604 49 /**
50 * Test rounded by currency function.
51 *
52 * In practice this only does rounding to 2 since rounding by any other amount is
53 * only place-holder supported.
54 */
55 public function testFormatLocaleNumericRoundedByCurrency() {
56 $result = CRM_Utils_Money::formatLocaleNumericRoundedByCurrency(8950.3678, 'NZD');
57 $this->assertEquals('8,950.37', $result);
58 }
59
60 /**
61 * Test rounded by currency function.
62 *
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).
66 */
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(',');
72 }
73
5fb64d51
PF
74 /**
75 * Test that using the space character as a currency works
76 */
77 public function testSpaceCurrency() {
78 $this->assertEquals(' 8,950.37', CRM_Utils_Money::format(8950.37, ' '));
79 }
80
81 /**
82 * Test that passing an invalid currency throws an error
83 */
84 public function testInvalidCurrency() {
fe5fa054 85 $this->expectException(\CRM_Core_Exception::class, 'Invalid currency "NOT_A_CURRENCY"');
5fb64d51
PF
86 CRM_Utils_Money::format(4.00, 'NOT_A_CURRENCY');
87 }
88
c10c4749 89}