|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Parser |
|
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_UnitTestCase; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Parser |
|
17
|
|
|
* |
|
18
|
|
|
* @author Remco Tolsma |
|
19
|
|
|
* @version 1.2.0 |
|
20
|
|
|
* @since 1.1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class ParserTest extends WP_UnitTestCase { |
|
23
|
|
|
/** |
|
24
|
|
|
* Setup. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function setUp() { |
|
|
|
|
|
|
27
|
|
|
parent::setUp(); |
|
28
|
|
|
|
|
29
|
|
|
$this->parser = new Parser(); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Test string to amount. |
|
34
|
|
|
* |
|
35
|
|
|
* @link https://github.com/pronamic/wp-pronamic-ideal/blob/3.7.3/classes/Pronamic/WP/Pay/Settings.php#L71-L91 |
|
36
|
|
|
* @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/class-wp-locale.php |
|
37
|
|
|
* @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/functions.php#L206-L237 |
|
38
|
|
|
* |
|
39
|
|
|
* @dataProvider string_to_amount_provider |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $thousands_sep Thousands seperator. |
|
42
|
|
|
* @param string $decimal_sep Decimal seperator. |
|
43
|
|
|
* @param string $string String value to convert. |
|
44
|
|
|
* @param float $expected Expected float value. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function test_string_to_amount( $thousands_sep, $decimal_sep, $string, $expected ) { |
|
47
|
|
|
global $wp_locale; |
|
48
|
|
|
|
|
49
|
|
|
$wp_locale->number_format['thousands_sep'] = $thousands_sep; |
|
50
|
|
|
$wp_locale->number_format['decimal_point'] = $decimal_sep; |
|
51
|
|
|
|
|
52
|
|
|
$money = $this->parser->parse( $string ); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals( $expected, $money->get_value() ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* String to amount provider. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function string_to_amount_provider() { |
|
63
|
|
|
return array( |
|
64
|
|
|
// Thousands seperator is '' and decimal seperator is '.'. |
|
65
|
|
|
array( '', '.', '1', 1 ), |
|
66
|
|
|
array( '', '.', '2,5', 2.5 ), |
|
67
|
|
|
array( '', '.', '2,50', 2.5 ), |
|
68
|
|
|
array( '', '.', '1250,00', 1250 ), |
|
69
|
|
|
array( '', '.', '1250,75', 1250.75 ), |
|
70
|
|
|
array( '', '.', '1250.75', 1250.75 ), |
|
71
|
|
|
array( '', '.', '1.250,00', 1250 ), |
|
72
|
|
|
array( '', '.', '2.500,75', 2500.75 ), |
|
73
|
|
|
// Thousands seperator is '.' and decimal seperator is ','. |
|
74
|
|
|
array( '.', ',', '1', 1 ), |
|
75
|
|
|
array( '.', ',', '2,5', 2.5 ), |
|
76
|
|
|
array( '.', ',', '2,50', 2.5 ), |
|
77
|
|
|
array( '.', ',', '1250,00', 1250 ), |
|
78
|
|
|
array( '.', ',', '2500,75', 2500.75 ), |
|
79
|
|
|
array( '.', ',', '1.250,00', 1250 ), |
|
80
|
|
|
array( '.', ',', '2.500,75', 2500.75 ), |
|
81
|
|
|
array( '.', ',', '2.500,750', 2500.75 ), |
|
82
|
|
|
array( '.', ',', '1.234.567.890', 1234567890 ), |
|
83
|
|
|
// Thousands seperator is ',' and decimal seperator is '.'. |
|
84
|
|
|
array( ',', '.', '1', 1 ), |
|
85
|
|
|
array( ',', '.', '2.5', 2.5 ), |
|
86
|
|
|
array( ',', '.', '2.50', 2.5 ), |
|
87
|
|
|
array( ',', '.', '1250.00', 1250 ), |
|
88
|
|
|
array( ',', '.', '1250.75', 1250.75 ), |
|
89
|
|
|
array( ',', '.', '1,250.00', 1250 ), |
|
90
|
|
|
array( ',', '.', '2,500.75', 2500.75 ), |
|
91
|
|
|
array( ',', '.', '2,500.', 2500 ), |
|
92
|
|
|
// Thousands seperator is ' ' and decimal seperator is '.'. |
|
93
|
|
|
array( ' ', '.', '2 500.75', 2500.75 ), |
|
94
|
|
|
// Thousands seperator is 't' and decimal seperator is '.'. |
|
95
|
|
|
array( 't', '.', '2t500.75', 2500.75 ), |
|
96
|
|
|
array( 't', '.', '2t500.7', 2500.7 ), |
|
97
|
|
|
// Thousands seperator is 't' and decimal seperator is '-'. |
|
98
|
|
|
array( 't', '-', '2t500-75', 2500.75 ), |
|
99
|
|
|
array( 't', '-', '2t500-7', 2500.7 ), |
|
100
|
|
|
// Thousands seperator is 't' and decimal seperator is ' '. |
|
101
|
|
|
array( 't', ' ', '2t500 75', 2500.75 ), |
|
102
|
|
|
array( 't', ' ', '2t500 7', 2500.7 ), |
|
103
|
|
|
// Thousands seperator is ' ' and decimal seperator is 'd'. |
|
104
|
|
|
array( ' ', 'd', '2 500d75', 2500.75 ), |
|
105
|
|
|
array( ' ', 'd', '2 500d7', 2500.7 ), |
|
106
|
|
|
array( ' ', 'd', '-2 500d75', -2500.75 ), |
|
107
|
|
|
array( ' ', 'd', '-2 500d7', -2500.7 ), |
|
108
|
|
|
// Other. |
|
109
|
|
|
array( '', '', '123456789', 123456789 ), |
|
110
|
|
|
array( false, false, '123 456 789', 123456789 ), |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|