StubNumberFormatterTest.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. }