AbstractCurrencyDataProviderTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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\Component\Intl\Tests\Data\Provider;
  11. use Symfony\Component\Intl\Data\Provider\CurrencyDataProvider;
  12. use Symfony\Component\Intl\Intl;
  13. use Symfony\Component\Intl\Locale;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. abstract class AbstractCurrencyDataProviderTest extends AbstractDataProviderTest
  18. {
  19. // The below arrays document the state of the ICU data bundled with this package.
  20. protected static $currencies = array(
  21. 'ADP',
  22. 'AED',
  23. 'AFA',
  24. 'AFN',
  25. 'ALK',
  26. 'ALL',
  27. 'AMD',
  28. 'ANG',
  29. 'AOA',
  30. 'AOK',
  31. 'AON',
  32. 'AOR',
  33. 'ARA',
  34. 'ARL',
  35. 'ARM',
  36. 'ARP',
  37. 'ARS',
  38. 'ATS',
  39. 'AUD',
  40. 'AWG',
  41. 'AZM',
  42. 'AZN',
  43. 'BAD',
  44. 'BAM',
  45. 'BAN',
  46. 'BBD',
  47. 'BDT',
  48. 'BEC',
  49. 'BEF',
  50. 'BEL',
  51. 'BGL',
  52. 'BGM',
  53. 'BGN',
  54. 'BGO',
  55. 'BHD',
  56. 'BIF',
  57. 'BMD',
  58. 'BND',
  59. 'BOB',
  60. 'BOL',
  61. 'BOP',
  62. 'BOV',
  63. 'BRB',
  64. 'BRC',
  65. 'BRE',
  66. 'BRL',
  67. 'BRN',
  68. 'BRR',
  69. 'BRZ',
  70. 'BSD',
  71. 'BTN',
  72. 'BUK',
  73. 'BWP',
  74. 'BYB',
  75. 'BYR',
  76. 'BZD',
  77. 'CAD',
  78. 'CDF',
  79. 'CHE',
  80. 'CHF',
  81. 'CHW',
  82. 'CLE',
  83. 'CLF',
  84. 'CLP',
  85. 'CNX',
  86. 'CNY',
  87. 'COP',
  88. 'COU',
  89. 'CRC',
  90. 'CSD',
  91. 'CSK',
  92. 'CUC',
  93. 'CUP',
  94. 'CVE',
  95. 'CYP',
  96. 'CZK',
  97. 'DDM',
  98. 'DEM',
  99. 'DJF',
  100. 'DKK',
  101. 'DOP',
  102. 'DZD',
  103. 'ECS',
  104. 'ECV',
  105. 'EEK',
  106. 'EGP',
  107. 'ERN',
  108. 'ESA',
  109. 'ESB',
  110. 'ESP',
  111. 'ETB',
  112. 'EUR',
  113. 'FIM',
  114. 'FJD',
  115. 'FKP',
  116. 'FRF',
  117. 'GBP',
  118. 'GEK',
  119. 'GEL',
  120. 'GHC',
  121. 'GHS',
  122. 'GIP',
  123. 'GMD',
  124. 'GNF',
  125. 'GNS',
  126. 'GQE',
  127. 'GRD',
  128. 'GTQ',
  129. 'GWE',
  130. 'GWP',
  131. 'GYD',
  132. 'HKD',
  133. 'HNL',
  134. 'HRD',
  135. 'HRK',
  136. 'HTG',
  137. 'HUF',
  138. 'IDR',
  139. 'IEP',
  140. 'ILP',
  141. 'ILR',
  142. 'ILS',
  143. 'INR',
  144. 'IQD',
  145. 'IRR',
  146. 'ISJ',
  147. 'ISK',
  148. 'ITL',
  149. 'JMD',
  150. 'JOD',
  151. 'JPY',
  152. 'KES',
  153. 'KGS',
  154. 'KHR',
  155. 'KMF',
  156. 'KPW',
  157. 'KRH',
  158. 'KRO',
  159. 'KRW',
  160. 'KWD',
  161. 'KYD',
  162. 'KZT',
  163. 'LAK',
  164. 'LBP',
  165. 'LKR',
  166. 'LRD',
  167. 'LSL',
  168. 'LTL',
  169. 'LTT',
  170. 'LUC',
  171. 'LUF',
  172. 'LUL',
  173. 'LVL',
  174. 'LVR',
  175. 'LYD',
  176. 'MAD',
  177. 'MAF',
  178. 'MCF',
  179. 'MDC',
  180. 'MDL',
  181. 'MGA',
  182. 'MGF',
  183. 'MKD',
  184. 'MKN',
  185. 'MLF',
  186. 'MMK',
  187. 'MNT',
  188. 'MOP',
  189. 'MRO',
  190. 'MTL',
  191. 'MTP',
  192. 'MUR',
  193. 'MVP',
  194. 'MVR',
  195. 'MWK',
  196. 'MXN',
  197. 'MXP',
  198. 'MXV',
  199. 'MYR',
  200. 'MZE',
  201. 'MZM',
  202. 'MZN',
  203. 'NAD',
  204. 'NGN',
  205. 'NIC',
  206. 'NIO',
  207. 'NLG',
  208. 'NOK',
  209. 'NPR',
  210. 'NZD',
  211. 'OMR',
  212. 'PAB',
  213. 'PEI',
  214. 'PEN',
  215. 'PES',
  216. 'PGK',
  217. 'PHP',
  218. 'PKR',
  219. 'PLN',
  220. 'PLZ',
  221. 'PTE',
  222. 'PYG',
  223. 'QAR',
  224. 'RHD',
  225. 'ROL',
  226. 'RON',
  227. 'RSD',
  228. 'RUB',
  229. 'RUR',
  230. 'RWF',
  231. 'SAR',
  232. 'SBD',
  233. 'SCR',
  234. 'SDD',
  235. 'SDG',
  236. 'SDP',
  237. 'SEK',
  238. 'SGD',
  239. 'SHP',
  240. 'SIT',
  241. 'SKK',
  242. 'SLL',
  243. 'SOS',
  244. 'SRD',
  245. 'SRG',
  246. 'SSP',
  247. 'STD',
  248. 'SUR',
  249. 'SVC',
  250. 'SYP',
  251. 'SZL',
  252. 'THB',
  253. 'TJR',
  254. 'TJS',
  255. 'TMM',
  256. 'TMT',
  257. 'TND',
  258. 'TOP',
  259. 'TPE',
  260. 'TRL',
  261. 'TRY',
  262. 'TTD',
  263. 'TWD',
  264. 'TZS',
  265. 'UAH',
  266. 'UAK',
  267. 'UGS',
  268. 'UGX',
  269. 'USD',
  270. 'USN',
  271. 'USS',
  272. 'UYI',
  273. 'UYP',
  274. 'UYU',
  275. 'UZS',
  276. 'VEB',
  277. 'VEF',
  278. 'VND',
  279. 'VNN',
  280. 'VUV',
  281. 'WST',
  282. 'XAF',
  283. 'XCD',
  284. 'XEU',
  285. 'XFO',
  286. 'XFU',
  287. 'XOF',
  288. 'XPF',
  289. 'XRE',
  290. 'YDD',
  291. 'YER',
  292. 'YUD',
  293. 'YUM',
  294. 'YUN',
  295. 'YUR',
  296. 'ZAL',
  297. 'ZAR',
  298. 'ZMK',
  299. 'ZMW',
  300. 'ZRN',
  301. 'ZRZ',
  302. 'ZWD',
  303. 'ZWL',
  304. 'ZWR',
  305. );
  306. protected static $alpha3ToNumeric = array(
  307. 'ADP' => 20,
  308. 'AED' => 784,
  309. 'AFA' => 4,
  310. 'AFN' => 971,
  311. 'ALL' => 8,
  312. 'AMD' => 51,
  313. 'ANG' => 532,
  314. 'AOA' => 973,
  315. 'AON' => 24,
  316. 'AOR' => 982,
  317. 'ARA' => 32,
  318. 'ARP' => 32,
  319. 'ARS' => 32,
  320. 'ATS' => 40,
  321. 'AUD' => 36,
  322. 'AWG' => 533,
  323. 'AZM' => 31,
  324. 'AZN' => 944,
  325. 'BAD' => 70,
  326. 'BAM' => 977,
  327. 'BBD' => 52,
  328. 'BDT' => 50,
  329. 'BEC' => 993,
  330. 'BEF' => 56,
  331. 'BEL' => 992,
  332. 'BGL' => 100,
  333. 'BGN' => 975,
  334. 'BHD' => 48,
  335. 'BIF' => 108,
  336. 'BMD' => 60,
  337. 'BND' => 96,
  338. 'BOB' => 68,
  339. 'BOV' => 984,
  340. 'BRC' => 76,
  341. 'BRE' => 76,
  342. 'BRL' => 986,
  343. 'BRN' => 76,
  344. 'BRR' => 987,
  345. 'BSD' => 44,
  346. 'BTN' => 64,
  347. 'BWP' => 72,
  348. 'BYB' => 112,
  349. 'BYR' => 974,
  350. 'BZD' => 84,
  351. 'CAD' => 124,
  352. 'CDF' => 976,
  353. 'CHE' => 947,
  354. 'CHF' => 756,
  355. 'CHW' => 948,
  356. 'CLF' => 990,
  357. 'CLP' => 152,
  358. 'CNY' => 156,
  359. 'COP' => 170,
  360. 'COU' => 970,
  361. 'CRC' => 188,
  362. 'CSD' => 891,
  363. 'CSK' => 200,
  364. 'CUC' => 931,
  365. 'CUP' => 192,
  366. 'CVE' => 132,
  367. 'CYP' => 196,
  368. 'CZK' => 203,
  369. 'DDM' => 278,
  370. 'DEM' => 276,
  371. 'DJF' => 262,
  372. 'DKK' => 208,
  373. 'DOP' => 214,
  374. 'DZD' => 12,
  375. 'ECS' => 218,
  376. 'ECV' => 983,
  377. 'EEK' => 233,
  378. 'EGP' => 818,
  379. 'ERN' => 232,
  380. 'ESA' => 996,
  381. 'ESB' => 995,
  382. 'ESP' => 724,
  383. 'ETB' => 230,
  384. 'EUR' => 978,
  385. 'FIM' => 246,
  386. 'FJD' => 242,
  387. 'FKP' => 238,
  388. 'FRF' => 250,
  389. 'GBP' => 826,
  390. 'GEK' => 268,
  391. 'GEL' => 981,
  392. 'GHC' => 288,
  393. 'GHS' => 936,
  394. 'GIP' => 292,
  395. 'GMD' => 270,
  396. 'GNF' => 324,
  397. 'GQE' => 226,
  398. 'GRD' => 300,
  399. 'GTQ' => 320,
  400. 'GWP' => 624,
  401. 'GYD' => 328,
  402. 'HKD' => 344,
  403. 'HNL' => 340,
  404. 'HRD' => 191,
  405. 'HRK' => 191,
  406. 'HTG' => 332,
  407. 'HUF' => 348,
  408. 'IDR' => 360,
  409. 'IEP' => 372,
  410. 'ILS' => 376,
  411. 'INR' => 356,
  412. 'IQD' => 368,
  413. 'IRR' => 364,
  414. 'ISK' => 352,
  415. 'ITL' => 380,
  416. 'JMD' => 388,
  417. 'JOD' => 400,
  418. 'JPY' => 392,
  419. 'KES' => 404,
  420. 'KGS' => 417,
  421. 'KHR' => 116,
  422. 'KMF' => 174,
  423. 'KPW' => 408,
  424. 'KRW' => 410,
  425. 'KWD' => 414,
  426. 'KYD' => 136,
  427. 'KZT' => 398,
  428. 'LAK' => 418,
  429. 'LBP' => 422,
  430. 'LKR' => 144,
  431. 'LRD' => 430,
  432. 'LSL' => 426,
  433. 'LTL' => 440,
  434. 'LTT' => 440,
  435. 'LUC' => 989,
  436. 'LUF' => 442,
  437. 'LUL' => 988,
  438. 'LVL' => 428,
  439. 'LVR' => 428,
  440. 'LYD' => 434,
  441. 'MAD' => 504,
  442. 'MDL' => 498,
  443. 'MGA' => 969,
  444. 'MGF' => 450,
  445. 'MKD' => 807,
  446. 'MLF' => 466,
  447. 'MMK' => 104,
  448. 'MNT' => 496,
  449. 'MOP' => 446,
  450. 'MRO' => 478,
  451. 'MTL' => 470,
  452. 'MUR' => 480,
  453. 'MVR' => 462,
  454. 'MWK' => 454,
  455. 'MXN' => 484,
  456. 'MXV' => 979,
  457. 'MYR' => 458,
  458. 'MZM' => 508,
  459. 'MZN' => 943,
  460. 'NAD' => 516,
  461. 'NGN' => 566,
  462. 'NIO' => 558,
  463. 'NLG' => 528,
  464. 'NOK' => 578,
  465. 'NPR' => 524,
  466. 'NZD' => 554,
  467. 'OMR' => 512,
  468. 'PAB' => 590,
  469. 'PEI' => 604,
  470. 'PEN' => 604,
  471. 'PES' => 604,
  472. 'PGK' => 598,
  473. 'PHP' => 608,
  474. 'PKR' => 586,
  475. 'PLN' => 985,
  476. 'PLZ' => 616,
  477. 'PTE' => 620,
  478. 'PYG' => 600,
  479. 'QAR' => 634,
  480. 'ROL' => 642,
  481. 'RON' => 946,
  482. 'RSD' => 941,
  483. 'RUB' => 643,
  484. 'RUR' => 810,
  485. 'RWF' => 646,
  486. 'SAR' => 682,
  487. 'SBD' => 90,
  488. 'SCR' => 690,
  489. 'SDD' => 736,
  490. 'SDG' => 938,
  491. 'SEK' => 752,
  492. 'SGD' => 702,
  493. 'SHP' => 654,
  494. 'SIT' => 705,
  495. 'SKK' => 703,
  496. 'SLL' => 694,
  497. 'SOS' => 706,
  498. 'SRD' => 968,
  499. 'SRG' => 740,
  500. 'SSP' => 728,
  501. 'STD' => 678,
  502. 'SVC' => 222,
  503. 'SYP' => 760,
  504. 'SZL' => 748,
  505. 'THB' => 764,
  506. 'TJR' => 762,
  507. 'TJS' => 972,
  508. 'TMM' => 795,
  509. 'TMT' => 934,
  510. 'TND' => 788,
  511. 'TOP' => 776,
  512. 'TPE' => 626,
  513. 'TRL' => 792,
  514. 'TRY' => 949,
  515. 'TTD' => 780,
  516. 'TWD' => 901,
  517. 'TZS' => 834,
  518. 'UAH' => 980,
  519. 'UAK' => 804,
  520. 'UGX' => 800,
  521. 'USD' => 840,
  522. 'USN' => 997,
  523. 'USS' => 998,
  524. 'UYI' => 940,
  525. 'UYU' => 858,
  526. 'UZS' => 860,
  527. 'VEB' => 862,
  528. 'VEF' => 937,
  529. 'VND' => 704,
  530. 'VUV' => 548,
  531. 'WST' => 882,
  532. 'XAF' => 950,
  533. 'XCD' => 951,
  534. 'XEU' => 954,
  535. 'XOF' => 952,
  536. 'XPF' => 953,
  537. 'YDD' => 720,
  538. 'YER' => 886,
  539. 'YUM' => 891,
  540. 'YUN' => 890,
  541. 'ZAL' => 991,
  542. 'ZAR' => 710,
  543. 'ZMK' => 894,
  544. 'ZMW' => 967,
  545. 'ZRN' => 180,
  546. 'ZRZ' => 180,
  547. 'ZWD' => 716,
  548. 'ZWL' => 932,
  549. 'ZWR' => 935,
  550. );
  551. /**
  552. * @var CurrencyDataProvider
  553. */
  554. protected $dataProvider;
  555. protected function setUp()
  556. {
  557. parent::setUp();
  558. $this->dataProvider = new CurrencyDataProvider(
  559. $this->getDataDirectory().'/'.Intl::CURRENCY_DIR,
  560. $this->createEntryReader()
  561. );
  562. }
  563. abstract protected function getDataDirectory();
  564. public function testGetCurrencies()
  565. {
  566. $this->assertSame(static::$currencies, $this->dataProvider->getCurrencies());
  567. }
  568. /**
  569. * @dataProvider provideLocales
  570. */
  571. public function testGetNames($displayLocale)
  572. {
  573. $names = $this->dataProvider->getNames($displayLocale);
  574. $keys = array_keys($names);
  575. sort($keys);
  576. $this->assertEquals(static::$currencies, $keys);
  577. // Names should be sorted
  578. $sortedNames = $names;
  579. $collator = new \Collator($displayLocale);
  580. $collator->asort($names);
  581. $this->assertSame($sortedNames, $names);
  582. }
  583. public function testGetNamesDefaultLocale()
  584. {
  585. Locale::setDefault('de_AT');
  586. $this->assertSame(
  587. $this->dataProvider->getNames('de_AT'),
  588. $this->dataProvider->getNames()
  589. );
  590. }
  591. /**
  592. * @dataProvider provideLocaleAliases
  593. */
  594. public function testGetNamesSupportsAliases($alias, $ofLocale)
  595. {
  596. // Can't use assertSame(), because some aliases contain scripts with
  597. // different collation (=order of output) than their aliased locale
  598. // e.g. sr_Latn_ME => sr_ME
  599. $this->assertEquals(
  600. $this->dataProvider->getNames($ofLocale),
  601. $this->dataProvider->getNames($alias)
  602. );
  603. }
  604. /**
  605. * @dataProvider provideLocales
  606. */
  607. public function testGetName($displayLocale)
  608. {
  609. $expected = $this->dataProvider->getNames($displayLocale);
  610. $actual = array();
  611. foreach ($expected as $currency => $name) {
  612. $actual[$currency] = $this->dataProvider->getName($currency, $displayLocale);
  613. }
  614. $this->assertSame($expected, $actual);
  615. }
  616. public function testGetNameDefaultLocale()
  617. {
  618. Locale::setDefault('de_AT');
  619. $expected = $this->dataProvider->getNames('de_AT');
  620. $actual = array();
  621. foreach ($expected as $currency => $name) {
  622. $actual[$currency] = $this->dataProvider->getName($currency);
  623. }
  624. $this->assertSame($expected, $actual);
  625. }
  626. /**
  627. * @dataProvider provideLocales
  628. */
  629. public function testGetSymbol($displayLocale)
  630. {
  631. $currencies = $this->dataProvider->getCurrencies();
  632. foreach ($currencies as $currency) {
  633. $this->assertGreaterThan(0, mb_strlen($this->dataProvider->getSymbol($currency, $displayLocale)));
  634. }
  635. }
  636. public function provideCurrencies()
  637. {
  638. return array_map(
  639. function ($currency) { return array($currency); },
  640. static::$currencies
  641. );
  642. }
  643. /**
  644. * @dataProvider provideCurrencies
  645. */
  646. public function testGetFractionDigits($currency)
  647. {
  648. $this->assertInternalType('numeric', $this->dataProvider->getFractionDigits($currency));
  649. }
  650. /**
  651. * @dataProvider provideCurrencies
  652. */
  653. public function testGetRoundingIncrement($currency)
  654. {
  655. $this->assertInternalType('numeric', $this->dataProvider->getRoundingIncrement($currency));
  656. }
  657. public function provideCurrenciesWithNumericEquivalent()
  658. {
  659. return array_map(
  660. function ($value) { return array($value); },
  661. array_keys(static::$alpha3ToNumeric)
  662. );
  663. }
  664. /**
  665. * @dataProvider provideCurrenciesWithNumericEquivalent
  666. */
  667. public function testGetNumericCode($currency)
  668. {
  669. $this->assertSame(static::$alpha3ToNumeric[$currency], $this->dataProvider->getNumericCode($currency));
  670. }
  671. public function provideCurrenciesWithoutNumericEquivalent()
  672. {
  673. return array_map(
  674. function ($value) { return array($value); },
  675. array_diff(static::$currencies, array_keys(static::$alpha3ToNumeric))
  676. );
  677. }
  678. /**
  679. * @dataProvider provideCurrenciesWithoutNumericEquivalent
  680. * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
  681. */
  682. public function testGetNumericCodeFailsIfNoNumericEquivalent($currency)
  683. {
  684. $this->dataProvider->getNumericCode($currency);
  685. }
  686. public function provideValidNumericCodes()
  687. {
  688. $numericToAlpha3 = $this->getNumericToAlpha3Mapping();
  689. return array_map(
  690. function ($numeric, $alpha3) { return array($numeric, $alpha3); },
  691. array_keys($numericToAlpha3),
  692. $numericToAlpha3
  693. );
  694. }
  695. /**
  696. * @dataProvider provideValidNumericCodes
  697. */
  698. public function testForNumericCode($numeric, $expected)
  699. {
  700. $actual = $this->dataProvider->forNumericCode($numeric);
  701. // Make sure that a different array order doesn't break the test
  702. sort($actual);
  703. sort($expected);
  704. $this->assertEquals($expected, $actual);
  705. }
  706. public function provideInvalidNumericCodes()
  707. {
  708. $validNumericCodes = array_keys($this->getNumericToAlpha3Mapping());
  709. $invalidNumericCodes = array_diff(range(0, 1000), $validNumericCodes);
  710. return array_map(
  711. function ($value) { return array($value); },
  712. $invalidNumericCodes
  713. );
  714. }
  715. /**
  716. * @dataProvider provideInvalidNumericCodes
  717. * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
  718. */
  719. public function testForNumericCodeFailsIfInvalidNumericCode($currency)
  720. {
  721. $this->dataProvider->forNumericCode($currency);
  722. }
  723. private function getNumericToAlpha3Mapping()
  724. {
  725. $numericToAlpha3 = array();
  726. foreach (static::$alpha3ToNumeric as $alpha3 => $numeric) {
  727. if (!isset($numericToAlpha3[$numeric])) {
  728. $numericToAlpha3[$numeric] = array();
  729. }
  730. $numericToAlpha3[$numeric][] = $alpha3;
  731. }
  732. return $numericToAlpha3;
  733. }
  734. }