Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Get currency codes from library to stay up to date #1360

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 8 additions & 21 deletions src/Faker/Provider/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider;

use Alcohol\ISO4217;

class Miscellaneous extends Base
{
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -304,7 +291,7 @@ public static function languageCode()
*/
public static function currencyCode()
{
return static::randomElement(static::$currencyCode);
return static::randomElement(static::currencyCodes());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/Faker/Provider/MiscellaneousTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Faker\Test\Provider;

use Alcohol\ISO4217;
use Faker\Provider\Miscellaneous;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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());
Expand Down