|
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.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class MoneyTest extends WP_UnitTestCase { |
|
22
|
|
|
/** |
|
23
|
|
|
* Test default format. |
|
24
|
|
|
* |
|
25
|
|
|
* @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php |
|
26
|
|
|
* |
|
27
|
|
|
* @dataProvider default_format_provider |
|
28
|
|
|
*/ |
|
29
|
|
|
public function test_default_format( $locale, $expected ) { |
|
30
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
31
|
|
|
switch_to_locale( $locale ); |
|
32
|
|
|
|
|
33
|
|
|
$value = Money::get_default_format(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( $locale, get_locale() ); |
|
36
|
|
|
$this->assertEquals( $expected, $value ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function default_format_provider() { |
|
40
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
41
|
|
|
return array( |
|
42
|
|
|
array( 'en_US', '%1$s%2$s %3$s' ), |
|
43
|
|
|
array( 'fr_FR', '%1$s%2$s %3$s' ), |
|
44
|
|
|
array( 'nl_NL', '%1$s %2$s' ), |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Test format. |
|
50
|
|
|
* |
|
51
|
|
|
* @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php |
|
52
|
|
|
* |
|
53
|
|
|
* @param float $amount |
|
54
|
|
|
* @param string $currency |
|
55
|
|
|
* @param string $locale |
|
56
|
|
|
* @param string $expected |
|
57
|
|
|
* |
|
58
|
|
|
* @dataProvider format_provider |
|
59
|
|
|
*/ |
|
60
|
|
|
public function test_format( $locale, $currency, $amount, $expected ) { |
|
61
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
62
|
|
|
switch_to_locale( $locale ); |
|
63
|
|
|
|
|
64
|
|
|
$money = new Money( $amount, $currency ); |
|
65
|
|
|
|
|
66
|
|
|
$value = $money->format_i18n(); |
|
67
|
|
|
|
|
68
|
|
|
// Expect space instead of non-breaking space with PHP < 5.4. |
|
69
|
|
|
// @link https://core.trac.wordpress.org/ticket/10373 |
|
70
|
|
View Code Duplication |
if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { |
|
|
|
|
|
|
71
|
|
|
// PHP < 5.4.0 does not support multiple bytes in thousands separator. |
|
72
|
|
|
$nbsp = ' '; |
|
73
|
|
|
|
|
74
|
|
|
$expected = str_replace( $nbsp, chr( ord( $nbsp ) ), $expected ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals( $locale, get_locale() ); |
|
78
|
|
|
$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' ) ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function fix_expected_php53( $value ) { |
|
82
|
|
|
// PHP < 5.4.0 does not support multiple bytes in thousands separator. |
|
83
|
|
View Code Duplication |
if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { |
|
|
|
|
|
|
84
|
|
|
$nbsp = ' '; |
|
85
|
|
|
|
|
86
|
|
|
$replace = chr( ord( $nbsp ) ); |
|
87
|
|
|
|
|
88
|
|
|
$value = str_replace( $nbsp, $replace, $value ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function format_provider() { |
|
95
|
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?). |
|
98
|
|
|
return array( |
|
99
|
|
|
// Dutch |
|
100
|
|
|
array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ), |
|
101
|
|
|
array( 'nl_NL', 'NLG', 49.7512, 'G 49,7512' ), |
|
102
|
|
|
array( 'nl_NL', 'USD', 49.7512, '$ 49,75' ), |
|
103
|
|
|
array( 'nl_NL', 'USD', 1234567890.1234, '$ 1.234.567.890,12' ), |
|
104
|
|
|
// English |
|
105
|
|
|
array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ), |
|
106
|
|
|
array( 'en_US', 'USD', 1234567890.1234, '$1,234,567,890.12 USD' ), |
|
107
|
|
|
// French |
|
108
|
|
|
array( 'fr_FR', 'USD', 1234567890.1234, '$' . self::fix_expected_php53( '1 234 567 890,12' ) . ' USD' ), |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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.