Test Failed
Push — develop ( 88d89c...00d2c4 )
by Remco
12:20
created

MoneyTest::test_add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
cc 1
nc 1
nop 0
rs 9.9666
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.1
21
 * @since   1.0.0
22
 */
23
class MoneyTest extends WP_UnitTestCase {
24
	/**
25
	 * Setup.
26
	 */
27
	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...
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
		switch_to_locale( $locale );
86
87
		$value = Money::get_default_format();
88
89
		$this->assertEquals( $locale, get_locale() );
90
		$this->assertEquals( $expected, $value );
91
	}
92
93
	/**
94
	 * Default format provider.
95
	 *
96
	 * @return array
97
	 */
98
	public function default_format_provider() {
99
		return array(
100
			array( 'en_US', '%1$s%2$s %3$s' ),
101
			array( 'fr_FR', '%1$s%2$s %3$s' ),
102
			array( 'nl_NL', '%1$s %2$s' ),
103
		);
104
	}
105
106
	/**
107
	 * Test format i18n.
108
	 *
109
	 * @link https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
110
	 *
111
	 * @param string $locale   Locale.
112
	 * @param string $currency Money currency.
113
	 * @param float  $value    Money value.
114
	 * @param string $expected Expected format.
115
	 *
116
	 * @dataProvider format_i18n_provider
117
	 */
118 View Code Duplication
	public function test_format_i18n( $locale, $currency, $value, $expected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
		switch_to_locale( $locale );
120
121
		$money = new Money( $value, $currency );
122
123
		$string = $money->format_i18n();
124
125
		$this->assertEquals( $locale, get_locale() );
126
		/* translators: 1: currency symbol, 2: amount value, 3: currency code, note: use non-breaking space! */
127
		$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' ) );
128
	}
129
130
	/**
131
	 * Format i18n provider.
132
	 *
133
	 * @return array
134
	 */
135 View Code Duplication
	public function format_i18n_provider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
		return array(
137
			// Dutch.
138
			array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ),
139
			array( 'nl_NL', 'NLG', 49.7512, 'G 49,7512' ),
140
			array( 'nl_NL', 'USD', 49.7512, '$ 49,75' ),
141
			array( 'nl_NL', 'USD', 1234567890.1234, '$ 1.234.567.890,12' ),
142
143
			// English.
144
			array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ),
145
			array( 'en_US', 'USD', 1234567890.1234, '$1,234,567,890.12 USD' ),
146
147
			// French.
148
			array( 'fr_FR', 'USD', 1234567890.1234, '$1 234 567 890,12 USD' ),
149
		);
150
	}
151
152
	/**
153
	 * Test format i18n without trailing zeros.
154
	 *
155
	 * @link         https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
156
	 *
157
	 * @param string $locale   Locale.
158
	 * @param string $currency Money currency.
159
	 * @param float  $value    Money value.
160
	 * @param string $expected Expected format.
161
	 *
162
	 * @dataProvider format_i18n_non_trailing_zeros_provider
163
	 */
164 View Code Duplication
	public function test_format_i18n_non_trailing_zeros( $locale, $currency, $value, $expected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
		switch_to_locale( $locale );
166
167
		$money = new Money( $value, $currency );
168
169
		$string = $money->format_i18n_non_trailing_zeros();
170
171
		$this->assertEquals( $locale, get_locale() );
172
173
		/* translators: 1: currency symbol, 2: amount value, 3: currency code, note: use non-breaking space! */
174
		$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' ) );
175
	}
176
177
	/**
178
	 * Format i18n without trailing zeros provider.
179
	 *
180
	 * @return array
181
	 */
182 View Code Duplication
	public function format_i18n_non_trailing_zeros_provider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
		return array(
184
			// Dutch.
185
			array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ),
186
			array( 'nl_NL', 'NLG', 49, 'G 49' ),
187
			array( 'nl_NL', 'USD', 49.00, '$ 49' ),
188
			array( 'nl_NL', 'USD', 1234567890.00, '$ 1.234.567.890' ),
189
190
			// English.
191
			array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ),
192
			array( 'en_US', 'USD', 1234567890.00, '$1,234,567,890 USD' ),
193
194
			// French.
195
			array( 'fr_FR', 'USD', 1234567890, '$1 234 567 890 USD' ),
196
		);
197
	}
198
199
	/**
200
	 * Test cents.
201
	 */
202
	public function test_cents() {
203
		$money = new Money( 100.65, 'EUR' );
204
205
		$this->assertEquals( 10065, $money->get_cents() );
0 ignored issues
show
Deprecated Code introduced by
The method Pronamic\WordPress\Money\Money::get_cents() has been deprecated with message: 1.2.2 Use `Money::get_minor_units()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
206
207
		$money = new Money( 0.00010, 'NLG' );
208
209
		$this->assertEquals( 1, $money->get_cents() );
0 ignored issues
show
Deprecated Code introduced by
The method Pronamic\WordPress\Money\Money::get_cents() has been deprecated with message: 1.2.2 Use `Money::get_minor_units()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
210
	}
211
212
	/**
213
	 * Test minor units.
214
	 *
215
	 * @since 1.2.1
216
	 *
217
	 * @dataProvider minor_units_provider
218
	 *
219
	 * @param string    $currency Currency.
220
	 * @param int|float $value    Money value to test.
221
	 * @param int       $expected Expected value.
222
	 */
223
	public function test_minor_units( $currency, $value, $expected ) {
224
		$money = new Money( $value, $currency );
225
226
		$this->assertEquals( $expected, $money->get_minor_units() );
227
	}
228
229
	/**
230
	 * Minor units provider.
231
	 *
232
	 * @since 1.2.1
233
	 *
234
	 * @return array
235
	 */
236
	public function minor_units_provider() {
237
		return array(
238
			// Value 10.
239
			array( 'JPY', 10, 10 ),
240
			array( 'EUR', 10, 1000 ),
241
			array( 'BHD', 10, 10000 ),
242
			array( 'NLG', 10, 100000 ),
243
244
			// Value 100.65.
245
			array( 'JPY', 100.65, 100 ),
246
			array( 'EUR', 100.65, 10065 ),
247
			array( 'BHD', 100.65, 100650 ),
248
			array( 'NLG', 100.65, 1006500 ),
249
250
			// Value 100.655.
251
			array( 'JPY', 100.655, 100 ),
252
			array( 'EUR', 100.655, 10065 ),
253
			array( 'BHD', 100.655, 100655 ),
254
			array( 'NLG', 100.655, 1006550 ),
255
256
			// Value 0.00010.
257
			array( 'JPY', 0.00010, 0 ),
258
			array( 'EUR', 0.00010, 0 ),
259
			array( 'BHD', 0.00010, 0 ),
260
			array( 'NLG', 0.00010, 1 ),
261
262
			// No currency.
263
			array( null, 10, 1000 ),
264
			array( null, 100.65, 10065 ),
265
			array( null, 100.655, 10065 ),
266
			array( null, 0.00010, 0 ),
267
		);
268
	}
269
270
	/**
271
	 * Test add.
272
	 *
273
	 * @since 1.3.0
274
	 */
275
	public function test_add() {
276
		$money_1 = new Money( 99.75, 'EUR' );
277
278
		$money_2 = new Money( 0.25, 'EUR' );
279
280
		$money_3 = $money_1->add( $money_2 );
281
282
		$this->assertEquals( 100, $money_3->get_value() );
283
	}
284
}
285