Passed
Push — develop ( 24acc5...020914 )
by Reüel
08:17
created

ParserTest::string_to_amount_provider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 70
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 60
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 70
rs 8.8727

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Parser
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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.2
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 array(
77
			// Thousands separator is '' and decimal separator is '.'.
78
			array( '', '.', '1', 1 ),
79
			array( '', '.', '2,5', 2.5 ),
80
			array( '', '.', '2,50', 2.5 ),
81
			array( '', '.', '1250,00', 1250 ),
82
			array( '', '.', '1250,75', 1250.75 ),
83
			array( '', '.', '1250.75', 1250.75 ),
84
			array( '', '.', '1.250,00', 1250 ),
85
			array( '', '.', '2.500,75', 2500.75 ),
86
			array( '', '.', '2500,75-', -2500.75 ),
87
			array( '', '.', '-2500,75', -2500.75 ),
88
			array( '', '.', '2500-', -2500 ),
89
			array( '', '.', '-2500', -2500 ),
90
			array( '', '.', '1-', -1 ),
91
			// Thousands separator is '.' and decimal separator is ','.
92
			array( '.', ',', '1', 1 ),
93
			array( '.', ',', '2,5', 2.5 ),
94
			array( '.', ',', '2,50', 2.5 ),
95
			array( '.', ',', '1250,00', 1250 ),
96
			array( '.', ',', '2500,75', 2500.75 ),
97
			array( '.', ',', '1.250,00', 1250 ),
98
			array( '.', ',', '2.500,75', 2500.75 ),
99
			array( '.', ',', '2.500,750', 2500.75 ),
100
			array( '.', ',', '1.234.567.890', 1234567890 ),
101
			array( '.', ',', '2.500,75-', -2500.75 ),
102
			array( '.', ',', '-2.500,75', -2500.75 ),
103
			array( '.', ',', '2.500-', -2500 ),
104
			array( '.', ',', '-2.500', -2500 ),
105
			array( '.', ',', '1-', -1 ),
106
			// Thousands separator is ',' and decimal separator is '.'.
107
			array( ',', '.', '1', 1 ),
108
			array( ',', '.', '2.5', 2.5 ),
109
			array( ',', '.', '2.50', 2.5 ),
110
			array( ',', '.', '1250.00', 1250 ),
111
			array( ',', '.', '1250.75', 1250.75 ),
112
			array( ',', '.', '1,250.00', 1250 ),
113
			array( ',', '.', '2,500.75', 2500.75 ),
114
			array( ',', '.', '2,500.', 2500 ),
115
			array( ',', '.', '2,500.75-', -2500.75 ),
116
			array( ',', '.', '-2,500.75', -2500.75 ),
117
			array( ',', '.', '2,500-', -2500 ),
118
			array( ',', '.', '-2,500', -2500 ),
119
			array( ',', '.', '1-', -1 ),
120
			// Thousands separator is ' ' and decimal separator is '.'.
121
			array( ' ', '.', '2 500.75', 2500.75 ),
122
			// Thousands separator is 't' and decimal separator is '.'.
123
			array( 't', '.', '2t500.75', 2500.75 ),
124
			array( 't', '.', '2t500.7', 2500.7 ),
125
			// Thousands separator is 't' and decimal separator is '-'.
126
			array( 't', '-', '2t500-75', 2500.75 ),
127
			array( 't', '-', '2t500-7', 2500.7 ),
128
			// Thousands separator is 't' and decimal separator is ' '.
129
			array( 't', ' ', '2t500 75', 2500.75 ),
130
			array( 't', ' ', '2t500 7', 2500.7 ),
131
			// Thousands separator is ' ' and decimal separator is 'd'.
132
			array( ' ', 'd', '2 500d75', 2500.75 ),
133
			array( ' ', 'd', '2 500d7', 2500.7 ),
134
			array( ' ', 'd', '-2 500d75', -2500.75 ),
135
			array( ' ', 'd', '-2 500d7', -2500.7 ),
136
			// Other.
137
			array( '.', ',', 'EUR 1.250', 1250 ),
138
			array( '.', ',', 'EUR 1.250,75', 1250.75 ),
139
			array( '.', ',', 'EUR -1.250', -1250 ),
140
			array( '.', ',', 'EUR -1.250,75', -1250.75 ),
141
			array( '.', ',', '1.250,-', 1250 ),
142
			array( '.', ',', '-1.250,-', -1250 ),
143
			array( '', '', '123456789', 123456789 ),
144
			array( false, false, '123 456 789', 123456789 ),
145
		);
146
	}
147
}
148