StubNumberFormatterTest.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. $errorCode = StubIntl::U_ZERO_ERROR;
  199. $errorMessage = 'U_ZERO_ERROR';
  200. $formatter = $this->getStubFormatterWithDecimalStyle();
  201. $this->assertSame('9.555', $formatter->format(9.555));
  202. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  203. $this->assertSame($errorCode, StubIntl::getErrorCode());
  204. $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode()));
  205. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  206. $this->assertSame($errorCode, $formatter->getErrorCode());
  207. $this->assertFalse(StubIntl::isFailure($formatter->getErrorCode()));
  208. }
  209. public function testFormatIntl()
  210. {
  211. $this->skipIfIntlExtensionIsNotLoaded();
  212. $errorCode = StubIntl::U_ZERO_ERROR;
  213. $errorMessage = 'U_ZERO_ERROR';
  214. $formatter = $this->getIntlFormatterWithDecimalStyle();
  215. $this->assertSame('9.555', $formatter->format(9.555));
  216. $this->assertSame($errorMessage, intl_get_error_message());
  217. $this->assertSame($errorCode, intl_get_error_code());
  218. $this->assertFalse(intl_is_failure(intl_get_error_code()));
  219. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  220. $this->assertSame($errorCode, $formatter->getErrorCode());
  221. $this->assertFalse(intl_is_failure($formatter->getErrorCode()));
  222. }
  223. /**
  224. * @expectedException RuntimeException
  225. */
  226. public function testFormatWithCurrencyStyleStub()
  227. {
  228. $formatter = $this->getStubFormatterWithCurrencyStyle();
  229. $formatter->format(1);
  230. }
  231. public function testFormatWithCurrencyStyleIntl()
  232. {
  233. $this->skipIfIntlExtensionIsNotLoaded();
  234. $formatter = $this->getIntlFormatterWithCurrencyStyle();
  235. $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
  236. $this->assertEquals('SFD1.00', $formatter->format(1));
  237. }
  238. /**
  239. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  240. */
  241. public function testFormatTypeInt32Stub()
  242. {
  243. $formatter = $this->getStubFormatterWithDecimalStyle();
  244. $formatter->format(1, StubNumberFormatter::TYPE_INT32);
  245. }
  246. /**
  247. * @dataProvider formatTypeInt32Provider
  248. */
  249. public function testFormatTypeInt32Intl($formatter, $value, $expected, $message = '')
  250. {
  251. $this->skipIfIntlExtensionIsNotLoaded();
  252. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT32);
  253. $this->assertEquals($expected, $formattedValue, $message);
  254. }
  255. public function formatTypeInt32Provider()
  256. {
  257. $df = $this->getIntlFormatterWithDecimalStyle();
  258. $cf = $this->getIntlFormatterWithCurrencyStyle();
  259. $message = '->format() TYPE_INT32 formats inconsistently an integer if out of the 32 bit range.';
  260. return array(
  261. array($df, 1, '1'),
  262. array($df, 1.1, '1'),
  263. array($df, 2147483648, '-2,147,483,648', $message),
  264. array($df, -2147483649, '2,147,483,647', $message),
  265. array($cf, 1, 'SFD1.00'),
  266. array($cf, 1.1, 'SFD1.00'),
  267. array($cf, 2147483648, '(SFD2,147,483,648.00)', $message),
  268. array($cf, -2147483649, 'SFD2,147,483,647.00', $message)
  269. );
  270. }
  271. /**
  272. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  273. */
  274. public function testFormatTypeInt64Stub()
  275. {
  276. $formatter = $this->getStubFormatterWithDecimalStyle();
  277. $formatter->format(1, StubNumberFormatter::TYPE_INT64);
  278. }
  279. /**
  280. * The parse() method works differently with integer out of the 32 bit range. format() works fine.
  281. * @dataProvider formatTypeInt64Provider
  282. */
  283. public function testFormatTypeInt64Intl($formatter, $value, $expected)
  284. {
  285. $this->skipIfIntlExtensionIsNotLoaded();
  286. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT64);
  287. $this->assertEquals($expected, $formattedValue);
  288. }
  289. public function formatTypeInt64Provider()
  290. {
  291. $df = $this->getIntlFormatterWithDecimalStyle();
  292. $cf = $this->getIntlFormatterWithCurrencyStyle();
  293. return array(
  294. array($df, 1, '1'),
  295. array($df, 1.1, '1'),
  296. array($df, 2147483648, '2,147,483,648'),
  297. array($df, -2147483649, '-2,147,483,649'),
  298. array($cf, 1, 'SFD1.00'),
  299. array($cf, 1.1, 'SFD1.00'),
  300. array($cf, 2147483648, 'SFD2,147,483,648.00'),
  301. array($cf, -2147483649, '(SFD2,147,483,649.00)')
  302. );
  303. }
  304. /**
  305. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  306. */
  307. public function testFormatTypeDoubleStub()
  308. {
  309. $formatter = $this->getStubFormatterWithDecimalStyle();
  310. $formatter->format(1, StubNumberFormatter::TYPE_DOUBLE);
  311. }
  312. /**
  313. * @dataProvider formatTypeDoubleProvider
  314. */
  315. public function testFormatTypeDoubleIntl($formatter, $value, $expected)
  316. {
  317. $this->skipIfIntlExtensionIsNotLoaded();
  318. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_DOUBLE);
  319. $this->assertEquals($expected, $formattedValue);
  320. }
  321. public function formatTypeDoubleProvider()
  322. {
  323. $df = $this->getIntlFormatterWithDecimalStyle();
  324. $cf = $this->getIntlFormatterWithCurrencyStyle();
  325. return array(
  326. array($df, 1, '1'),
  327. array($df, 1.1, '1.1'),
  328. array($cf, 1, 'SFD1.00'),
  329. array($cf, 1.1, 'SFD1.10'),
  330. );
  331. }
  332. /**
  333. * @expectedException PHPUnit_Framework_Error_Warning
  334. */
  335. public function testFormatTypeCurrencyStub()
  336. {
  337. $formatter = $this->getStubFormatterWithDecimalStyle();
  338. $formatter->format(1, StubNumberFormatter::TYPE_CURRENCY);
  339. }
  340. public function testFormatTypeCurrencyReturnStub()
  341. {
  342. $formatter = $this->getStubFormatterWithDecimalStyle();
  343. $this->assertFalse(@$formatter->format(1, StubNumberFormatter::TYPE_CURRENCY));
  344. }
  345. /**
  346. * @dataProvider formatTypeCurrencyProvider
  347. * @expectedException PHPUnit_Framework_Error_Warning
  348. */
  349. public function testFormatTypeCurrencyIntl($formatter, $value)
  350. {
  351. $this->skipIfIntlExtensionIsNotLoaded();
  352. $formattedValue = $formatter->format($value, \NumberFormatter::TYPE_CURRENCY);
  353. }
  354. public function formatTypeCurrencyProvider()
  355. {
  356. $df = $this->getIntlFormatterWithDecimalStyle();
  357. $cf = $this->getIntlFormatterWithCurrencyStyle();
  358. return array(
  359. array($df, 1),
  360. array($df, 1),
  361. );
  362. }
  363. /**
  364. * @dataProvider formatFractionDigitsProvider
  365. */
  366. public function testFormatFractionDigitsStub($value, $expected, $fractionDigits = null, $expectedFractionDigits = 1)
  367. {
  368. $formatter = $this->getStubFormatterWithDecimalStyle();
  369. if (null !== $fractionDigits) {
  370. $attributeRet = $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, $fractionDigits);
  371. }
  372. $formattedValue = $formatter->format($value);
  373. $this->assertSame($expected, $formattedValue);
  374. $this->assertSame($expectedFractionDigits, $formatter->getAttribute(StubNumberFormatter::FRACTION_DIGITS));
  375. if (isset($attributeRet)) {
  376. $this->assertTrue($attributeRet);
  377. }
  378. }
  379. /**
  380. * @dataProvider formatFractionDigitsProvider
  381. */
  382. public function testFormatFractionDigitsIntl($value, $expected, $fractionDigits = null, $expectedFractionDigits = 1)
  383. {
  384. $this->skipIfIntlExtensionIsNotLoaded();
  385. $formatter = $this->getIntlFormatterWithDecimalStyle();
  386. if (null !== $fractionDigits) {
  387. $attributeRet = $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $fractionDigits);
  388. }
  389. $formattedValue = $formatter->format($value);
  390. $this->assertSame($expected, $formattedValue);
  391. $this->assertSame($expectedFractionDigits, $formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS));
  392. if (isset($attributeRet)) {
  393. $this->assertTrue($attributeRet);
  394. }
  395. }
  396. public function formatFractionDigitsProvider()
  397. {
  398. return array(
  399. array(1.123, '1.123', null, 0),
  400. array(1.123, '1', 0, 0),
  401. array(1.123, '1.1', 1, 1),
  402. array(1.123, '1.12', 2, 2),
  403. array(1.123, '1', -1, 0),
  404. array(1.123, '1', 'abc', 0)
  405. );
  406. }
  407. /**
  408. * @dataProvider formatGroupingUsedProvider
  409. */
  410. public function testFormatGroupingUsedStub($value, $expected, $groupingUsed = null, $expectedGroupingUsed = 1)
  411. {
  412. $formatter = $this->getStubFormatterWithDecimalStyle();
  413. if (null !== $groupingUsed) {
  414. $attributeRet = $formatter->setAttribute(StubNumberFormatter::GROUPING_USED, $groupingUsed);
  415. }
  416. $formattedValue = $formatter->format($value);
  417. $this->assertSame($expected, $formattedValue);
  418. $this->assertSame($expectedGroupingUsed, $formatter->getAttribute(StubNumberFormatter::GROUPING_USED));
  419. if (isset($attributeRet)) {
  420. $this->assertTrue($attributeRet);
  421. }
  422. }
  423. /**
  424. * @dataProvider formatGroupingUsedProvider
  425. */
  426. public function testFormatGroupingUsedIntl($value, $expected, $groupingUsed = null, $expectedGroupingUsed = 1)
  427. {
  428. $this->skipIfIntlExtensionIsNotLoaded();
  429. $formatter = $this->getIntlFormatterWithDecimalStyle();
  430. if (null !== $groupingUsed) {
  431. $attributeRet = $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $groupingUsed);
  432. }
  433. $formattedValue = $formatter->format($value);
  434. $this->assertSame($expected, $formattedValue);
  435. $this->assertSame($expectedGroupingUsed, $formatter->getAttribute(\NumberFormatter::GROUPING_USED));
  436. if (isset($attributeRet)) {
  437. $this->assertTrue($attributeRet);
  438. }
  439. }
  440. public function formatGroupingUsedProvider()
  441. {
  442. return array(
  443. array(1000, '1,000', null, 1),
  444. array(1000, '1000', 0, 0),
  445. array(1000, '1,000', 1, 1),
  446. array(1000, '1,000', 2, 1),
  447. array(1000, '1000', 'abc', 0),
  448. array(1000, '1,000', -1, 1),
  449. );
  450. }
  451. /**
  452. * @dataProvider formatRoundingModeRoundHalfUpProvider
  453. */
  454. public function testFormatRoundingModeStubRoundHalfUp($value, $expected)
  455. {
  456. $formatter = $this->getStubFormatterWithDecimalStyle();
  457. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  458. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFUP);
  459. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFUP rounding mode.');
  460. }
  461. /**
  462. * @dataProvider formatRoundingModeRoundHalfUpProvider
  463. */
  464. public function testFormatRoundingModeHalfUpIntl($value, $expected)
  465. {
  466. $this->skipIfIntlExtensionIsNotLoaded();
  467. $formatter = $this->getIntlFormatterWithDecimalStyle();
  468. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  469. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFUP);
  470. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFUP rounding mode.');
  471. }
  472. public function formatRoundingModeRoundHalfUpProvider()
  473. {
  474. // The commented value is differently rounded by intl's NumberFormatter in 32 and 64 bit architectures
  475. return array(
  476. array(1.121, '1.12'),
  477. array(1.123, '1.12'),
  478. // array(1.125, '1.13'),
  479. array(1.127, '1.13'),
  480. array(1.129, '1.13'),
  481. );
  482. }
  483. /**
  484. * @dataProvider formatRoundingModeRoundHalfDownProvider
  485. */
  486. public function testFormatRoundingModeStubRoundHalfDown($value, $expected)
  487. {
  488. $formatter = $this->getStubFormatterWithDecimalStyle();
  489. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  490. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFDOWN);
  491. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFDOWN rounding mode.');
  492. }
  493. /**
  494. * @dataProvider formatRoundingModeRoundHalfDownProvider
  495. */
  496. public function testFormatRoundingModeHalfDownIntl($value, $expected)
  497. {
  498. $this->skipIfIntlExtensionIsNotLoaded();
  499. $formatter = $this->getIntlFormatterWithDecimalStyle();
  500. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  501. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFDOWN);
  502. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFDOWN rounding mode.');
  503. }
  504. public function formatRoundingModeRoundHalfDownProvider()
  505. {
  506. return array(
  507. array(1.121, '1.12'),
  508. array(1.123, '1.12'),
  509. array(1.125, '1.12'),
  510. array(1.127, '1.13'),
  511. array(1.129, '1.13'),
  512. );
  513. }
  514. /**
  515. * @dataProvider formatRoundingModeRoundHalfEvenProvider
  516. */
  517. public function testFormatRoundingModeStubRoundHalfEven($value, $expected)
  518. {
  519. $formatter = $this->getStubFormatterWithDecimalStyle();
  520. $formatter->setAttribute(StubNumberFormatter::FRACTION_DIGITS, 2);
  521. $formatter->setAttribute(StubNumberFormatter::ROUNDING_MODE, StubNumberFormatter::ROUND_HALFEVEN);
  522. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFEVEN rounding mode.');
  523. }
  524. /**
  525. * @dataProvider formatRoundingModeRoundHalfEvenProvider
  526. */
  527. public function testFormatRoundingModeHalfEvenIntl($value, $expected)
  528. {
  529. $this->skipIfIntlExtensionIsNotLoaded();
  530. $formatter = $this->getIntlFormatterWithDecimalStyle();
  531. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
  532. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFEVEN);
  533. $this->assertSame($expected, $formatter->format($value), '->format() with ROUND_HALFEVEN rounding mode.');
  534. }
  535. public function formatRoundingModeRoundHalfEvenProvider()
  536. {
  537. return array(
  538. array(1.121, '1.12'),
  539. array(1.123, '1.12'),
  540. array(1.125, '1.12'),
  541. array(1.127, '1.13'),
  542. array(1.129, '1.13'),
  543. );
  544. }
  545. public function testGetErrorCode()
  546. {
  547. $formatter = $this->getStubFormatterWithDecimalStyle();
  548. $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
  549. }
  550. public function testGetLocale()
  551. {
  552. $formatter = $this->getStubFormatterWithDecimalStyle();
  553. $this->assertEquals('en', $formatter->getLocale());
  554. }
  555. /**
  556. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  557. */
  558. public function testGetPattern()
  559. {
  560. $formatter = $this->getStubFormatterWithDecimalStyle();
  561. $formatter->getPattern();
  562. }
  563. /**
  564. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  565. */
  566. public function testGetSymbol()
  567. {
  568. $formatter = $this->getStubFormatterWithDecimalStyle();
  569. $formatter->getSymbol(null);
  570. }
  571. /**
  572. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  573. */
  574. public function testGetTextAttribute()
  575. {
  576. $formatter = $this->getStubFormatterWithDecimalStyle();
  577. $formatter->getTextAttribute(null);
  578. }
  579. /**
  580. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  581. */
  582. public function testParseCurrency()
  583. {
  584. $formatter = $this->getStubFormatterWithDecimalStyle();
  585. $formatter->parseCurrency(null, $currency);
  586. }
  587. /**
  588. * @dataProvider parseProvider
  589. */
  590. public function testParseStub($value, $expected, $message = '')
  591. {
  592. $formatter = $this->getStubFormatterWithDecimalStyle();
  593. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
  594. $this->assertSame($expected, $parsedValue, $message);
  595. if ($expected === false) {
  596. $errorCode = StubIntl::U_PARSE_ERROR;
  597. $errorMessage = 'Number parsing failed: U_PARSE_ERROR';
  598. } else {
  599. $errorCode = StubIntl::U_ZERO_ERROR;
  600. $errorMessage = 'U_ZERO_ERROR';
  601. }
  602. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  603. $this->assertSame($errorCode, StubIntl::getErrorCode());
  604. $this->assertSame($errorCode !== 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  605. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  606. $this->assertSame($errorCode, $formatter->getErrorCode());
  607. $this->assertSame($errorCode !== 0, StubIntl::isFailure($formatter->getErrorCode()));
  608. }
  609. /**
  610. * @dataProvider parseProvider
  611. */
  612. public function testParseIntl($value, $expected, $message = '')
  613. {
  614. $this->skipIfIntlExtensionIsNotLoaded();
  615. $this->skipIfICUVersionIsTooOld();
  616. $formatter = $this->getIntlFormatterWithDecimalStyle();
  617. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  618. $this->assertSame($expected, $parsedValue, $message);
  619. if ($expected === false) {
  620. $errorCode = StubIntl::U_PARSE_ERROR;
  621. $errorMessage = 'Number parsing failed: U_PARSE_ERROR';
  622. } else {
  623. $errorCode = StubIntl::U_ZERO_ERROR;
  624. $errorMessage = 'U_ZERO_ERROR';
  625. }
  626. $this->assertSame($errorMessage, intl_get_error_message());
  627. $this->assertSame($errorCode, intl_get_error_code());
  628. $this->assertSame($errorCode > 0, intl_is_failure(intl_get_error_code()));
  629. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  630. $this->assertSame($errorCode, $formatter->getErrorCode());
  631. $this->assertSame($errorCode > 0, intl_is_failure($formatter->getErrorCode()));
  632. }
  633. public function parseProvider()
  634. {
  635. return array(
  636. array('prefix1', false, '->parse() does not parse a number with a string prefix.'),
  637. array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'),
  638. );
  639. }
  640. /**
  641. * @expectedException PHPUnit_Framework_Error_Warning
  642. */
  643. public function testParseTypeDefaultStub()
  644. {
  645. $formatter = $this->getStubFormatterWithDecimalStyle();
  646. $formatter->parse('1', StubNumberFormatter::TYPE_DEFAULT);
  647. }
  648. /**
  649. * @expectedException PHPUnit_Framework_Error_Warning
  650. */
  651. public function testParseTypeDefaultIntl()
  652. {
  653. $this->skipIfIntlExtensionIsNotLoaded();
  654. $formatter = $this->getIntlFormatterWithDecimalStyle();
  655. $formatter->parse('1', \NumberFormatter::TYPE_DEFAULT);
  656. }
  657. /**
  658. * @dataProvider parseTypeInt32Provider
  659. */
  660. public function testParseTypeInt32Stub($value, $expected, $message = '')
  661. {
  662. $formatter = $this->getStubFormatterWithDecimalStyle();
  663. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_INT32);
  664. $this->assertSame($expected, $parsedValue);
  665. }
  666. /**
  667. * @dataProvider parseTypeInt32Provider
  668. */
  669. public function testParseTypeInt32Intl($value, $expected, $message = '')
  670. {
  671. $this->skipIfIntlExtensionIsNotLoaded();
  672. $formatter = $this->getIntlFormatterWithDecimalStyle();
  673. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_INT32);
  674. $this->assertSame($expected, $parsedValue);
  675. }
  676. public function parseTypeInt32Provider()
  677. {
  678. return array(
  679. array('1', 1),
  680. array('1.1', 1),
  681. array('2,147,483,647', 2147483647),
  682. array('-2,147,483,648', -2147483647 - 1),
  683. array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'),
  684. array('-2,147,483,649', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer negative range.')
  685. );
  686. }
  687. // Stub Tests
  688. public function testParseTypeInt64StubWith32BitIntegerInPhp32Bit()
  689. {
  690. $this->skipIfIntlExtensionIsNotLoaded();
  691. $this->skipIfNot32Bit();
  692. $formatter = $this->getStubFormatterWithDecimalStyle();
  693. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  694. $this->assertInternalType('integer', $parsedValue);
  695. $this->assertEquals(2147483647, $parsedValue);
  696. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  697. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  698. // The negative PHP_INT_MAX was being converted to float
  699. if (
  700. (version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) ||
  701. version_compare(PHP_VERSION, '5.4.4', '>=')
  702. ) {
  703. $this->assertInternalType('int', $parsedValue);
  704. } else {
  705. $this->assertInternalType('float', $parsedValue);
  706. }
  707. $this->assertEquals(-2147483648, $parsedValue);
  708. }
  709. public function testParseTypeInt64StubWith32BitIntegerInPhp64Bit()
  710. {
  711. $this->skipIfNot64Bit();
  712. $formatter = $this->getStubFormatterWithDecimalStyle();
  713. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  714. $this->assertInternalType('integer', $parsedValue);
  715. $this->assertEquals(2147483647, $parsedValue);
  716. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  717. $this->assertInternalType('integer', $parsedValue);
  718. $this->assertEquals(-2147483647 - 1, $parsedValue);
  719. }
  720. /**
  721. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  722. */
  723. public function testParseTypeInt64StubWith64BitIntegerInPhp32Bit()
  724. {
  725. $this->skipIfNot32Bit();
  726. $formatter = $this->getStubFormatterWithDecimalStyle();
  727. // int 64 using only 32 bit range strangeness
  728. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  729. $this->assertInternalType('float', $parsedValue);
  730. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  731. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  732. $this->assertInternalType('float', $parsedValue);
  733. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  734. }
  735. /**
  736. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  737. */
  738. public function testParseTypeInt64StubWith64BitIntegerInPhp64Bit()
  739. {
  740. $this->skipIfNot64Bit();
  741. $formatter = $this->getStubFormatterWithDecimalStyle();
  742. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  743. $this->assertInternalType('integer', $parsedValue);
  744. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  745. // A 32 bit integer was being generated instead of a 64 bit integer
  746. if (
  747. (version_compare(PHP_VERSION, '5.3.14', '<')) ||
  748. (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
  749. ) {
  750. $this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
  751. } else {
  752. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
  753. }
  754. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  755. $this->assertInternalType('integer', $parsedValue);
  756. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  757. // A 32 bit integer was being generated instead of a 64 bit integer
  758. if (
  759. (version_compare(PHP_VERSION, '5.3.14', '<')) ||
  760. (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
  761. ) {
  762. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
  763. } else {
  764. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
  765. }
  766. }
  767. // Intl Tests
  768. public function testParseTypeInt64IntlWith32BitIntegerInPhp32Bit()
  769. {
  770. $this->skipIfIntlExtensionIsNotLoaded();
  771. $this->skipIfNot32Bit();
  772. $formatter = $this->getIntlFormatterWithDecimalStyle();
  773. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  774. $this->assertInternalType('integer', $parsedValue);
  775. $this->assertEquals(2147483647, $parsedValue);
  776. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  777. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  778. // The negative PHP_INT_MAX was being converted to float.
  779. if (
  780. (version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) ||
  781. version_compare(PHP_VERSION, '5.4.4', '>=')
  782. ) {
  783. $this->assertInternalType('int', $parsedValue);
  784. } else {
  785. $this->assertInternalType('float', $parsedValue);
  786. }
  787. $this->assertEquals(-2147483648, $parsedValue);
  788. }
  789. public function testParseTypeInt64IntlWith32BitIntegerInPhp64Bit()
  790. {
  791. $this->skipIfIntlExtensionIsNotLoaded();
  792. $this->skipIfNot64Bit();
  793. $formatter = $this->getIntlFormatterWithDecimalStyle();
  794. $parsedValue = $formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
  795. $this->assertInternalType('integer', $parsedValue);
  796. $this->assertEquals(2147483647, $parsedValue);
  797. $parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
  798. $this->assertInternalType('integer', $parsedValue);
  799. $this->assertEquals(-2147483647 - 1, $parsedValue);
  800. }
  801. /**
  802. * If PHP is compiled in 32bit mode, the returned value for a 64bit integer are float numbers.
  803. */
  804. public function testParseTypeInt64IntlWith64BitIntegerInPhp32Bit()
  805. {
  806. $this->skipIfIntlExtensionIsNotLoaded();
  807. $this->skipIfNot32Bit();
  808. $formatter = $this->getIntlFormatterWithDecimalStyle();
  809. // int 64 using only 32 bit range strangeness
  810. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  811. $this->assertInternalType('float', $parsedValue);
  812. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  813. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  814. $this->assertInternalType('float', $parsedValue);
  815. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
  816. }
  817. /**
  818. * If PHP is compiled in 64bit mode, the returned value for a 64bit integer are 32bit integer numbers.
  819. */
  820. public function testParseTypeInt64IntlWith64BitIntegerInPhp64Bit()
  821. {
  822. $this->skipIfIntlExtensionIsNotLoaded();
  823. $this->skipIfNot64Bit();
  824. $formatter = $this->getIntlFormatterWithDecimalStyle();
  825. $parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
  826. $this->assertInternalType('integer', $parsedValue);
  827. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  828. // A 32 bit integer was being generated instead of a 64 bit integer
  829. if (
  830. (version_compare(PHP_VERSION, '5.3.14', '<')) ||
  831. (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
  832. ) {
  833. $this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
  834. } else {
  835. $this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
  836. }
  837. $parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
  838. $this->assertInternalType('integer', $parsedValue);
  839. // Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
  840. // A 32 bit integer was being generated instead of a 64 bit integer
  841. if (
  842. (version_compare(PHP_VERSION, '5.3.14', '<')) ||
  843. (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
  844. ) {
  845. $this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
  846. } else {
  847. $this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
  848. }
  849. }
  850. /**
  851. * @dataProvider parseTypeDoubleProvider
  852. */
  853. public function testParseTypeDoubleStub($value, $expectedValue)
  854. {
  855. $formatter = $this->getStubFormatterWithDecimalStyle();
  856. $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
  857. $this->assertSame($expectedValue, $parsedValue);
  858. }
  859. /**
  860. * @dataProvider parseTypeDoubleProvider
  861. */
  862. public function testParseTypeDoubleIntl($value, $expectedValue)
  863. {
  864. $this->skipIfIntlExtensionIsNotLoaded();
  865. $formatter = $this->getIntlFormatterWithDecimalStyle();
  866. $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
  867. $this->assertSame($expectedValue, $parsedValue);
  868. }
  869. public function parseTypeDoubleProvider()
  870. {
  871. return array(
  872. array('1', (float) 1),
  873. array('1.1', 1.1),
  874. array('9,223,372,036,854,775,808', 9223372036854775808),
  875. array('-9,223,372,036,854,775,809', -9223372036854775809),
  876. );
  877. }
  878. /**
  879. * @expectedException PHPUnit_Framework_Error_Warning
  880. */
  881. public function testParseTypeCurrencyStub()
  882. {
  883. $formatter = $this->getStubFormatterWithDecimalStyle();
  884. $formatter->parse('1', StubNumberFormatter::TYPE_CURRENCY);
  885. }
  886. /**
  887. * @expectedException PHPUnit_Framework_Error_Warning
  888. */
  889. public function testParseTypeCurrencyIntl()
  890. {
  891. $this->skipIfIntlExtensionIsNotLoaded();
  892. $formatter = $this->getIntlFormatterWithDecimalStyle();
  893. $formatter->parse('1', \NumberFormatter::TYPE_CURRENCY);
  894. }
  895. public function testParseWithNullPositionValueStub()
  896. {
  897. $position = null;
  898. $formatter = $this->getStubFormatterWithDecimalStyle();
  899. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  900. $this->assertNull($position);
  901. }
  902. public function testParseWithNullPositionValueIntl()
  903. {
  904. $this->skipIfIntlExtensionIsNotLoaded();
  905. $position = 0;
  906. $formatter = $this->getIntlFormatterWithDecimalStyle();
  907. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  908. $this->assertEquals(3, $position);
  909. }
  910. /**
  911. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  912. */
  913. public function testParseWithNotNullPositionValueStub()
  914. {
  915. $position = 1;
  916. $formatter = $this->getStubFormatterWithDecimalStyle();
  917. $formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
  918. }
  919. public function testParseWithNotNullPositionValueIntl()
  920. {
  921. $this->skipIfIntlExtensionIsNotLoaded();
  922. $position = 1;
  923. $formatter = $this->getIntlFormatterWithDecimalStyle();
  924. $parsedValue = $formatter->parse('123', \NumberFormatter::TYPE_DOUBLE, $position);
  925. $this->assertEquals(3, $position);
  926. }
  927. /**
  928. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  929. */
  930. public function testSetPattern()
  931. {
  932. $formatter = $this->getStubFormatterWithDecimalStyle();
  933. $formatter->setPattern(null);
  934. }
  935. /**
  936. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  937. */
  938. public function testSetSymbol()
  939. {
  940. $formatter = $this->getStubFormatterWithDecimalStyle();
  941. $formatter->setSymbol(null, null);
  942. }
  943. /**
  944. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  945. */
  946. public function testSetTextAttribute()
  947. {
  948. $formatter = $this->getStubFormatterWithDecimalStyle();
  949. $formatter->setTextAttribute(null, null);
  950. }
  951. protected function getStubFormatterWithDecimalStyle()
  952. {
  953. return new StubNumberFormatter('en', StubNumberFormatter::DECIMAL);
  954. }
  955. protected function getStubFormatterWithCurrencyStyle()
  956. {
  957. return new StubNumberFormatter('en', StubNumberFormatter::CURRENCY);
  958. }
  959. protected function getIntlFormatterWithDecimalStyle()
  960. {
  961. if (!$this->isIntlExtensionLoaded()) {
  962. return null;
  963. }
  964. return new \NumberFormatter('en', \NumberFormatter::DECIMAL);
  965. }
  966. protected function getIntlFormatterWithCurrencyStyle()
  967. {
  968. if (!$this->isIntlExtensionLoaded()) {
  969. return null;
  970. }
  971. $formatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
  972. $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
  973. return $formatter;
  974. }
  975. }