StubNumberFormatterTest.php 37 KB

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