StubNumberFormatterTest.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Locale\Stub;
  11. require_once __DIR__.'/../TestCase.php';
  12. use Symfony\Component\Locale\Locale;
  13. use Symfony\Component\Locale\Stub\StubIntl;
  14. use Symfony\Component\Locale\Stub\StubNumberFormatter;
  15. use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
  16. /**
  17. * Note that there are some values written like -2147483647 - 1. This is the lower 32bit int max and is a known
  18. * behavior of PHP.
  19. */
  20. class StubNumberFormatterTest extends LocaleTestCase
  21. {
  22. /**
  23. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  24. */
  25. public function testConstructorWithUnsupportedLocale()
  26. {
  27. $formatter = new StubNumberFormatter('pt_BR');
  28. }
  29. /**
  30. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  31. */
  32. public function testConstructorWithUnsupportedStyle()
  33. {
  34. $formatter = new StubNumberFormatter('en', StubNumberFormatter::PATTERN_DECIMAL);
  35. }
  36. /**
  37. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  38. */
  39. public function testConstructorWithPatternDifferentThanNull()
  40. {
  41. $formatter = new StubNumberFormatter('en', StubNumberFormatter::DECIMAL, '');
  42. }
  43. /**
  44. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  45. */
  46. public function testSetAttributeWithUnsupportedAttribute()
  47. {
  48. $formatter = $this->getStubFormatterWithDecimalStyle();
  49. $formatter->setAttribute(StubNumberFormatter::LENIENT_PARSE, null);
  50. }
  51. /**
  52. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  53. */
  54. public function testSetAttributeInvalidRoundingMode()
  55. {
  56. $formatter = $this->getStubFormatterWithDecimalStyle();
  57. $ret = $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, null);
  58. }
  59. public function testCreateStub()
  60. {
  61. $this->assertInstanceOf(
  62. 'Symfony\Component\Locale\Stub\StubNumberFormatter',
  63. StubNumberFormatter::create('en', StubNumberFormatter::DECIMAL)
  64. );
  65. }
  66. public function testCreateIntl()
  67. {
  68. $this->skipIfIntlExtensionIsNotLoaded();
  69. $this->assertInstanceOf('\NumberFormatter', \NumberFormatter::create('en', \NumberFormatter::DECIMAL));
  70. }
  71. /**
  72. * @dataProvider formatCurrencyWithDecimalStyleProvider
  73. */
  74. public function testFormatCurrencyWithDecimalStyleStub($value, $currency, $expected)
  75. {
  76. $formatter = $this->getStubFormatterWithDecimalStyle();
  77. $this->assertEquals($expected, $formatter->formatCurrency($value, $currency));
  78. }
  79. /**
  80. * @dataProvider formatCurrencyWithDecimalStyleProvider
  81. */
  82. public function testFormatCurrencyWithDecimalStyleIntl($value, $currency, $expected)
  83. {
  84. $this->skipIfIntlExtensionIsNotLoaded();
  85. $formatter = $this->getIntlFormatterWithDecimalStyle();
  86. $this->assertEquals($expected, $formatter->formatCurrency($value, $currency));
  87. }
  88. public function formatCurrencyWithDecimalStyleProvider()
  89. {
  90. return array(
  91. array(100, 'ALL', '100'),
  92. array(100, 'BRL', '100.00'),
  93. array(100, 'CRC', '100'),
  94. array(100, 'JPY', '100'),
  95. array(100, 'CHF', '100'),
  96. array(-100, 'ALL', '-100'),
  97. array(-100, 'BRL', '-100'),
  98. array(-100, 'CRC', '-100'),
  99. array(-100, 'JPY', '-100'),
  100. array(-100, 'CHF', '-100'),
  101. array(1000.12, 'ALL', '1,000.12'),
  102. array(1000.12, 'BRL', '1,000.12'),
  103. array(1000.12, 'CRC', '1,000.12'),
  104. array(1000.12, 'JPY', '1,000.12'),
  105. array(1000.12, 'CHF', '1,000.12')
  106. );
  107. }
  108. /**
  109. * @dataProvider formatCurrencyWithCurrencyStyleProvider
  110. */
  111. public function testFormatCurrencyWithCurrencyStyleStub($value, $currency, $expected)
  112. {
  113. $formatter = $this->getStubFormatterWithCurrencyStyle();
  114. $this->assertEquals($expected, $formatter->formatCurrency($value, $currency));
  115. }
  116. /**
  117. * @dataProvider formatCurrencyWithCurrencyStyleProvider
  118. */
  119. public function testFormatCurrencyWithCurrencyStyleIntl($value, $currency, $expected)
  120. {
  121. $this->skipIfIntlExtensionIsNotLoaded();
  122. $this->skipIfICUVersionIsTooOld();
  123. $formatter = $this->getIntlFormatterWithCurrencyStyle();
  124. $this->assertEquals($expected, $formatter->formatCurrency($value, $currency));
  125. }
  126. public function formatCurrencyWithCurrencyStyleProvider()
  127. {
  128. return array(
  129. array(100, 'ALL', 'ALL100'),
  130. array(-100, 'ALL', '(ALL100)'),
  131. array(1000.12, 'ALL', 'ALL1,000'),
  132. array(100, 'BRL', 'R$100.00'),
  133. array(-100, 'BRL', '(R$100.00)'),
  134. array(1000.12, 'BRL', 'R$1,000.12'),
  135. array(100, 'CRC', '₡100'),
  136. array(-100, 'CRC', '(₡100)'),
  137. array(1000.12, 'CRC', '₡1,000'),
  138. array(100, 'JPY', '¥100'),
  139. array(-100, 'JPY', '(¥100)'),
  140. array(1000.12, 'JPY', '¥1,000'),
  141. array(100, 'EUR', '€100.00'),
  142. array(-100, 'EUR', '(€100.00)'),
  143. array(1000.12, 'EUR', '€1,000.12'),
  144. // Rounding checks
  145. array(1000.121, 'BRL', 'R$1,000.12'),
  146. array(1000.123, 'BRL', 'R$1,000.12'),
  147. array(1000.125, 'BRL', 'R$1,000.12'),
  148. array(1000.127, 'BRL', 'R$1,000.13'),
  149. array(1000.129, 'BRL', 'R$1,000.13'),
  150. array(11.50999, 'BRL', 'R$11.51'),
  151. array(11.9999464, 'BRL', 'R$12.00')
  152. );
  153. }
  154. /**
  155. * @dataProvider formatCurrencyWithCurrencyStyleSwissRoundingProvider
  156. */
  157. public function testFormatCurrencyWithCurrencyStyleSwissRoundingStub($value, $currency, $symbol, $expected)
  158. {
  159. $formatter = $this->getStubFormatterWithCurrencyStyle();
  160. $this->assertEquals(sprintf($expected, 'CHF'), $formatter->formatCurrency($value, $currency));
  161. }
  162. /**
  163. * @dataProvider formatCurrencyWithCurrencyStyleSwissRoundingProvider
  164. */
  165. public function testFormatCurrencyWithCurrencyStyleSwissRoundingIntl($value, $currency, $symbol, $expected)
  166. {
  167. $this->skipIfIntlExtensionIsNotLoaded();
  168. $this->skipIfICUVersionIsTooOld();
  169. $formatter = $this->getIntlFormatterWithCurrencyStyle();
  170. $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency));
  171. }
  172. public function formatCurrencyWithCurrencyStyleSwissRoundingProvider()
  173. {
  174. // The currency symbol was updated from 4.2 to the 4.4 version. The ICU CLDR data was updated in 2010-03-03,
  175. // the 4.2 release is from 2009-05-08 and the 4.4 from 2010-03-17. It's ugly we want to compare if the
  176. // stub implementation is behaving like the intl one
  177. // http://bugs.icu-project.org/trac/changeset/27776/icu/trunk/source/data/curr/en.txt
  178. $chf = $this->isIntlExtensionLoaded() && $this->isLowerThanIcuVersion('4.4') ? 'Fr.' : 'CHF';
  179. return array(
  180. array(100, 'CHF', $chf, '%s100.00'),
  181. array(-100, 'CHF', $chf, '(%s100.00)'),
  182. array(1000.12, 'CHF', $chf, '%s1,000.10'),
  183. array('1000.12', 'CHF', $chf, '%s1,000.10'),
  184. // Rounding checks
  185. array(1000.121, 'CHF', $chf, '%s1,000.10'),
  186. array(1000.123, 'CHF', $chf, '%s1,000.10'),
  187. array(1000.125, 'CHF', $chf, '%s1,000.10'),
  188. array(1000.127, 'CHF', $chf, '%s1,000.15'),
  189. array(1000.129, 'CHF', $chf, '%s1,000.15'),
  190. array(1200000.00, 'CHF', $chf, '%s1,200,000.00'),
  191. array(1200000.1, 'CHF', $chf, '%s1,200,000.10'),
  192. array(1200000.10, 'CHF', $chf, '%s1,200,000.10'),
  193. array(1200000.101, 'CHF', $chf, '%s1,200,000.10')
  194. );
  195. }
  196. public function testFormatStub()
  197. {
  198. $formatter = $this->getStubFormatterWithDecimalStyle();
  199. $this->assertSame('9.555', $formatter->format(9.555));
  200. }
  201. public function testFormatIntl()
  202. {
  203. $this->skipIfIntlExtensionIsNotLoaded();
  204. $formatter = $this->getIntlFormatterWithDecimalStyle();
  205. $this->assertSame('9.555', $formatter->format(9.555));
  206. }
  207. /**
  208. * @expectedException RuntimeException
  209. */
  210. public function testFormatWithCurrencyStyleStub()
  211. {
  212. $formatter = $this->getStubFormatterWithCurrencyStyle();
  213. $formatter->format(1);
  214. }
  215. public function testFormatWithCurrencyStyleIntl()
  216. {
  217. $this->skipIfIntlExtensionIsNotLoaded();
  218. $formatter = $this->getIntlFormatterWithCurrencyStyle();
  219. $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
  220. $this->assertEquals('SFD1.00', $formatter->format(1));
  221. }
  222. /**
  223. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  224. */
  225. public function testFormatTypeInt32Stub()
  226. {
  227. $formatter = $this->getStubFormatterWithDecimalStyle();
  228. $formatter->format(1, StubNumberFormatter::TYPE_INT32);
  229. }
  230. /**
  231. * @dataProvider formatTypeInt32Provider
  232. */
  233. public function testFormatTypeInt32Intl($formatter, $value, $expected, $message = '')
  234. {
  235. $this->skipIfIntlExtensionIsNotLoaded();
  236. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT32);
  237. $this->assertEquals($expected, $formattedValue, $message);
  238. }
  239. public function formatTypeInt32Provider()
  240. {
  241. $df = $this->getIntlFormatterWithDecimalStyle();
  242. $cf = $this->getIntlFormatterWithCurrencyStyle();
  243. $message = '->format() TYPE_INT32 formats inconsistently an integer if out of the 32 bit range.';
  244. return array(
  245. array($df, 1, '1'),
  246. array($df, 1.1, '1'),
  247. array($df, 2147483648, '-2,147,483,648', $message),
  248. array($df, -2147483649, '2,147,483,647', $message),
  249. array($cf, 1, 'SFD1.00'),
  250. array($cf, 1.1, 'SFD1.00'),
  251. array($cf, 2147483648, '(SFD2,147,483,648.00)', $message),
  252. array($cf, -2147483649, 'SFD2,147,483,647.00', $message)
  253. );
  254. }
  255. /**
  256. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  257. */
  258. public function testFormatTypeInt64Stub()
  259. {
  260. $formatter = $this->getStubFormatterWithDecimalStyle();
  261. $formatter->format(1, StubNumberFormatter::TYPE_INT64);
  262. }
  263. /**
  264. * The parse() method works differently with integer out of the 32 bit range. format() works fine.
  265. * @dataProvider formatTypeInt64Provider
  266. */
  267. public function testFormatTypeInt64Intl($formatter, $value, $expected)
  268. {
  269. $this->skipIfIntlExtensionIsNotLoaded();
  270. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT64);
  271. $this->assertEquals($expected, $formattedValue);
  272. }
  273. public function formatTypeInt64Provider()
  274. {
  275. $df = $this->getIntlFormatterWithDecimalStyle();
  276. $cf = $this->getIntlFormatterWithCurrencyStyle();
  277. return array(
  278. array($df, 1, '1'),
  279. array($df, 1.1, '1'),
  280. array($df, 2147483648, '2,147,483,648'),
  281. array($df, -2147483649, '-2,147,483,649'),
  282. array($cf, 1, 'SFD1.00'),
  283. array($cf, 1.1, 'SFD1.00'),
  284. array($cf, 2147483648, 'SFD2,147,483,648.00'),
  285. array($cf, -2147483649, '(SFD2,147,483,649.00)')
  286. );
  287. }
  288. /**
  289. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  290. */
  291. public function testFormatTypeDoubleStub()
  292. {
  293. $formatter = $this->getStubFormatterWithDecimalStyle();
  294. $formatter->format(1, StubNumberFormatter::TYPE_DOUBLE);
  295. }
  296. /**
  297. * @dataProvider formatTypeDoubleProvider
  298. */
  299. public function testFormatTypeDoubleIntl($formatter, $value, $expected)
  300. {
  301. $this->skipIfIntlExtensionIsNotLoaded();
  302. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_DOUBLE);
  303. $this->assertEquals($expected, $formattedValue);
  304. }
  305. public function formatTypeDoubleProvider()
  306. {
  307. $df = $this->getIntlFormatterWithDecimalStyle();
  308. $cf = $this->getIntlFormatterWithCurrencyStyle();
  309. return array(
  310. array($df, 1, '1'),
  311. array($df, 1.1, '1.1'),
  312. array($cf, 1, 'SFD1.00'),
  313. array($cf, 1.1, 'SFD1.10'),
  314. );
  315. }
  316. /**
  317. * @expectedException PHPUnit_Framework_Error_Warning
  318. */
  319. public function testFormatTypeCurrencyStub()
  320. {
  321. $formatter = $this->getStubFormatterWithDecimalStyle();
  322. $formatter->format(1, StubNumberFormatter::TYPE_CURRENCY);
  323. }
  324. public function testFormatTypeCurrencyReturnStub()
  325. {
  326. $formatter = $this->getStubFormatterWithDecimalStyle();
  327. $this->assertFalse(@$formatter->format(1, StubNumberFormatter::TYPE_CURRENCY));
  328. }
  329. /**
  330. * @dataProvider formatTypeCurrencyProvider
  331. * @expectedException PHPUnit_Framework_Error_Warning
  332. */
  333. public function testFormatTypeCurrencyIntl($formatter, $value)
  334. {
  335. $this->skipIfIntlExtensionIsNotLoaded();
  336. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_CURRENCY);
  337. }
  338. public function formatTypeCurrencyProvider()
  339. {
  340. $df = $this->getIntlFormatterWithDecimalStyle();
  341. $cf = $this->getIntlFormatterWithCurrencyStyle();
  342. return array(
  343. array($df, 1),
  344. array($df, 1),
  345. );
  346. }
  347. /**
  348. * @dataProvider formatFractionDigitsProvider
  349. */
  350. public function testFormatFractionDigitsStub($value, $expected, $fractionDigits = null, $expectedFractionDigits = 1)
  351. {
  352. $formatter = $this->getStubFormatterWithDecimalStyle();
  353. if (null !== $fractionDigits) {
  354. $attributeRet = $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, $fractionDigits);
  355. }
  356. $formattedValue = $formatter->format($value);
  357. $this->assertSame($expected, $formattedValue);
  358. $this->assertSame($expectedFractionDigits, $formatter->getAttribute(StubNumberFormatter::FRACTION_DIGITS));
  359. if (isset($attributeRet)) {
  360. $this->assertTrue($attributeRet);
  361. }
  362. }
  363. /**
  364. * @dataProvider formatFractionDigitsProvider
  365. */
  366. public function testFormatFractionDigitsIntl($value, $expected, $fractionDigits = null, $expectedFractionDigits = 1)
  367. {
  368. $this->skipIfIntlExtensionIsNotLoaded();
  369. $formatter = $this->getIntlFormatterWithDecimalStyle();
  370. if (null !== $fractionDigits) {
  371. $attributeRet = $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $fractionDigits);
  372. }
  373. $formattedValue = $formatter->format($value);
  374. $this->assertSame($expected, $formattedValue);
  375. $this->assertSame($expectedFractionDigits, $formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS));
  376. if (isset($attributeRet)) {
  377. $this->assertTrue($attributeRet);
  378. }
  379. }
  380. public function formatFractionDigitsProvider()
  381. {
  382. return array(
  383. array(1.123, '1.123', null, 0),
  384. array(1.123, '1', 0, 0),
  385. array(1.123, '1.1', 1, 1),
  386. array(1.123, '1.12', 2, 2),
  387. array(1.123, '1', -1, 0),
  388. array(1.123, '1', 'abc', 0)
  389. );
  390. }
  391. /**
  392. * @dataProvider formatGroupingUsedProvider
  393. */
  394. public function testFormatGroupingUsedStub($value, $expected, $groupingUsed = null, $expectedGroupingUsed = 1)
  395. {
  396. $formatter = $this->getStubFormatterWithDecimalStyle();
  397. if (null !== $groupingUsed) {
  398. $attributeRet = $formatter->setAttribute(StubNumberFormatter::GROUPING_USED, $groupingUsed);
  399. }
  400. $formattedValue = $formatter->format($value);
  401. $this->assertSame($expected, $formattedValue);
  402. $this->assertSame($expectedGroupingUsed, $formatter->getAttribute(StubNumberFormatter::GROUPING_USED));
  403. if (isset($attributeRet)) {
  404. $this->assertTrue($attributeRet);
  405. }
  406. }
  407. /**
  408. * @dataProvider formatGroupingUsedProvider
  409. */
  410. public function testFormatGroupingUsedIntl($value, $expected, $groupingUsed = null, $expectedGroupingUsed = 1)
  411. {
  412. $this->skipIfIntlExtensionIsNotLoaded();
  413. $formatter = $this->getIntlFormatterWithDecimalStyle();
  414. if (null !== $groupingUsed) {
  415. $attributeRet = $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $groupingUsed);
  416. }
  417. $formattedValue = $formatter->format($value);
  418. $this->assertSame($expected, $formattedValue);
  419. $this->assertSame($expectedGroupingUsed, $formatter->getAttribute(\NumberFormatter::GROUPING_USED));
  420. if (isset($attributeRet)) {
  421. $this->assertTrue($attributeRet);
  422. }
  423. }
  424. public function formatGroupingUsedProvider()
  425. {
  426. return array(
  427. array(1000, '1,000', null, 1),
  428. array(1000, '1000', 0, 0),
  429. array(1000, '1,000', 1, 1),
  430. array(1000, '1,000', 2, 1),
  431. array(1000, '1000', 'abc', 0),
  432. array(1000, '1,000', -1, 1),
  433. );
  434. }
  435. /**
  436. * @dataProvider formatRoundingModeRoundHalfUpProvider
  437. */
  438. public function testFormatRoundingModeStubRoundHalfUp($value, $expected)
  439. {
  440. $formatter = $this->getStubFormatterWithDecimalStyle();
  441. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  442. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFUP);
  443. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFUP rounding mode.');
  444. }
  445. /**
  446. * @dataProvider formatRoundingModeRoundHalfUpProvider
  447. */
  448. public function testFormatRoundingModeHalfUpIntl($value, $expected)
  449. {
  450. $this->skipIfIntlExtensionIsNotLoaded();
  451. $formatter = $this->getIntlFormatterWithDecimalStyle();
  452. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  453. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFUP);
  454. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFUP rounding mode.');
  455. }
  456. public function formatRoundingModeRoundHalfUpProvider()
  457. {
  458. // The commented value is differently rounded by intl's NumberFormatter in 32 and 64 bit architectures
  459. return array(
  460. array(1.121, '1.12'),
  461. array(1.123, '1.12'),
  462. // array(1.125, '1.13'),
  463. array(1.127, '1.13'),
  464. array(1.129, '1.13'),
  465. );
  466. }
  467. /**
  468. * @dataProvider formatRoundingModeRoundHalfDownProvider
  469. */
  470. public function testFormatRoundingModeStubRoundHalfDown($value, $expected)
  471. {
  472. $formatter = $this->getStubFormatterWithDecimalStyle();
  473. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  474. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFDOWN);
  475. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFDOWN rounding mode.');
  476. }
  477. /**
  478. * @dataProvider formatRoundingModeRoundHalfDownProvider
  479. */
  480. public function testFormatRoundingModeHalfDownIntl($value, $expected)
  481. {
  482. $this->skipIfIntlExtensionIsNotLoaded();
  483. $formatter = $this->getIntlFormatterWithDecimalStyle();
  484. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  485. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFDOWN);
  486. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFDOWN rounding mode.');
  487. }
  488. public function formatRoundingModeRoundHalfDownProvider()
  489. {
  490. return array(
  491. array(1.121, '1.12'),
  492. array(1.123, '1.12'),
  493. array(1.125, '1.12'),
  494. array(1.127, '1.13'),
  495. array(1.129, '1.13'),
  496. );
  497. }
  498. /**
  499. * @dataProvider formatRoundingModeRoundHalfEvenProvider
  500. */
  501. public function testFormatRoundingModeStubRoundHalfEven($value, $expected)
  502. {
  503. $formatter = $this->getStubFormatterWithDecimalStyle();
  504. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  505. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFEVEN);
  506. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFEVEN rounding mode.');
  507. }
  508. /**
  509. * @dataProvider formatRoundingModeRoundHalfEvenProvider
  510. */
  511. public function testFormatRoundingModeHalfEvenIntl($value, $expected)
  512. {
  513. $this->skipIfIntlExtensionIsNotLoaded();
  514. $formatter = $this->getIntlFormatterWithDecimalStyle();
  515. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  516. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFEVEN);
  517. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFEVEN rounding mode.');
  518. }
  519. public function formatRoundingModeRoundHalfEvenProvider()
  520. {
  521. return array(
  522. array(1.121, '1.12'),
  523. array(1.123, '1.12'),
  524. array(1.125, '1.12'),
  525. array(1.127, '1.13'),
  526. array(1.129, '1.13'),
  527. );
  528. }
  529. public function testGetErrorCode()
  530. {
  531. $formatter = $this->getStubFormatterWithDecimalStyle();
  532. $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
  533. }
  534. public function testGetLocale()
  535. {
  536. $formatter = $this->getStubFormatterWithDecimalStyle();
  537. $this->assertEquals('en', $formatter->getLocale());
  538. }
  539. /**
  540. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  541. */
  542. public function testGetPattern()
  543. {
  544. $formatter = $this->getStubFormatterWithDecimalStyle();
  545. $formatter->getPattern();
  546. }
  547. /**
  548. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  549. */
  550. public function testGetSymbol()
  551. {
  552. $formatter = $this->getStubFormatterWithDecimalStyle();
  553. $formatter->getSymbol(null);
  554. }
  555. /**
  556. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  557. */
  558. public function testGetTextAttribute()
  559. {
  560. $formatter = $this->getStubFormatterWithDecimalStyle();
  561. $formatter->getTextAttribute(null);
  562. }
  563. /**
  564. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  565. */
  566. public function testParseCurrency()
  567. {
  568. $formatter = $this->getStubFormatterWithDecimalStyle();
  569. $formatter->parseCurrency(null, $currency);
  570. }
  571. /**
  572. * @dataProvider parseProvider
  573. */
  574. public function testParseStub($value, $expected, $message = '')
  575. {
  576. $formatter = $this->getStubFormatterWithDecimalStyle();
  577. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
  578. $this->assertSame($expected, $parsedValue, $message);
  579. if ($expected === false) {
  580. $errorCode = StubIntl::U_PARSE_ERROR;
  581. $errorMessage = 'Number parsing failed: U_PARSE_ERROR';
  582. } else {
  583. $errorCode = StubIntl::U_ZERO_ERROR;
  584. $errorMessage = 'U_ZERO_ERROR';
  585. }
  586. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  587. $this->assertSame($errorCode, StubIntl::getErrorCode());
  588. $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  589. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  590. $this->assertSame($errorCode, $formatter->getErrorCode());
  591. $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
  592. }
  593. /**
  594. * @dataProvider parseProvider
  595. */
  596. public function testParseIntl($value, $expected, $message = '')
  597. {
  598. $this->skipIfIntlExtensionIsNotLoaded();
  599. $this->skipIfICUVersionIsTooOld();
  600. $formatter = $this->getIntlFormatterWithDecimalStyle();
  601. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  602. $this->assertSame($expected, $parsedValue, $message);
  603. if ($expected === false) {
  604. $errorCode = StubIntl::U_PARSE_ERROR;
  605. $errorMessage = 'Number parsing failed: U_PARSE_ERROR';
  606. } else {
  607. $errorCode = StubIntl::U_ZERO_ERROR;
  608. $errorMessage = 'U_ZERO_ERROR';
  609. }
  610. $this->assertSame($errorMessage, intl_get_error_message());
  611. $this->assertSame($errorCode, intl_get_error_code());
  612. $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
  613. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  614. $this->assertSame($errorCode, $formatter->getErrorCode());
  615. $this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode()));
  616. }
  617. public function parseProvider()
  618. {
  619. return array(
  620. array('prefix1', false, '->parse() does not parse a number with a string prefix.'),
  621. array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'),
  622. );
  623. }
  624. /**
  625. * @expectedException PHPUnit_Framework_Error_Warning
  626. */
  627. public function testParseTypeDefaultStub()
  628. {
  629. $formatter = $this->getStubFormatterWithDecimalStyle();
  630. $formatter->parse('1', StubNumberFormatter::TYPE_DEFAULT);
  631. }
  632. /**
  633. * @expectedException PHPUnit_Framework_Error_Warning
  634. */
  635. public function testParseTypeDefaultIntl()
  636. {
  637. $this->skipIfIntlExtensionIsNotLoaded();
  638. $formatter = $this->getIntlFormatterWithDecimalStyle();
  639. $formatter->parse('1', \NumberFormatter::TYPE_DEFAULT);
  640. }
  641. /**
  642. * @dataProvider parseTypeInt32Provider
  643. */
  644. public function testParseTypeInt32Stub($value, $expected, $message = '')
  645. {
  646. $formatter = $this->getStubFormatterWithDecimalStyle();
  647. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_INT32);
  648. $this->assertSame($expected, $parsedValue);
  649. }
  650. /**
  651. * @dataProvider parseTypeInt32Provider
  652. */
  653. public function testParseTypeInt32Intl($value, $expected, $message = '')
  654. {
  655. $this->skipIfIntlExtensionIsNotLoaded();
  656. $formatter = $this->getIntlFormatterWithDecimalStyle();
  657. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_INT32);
  658. $this->assertSame($expected, $parsedValue);
  659. }
  660. public function parseTypeInt32Provider()
  661. {
  662. return array(
  663. array('1', 1),
  664. array('1.1', 1),
  665. array('2,147,483,647', 2147483647),
  666. array('-2,147,483,648', -2147483647 - 1),
  667. array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'),
  668. array('-2,147,483,649', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer negative range.')
  669. );
  670. }
  671. // Stub Tests
  672. public function testParseTypeInt64StubWith32BitIntegerInPhp32Bit()
  673. {
  674. $this->skipIfIntlExtensionIsNotLoaded();
  675. $this->skipIfNot32Bit();
  676. $formatter = $this->getStubFormatterWithDecimalStyle();
  677. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  678. $this->assertInternalType('integer', $parsedValue);
  679. $this->assertEquals(2147483647, $parsedValue);
  680. // Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
  681. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  682. $this->assertInternalType('float', $parsedValue);
  683. $this->assertEquals(((float) -2147483647 - 1), $parsedValue);
  684. }
  685. public function testParseTypeInt64StubWith32BitIntegerInPhp64Bit()
  686. {
  687. $this->skipIfNot64Bit();
  688. $formatter = $this->getStubFormatterWithDecimalStyle();
  689. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  690. $this->assertInternalType('integer', $parsedValue);
  691. $this->assertEquals(2147483647, $parsedValue);
  692. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  693. $this->assertInternalType('integer', $parsedValue);
  694. $this->assertEquals(-2147483647 - 1, $parsedValue);
  695. }
  696. /**
  697. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  698. */
  699. public function testParseTypeInt64StubWith64BitIntegerInPhp32Bit()
  700. {
  701. $this->skipIfNot32Bit();
  702. $formatter = $this->getStubFormatterWithDecimalStyle();
  703. // int 64 using only 32 bit range strangeness
  704. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  705. $this->assertInternalType('float', $parsedValue);
  706. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  707. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  708. $this->assertInternalType('float', $parsedValue);
  709. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  710. }
  711. /**
  712. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  713. */
  714. public function testParseTypeInt64StubWith64BitIntegerInPhp64Bit()
  715. {
  716. $this->skipIfNot64Bit();
  717. $formatter = $this->getStubFormatterWithDecimalStyle();
  718. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  719. $this->assertInternalType('integer', $parsedValue);
  720. $this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  721. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  722. $this->assertInternalType('integer', $parsedValue);
  723. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  724. }
  725. // Intl Tests
  726. public function testParseTypeInt64IntlWith32BitIntegerInPhp32Bit()
  727. {
  728. $this->skipIfIntlExtensionIsNotLoaded();
  729. $this->skipIfNot32Bit();
  730. $formatter = $this->getIntlFormatterWithDecimalStyle();
  731. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  732. $this->assertInternalType('integer', $parsedValue);
  733. $this->assertEquals(2147483647, $parsedValue);
  734. // Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
  735. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  736. $this->assertInternalType('float', $parsedValue);
  737. $this->assertEquals(((float) -2147483647 - 1), $parsedValue);
  738. }
  739. public function testParseTypeInt64IntlWith32BitIntegerInPhp64Bit()
  740. {
  741. $this->skipIfIntlExtensionIsNotLoaded();
  742. $this->skipIfNot64Bit();
  743. $formatter = $this->getIntlFormatterWithDecimalStyle();
  744. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  745. $this->assertInternalType('integer', $parsedValue);
  746. $this->assertEquals(2147483647, $parsedValue);
  747. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  748. $this->assertInternalType('integer', $parsedValue);
  749. $this->assertEquals(-2147483647 - 1, $parsedValue);
  750. }
  751. /**
  752. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  753. */
  754. public function testParseTypeInt64IntlWith64BitIntegerInPhp32Bit()
  755. {
  756. $this->skipIfIntlExtensionIsNotLoaded();
  757. $this->skipIfNot32Bit();
  758. $formatter = $this->getIntlFormatterWithDecimalStyle();
  759. // int 64 using only 32 bit range strangeness
  760. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  761. $this->assertInternalType('float', $parsedValue);
  762. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  763. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  764. $this->assertInternalType('float', $parsedValue);
  765. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  766. }
  767. /**
  768. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  769. */
  770. public function testParseTypeInt64IntlWith64BitIntegerInPhp64Bit()
  771. {
  772. $this->skipIfIntlExtensionIsNotLoaded();
  773. $this->skipIfNot64Bit();
  774. $formatter = $this->getIntlFormatterWithDecimalStyle();
  775. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  776. $this->assertInternalType('integer', $parsedValue);
  777. $this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  778. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  779. $this->assertInternalType('integer', $parsedValue);
  780. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  781. }
  782. /**
  783. * @dataProvider parseTypeDoubleProvider
  784. */
  785. public function testParseTypeDoubleStub($value, $expectedValue)
  786. {
  787. $formatter = $this->getStubFormatterWithDecimalStyle();
  788. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
  789. $this->assertSame($expectedValue, $parsedValue);
  790. }
  791. /**
  792. * @dataProvider parseTypeDoubleProvider
  793. */
  794. public function testParseTypeDoubleIntl($value, $expectedValue)
  795. {
  796. $this->skipIfIntlExtensionIsNotLoaded();
  797. $formatter = $this->getIntlFormatterWithDecimalStyle();
  798. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  799. $this->assertSame($expectedValue, $parsedValue);
  800. }
  801. public function parseTypeDoubleProvider()
  802. {
  803. return array(
  804. array('1', (float) 1),
  805. array('1.1', 1.1),
  806. array('9,223,372,036,854,775,808', 9223372036854775808),
  807. array('-9,223,372,036,854,775,809', -9223372036854775809),
  808. );
  809. }
  810. /**
  811. * @expectedException PHPUnit_Framework_Error_Warning
  812. */
  813. public function testParseTypeCurrencyStub()
  814. {
  815. $formatter = $this->getStubFormatterWithDecimalStyle();
  816. $formatter->parse('1', StubNumberFormatter::TYPE_CURRENCY);
  817. }
  818. /**
  819. * @expectedException PHPUnit_Framework_Error_Warning
  820. */
  821. public function testParseTypeCurrencyIntl()
  822. {
  823. $this->skipIfIntlExtensionIsNotLoaded();
  824. $formatter = $this->getIntlFormatterWithDecimalStyle();
  825. $formatter->parse('1', \NumberFormatter::TYPE_CURRENCY);
  826. }
  827. public function testParseWithNullPositionValueStub()
  828. {
  829. $position = null;
  830. $formatter = $this->getStubFormatterWithDecimalStyle();
  831. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  832. $this->assertNull($position);
  833. }
  834. public function testParseWithNullPositionValueIntl()
  835. {
  836. $this->skipIfIntlExtensionIsNotLoaded();
  837. $position = 0;
  838. $formatter = $this->getIntlFormatterWithDecimalStyle();
  839. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  840. $this->assertEquals(3, $position);
  841. }
  842. /**
  843. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  844. */
  845. public function testParseWithNotNullPositionValueStub()
  846. {
  847. $position = 1;
  848. $formatter = $this->getStubFormatterWithDecimalStyle();
  849. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  850. }
  851. public function testParseWithNotNullPositionValueIntl()
  852. {
  853. $this->skipIfIntlExtensionIsNotLoaded();
  854. $position = 1;
  855. $formatter = $this->getIntlFormatterWithDecimalStyle();
  856. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  857. $this->assertEquals(3, $position);
  858. }
  859. /**
  860. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  861. */
  862. public function testSetPattern()
  863. {
  864. $formatter = $this->getStubFormatterWithDecimalStyle();
  865. $formatter->setPattern(null);
  866. }
  867. /**
  868. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  869. */
  870. public function testSetSymbol()
  871. {
  872. $formatter = $this->getStubFormatterWithDecimalStyle();
  873. $formatter->setSymbol(null, null);
  874. }
  875. /**
  876. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  877. */
  878. public function testSetTextAttribute()
  879. {
  880. $formatter = $this->getStubFormatterWithDecimalStyle();
  881. $formatter->setTextAttribute(null, null);
  882. }
  883. protected function getStubFormatterWithDecimalStyle()
  884. {
  885. return new StubNumberFormatter('en', StubNumberFormatter::DECIMAL);
  886. }
  887. protected function getStubFormatterWithCurrencyStyle()
  888. {
  889. return new StubNumberFormatter('en', StubNumberFormatter::CURRENCY);
  890. }
  891. protected function getIntlFormatterWithDecimalStyle()
  892. {
  893. if (!$this->isIntlExtensionLoaded()) {
  894. return null;
  895. }
  896. return new \NumberFormatter('en', \NumberFormatter::DECIMAL);
  897. }
  898. protected function getIntlFormatterWithCurrencyStyle()
  899. {
  900. if (!$this->isIntlExtensionLoaded()) {
  901. return null;
  902. }
  903. $formatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
  904. $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
  905. return $formatter;
  906. }
  907. }