StubNumberFormatterTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. $this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode());
  581. } else {
  582. $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
  583. }
  584. }
  585. /**
  586. * @dataProvider parseProvider
  587. */
  588. public function testParseIntl($value, $expected, $message = '')
  589. {
  590. $this->skipIfIntlExtensionIsNotLoaded();
  591. $this->skipIfICUVersionIsTooOld();
  592. $formatter = $this->getIntlFormatterWithDecimalStyle();
  593. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  594. $this->assertSame($expected, $parsedValue, $message);
  595. if ($expected === false) {
  596. $this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode());
  597. } else {
  598. $this->assertEquals(U_ZERO_ERROR, $formatter->getErrorCode());
  599. }
  600. }
  601. public function parseProvider()
  602. {
  603. return array(
  604. array('prefix1', false, '->parse() does not parse a number with a string prefix.'),
  605. array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'),
  606. );
  607. }
  608. /**
  609. * @expectedException PHPUnit_Framework_Error_Warning
  610. */
  611. public function testParseTypeDefaultStub()
  612. {
  613. $formatter = $this->getStubFormatterWithDecimalStyle();
  614. $formatter->parse('1', StubNumberFormatter::TYPE_DEFAULT);
  615. }
  616. /**
  617. * @expectedException PHPUnit_Framework_Error_Warning
  618. */
  619. public function testParseTypeDefaultIntl()
  620. {
  621. $this->skipIfIntlExtensionIsNotLoaded();
  622. $formatter = $this->getIntlFormatterWithDecimalStyle();
  623. $formatter->parse('1', \NumberFormatter::TYPE_DEFAULT);
  624. }
  625. /**
  626. * @dataProvider parseTypeInt32Provider
  627. */
  628. public function testParseTypeInt32Stub($value, $expected, $message = '')
  629. {
  630. $formatter = $this->getStubFormatterWithDecimalStyle();
  631. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_INT32);
  632. $this->assertSame($expected, $parsedValue);
  633. }
  634. /**
  635. * @dataProvider parseTypeInt32Provider
  636. */
  637. public function testParseTypeInt32Intl($value, $expected, $message = '')
  638. {
  639. $this->skipIfIntlExtensionIsNotLoaded();
  640. $formatter = $this->getIntlFormatterWithDecimalStyle();
  641. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_INT32);
  642. $this->assertSame($expected, $parsedValue);
  643. }
  644. public function parseTypeInt32Provider()
  645. {
  646. return array(
  647. array('1', 1),
  648. array('1.1', 1),
  649. array('2,147,483,647', 2147483647),
  650. array('-2,147,483,648', -2147483647 - 1),
  651. array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'),
  652. array('-2,147,483,649', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer negative range.')
  653. );
  654. }
  655. // Stub Tests
  656. public function testParseTypeInt64StubWith32BitIntegerInPhp32Bit()
  657. {
  658. $this->skipIfIntlExtensionIsNotLoaded();
  659. $this->skipIfNot32Bit();
  660. $formatter = $this->getStubFormatterWithDecimalStyle();
  661. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  662. $this->assertInternalType('integer', $parsedValue);
  663. $this->assertEquals(2147483647, $parsedValue);
  664. // Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
  665. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  666. $this->assertInternalType('float', $parsedValue);
  667. $this->assertEquals(((float) -2147483647 - 1), $parsedValue);
  668. }
  669. public function testParseTypeInt64StubWith32BitIntegerInPhp64Bit()
  670. {
  671. $this->skipIfNot64Bit();
  672. $formatter = $this->getStubFormatterWithDecimalStyle();
  673. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  674. $this->assertInternalType('integer', $parsedValue);
  675. $this->assertEquals(2147483647, $parsedValue);
  676. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  677. $this->assertInternalType('integer', $parsedValue);
  678. $this->assertEquals(-2147483647 - 1, $parsedValue);
  679. }
  680. /**
  681. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  682. */
  683. public function testParseTypeInt64StubWith64BitIntegerInPhp32Bit()
  684. {
  685. $this->skipIfNot32Bit();
  686. $formatter = $this->getStubFormatterWithDecimalStyle();
  687. // int 64 using only 32 bit range strangeness
  688. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  689. $this->assertInternalType('float', $parsedValue);
  690. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  691. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  692. $this->assertInternalType('float', $parsedValue);
  693. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  694. }
  695. /**
  696. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  697. */
  698. public function testParseTypeInt64StubWith64BitIntegerInPhp64Bit()
  699. {
  700. $this->skipIfNot64Bit();
  701. $formatter = $this->getStubFormatterWithDecimalStyle();
  702. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  703. $this->assertInternalType('integer', $parsedValue);
  704. $this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  705. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  706. $this->assertInternalType('integer', $parsedValue);
  707. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  708. }
  709. // Intl Tests
  710. public function testParseTypeInt64IntlWith32BitIntegerInPhp32Bit()
  711. {
  712. $this->skipIfIntlExtensionIsNotLoaded();
  713. $this->skipIfNot32Bit();
  714. $formatter = $this->getIntlFormatterWithDecimalStyle();
  715. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  716. $this->assertInternalType('integer', $parsedValue);
  717. $this->assertEquals(2147483647, $parsedValue);
  718. // Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
  719. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  720. $this->assertInternalType('float', $parsedValue);
  721. $this->assertEquals(((float) -2147483647 - 1), $parsedValue);
  722. }
  723. public function testParseTypeInt64IntlWith32BitIntegerInPhp64Bit()
  724. {
  725. $this->skipIfIntlExtensionIsNotLoaded();
  726. $this->skipIfNot64Bit();
  727. $formatter = $this->getIntlFormatterWithDecimalStyle();
  728. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  729. $this->assertInternalType('integer', $parsedValue);
  730. $this->assertEquals(2147483647, $parsedValue);
  731. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  732. $this->assertInternalType('integer', $parsedValue);
  733. $this->assertEquals(-2147483647 - 1, $parsedValue);
  734. }
  735. /**
  736. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  737. */
  738. public function testParseTypeInt64IntlWith64BitIntegerInPhp32Bit()
  739. {
  740. $this->skipIfIntlExtensionIsNotLoaded();
  741. $this->skipIfNot32Bit();
  742. $formatter = $this->getIntlFormatterWithDecimalStyle();
  743. // int 64 using only 32 bit range strangeness
  744. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  745. $this->assertInternalType('float', $parsedValue);
  746. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  747. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  748. $this->assertInternalType('float', $parsedValue);
  749. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  750. }
  751. /**
  752. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  753. */
  754. public function testParseTypeInt64IntlWith64BitIntegerInPhp64Bit()
  755. {
  756. $this->skipIfIntlExtensionIsNotLoaded();
  757. $this->skipIfNot64Bit();
  758. $formatter = $this->getIntlFormatterWithDecimalStyle();
  759. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  760. $this->assertInternalType('integer', $parsedValue);
  761. $this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  762. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  763. $this->assertInternalType('integer', $parsedValue);
  764. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  765. }
  766. /**
  767. * @dataProvider parseTypeDoubleProvider
  768. */
  769. public function testParseTypeDoubleStub($value, $expectedValue)
  770. {
  771. $formatter = $this->getStubFormatterWithDecimalStyle();
  772. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
  773. $this->assertSame($expectedValue, $parsedValue);
  774. }
  775. /**
  776. * @dataProvider parseTypeDoubleProvider
  777. */
  778. public function testParseTypeDoubleIntl($value, $expectedValue)
  779. {
  780. $this->skipIfIntlExtensionIsNotLoaded();
  781. $formatter = $this->getIntlFormatterWithDecimalStyle();
  782. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  783. $this->assertSame($expectedValue, $parsedValue);
  784. }
  785. public function parseTypeDoubleProvider()
  786. {
  787. return array(
  788. array('1', (float) 1),
  789. array('1.1', 1.1),
  790. array('9,223,372,036,854,775,808', 9223372036854775808),
  791. array('-9,223,372,036,854,775,809', -9223372036854775809),
  792. );
  793. }
  794. /**
  795. * @expectedException PHPUnit_Framework_Error_Warning
  796. */
  797. public function testParseTypeCurrencyStub()
  798. {
  799. $formatter = $this->getStubFormatterWithDecimalStyle();
  800. $formatter->parse('1', StubNumberFormatter::TYPE_CURRENCY);
  801. }
  802. /**
  803. * @expectedException PHPUnit_Framework_Error_Warning
  804. */
  805. public function testParseTypeCurrencyIntl()
  806. {
  807. $this->skipIfIntlExtensionIsNotLoaded();
  808. $formatter = $this->getIntlFormatterWithDecimalStyle();
  809. $formatter->parse('1', \NumberFormatter::TYPE_CURRENCY);
  810. }
  811. public function testParseWithNullPositionValueStub()
  812. {
  813. $position = null;
  814. $formatter = $this->getStubFormatterWithDecimalStyle();
  815. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  816. $this->assertNull($position);
  817. }
  818. public function testParseWithNullPositionValueIntl()
  819. {
  820. $this->skipIfIntlExtensionIsNotLoaded();
  821. $position = 0;
  822. $formatter = $this->getIntlFormatterWithDecimalStyle();
  823. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  824. $this->assertEquals(3, $position);
  825. }
  826. /**
  827. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  828. */
  829. public function testParseWithNotNullPositionValueStub()
  830. {
  831. $position = 1;
  832. $formatter = $this->getStubFormatterWithDecimalStyle();
  833. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  834. }
  835. public function testParseWithNotNullPositionValueIntl()
  836. {
  837. $this->skipIfIntlExtensionIsNotLoaded();
  838. $position = 1;
  839. $formatter = $this->getIntlFormatterWithDecimalStyle();
  840. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  841. $this->assertEquals(3, $position);
  842. }
  843. /**
  844. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  845. */
  846. public function testSetPattern()
  847. {
  848. $formatter = $this->getStubFormatterWithDecimalStyle();
  849. $formatter->setPattern(null);
  850. }
  851. /**
  852. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  853. */
  854. public function testSetSymbol()
  855. {
  856. $formatter = $this->getStubFormatterWithDecimalStyle();
  857. $formatter->setSymbol(null, null);
  858. }
  859. /**
  860. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  861. */
  862. public function testSetTextAttribute()
  863. {
  864. $formatter = $this->getStubFormatterWithDecimalStyle();
  865. $formatter->setTextAttribute(null, null);
  866. }
  867. protected function getStubFormatterWithDecimalStyle()
  868. {
  869. return new StubNumberFormatter('en', StubNumberFormatter::DECIMAL);
  870. }
  871. protected function getStubFormatterWithCurrencyStyle()
  872. {
  873. return new StubNumberFormatter('en', StubNumberFormatter::CURRENCY);
  874. }
  875. protected function getIntlFormatterWithDecimalStyle()
  876. {
  877. if (!$this->isIntlExtensionLoaded()) {
  878. return null;
  879. }
  880. return new \NumberFormatter('en', \NumberFormatter::DECIMAL);
  881. }
  882. protected function getIntlFormatterWithCurrencyStyle()
  883. {
  884. if (!$this->isIntlExtensionLoaded()) {
  885. return null;
  886. }
  887. $formatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
  888. $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
  889. return $formatter;
  890. }
  891. }