From 101656401dbb7f66b654ec8750ce48577b04b43c Mon Sep 17 00:00:00 2001 From: Thomas Praxl Date: Thu, 23 Nov 2017 17:36:10 +0100 Subject: [PATCH] Get currency codes from library to stay up to date --- composer.json | 3 ++- src/Faker/Provider/Miscellaneous.php | 29 +++++++---------------- test/Faker/Provider/MiscellaneousTest.php | 11 +++++++++ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index e52016ee0c..8957776880 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ } ], "require": { - "php": "^5.3.3 || ^7.0" + "php": "^5.3.3 || ^7.0", + "alcohol/iso4217": "^3.1" }, "require-dev": { "phpunit/phpunit": "^4.8.35 || ^5.7", diff --git a/src/Faker/Provider/Miscellaneous.php b/src/Faker/Provider/Miscellaneous.php index b15b0e79a0..ab4a5197b5 100644 --- a/src/Faker/Provider/Miscellaneous.php +++ b/src/Faker/Provider/Miscellaneous.php @@ -2,6 +2,8 @@ namespace Faker\Provider; +use Alcohol\ISO4217; + class Miscellaneous extends Base { /** @@ -201,27 +203,12 @@ class Miscellaneous extends Base ); /** - * @link https://en.wikipedia.org/wiki/ISO_4217 - * On date of 2017-07-07 + * @return array of valid ISO 4217 currency codes */ - protected static $currencyCode = array( - 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', - 'BAM', 'BBD', 'BDT', 'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB', 'BRL', - 'BSD', 'BTN', 'BWP', 'BYN', 'BZD', 'CAD', 'CDF', 'CHF', 'CLP', 'CNY', - 'COP', 'CRC', 'CUC', 'CUP', 'CVE', 'CZK', 'DJF', 'DKK', 'DOP', 'DZD', - 'EGP', 'ERN', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GHS', 'GIP', - 'GMD', 'GNF', 'GTQ', 'GYD', 'HKD', 'HNL', 'HRK', 'HTG', 'HUF', 'IDR', - 'ILS', 'INR', 'IQD', 'IRR', 'ISK', 'JMD', 'JOD', 'JPY', 'KES', 'KGS', - 'KHR', 'KMF', 'KPW', 'KRW', 'KWD', 'KYD', 'KZT', 'LAK', 'LBP', 'LKR', - 'LRD', 'LSL', 'LYD', 'MAD', 'MDL', 'MGA', 'MKD', 'MMK', 'MNT', 'MOP', - 'MRO', 'MUR', 'MVR', 'MWK', 'MXN', 'MYR', 'MZN', 'NAD', 'NGN', 'NIO', - 'NOK', 'NPR', 'NZD', 'OMR', 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', - 'PYG', 'QAR', 'RON', 'RSD', 'RUB', 'RWF', 'SAR', 'SBD', 'SCR', 'SDG', - 'SEK', 'SGD', 'SHP', 'SLL', 'SOS', 'SRD', 'SSP', 'STD', 'SVC', 'SYP', - 'SZL', 'THB', 'TJS', 'TMT', 'TND', 'TOP', 'TRY', 'TTD', 'TWD', 'TZS', - 'UAH', 'UGX', 'USD', 'UYU', 'UZS', 'VEF', 'VND', 'VUV', 'WST', 'XAF', - 'XCD', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMW', 'ZWL', - ); + protected static function currencyCodes() + { + return array_column((new ISO4217)->getAll(), 'alpha3'); + } /** * Return a boolean, true or false. @@ -304,7 +291,7 @@ public static function languageCode() */ public static function currencyCode() { - return static::randomElement(static::$currencyCode); + return static::randomElement(static::currencyCodes()); } /** diff --git a/test/Faker/Provider/MiscellaneousTest.php b/test/Faker/Provider/MiscellaneousTest.php index 6a29cd5531..983346c2d4 100644 --- a/test/Faker/Provider/MiscellaneousTest.php +++ b/test/Faker/Provider/MiscellaneousTest.php @@ -2,6 +2,7 @@ namespace Faker\Test\Provider; +use Alcohol\ISO4217; use Faker\Provider\Miscellaneous; use PHPUnit\Framework\TestCase; @@ -52,6 +53,16 @@ public function testCurrencyCode() $this->assertRegExp('/^[A-Z]{3}$/', Miscellaneous::currencyCode()); } + public function testValidCurrencyCode() + { + // fetch all valid currency codes + $validCodes = array_column((new ISO4217)->getAll(), 'alpha3'); + // repeat often to detect errors in randomly generated codes + for($i=0; $i < 100; ++$i) { + $this->assertContains(Miscellaneous::currencyCode(), $validCodes); + } + } + public function testEmoji() { $this->assertRegExp('/^[\x{1F600}-\x{1F637}]$/u', Miscellaneous::emoji());