StubNumberFormatterTest.php 33 KB

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