"Fossies" - the Fresh Open Source Software Archive

Member "Symfony/vendor/symfony/symfony/src/Symfony/Component/Intl/Tests/Data/Provider/AbstractCurrencyDataProviderTest.php" (30 Mar 2020, 17277 Bytes) of package /linux/www/Symfony_Standard_Vendors_3.4.39.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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