1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests; |
4
|
|
|
|
5
|
|
|
use Language; |
6
|
|
|
use SMW\IntlNumberFormatter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \SMW\IntlNumberFormatter |
10
|
|
|
* @group semantic-mediawiki |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.1 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class IntlNumberFormatterTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
'\SMW\IntlNumberFormatter', |
23
|
|
|
new IntlNumberFormatter( 10000 ) |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf( |
27
|
|
|
'\SMW\IntlNumberFormatter', |
28
|
|
|
IntlNumberFormatter::getInstance() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider numberProvider |
34
|
|
|
*/ |
35
|
|
|
public function testLocalizedFormattedNumber( $maxNonExpNumber, $number, $userLanguage, $contentLanguage, $expected ) { |
36
|
|
|
|
37
|
|
|
$instance = new IntlNumberFormatter( $maxNonExpNumber ); |
38
|
|
|
|
39
|
|
|
$instance->setOption( 'user.language', $userLanguage ); |
40
|
|
|
$instance->setOption( 'content.language', $contentLanguage ); |
41
|
|
|
|
42
|
|
|
$this->assertEquals( |
43
|
|
|
$expected, |
44
|
|
|
$instance->format( $number ) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider unformattedNumberByPrecisionProvider |
50
|
|
|
*/ |
51
|
|
|
public function testGetUnformattedNumberByPrecision( $maxNonExpNumber, $number, $precision, $userLanguage, $contentLanguage, $expected ) { |
52
|
|
|
|
53
|
|
|
$instance = new IntlNumberFormatter( $maxNonExpNumber ); |
54
|
|
|
|
55
|
|
|
$instance->setOption( 'user.language', $userLanguage ); |
56
|
|
|
$instance->setOption( 'content.language', $contentLanguage ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( |
59
|
|
|
$expected, |
60
|
|
|
$instance->format( $number, $precision, IntlNumberFormatter::VALUE_FORMAT ) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCompareFloatValue() { |
65
|
|
|
|
66
|
|
|
$instance = new IntlNumberFormatter( 1000 ); |
67
|
|
|
|
68
|
|
|
$instance->setOption( 'user.language', 'en' ); |
69
|
|
|
$instance->setOption( 'content.language', 'en' ); |
70
|
|
|
|
71
|
|
|
$this->assertSame( |
72
|
|
|
$instance->format( 100.0, false, IntlNumberFormatter::VALUE_FORMAT ), |
73
|
|
|
$instance->format( 100, false, IntlNumberFormatter::VALUE_FORMAT ) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @dataProvider separatorProvider |
79
|
|
|
*/ |
80
|
|
|
public function testGetSeparator( $type, $locale, $userLanguage, $contentLanguage, $expected ) { |
81
|
|
|
|
82
|
|
|
$instance = new IntlNumberFormatter( 10000000 ); |
83
|
|
|
|
84
|
|
|
$instance->setOption( 'user.language', $userLanguage ); |
85
|
|
|
$instance->setOption( 'content.language', $contentLanguage ); |
86
|
|
|
|
87
|
|
|
$this->assertEquals( |
88
|
|
|
$expected, |
89
|
|
|
$instance->getSeparator( $type, $locale ) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testCustomSeparator() { |
94
|
|
|
|
95
|
|
|
$instance = new IntlNumberFormatter( 10000000 ); |
96
|
|
|
|
97
|
|
|
$instance->setOption( 'separator.decimal', 'FOO' ); |
98
|
|
|
$instance->setOption( 'separator.thousands', 'BAR' ); |
99
|
|
|
|
100
|
|
|
$this->assertEquals( |
101
|
|
|
'FOO', |
102
|
|
|
$instance->getSeparator( IntlNumberFormatter::DECIMAL_SEPARATOR, 'zzz' ) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->assertEquals( |
106
|
|
|
'BAR', |
107
|
|
|
$instance->getSeparator( IntlNumberFormatter::THOUSANDS_SEPARATOR, 'zzz' ) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testTryToGetSeparatorOnInvalidTypeThrowsException() { |
112
|
|
|
|
113
|
|
|
$instance = new IntlNumberFormatter( 10000000 ); |
114
|
|
|
|
115
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
116
|
|
|
$instance->getSeparator( 'Foo' ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function numberProvider() { |
120
|
|
|
|
121
|
|
|
$provider[] = array( |
|
|
|
|
122
|
|
|
10000, |
123
|
|
|
1000, |
124
|
|
|
'en', |
125
|
|
|
'en', |
126
|
|
|
'1,000' |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$provider[] = array( |
130
|
|
|
10000, |
131
|
|
|
1000.42, |
132
|
|
|
'en', |
133
|
|
|
'en', |
134
|
|
|
'1,000.42' |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$provider[] = array( |
138
|
|
|
10000, |
139
|
|
|
1000000, |
140
|
|
|
'en', |
141
|
|
|
'en', |
142
|
|
|
'1.0e+6' |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$provider[] = array( |
146
|
|
|
10000000, |
147
|
|
|
1000000, |
148
|
|
|
'en', |
149
|
|
|
'en', |
150
|
|
|
'1,000,000' |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return $provider; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function unformattedNumberByPrecisionProvider() { |
157
|
|
|
|
158
|
|
|
$provider['un.1'] = array( |
|
|
|
|
159
|
|
|
10000, |
160
|
|
|
1000, |
161
|
|
|
2, |
162
|
|
|
'en', |
163
|
|
|
'en', |
164
|
|
|
'1000.00' |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$provider['un.2'] = array( |
168
|
|
|
10000, |
169
|
|
|
1000.42, |
170
|
|
|
3, |
171
|
|
|
'en', |
172
|
|
|
'en', |
173
|
|
|
'1000.420' |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$provider['un.3'] = array( |
177
|
|
|
10000, |
178
|
|
|
1000000, |
179
|
|
|
0, |
180
|
|
|
'en', |
181
|
|
|
'en', |
182
|
|
|
'1000000' |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$provider['un.4'] = array( |
186
|
|
|
10000000, |
187
|
|
|
1000000, |
188
|
|
|
2, |
189
|
|
|
'en', |
190
|
|
|
'en', |
191
|
|
|
'1000000.00' |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$provider['un.5'] = array( |
195
|
|
|
10000000, |
196
|
|
|
1000000, |
197
|
|
|
false, |
198
|
|
|
'en', |
199
|
|
|
'en', |
200
|
|
|
'1000000' |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$provider['un.6'] = array( |
204
|
|
|
10000000, |
205
|
|
|
312.23545555, |
206
|
|
|
false, |
207
|
|
|
'en', |
208
|
|
|
'en', |
209
|
|
|
'312.23545555' |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$provider['un.7'] = array( |
213
|
|
|
10000000, |
214
|
|
|
312.23545555, |
215
|
|
|
6, |
216
|
|
|
'en', |
217
|
|
|
'en', |
218
|
|
|
'312.235456' |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
$provider['un.8'] = array( |
222
|
|
|
10000000, |
223
|
|
|
312.23545555, |
224
|
|
|
9, |
225
|
|
|
'en', |
226
|
|
|
'en', |
227
|
|
|
'312.235455550' |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$provider['un.9'] = array( |
231
|
|
|
10000000, |
232
|
|
|
1.334e-13, |
233
|
|
|
false, |
234
|
|
|
'en', |
235
|
|
|
'en', |
236
|
|
|
'1.334e-13' |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$provider['un.10'] = array( |
240
|
|
|
10000000, |
241
|
|
|
1.334e-13, |
242
|
|
|
false, |
243
|
|
|
'en', |
244
|
|
|
'fr', |
245
|
|
|
'1,334e-13' |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
return $provider; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function separatorProvider() { |
252
|
|
|
|
253
|
|
|
$provider['1.en'] = array( |
|
|
|
|
254
|
|
|
IntlNumberFormatter::DECIMAL_SEPARATOR, |
255
|
|
|
IntlNumberFormatter::USER_LANGUAGE, |
256
|
|
|
'en', |
257
|
|
|
'en', |
258
|
|
|
'.' |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
$provider['2.en'] = array( |
262
|
|
|
IntlNumberFormatter::THOUSANDS_SEPARATOR, |
263
|
|
|
IntlNumberFormatter::USER_LANGUAGE, |
264
|
|
|
'en', |
265
|
|
|
'en', |
266
|
|
|
',' |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
$provider['3.en'] = array( |
270
|
|
|
IntlNumberFormatter::DECIMAL_SEPARATOR, |
271
|
|
|
IntlNumberFormatter::CONTENT_LANGUAGE, |
272
|
|
|
'en', |
273
|
|
|
'en', |
274
|
|
|
'.' |
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
$provider['4.en'] = array( |
278
|
|
|
IntlNumberFormatter::THOUSANDS_SEPARATOR, |
279
|
|
|
IntlNumberFormatter::CONTENT_LANGUAGE, |
280
|
|
|
'en', |
281
|
|
|
'en', |
282
|
|
|
',' |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
$provider['5.fr'] = array( |
286
|
|
|
IntlNumberFormatter::DECIMAL_SEPARATOR, |
287
|
|
|
IntlNumberFormatter::USER_LANGUAGE, |
288
|
|
|
'fr', |
289
|
|
|
'en', |
290
|
|
|
',' |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
$provider['6.fr'] = array( |
294
|
|
|
IntlNumberFormatter::THOUSANDS_SEPARATOR, |
295
|
|
|
IntlNumberFormatter::USER_LANGUAGE, |
296
|
|
|
'fr', |
297
|
|
|
'en', |
298
|
|
|
' ' |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
$provider['7.fr'] = array( |
302
|
|
|
IntlNumberFormatter::DECIMAL_SEPARATOR, |
303
|
|
|
IntlNumberFormatter::CONTENT_LANGUAGE, |
304
|
|
|
'fr', |
305
|
|
|
'fr', |
306
|
|
|
',' |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
$provider['8.fr'] = array( |
310
|
|
|
IntlNumberFormatter::THOUSANDS_SEPARATOR, |
311
|
|
|
IntlNumberFormatter::CONTENT_LANGUAGE, |
312
|
|
|
'fr', |
313
|
|
|
'fr', |
314
|
|
|
' ' |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
return $provider; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.