StubNumberFormatterTest.php 34 KB

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