diff options
Diffstat (limited to 'vendor/symfony/intl/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php')
-rw-r--r-- | vendor/symfony/intl/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php | 707 |
1 files changed, 707 insertions, 0 deletions
diff --git a/vendor/symfony/intl/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php b/vendor/symfony/intl/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php new file mode 100644 index 00000000..ad5581b4 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php | |||
@@ -0,0 +1,707 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Intl\Tests\NumberFormatter; | ||
13 | |||
14 | use Symfony\Component\Intl\Globals\IntlGlobals; | ||
15 | use Symfony\Component\Intl\Intl; | ||
16 | use Symfony\Component\Intl\Locale; | ||
17 | use Symfony\Component\Intl\NumberFormatter\NumberFormatter; | ||
18 | use Symfony\Component\Intl\Util\IntlTestHelper; | ||
19 | |||
20 | /** | ||
21 | * Note that there are some values written like -2147483647 - 1. This is the lower 32bit int max and is a known | ||
22 | * behavior of PHP. | ||
23 | */ | ||
24 | abstract class AbstractNumberFormatterTest extends \PHPUnit_Framework_TestCase | ||
25 | { | ||
26 | /** | ||
27 | * @dataProvider formatCurrencyWithDecimalStyleProvider | ||
28 | */ | ||
29 | public function testFormatCurrencyWithDecimalStyle($value, $currency, $expected) | ||
30 | { | ||
31 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
32 | $this->assertEquals($expected, $formatter->formatCurrency($value, $currency)); | ||
33 | } | ||
34 | |||
35 | public function formatCurrencyWithDecimalStyleProvider() | ||
36 | { | ||
37 | return array( | ||
38 | array(100, 'ALL', '100'), | ||
39 | array(100, 'BRL', '100.00'), | ||
40 | array(100, 'CRC', '100'), | ||
41 | array(100, 'JPY', '100'), | ||
42 | array(100, 'CHF', '100'), | ||
43 | array(-100, 'ALL', '-100'), | ||
44 | array(-100, 'BRL', '-100'), | ||
45 | array(-100, 'CRC', '-100'), | ||
46 | array(-100, 'JPY', '-100'), | ||
47 | array(-100, 'CHF', '-100'), | ||
48 | array(1000.12, 'ALL', '1,000.12'), | ||
49 | array(1000.12, 'BRL', '1,000.12'), | ||
50 | array(1000.12, 'CRC', '1,000.12'), | ||
51 | array(1000.12, 'JPY', '1,000.12'), | ||
52 | array(1000.12, 'CHF', '1,000.12') | ||
53 | ); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * @dataProvider formatCurrencyWithCurrencyStyleProvider | ||
58 | */ | ||
59 | public function testFormatCurrencyWithCurrencyStyle($value, $currency, $expected) | ||
60 | { | ||
61 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
62 | $this->assertEquals($expected, $formatter->formatCurrency($value, $currency)); | ||
63 | } | ||
64 | |||
65 | public function formatCurrencyWithCurrencyStyleProvider() | ||
66 | { | ||
67 | return array( | ||
68 | array(100, 'ALL', 'ALL100'), | ||
69 | array(-100, 'ALL', '(ALL100)'), | ||
70 | array(1000.12, 'ALL', 'ALL1,000'), | ||
71 | |||
72 | array(100, 'JPY', '¥100'), | ||
73 | array(-100, 'JPY', '(¥100)'), | ||
74 | array(1000.12, 'JPY', '¥1,000'), | ||
75 | |||
76 | array(100, 'EUR', '€100.00'), | ||
77 | array(-100, 'EUR', '(€100.00)'), | ||
78 | array(1000.12, 'EUR', '€1,000.12'), | ||
79 | ); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * @dataProvider formatCurrencyWithCurrencyStyleCostaRicanColonsRoundingProvider | ||
84 | */ | ||
85 | public function testFormatCurrencyWithCurrencyStyleCostaRicanColonsRounding($value, $currency, $symbol, $expected) | ||
86 | { | ||
87 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
88 | $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); | ||
89 | } | ||
90 | |||
91 | public function formatCurrencyWithCurrencyStyleCostaRicanColonsRoundingProvider() | ||
92 | { | ||
93 | return array( | ||
94 | array(100, 'CRC', 'CRC', '%s100'), | ||
95 | array(-100, 'CRC', 'CRC', '(%s100)'), | ||
96 | array(1000.12, 'CRC', 'CRC', '%s1,000'), | ||
97 | ); | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * @dataProvider formatCurrencyWithCurrencyStyleBrazilianRealRoundingProvider | ||
102 | */ | ||
103 | public function testFormatCurrencyWithCurrencyStyleBrazilianRealRounding($value, $currency, $symbol, $expected) | ||
104 | { | ||
105 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
106 | $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); | ||
107 | } | ||
108 | |||
109 | public function formatCurrencyWithCurrencyStyleBrazilianRealRoundingProvider() | ||
110 | { | ||
111 | return array( | ||
112 | array(100, 'BRL', 'R', '%s$100.00'), | ||
113 | array(-100, 'BRL', 'R', '(%s$100.00)'), | ||
114 | array(1000.12, 'BRL', 'R', '%s$1,000.12'), | ||
115 | |||
116 | // Rounding checks | ||
117 | array(1000.121, 'BRL', 'R', '%s$1,000.12'), | ||
118 | array(1000.123, 'BRL', 'R', '%s$1,000.12'), | ||
119 | array(1000.125, 'BRL', 'R', '%s$1,000.12'), | ||
120 | array(1000.127, 'BRL', 'R', '%s$1,000.13'), | ||
121 | array(1000.129, 'BRL', 'R', '%s$1,000.13'), | ||
122 | array(11.50999, 'BRL', 'R', '%s$11.51'), | ||
123 | array(11.9999464, 'BRL', 'R', '%s$12.00') | ||
124 | ); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * @dataProvider formatCurrencyWithCurrencyStyleSwissRoundingProvider | ||
129 | */ | ||
130 | public function testFormatCurrencyWithCurrencyStyleSwissRounding($value, $currency, $symbol, $expected) | ||
131 | { | ||
132 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
133 | $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); | ||
134 | } | ||
135 | |||
136 | public function formatCurrencyWithCurrencyStyleSwissRoundingProvider() | ||
137 | { | ||
138 | return array( | ||
139 | array(100, 'CHF', 'CHF', '%s100.00'), | ||
140 | array(-100, 'CHF', 'CHF', '(%s100.00)'), | ||
141 | array(1000.12, 'CHF', 'CHF', '%s1,000.10'), | ||
142 | array('1000.12', 'CHF', 'CHF', '%s1,000.10'), | ||
143 | |||
144 | // Rounding checks | ||
145 | array(1000.121, 'CHF', 'CHF', '%s1,000.10'), | ||
146 | array(1000.123, 'CHF', 'CHF', '%s1,000.10'), | ||
147 | array(1000.125, 'CHF', 'CHF', '%s1,000.10'), | ||
148 | array(1000.127, 'CHF', 'CHF', '%s1,000.15'), | ||
149 | array(1000.129, 'CHF', 'CHF', '%s1,000.15'), | ||
150 | |||
151 | array(1200000.00, 'CHF', 'CHF', '%s1,200,000.00'), | ||
152 | array(1200000.1, 'CHF', 'CHF', '%s1,200,000.10'), | ||
153 | array(1200000.10, 'CHF', 'CHF', '%s1,200,000.10'), | ||
154 | array(1200000.101, 'CHF', 'CHF', '%s1,200,000.10') | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | public function testFormat() | ||
159 | { | ||
160 | $errorCode = IntlGlobals::U_ZERO_ERROR; | ||
161 | $errorMessage = 'U_ZERO_ERROR'; | ||
162 | |||
163 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
164 | $this->assertSame('9.555', $formatter->format(9.555)); | ||
165 | |||
166 | $this->assertSame($errorMessage, $this->getIntlErrorMessage()); | ||
167 | $this->assertSame($errorCode, $this->getIntlErrorCode()); | ||
168 | $this->assertFalse($this->isIntlFailure($this->getIntlErrorCode())); | ||
169 | $this->assertSame($errorMessage, $formatter->getErrorMessage()); | ||
170 | $this->assertSame($errorCode, $formatter->getErrorCode()); | ||
171 | $this->assertFalse($this->isIntlFailure($formatter->getErrorCode())); | ||
172 | } | ||
173 | |||
174 | public function testFormatWithCurrencyStyle() | ||
175 | { | ||
176 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
177 | $this->assertEquals('¤1.00', $formatter->format(1)); | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * @dataProvider formatTypeInt32Provider | ||
182 | */ | ||
183 | public function testFormatTypeInt32($formatter, $value, $expected, $message = '') | ||
184 | { | ||
185 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_INT32); | ||
186 | $this->assertEquals($expected, $formattedValue, $message); | ||
187 | } | ||
188 | |||
189 | public function formatTypeInt32Provider() | ||
190 | { | ||
191 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
192 | |||
193 | $message = '->format() TYPE_INT32 formats inconsistently an integer if out of the 32 bit range.'; | ||
194 | |||
195 | return array( | ||
196 | array($formatter, 1, '1'), | ||
197 | array($formatter, 1.1, '1'), | ||
198 | array($formatter, 2147483648, '-2,147,483,648', $message), | ||
199 | array($formatter, -2147483649, '2,147,483,647', $message), | ||
200 | ); | ||
201 | } | ||
202 | |||
203 | /** | ||
204 | * @dataProvider formatTypeInt32WithCurrencyStyleProvider | ||
205 | */ | ||
206 | public function testFormatTypeInt32WithCurrencyStyle($formatter, $value, $expected, $message = '') | ||
207 | { | ||
208 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_INT32); | ||
209 | $this->assertEquals($expected, $formattedValue, $message); | ||
210 | } | ||
211 | |||
212 | public function formatTypeInt32WithCurrencyStyleProvider() | ||
213 | { | ||
214 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
215 | |||
216 | $message = '->format() TYPE_INT32 formats inconsistently an integer if out of the 32 bit range.'; | ||
217 | |||
218 | return array( | ||
219 | array($formatter, 1, '¤1.00'), | ||
220 | array($formatter, 1.1, '¤1.00'), | ||
221 | array($formatter, 2147483648, '(¤2,147,483,648.00)', $message), | ||
222 | array($formatter, -2147483649, '¤2,147,483,647.00', $message) | ||
223 | ); | ||
224 | } | ||
225 | |||
226 | /** | ||
227 | * The parse() method works differently with integer out of the 32 bit range. format() works fine. | ||
228 | * @dataProvider formatTypeInt64Provider | ||
229 | */ | ||
230 | public function testFormatTypeInt64($formatter, $value, $expected) | ||
231 | { | ||
232 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_INT64); | ||
233 | $this->assertEquals($expected, $formattedValue); | ||
234 | } | ||
235 | |||
236 | public function formatTypeInt64Provider() | ||
237 | { | ||
238 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
239 | |||
240 | return array( | ||
241 | array($formatter, 1, '1'), | ||
242 | array($formatter, 1.1, '1'), | ||
243 | array($formatter, 2147483648, '2,147,483,648'), | ||
244 | array($formatter, -2147483649, '-2,147,483,649'), | ||
245 | ); | ||
246 | } | ||
247 | |||
248 | /** | ||
249 | * @dataProvider formatTypeInt64WithCurrencyStyleProvider | ||
250 | */ | ||
251 | public function testFormatTypeInt64WithCurrencyStyle($formatter, $value, $expected) | ||
252 | { | ||
253 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_INT64); | ||
254 | $this->assertEquals($expected, $formattedValue); | ||
255 | } | ||
256 | |||
257 | public function formatTypeInt64WithCurrencyStyleProvider() | ||
258 | { | ||
259 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
260 | |||
261 | return array( | ||
262 | array($formatter, 1, '¤1.00'), | ||
263 | array($formatter, 1.1, '¤1.00'), | ||
264 | array($formatter, 2147483648, '¤2,147,483,648.00'), | ||
265 | array($formatter, -2147483649, '(¤2,147,483,649.00)') | ||
266 | ); | ||
267 | } | ||
268 | |||
269 | /** | ||
270 | * @dataProvider formatTypeDoubleProvider | ||
271 | */ | ||
272 | public function testFormatTypeDouble($formatter, $value, $expected) | ||
273 | { | ||
274 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_DOUBLE); | ||
275 | $this->assertEquals($expected, $formattedValue); | ||
276 | } | ||
277 | |||
278 | public function formatTypeDoubleProvider() | ||
279 | { | ||
280 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
281 | |||
282 | return array( | ||
283 | array($formatter, 1, '1'), | ||
284 | array($formatter, 1.1, '1.1'), | ||
285 | ); | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * @dataProvider formatTypeDoubleWithCurrencyStyleProvider | ||
290 | */ | ||
291 | public function testFormatTypeDoubleWithCurrencyStyle($formatter, $value, $expected) | ||
292 | { | ||
293 | $formattedValue = $formatter->format($value, NumberFormatter::TYPE_DOUBLE); | ||
294 | $this->assertEquals($expected, $formattedValue); | ||
295 | } | ||
296 | |||
297 | public function formatTypeDoubleWithCurrencyStyleProvider() | ||
298 | { | ||
299 | $formatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
300 | |||
301 | return array( | ||
302 | array($formatter, 1, '¤1.00'), | ||
303 | array($formatter, 1.1, '¤1.10'), | ||
304 | ); | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * @dataProvider formatTypeCurrencyProvider | ||
309 | * @expectedException \PHPUnit_Framework_Error_Warning | ||
310 | */ | ||
311 | public function testFormatTypeCurrency($formatter, $value) | ||
312 | { | ||
313 | $formatter->format($value, NumberFormatter::TYPE_CURRENCY); | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * @dataProvider formatTypeCurrencyProvider | ||
318 | */ | ||
319 | public function testFormatTypeCurrencyReturn($formatter, $value) | ||
320 | { | ||
321 | $this->assertFalse(@$formatter->format($value, NumberFormatter::TYPE_CURRENCY)); | ||
322 | } | ||
323 | |||
324 | public function formatTypeCurrencyProvider() | ||
325 | { | ||
326 | $df = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
327 | $cf = $this->getNumberFormatter('en', NumberFormatter::CURRENCY); | ||
328 | |||
329 | return array( | ||
330 | array($df, 1), | ||
331 | array($cf, 1), | ||
332 | ); | ||
333 | } | ||
334 | |||
335 | /** | ||
336 | * @dataProvider formatFractionDigitsProvider | ||
337 | */ | ||
338 | public function testFormatFractionDigits($value, $expected, $fractionDigits = null, $expectedFractionDigits = 1) | ||
339 | { | ||
340 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
341 | |||
342 | if (null !== $fractionDigits) { | ||
343 | $attributeRet = $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $fractionDigits); | ||
344 | } | ||
345 | |||
346 | $formattedValue = $formatter->format($value); | ||
347 | $this->assertSame($expected, $formattedValue); | ||
348 | $this->assertSame($expectedFractionDigits, $formatter->getAttribute(NumberFormatter::FRACTION_DIGITS)); | ||
349 | |||
350 | if (isset($attributeRet)) { | ||
351 | $this->assertTrue($attributeRet); | ||
352 | } | ||
353 | } | ||
354 | |||
355 | public function formatFractionDigitsProvider() | ||
356 | { | ||
357 | return array( | ||
358 | array(1.123, '1.123', null, 0), | ||
359 | array(1.123, '1', 0, 0), | ||
360 | array(1.123, '1.1', 1, 1), | ||
361 | array(1.123, '1.12', 2, 2), | ||
362 | array(1.123, '1', -1, 0), | ||
363 | array(1.123, '1', 'abc', 0) | ||
364 | ); | ||
365 | } | ||
366 | |||
367 | /** | ||
368 | * @dataProvider formatGroupingUsedProvider | ||
369 | */ | ||
370 | public function testFormatGroupingUsed($value, $expected, $groupingUsed = null, $expectedGroupingUsed = 1) | ||
371 | { | ||
372 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
373 | |||
374 | if (null !== $groupingUsed) { | ||
375 | $attributeRet = $formatter->setAttribute(NumberFormatter::GROUPING_USED, $groupingUsed); | ||
376 | } | ||
377 | |||
378 | $formattedValue = $formatter->format($value); | ||
379 | $this->assertSame($expected, $formattedValue); | ||
380 | $this->assertSame($expectedGroupingUsed, $formatter->getAttribute(NumberFormatter::GROUPING_USED)); | ||
381 | |||
382 | if (isset($attributeRet)) { | ||
383 | $this->assertTrue($attributeRet); | ||
384 | } | ||
385 | } | ||
386 | |||
387 | public function formatGroupingUsedProvider() | ||
388 | { | ||
389 | return array( | ||
390 | array(1000, '1,000', null, 1), | ||
391 | array(1000, '1000', 0, 0), | ||
392 | array(1000, '1,000', 1, 1), | ||
393 | array(1000, '1,000', 2, 1), | ||
394 | array(1000, '1000', 'abc', 0), | ||
395 | array(1000, '1,000', -1, 1), | ||
396 | ); | ||
397 | } | ||
398 | |||
399 | /** | ||
400 | * @dataProvider formatRoundingModeRoundHalfUpProvider | ||
401 | */ | ||
402 | public function testFormatRoundingModeHalfUp($value, $expected) | ||
403 | { | ||
404 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
405 | $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); | ||
406 | |||
407 | $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_HALFUP); | ||
408 | $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFUP rounding mode.'); | ||
409 | } | ||
410 | |||
411 | public function formatRoundingModeRoundHalfUpProvider() | ||
412 | { | ||
413 | // The commented value is differently rounded by intl's NumberFormatter in 32 and 64 bit architectures | ||
414 | return array( | ||
415 | array(1.121, '1.12'), | ||
416 | array(1.123, '1.12'), | ||
417 | // array(1.125, '1.13'), | ||
418 | array(1.127, '1.13'), | ||
419 | array(1.129, '1.13'), | ||
420 | ); | ||
421 | } | ||
422 | |||
423 | /** | ||
424 | * @dataProvider formatRoundingModeRoundHalfDownProvider | ||
425 | */ | ||
426 | public function testFormatRoundingModeHalfDown($value, $expected) | ||
427 | { | ||
428 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
429 | $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); | ||
430 | |||
431 | $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_HALFDOWN); | ||
432 | $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFDOWN rounding mode.'); | ||
433 | } | ||
434 | |||
435 | public function formatRoundingModeRoundHalfDownProvider() | ||
436 | { | ||
437 | return array( | ||
438 | array(1.121, '1.12'), | ||
439 | array(1.123, '1.12'), | ||
440 | array(1.125, '1.12'), | ||
441 | array(1.127, '1.13'), | ||
442 | array(1.129, '1.13'), | ||
443 | ); | ||
444 | } | ||
445 | |||
446 | /** | ||
447 | * @dataProvider formatRoundingModeRoundHalfEvenProvider | ||
448 | */ | ||
449 | public function testFormatRoundingModeHalfEven($value, $expected) | ||
450 | { | ||
451 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
452 | $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); | ||
453 | |||
454 | $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_HALFEVEN); | ||
455 | $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFEVEN rounding mode.'); | ||
456 | } | ||
457 | |||
458 | public function formatRoundingModeRoundHalfEvenProvider() | ||
459 | { | ||
460 | return array( | ||
461 | array(1.121, '1.12'), | ||
462 | array(1.123, '1.12'), | ||
463 | array(1.125, '1.12'), | ||
464 | array(1.127, '1.13'), | ||
465 | array(1.129, '1.13'), | ||
466 | ); | ||
467 | } | ||
468 | |||
469 | public function testGetLocale() | ||
470 | { | ||
471 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
472 | $this->assertEquals('en', $formatter->getLocale()); | ||
473 | } | ||
474 | |||
475 | /** | ||
476 | * @dataProvider parseProvider | ||
477 | */ | ||
478 | public function testParse($value, $expected, $message = '') | ||
479 | { | ||
480 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
481 | $parsedValue = $formatter->parse($value, NumberFormatter::TYPE_DOUBLE); | ||
482 | $this->assertSame($expected, $parsedValue, $message); | ||
483 | |||
484 | if ($expected === false) { | ||
485 | $errorCode = IntlGlobals::U_PARSE_ERROR; | ||
486 | $errorMessage = 'Number parsing failed: U_PARSE_ERROR'; | ||
487 | } else { | ||
488 | $errorCode = IntlGlobals::U_ZERO_ERROR; | ||
489 | $errorMessage = 'U_ZERO_ERROR'; | ||
490 | } | ||
491 | |||
492 | $this->assertSame($errorMessage, $this->getIntlErrorMessage()); | ||
493 | $this->assertSame($errorCode, $this->getIntlErrorCode()); | ||
494 | $this->assertSame($errorCode !== 0, $this->isIntlFailure($this->getIntlErrorCode())); | ||
495 | $this->assertSame($errorMessage, $formatter->getErrorMessage()); | ||
496 | $this->assertSame($errorCode, $formatter->getErrorCode()); | ||
497 | $this->assertSame($errorCode !== 0, $this->isIntlFailure($formatter->getErrorCode())); | ||
498 | } | ||
499 | |||
500 | public function parseProvider() | ||
501 | { | ||
502 | return array( | ||
503 | array('prefix1', false, '->parse() does not parse a number with a string prefix.'), | ||
504 | array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'), | ||
505 | ); | ||
506 | } | ||
507 | |||
508 | /** | ||
509 | * @expectedException \PHPUnit_Framework_Error_Warning | ||
510 | */ | ||
511 | public function testParseTypeDefault() | ||
512 | { | ||
513 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
514 | $formatter->parse('1', NumberFormatter::TYPE_DEFAULT); | ||
515 | } | ||
516 | |||
517 | /** | ||
518 | * @dataProvider parseTypeInt32Provider | ||
519 | */ | ||
520 | public function testParseTypeInt32($value, $expected, $message = '') | ||
521 | { | ||
522 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
523 | $parsedValue = $formatter->parse($value, NumberFormatter::TYPE_INT32); | ||
524 | $this->assertSame($expected, $parsedValue); | ||
525 | } | ||
526 | |||
527 | public function parseTypeInt32Provider() | ||
528 | { | ||
529 | return array( | ||
530 | array('1', 1), | ||
531 | array('1.1', 1), | ||
532 | array('2,147,483,647', 2147483647), | ||
533 | array('-2,147,483,648', -2147483647 - 1), | ||
534 | array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'), | ||
535 | array('-2,147,483,649', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer negative range.') | ||
536 | ); | ||
537 | } | ||
538 | |||
539 | public function testParseTypeInt64With32BitIntegerInPhp32Bit() | ||
540 | { | ||
541 | IntlTestHelper::require32Bit($this); | ||
542 | |||
543 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
544 | |||
545 | $parsedValue = $formatter->parse('2,147,483,647', NumberFormatter::TYPE_INT64); | ||
546 | $this->assertInternalType('integer', $parsedValue); | ||
547 | $this->assertEquals(2147483647, $parsedValue); | ||
548 | |||
549 | $parsedValue = $formatter->parse('-2,147,483,648', NumberFormatter::TYPE_INT64); | ||
550 | |||
551 | // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4 | ||
552 | // The negative PHP_INT_MAX was being converted to float | ||
553 | if ( | ||
554 | (version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) || | ||
555 | version_compare(PHP_VERSION, '5.4.4', '>=') | ||
556 | ) { | ||
557 | $this->assertInternalType('int', $parsedValue); | ||
558 | } else { | ||
559 | $this->assertInternalType('float', $parsedValue); | ||
560 | } | ||
561 | |||
562 | $this->assertEquals(-2147483648, $parsedValue); | ||
563 | } | ||
564 | |||
565 | public function testParseTypeInt64With32BitIntegerInPhp64Bit() | ||
566 | { | ||
567 | IntlTestHelper::require64Bit($this); | ||
568 | |||
569 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
570 | |||
571 | $parsedValue = $formatter->parse('2,147,483,647', NumberFormatter::TYPE_INT64); | ||
572 | $this->assertInternalType('integer', $parsedValue); | ||
573 | $this->assertEquals(2147483647, $parsedValue); | ||
574 | |||
575 | $parsedValue = $formatter->parse('-2,147,483,648', NumberFormatter::TYPE_INT64); | ||
576 | $this->assertInternalType('integer', $parsedValue); | ||
577 | $this->assertEquals(-2147483647 - 1, $parsedValue); | ||
578 | } | ||
579 | |||
580 | /** | ||
581 | * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers. | ||
582 | */ | ||
583 | public function testParseTypeInt64With64BitIntegerInPhp32Bit() | ||
584 | { | ||
585 | IntlTestHelper::require32Bit($this); | ||
586 | |||
587 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
588 | |||
589 | // int 64 using only 32 bit range strangeness | ||
590 | $parsedValue = $formatter->parse('2,147,483,648', NumberFormatter::TYPE_INT64); | ||
591 | $this->assertInternalType('float', $parsedValue); | ||
592 | $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.'); | ||
593 | |||
594 | $parsedValue = $formatter->parse('-2,147,483,649', NumberFormatter::TYPE_INT64); | ||
595 | $this->assertInternalType('float', $parsedValue); | ||
596 | $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.'); | ||
597 | } | ||
598 | |||
599 | /** | ||
600 | * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers. | ||
601 | */ | ||
602 | public function testParseTypeInt64With64BitIntegerInPhp64Bit() | ||
603 | { | ||
604 | IntlTestHelper::require64Bit($this); | ||
605 | |||
606 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
607 | |||
608 | $parsedValue = $formatter->parse('2,147,483,648', NumberFormatter::TYPE_INT64); | ||
609 | $this->assertInternalType('integer', $parsedValue); | ||
610 | |||
611 | // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4 | ||
612 | // A 32 bit integer was being generated instead of a 64 bit integer | ||
613 | if ( | ||
614 | (version_compare(PHP_VERSION, '5.3.14', '<')) || | ||
615 | (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<')) | ||
616 | ) { | ||
617 | $this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).'); | ||
618 | } else { | ||
619 | $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).'); | ||
620 | } | ||
621 | |||
622 | $parsedValue = $formatter->parse('-2,147,483,649', NumberFormatter::TYPE_INT64); | ||
623 | $this->assertInternalType('integer', $parsedValue); | ||
624 | |||
625 | // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4 | ||
626 | // A 32 bit integer was being generated instead of a 64 bit integer | ||
627 | if ( | ||
628 | (version_compare(PHP_VERSION, '5.3.14', '<')) || | ||
629 | (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<')) | ||
630 | ) { | ||
631 | $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).'); | ||
632 | } else { | ||
633 | $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).'); | ||
634 | } | ||
635 | } | ||
636 | |||
637 | /** | ||
638 | * @dataProvider parseTypeDoubleProvider | ||
639 | */ | ||
640 | public function testParseTypeDouble($value, $expectedValue) | ||
641 | { | ||
642 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
643 | $parsedValue = $formatter->parse($value, NumberFormatter::TYPE_DOUBLE); | ||
644 | $this->assertSame($expectedValue, $parsedValue); | ||
645 | } | ||
646 | |||
647 | public function parseTypeDoubleProvider() | ||
648 | { | ||
649 | return array( | ||
650 | array('1', (float) 1), | ||
651 | array('1.1', 1.1), | ||
652 | array('9,223,372,036,854,775,808', 9223372036854775808), | ||
653 | array('-9,223,372,036,854,775,809', -9223372036854775809), | ||
654 | ); | ||
655 | } | ||
656 | |||
657 | /** | ||
658 | * @expectedException \PHPUnit_Framework_Error_Warning | ||
659 | */ | ||
660 | public function testParseTypeCurrency() | ||
661 | { | ||
662 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
663 | $formatter->parse('1', NumberFormatter::TYPE_CURRENCY); | ||
664 | } | ||
665 | |||
666 | public function testParseWithNullPositionValue() | ||
667 | { | ||
668 | $position = null; | ||
669 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
670 | $formatter->parse('123', NumberFormatter::TYPE_INT32, $position); | ||
671 | $this->assertNull($position); | ||
672 | } | ||
673 | |||
674 | public function testParseWithNotNullPositionValue() | ||
675 | { | ||
676 | $position = 1; | ||
677 | $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL); | ||
678 | $formatter->parse('123', NumberFormatter::TYPE_DOUBLE, $position); | ||
679 | $this->assertEquals(3, $position); | ||
680 | } | ||
681 | |||
682 | /** | ||
683 | * @param string $locale | ||
684 | * @param null $style | ||
685 | * @param null $pattern | ||
686 | * | ||
687 | * @return \NumberFormatter | ||
688 | */ | ||
689 | abstract protected function getNumberFormatter($locale = 'en', $style = null, $pattern = null); | ||
690 | |||
691 | /** | ||
692 | * @return string | ||
693 | */ | ||
694 | abstract protected function getIntlErrorMessage(); | ||
695 | |||
696 | /** | ||
697 | * @return integer | ||
698 | */ | ||
699 | abstract protected function getIntlErrorCode(); | ||
700 | |||
701 | /** | ||
702 | * @param integer $errorCode | ||
703 | * | ||
704 | * @return Boolean | ||
705 | */ | ||
706 | abstract protected function isIntlFailure($errorCode); | ||
707 | } | ||