|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Money |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2019 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Money |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Money; |
|
12
|
|
|
|
|
13
|
|
|
use WP_Locale; |
|
14
|
|
|
use WP_UnitTestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Money |
|
18
|
|
|
* |
|
19
|
|
|
* @author Remco Tolsma |
|
20
|
|
|
* @version 1.2.0 |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class MoneyTest extends WP_UnitTestCase { |
|
24
|
|
|
/** |
|
25
|
|
|
* Setup. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setUp() { |
|
|
|
|
|
|
28
|
|
|
parent::setUp(); |
|
29
|
|
|
|
|
30
|
|
|
if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { |
|
31
|
|
|
add_filter( 'number_format_i18n', array( $this, 'maybe_fix_multibyte_number_format' ), 10, 3 ); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Maybe fix multibyte number format. |
|
37
|
|
|
* |
|
38
|
|
|
* @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/functions.php#L206-L237 |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $formatted Formatted number. |
|
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
|
|
|
* |
|
44
|
|
|
* @return string Converted number in string format. |
|
45
|
|
|
* @global WP_Locale $wp_locale |
|
46
|
|
|
*/ |
|
47
|
|
|
public function maybe_fix_multibyte_number_format( $formatted, $number, $decimals ) { |
|
48
|
|
|
global $wp_locale; |
|
49
|
|
|
|
|
50
|
|
|
if ( empty( $wp_locale ) ) { |
|
51
|
|
|
return $formatted; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$dec_point = $wp_locale->number_format['decimal_point']; |
|
55
|
|
|
$thousands_sep = $wp_locale->number_format['thousands_sep']; |
|
56
|
|
|
|
|
57
|
|
|
if ( 1 === strlen( $dec_point ) && 1 === strlen( $thousands_sep ) ) { |
|
58
|
|
|
return $formatted; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$formatted = number_format( $number, $decimals, 'd', 't' ); |
|
62
|
|
|
|
|
63
|
|
|
$formatted = strtr( |
|
64
|
|
|
$formatted, |
|
65
|
|
|
array( |
|
66
|
|
|
'd' => $dec_point, |
|
67
|
|
|
't' => $thousands_sep, |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $formatted; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Test default format. |
|
76
|
|
|
* |
|
77
|
|
|
* @link https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php |
|
78
|
|
|
* |
|
79
|
|
|
* @dataProvider default_format_provider |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $locale Locale to test. |
|
82
|
|
|
* @param string $expected Expected default format. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function test_default_format( $locale, $expected ) { |
|
85
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
86
|
|
|
switch_to_locale( $locale ); |
|
87
|
|
|
|
|
88
|
|
|
$value = Money::get_default_format(); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals( $locale, get_locale() ); |
|
91
|
|
|
$this->assertEquals( $expected, $value ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Default format provider. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function default_format_provider() { |
|
100
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
101
|
|
|
return array( |
|
102
|
|
|
array( 'en_US', '%1$s%2$s %3$s' ), |
|
103
|
|
|
array( 'fr_FR', '%1$s%2$s %3$s' ), |
|
104
|
|
|
array( 'nl_NL', '%1$s %2$s' ), |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Test format. |
|
110
|
|
|
* |
|
111
|
|
|
* @link https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $locale Locale. |
|
114
|
|
|
* @param string $currency Money currency. |
|
115
|
|
|
* @param float $value Money value. |
|
116
|
|
|
* @param string $expected Expected format. |
|
117
|
|
|
* |
|
118
|
|
|
* @dataProvider format_provider |
|
119
|
|
|
*/ |
|
120
|
|
|
public function test_format( $locale, $currency, $value, $expected ) { |
|
121
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
122
|
|
|
switch_to_locale( $locale ); |
|
123
|
|
|
|
|
124
|
|
|
$money = new Money( $value, $currency ); |
|
125
|
|
|
|
|
126
|
|
|
$string = $money->format_i18n(); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals( $locale, get_locale() ); |
|
129
|
|
|
/* translators: 1: currency symbol, 2: amount value, 3: currency code, note: use non-breaking space! */ |
|
130
|
|
|
$this->assertEquals( $expected, $string, 'Locale: ' . get_locale() . ' Money format: ' . Money::get_default_format() . ' Test: ' . _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' ) ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Format provider. |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public function format_provider() { |
|
139
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
140
|
|
|
return array( |
|
141
|
|
|
// Dutch. |
|
142
|
|
|
array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ), |
|
143
|
|
|
array( 'nl_NL', 'NLG', 49.7512, 'G 49,7512' ), |
|
144
|
|
|
array( 'nl_NL', 'USD', 49.7512, '$ 49,75' ), |
|
145
|
|
|
array( 'nl_NL', 'USD', 1234567890.1234, '$ 1.234.567.890,12' ), |
|
146
|
|
|
|
|
147
|
|
|
// English. |
|
148
|
|
|
array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ), |
|
149
|
|
|
array( 'en_US', 'USD', 1234567890.1234, '$1,234,567,890.12 USD' ), |
|
150
|
|
|
|
|
151
|
|
|
// French. |
|
152
|
|
|
array( 'fr_FR', 'USD', 1234567890.1234, '$1 234 567 890,12 USD' ), |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Test cents. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function test_cents() { |
|
160
|
|
|
$money = new Money( 100.65, 'EUR' ); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals( 10065, $money->get_cents() ); |
|
163
|
|
|
|
|
164
|
|
|
$money = new Money( 0.00010, 'BTC' ); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals( 0.01, $money->get_cents() ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Test minor units. |
|
171
|
|
|
* |
|
172
|
|
|
* @dataProvider minor_units_provider |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $currency Currency. |
|
175
|
|
|
* @param int $expected Expected value. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function test_minor_units( $currency, $expected ) { |
|
178
|
|
|
$money = new Money( 10, $currency ); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertEquals( $expected, $money->get_minor_units() ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Minor units provider. |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
public function minor_units_provider() { |
|
189
|
|
|
return array( |
|
190
|
|
|
array( 'JPY', 10 ), |
|
191
|
|
|
array( 'EUR', 1000 ), |
|
192
|
|
|
array( 'BHD', 10000 ), |
|
193
|
|
|
array( 'NLG', 100000 ), |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|