Completed
Push — develop ( bb86d7...c508c0 )
by Remco
01:43
created

MoneyTest::test_cents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Money
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money;
12
13
use WP_UnitTestCase;
14
15
/**
16
 * Money
17
 *
18
 * @author Remco Tolsma
19
 * @version 1.1.0
20
 * @since   1.0.0
21
 */
22
class MoneyTest extends WP_UnitTestCase {
23
	/**
24
	 * Setup.
25
	 */
26
	public function setUp() {
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
27
		parent::setUp();
28
29
		if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
30
			add_filter( 'number_format_i18n', array( $this, 'maybe_fix_multibyte_number_format' ), 10, 3 );
31
		}
32
	}
33
34
	/**
35
	 * Maybe fix multibyte number format.
36
	 *
37
	 * @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/functions.php#L206-L237
38
	 *
39
	 * @global WP_Locale $wp_locale
40
	 *
41
	 * @param float $number   The number to convert based on locale.
42
	 * @param int   $decimals Optional. Precision of the number of decimal places. Default 0.
43
	 * @return string Converted number in string format.
44
	 */
45
	public function maybe_fix_multibyte_number_format( $formatted, $number, $decimals ) {
46
		global $wp_locale;
47
48
		if ( empty( $wp_locale ) ) {
49
			return $formatted;
50
		}
51
52
		$dec_point     = $wp_locale->number_format['decimal_point'];
53
		$thousands_sep = $wp_locale->number_format['thousands_sep'];
54
55
		if ( 1 === strlen( $dec_point ) && 1 === strlen( $thousands_sep ) ) {
56
			return $formatted;
57
		}
58
59
		$formatted = number_format( $number, $decimals, 'd', 't' );
60
61
		$formatted = strtr( $formatted, array(
62
			'd' => $dec_point,
63
			't' => $thousands_sep,
64
		) );
65
66
		return $formatted;
67
	}
68
69
	/**
70
	 * Test default format.
71
	 *
72
	 * @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
73
	 *
74
	 * @dataProvider default_format_provider
75
	 */
76
	public function test_default_format( $locale, $expected ) {
77
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
78
		switch_to_locale( $locale );
79
80
		$value = Money::get_default_format();
81
82
		$this->assertEquals( $locale, get_locale() );
83
		$this->assertEquals( $expected, $value );
84
	}
85
86
	public function default_format_provider() {
87
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
88
		return array(
89
			array( 'en_US', '%1$s%2$s %3$s' ),
90
			array( 'fr_FR', '%1$s%2$s %3$s' ),
91
			array( 'nl_NL', '%1$s %2$s' ),
92
		);
93
	}
94
95
	/**
96
	 * Test format.
97
	 *
98
	 * @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
99
	 *
100
	 * @param float $amount
101
	 * @param string $currency
102
	 * @param string $locale
103
	 * @param string $expected
104
	 *
105
	 * @dataProvider format_provider
106
	 */
107
	public function test_format( $locale, $currency, $amount, $expected ) {
108
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
109
		switch_to_locale( $locale );
110
111
		$money = new Money( $amount, $currency );
112
113
		$value = $money->format_i18n();
114
115
		$this->assertEquals( $locale, get_locale() );
116
		/* translators: 1: currency symbol, 2: amount, 3: currency code, note: use non-breaking space! */
117
		$this->assertEquals( $expected, $value, 'Locale: ' . get_locale() . ' Money format: ' . Money::get_default_format() . ' Test: ' . _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' ) );
118
	}
119
120
	public function format_provider() {
121
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
122
		return array(
123
			// Dutch
124
			array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ),
125
			array( 'nl_NL', 'NLG', 49.7512, 'G 49,7512' ),
126
			array( 'nl_NL', 'USD', 49.7512, '$ 49,75' ),
127
			array( 'nl_NL', 'USD', 1234567890.1234, '$ 1.234.567.890,12' ),
128
			// English
129
			array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ),
130
			array( 'en_US', 'USD', 1234567890.1234, '$1,234,567,890.12 USD' ),
131
			// French
132
			array( 'fr_FR', 'USD', 1234567890.1234, '$1 234 567 890,12 USD' ),
133
		);
134
	}
135
136
	/**
137
	 * Test cents.
138
	 */
139
	public function test_cents() {
140
		$money = new Money( 100.65, 'EUR' );
141
142
		$this->assertEquals( 10065, $money->get_cents() );
143
144
		$money = new Money( 0.00010, 'BTC' );
145
146
		$this->assertEquals( 0.01, $money->get_cents() );
147
	}
148
}
149