Test Failed
Push — develop ( f96e38...a28edd )
by Remco
28:54 queued 15:38
created

ParserTest::test_string_to_amount()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 15
rs 9.9666
1
<?php
2
/**
3
 * Parser
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 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.5
20
 * @since   1.1.0
21
 */
22
class ParserTest extends WP_UnitTestCase {
23
	/**
24
	 * Parser.
25
	 *
26
	 * @var Parser
27
	 */
28
	private $parser;
29
30
	/**
31
	 * Setup.
32
	 */
33
	public function setUp() {
34
		parent::setUp();
35
36
		$this->parser = new Parser();
37
	}
38
39
	/**
40
	 * Test string to amount.
41
	 *
42
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/3.7.3/classes/Pronamic/WP/Pay/Settings.php#L71-L91
43
	 * @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/class-wp-locale.php
44
	 * @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/functions.php#L206-L237
45
	 *
46
	 * @dataProvider string_to_amount_provider
47
	 *
48
	 * @param string $thousands_sep Thousands seperator.
49
	 * @param string $decimal_sep   Decimal seperator.
50
	 * @param string $string        String value to convert.
51
	 * @param float  $expected      Expected float value.
52
	 */
53
	public function test_string_to_amount( $thousands_sep, $decimal_sep, $string, $expected ) {
54
		global $wp_locale;
55
56
		$wp_locale->number_format['thousands_sep'] = $thousands_sep;
57
		$wp_locale->number_format['decimal_point'] = $decimal_sep;
58
59
		try {
60
			$money = $this->parser->parse( $string );
61
62
			$value = $money->get_value();
63
		} catch ( \Exception $e ) {
64
			$value = null;
65
		}
66
67
		$this->assertEquals( $expected, $value );
68
	}
69
70
	/**
71
	 * String to amount provider.
72
	 *
73
	 * @return array
74
	 */
75
	public function string_to_amount_provider() {
76
		return [
77
			// Thousands separator is '' and decimal separator is '.'.
78
			[ '', '.', '1', 1 ],
79
			[ '', '.', '2,5', 2.5 ],
80
			[ '', '.', '2,50', 2.5 ],
81
			[ '', '.', '1250,00', 1250 ],
82
			[ '', '.', '1250,75', 1250.75 ],
83
			[ '', '.', '1250.75', 1250.75 ],
84
			[ '', '.', '1.250,00', 1250 ],
85
			[ '', '.', '2.500,75', 2500.75 ],
86
			[ '', '.', '2500,75-', -2500.75 ],
87
			[ '', '.', '-2500,75', -2500.75 ],
88
			[ '', '.', '2500-', -2500 ],
89
			[ '', '.', '-2500', -2500 ],
90
			[ '', '.', '1-', -1 ],
91
			// Thousands separator is '.' and decimal separator is ','.
92
			[ '.', ',', '1', 1 ],
93
			[ '.', ',', '2,5', 2.5 ],
94
			[ '.', ',', '2,50', 2.5 ],
95
			[ '.', ',', '1250,00', 1250 ],
96
			[ '.', ',', '2500,75', 2500.75 ],
97
			[ '.', ',', '1.250,00', 1250 ],
98
			[ '.', ',', '2.500,75', 2500.75 ],
99
			[ '.', ',', '2.500,750', 2500.75 ],
100
			[ '.', ',', '1.234.567.890', 1234567890 ],
101
			[ '.', ',', '2.500,75-', -2500.75 ],
102
			[ '.', ',', '-2.500,75', -2500.75 ],
103
			[ '.', ',', '2.500-', -2500 ],
104
			[ '.', ',', '-2.500', -2500 ],
105
			[ '.', ',', '1-', -1 ],
106
			// Thousands separator is ',' and decimal separator is '.'.
107
			[ ',', '.', '1', 1 ],
108
			[ ',', '.', '2.5', 2.5 ],
109
			[ ',', '.', '2.50', 2.5 ],
110
			[ ',', '.', '1250.00', 1250 ],
111
			[ ',', '.', '1250.75', 1250.75 ],
112
			[ ',', '.', '1,250.00', 1250 ],
113
			[ ',', '.', '2,500.75', 2500.75 ],
114
			[ ',', '.', '2,500.', 2500 ],
115
			[ ',', '.', '2,500.75-', -2500.75 ],
116
			[ ',', '.', '-2,500.75', -2500.75 ],
117
			[ ',', '.', '2,500-', -2500 ],
118
			[ ',', '.', '-2,500', -2500 ],
119
			[ ',', '.', '1-', -1 ],
120
			// Thousands separator is ' ' and decimal separator is '.'.
121
			[ ' ', '.', '2 500.75', 2500.75 ],
122
			// Thousands separator is 't' and decimal separator is '.'.
123
			[ 't', '.', '2t500.75', 2500.75 ],
124
			[ 't', '.', '2t500.7', 2500.7 ],
125
			// Thousands separator is 't' and decimal separator is '-'.
126
			[ 't', '-', '2t500-75', 2500.75 ],
127
			[ 't', '-', '2t500-7', 2500.7 ],
128
			// Thousands separator is 't' and decimal separator is ' '.
129
			[ 't', ' ', '2t500 75', 2500.75 ],
130
			[ 't', ' ', '2t500 7', 2500.7 ],
131
			// Thousands separator is ' ' and decimal separator is 'd'.
132
			[ ' ', 'd', '2 500d75', 2500.75 ],
133
			[ ' ', 'd', '2 500d7', 2500.7 ],
134
			[ ' ', 'd', '-2 500d75', -2500.75 ],
135
			[ ' ', 'd', '-2 500d7', -2500.7 ],
136
			// Other.
137
			[ '.', ',', 'EUR 1.250', 1250 ],
138
			[ '.', ',', 'EUR 1.250,75', 1250.75 ],
139
			[ '.', ',', 'EUR -1.250', -1250 ],
140
			[ '.', ',', 'EUR -1.250,75', -1250.75 ],
141
			[ '.', ',', '1.250,-', 1250 ],
142
			[ '.', ',', '-1.250,-', -1250 ],
143
			[ '', '', '123456789', 123456789 ],
144
			[ false, false, '123 456 789', 123456789 ],
145
		];
146
	}
147
}
148