Skip to content

Commit

Permalink
Added Laravel .env support. PHPUnit testing now runs on all API sourc…
Browse files Browse the repository at this point in the history
…es and uses local test data.
  • Loading branch information
danielme85 committed Jan 30, 2018
1 parent 6d77046 commit e2d9f51
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ Use the three lettered ISO4217 code for to/from currencies: http://en.wikipedia.
|currencylayer | https://currencylayer.com/ | non-free | yes | non-free | yes |
|fixer | http://fixer.io/ | yes | yes | no | no |

### Version 0.1.1
* Added Laravel .env support for config settings.
* Added unit testing for all sources and test data is local file now.

### Version 0.1.0
* Changed composer requirements to match Laravel 5.5+, phpunit 6.0+
* Changed default currency data from Yahoo to source to fixer.io (Yahoo http source unreliable).
Expand Down
26 changes: 13 additions & 13 deletions config/CConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@

//API source.
//Possible values: 'openexchange' | 'yahoo' | 'currencylayer' | 'fixer'
'api-source' => 'fixer',
'api-source' => env('CC_API_SOURCE', 'currencylayer'),

//Your app id from openexchangerates.org
'openex-app-id' => '',
'openex-app-id' => env('CC_OPENEXCHANGE_APP_ID', ''),

//your API access key for currencylayer.com
'currencylayer-access-key' => '',
'currencylayer-access-key' => env('CC_CURRENCYLAYER_ACCESS_KEY', ''),

//use https? the free version of openexchange and jsonrates does not support https :(
'use-ssl' => env('CC_USE_SSL', false),

//use https? the free version of openexchange, jsonrates and currencylayer does not support https :(
'use-ssl' => false,

//When using the free account we can still calculate other currencies based on USD as a base thanks to some basic math.
//enable this if you want real base values insted of calculated ones. Requires enterprice account from openexchangerates.org
'openex-use-real-base' => false,
'openex-use-real-base' => env('CC_USE_REAL_BASE', false),

//use Laravel cache engine to cache the results.
'enable-cache' => true,
'enable-cache' => env('CC_ENABLE_CACHE', true),

//minutes cache should expire.
'cache-min' => 60,
'cache-min' => env('CC_ENABLE_CACHE',60),

//use Laravel logging
'enable-log' => false,
'enable-log' => env('CC_ENABLE_LOG',false),

//enabled currencies (only in use for yahoo)
//add/remove as needed.
Expand Down
40 changes: 35 additions & 5 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@

class Currency {

private $settings, $requestUrl, $base, $rates, $fromCache, $date, $fromDate, $toDate, $from, $to;
private $settings, $requestUrl, $base, $rates, $fromCache, $date, $fromDate, $toDate, $from, $to, $runastest;

/*
*
* @param string $api to use (will override config if set)
* @param boolean $https (true/false will override config if set)
* @param boolean $useCache (true/false will override config if set)
* @param integer $cacheMin (number of minutes for cache to expire will override config if set)
* @param boolean $runastest Run as test and use json test data in /tests instead of actually http-rest results from API's
*
*/
public function __construct($api = null, $https = null, $useCache = null, $cacheMin = null) {
public function __construct($api = null, $https = null, $useCache = null, $cacheMin = null, $runastest = false) {
if (!$this->settings = Config::get('CConverter')) {
Log::error('The CConverter config file is needed. Did you run: php artisan vendor:publish ?');
}
Expand All @@ -57,6 +58,7 @@ public function __construct($api = null, $https = null, $useCache = null, $cache
if (isset($cacheMin)) {
$this->settings['cache-min'] = $cacheMin;
}
$this->runastest = $runastest;
}

/**
Expand All @@ -65,6 +67,11 @@ public function __construct($api = null, $https = null, $useCache = null, $cache
* @return array
*/
protected function openExchange() {
//use test data if running as test
if ($this->runastest) {
return $this->convertFromOpenExchange(json_decode(file_get_contents(dirname(__FILE__). '/../tests/openExchangeTestData.json'), true));
}

$base = $this->base;
$date = $this->date;

Expand Down Expand Up @@ -96,6 +103,11 @@ protected function openExchange() {
* @return array
*/
protected function jsonRates() {
//use test data if running as test
if ($this->runastest) {
return $this->convertFromJsonRates(json_decode(file_get_contents(dirname(__FILE__). '/../tests/currencyLayerTestData.json'), true));
}

if ($this->settings['use-ssl']) {
$url = 'https';
}
Expand Down Expand Up @@ -147,9 +159,12 @@ protected function jsonRatesTimeSeries($from, $to, $dateStart, $dateEnd) {
* @return array
*/
protected function yahoo() {
//use test data if running as test
if ($this->runastest) {
return $this->convertFromYahoo(json_decode(file_get_contents(dirname(__FILE__). '/../tests/yahooTestData.json'), true));
}

$base = $this->base;

if ($this->settings['use-ssl']) {
$url = 'https';
}
Expand Down Expand Up @@ -216,6 +231,10 @@ protected function yahooTimeSeries($from, $to, $dateStart, $dateEnd) {
* @return array
*/
protected function fixer() {
//use test data if running as test
if ($this->runastest) {
return $this->convertFromFixer(json_decode(file_get_contents(dirname(__FILE__). '/../tests/fixerTestData.json'), true));
}
if ($this->settings['use-ssl']) {
$url = 'https';
}
Expand Down Expand Up @@ -313,7 +332,7 @@ public function getRates($base = null, $date = null) {
}

/**
* Get a RateSeries (not supported by OpenExchange)
* Get a RateSeries (not supported by OpenExchange or fixer.io)
*
* @param string $from
* @param string $to
Expand All @@ -337,6 +356,11 @@ public function getRateSeries($from, $to, $dateStart, $dateEnd) {
$result = $this->yahooTimeSeries($from, $to, $dateStart, $dateEnd);
}
else if ($api === 'openexchange') {
Log::error('Openexchange does not support currency rate time-series.');
return null;
}
else if ($api === 'fixer') {
Log::error('Fixer.io does not support currency rate time-series.');
return null;
}
else if ($api === 'currencylayer') {
Expand All @@ -356,6 +380,11 @@ public function getRateSeries($from, $to, $dateStart, $dateEnd) {
$result = $this->yahooTimeSeries($from, $to, $dateStart, $dateEnd);
}
else if ($api === 'openexchange') {
Log::error('Openexchange does not support currency rate time-series.');
return null;
}
else if ($api === 'fixer') {
Log::error('Fixer.io does not support currency rate time-series.');
return null;
}
else if ($api === 'currencylayer') {
Expand All @@ -382,7 +411,6 @@ public function getRateSeries($from, $to, $dateStart, $dateEnd) {
public function convert($from, $to, $value, $round = null, $date = null) {
$result = array();


if ($value === 0 or $value === null or $value === '' or empty($value)) {
return 0;
}
Expand Down Expand Up @@ -607,6 +635,8 @@ protected function convertFromJsonRates($data) {
* @return array
*/
protected function convertFromJsonRatesSeries($data) {
echo json_encode($data);
exit();
$base = $data['source'];
$output = array();
$output['base'] = $base;
Expand Down
133 changes: 122 additions & 11 deletions tests/CurrencyConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,146 @@ protected function getPackageProviders($app)
}

/**
* A basic test example. <- just a test for the test to test if the test is testing.
* Test to see if the Currency object can be created with fixer.
* @group fixer
*
* @return void
*/
public function testExample()
public function testCreateInstanceFixer()
{
$this->assertTrue(true);
$curreny = new \danielme85\CConverter\Currency('fixer', null, null, null, true);
$this->assertNotEmpty($curreny);
}

/**
* Full integration test default config, conversion.
* Test Currency conversion default config with fixer.io as source.
* @group fixer
*
* @return void
*/
public function testConvertFixer() {
$curreny = new \danielme85\CConverter\Currency('fixer', null, null, null, true);
$this->assertEquals(1, $curreny->convert('USD', 'USD', 1));
}


/**
* Test getting Currency rates with fixer.io as source.
* @group fixer
*
* @return void
*/
public function testGetRatesFixer() {
$curreny = new \danielme85\CConverter\Currency('fixer', null, null, null, true);
$rates = $curreny->getRates('USD');
$this->assertArrayHasKey('rates', $rates);
$this->assertGreaterThan(0, $this->count($rates['rates']));
}

/**
* Test to see if the Currency object can be created with CurrencyLayer.
* @group currencylayer
*
* @return void
*/
public function testCreateInstanceCurrencyLayer()
{
$curreny = new \danielme85\CConverter\Currency('currencylayer', null, null, null, true);
$this->assertNotEmpty($curreny);
}

/**
* Test Currency conversion default config with CurrencyLayer as source.
* @group currencylayer
*
* @return void
*/
public function testConvertCurrencyLayer() {
$curreny = new \danielme85\CConverter\Currency('currencylayer', null, null, null, true);
$this->assertEquals(1, $curreny->convert('USD', 'USD', 1));
}

/**
* Test getting Currency rates with CurrencyLayer as source.
* @group currencylayer
*
* @return void
*/
public function testGetRatesCurrencyLayer() {
$curreny = new \danielme85\CConverter\Currency('currencylayer', null, null, null, true);
$rates = $curreny->getRates('USD');
$this->assertArrayHasKey('rates', $rates);
$this->assertGreaterThan(0, $this->count($rates['rates']));
}

public function testConversionDefault() {
$currency = new \danielme85\CConverter\Currency('fixer');
$this->assertEquals(1, $currency->convert('USD', 'USD', 1));
/**
* Test to see if the Currency object can be created with Yahoo Finance.
* @group yahoo
*
* @return void
*/
public function testCreateInstanceYahoo()
{
$curreny = new \danielme85\CConverter\Currency('yahoo', null, null, null, true);
$this->assertNotEmpty($curreny);
}

/**
* Test Currency conversion default config with Yahoo Finance as source.
* @group yahoo
*
* @return void
*/
public function testConvertYahoo() {
$curreny = new \danielme85\CConverter\Currency('yahoo', null, null, null, true);
$this->assertEquals(1, $curreny->convert('USD', 'USD', 1));
}

/**
* Test getting Currency rates with Yahoo Finance as source.
* @group yahoo
*
* @return void
*/
public function testGetRatesYahoo() {
$curreny = new \danielme85\CConverter\Currency('yahoo', null, null, null, true);
$rates = $curreny->getRates('USD');
$this->assertArrayHasKey('rates', $rates);
$this->assertGreaterThan(0, $this->count($rates['rates']));
}

/**
* Test to see if the Currency object can be created with OpenExchange.
* @group openexchange
*
* @return void
*/
public function testCreateInstanceOpenExchange()
{
$curreny = new \danielme85\CConverter\Currency('openexchange', null, null, null, true);
$this->assertNotEmpty($curreny);
}

/**
* Test Currency conversion default config with OpenExchange as source.
* @group openexchange
*
* @return void
*/
public function testConvertOpenExchange() {
$curreny = new \danielme85\CConverter\Currency('openexchange', null, null, null, true);
$this->assertEquals(1, $curreny->convert('USD', 'USD', 1));
}

/**
* Full integration test default config, currency rates.
* Test getting Currency rates with OpenExchange as source.
* @group openexchange
*
* @return void
*/
public function testRatesDefault() {
$currency = new \danielme85\CConverter\Currency('fixer');
$rates = $currency->getRates('USD');
public function testGetRatesOpenExchange() {
$curreny = new \danielme85\CConverter\Currency('openexchange', null, null, null, true);
$rates = $curreny->getRates('USD');
$this->assertArrayHasKey('rates', $rates);
$this->assertGreaterThan(0, $this->count($rates['rates']));
}
Expand Down
1 change: 1 addition & 0 deletions tests/currencyLayerTestData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"success":true,"terms":"https:\/\/currencylayer.com\/terms","privacy":"https:\/\/currencylayer.com\/privacy","timestamp":1517330889,"source":"USD","quotes":{"USDAED":3.671698,"USDAFN":69.260002,"USDALL":107.249963,"USDAMD":480.130005,"USDANG":1.779745,"USDAOA":204.800995,"USDARS":19.594999,"USDAUD":1.237803,"USDAWG":1.78,"USDAZN":1.699603,"USDBAM":1.581196,"USDBBD":2,"USDBDT":82.870003,"USDBGN":1.574604,"USDBHD":0.376699,"USDBIF":1750.97998,"USDBMD":1,"USDBND":1.325701,"USDBOB":6.859606,"USDBRL":3.186297,"USDBSD":1,"USDBTC":9.6e-5,"USDBTN":63.549999,"USDBWP":9.619598,"USDBYN":2.019915,"USDBYR":19600,"USDBZD":1.997804,"USDCAD":1.23423,"USDCDF":1565.494877,"USDCHF":0.93542,"USDCLF":0.02241,"USDCLP":607.799988,"USDCNY":6.319799,"USDCOP":2845.5,"USDCRC":564.99975,"USDCUC":1,"USDCUP":26.5,"USDCVE":88.999927,"USDCZK":20.421699,"USDDJF":176.830002,"USDDKK":6.00614,"USDDOP":48.700001,"USDDZD":113.060997,"USDEGP":17.629999,"USDERN":14.9897,"USDETB":27.200001,"USDEUR":0.8069,"USDFJD":1.986007,"USDFKP":0.707898,"USDGBP":0.70854,"USDGEL":2.485001,"USDGGP":0.708565,"USDGHS":4.488504,"USDGIP":0.708202,"USDGMD":48.209999,"USDGNF":9003.999833,"USDGTQ":7.335982,"USDGYD":204.669998,"USDHKD":7.819299,"USDHNL":23.499357,"USDHRK":5.982702,"USDHTG":63.069852,"USDHUF":250.869995,"USDIDR":13428,"USDILS":3.415496,"USDIMP":0.708565,"USDINR":63.7854,"USDIQD":1184,"USDIRR":36904.999881,"USDISK":100.501776,"USDJEP":0.708565,"USDJMD":123.690002,"USDJOD":0.707495,"USDJPY":108.872002,"USDKES":102.150002,"USDKGS":68.364998,"USDKHR":4000.000349,"USDKMF":395.899994,"USDKPW":900.000221,"USDKRW":1072.02002,"USDKWD":0.299299,"USDKYD":0.819642,"USDKZT":321.609985,"USDLAK":8278.999854,"USDLBP":1510.999849,"USDLKR":153.899994,"USDLRD":128.929993,"USDLSL":11.970252,"USDLTL":3.048697,"USDLVL":0.62055,"USDLYD":1.328301,"USDMAD":9.162006,"USDMDL":16.808979,"USDMGA":3205.000057,"USDMKD":49.419998,"USDMMK":1327.999703,"USDMNT":2411.999857,"USDMOP":8.0475,"USDMRO":350.999966,"USDMUR":32.150002,"USDMVR":15.570552,"USDMWK":713.450012,"USDMXN":18.695299,"USDMYR":3.899499,"USDMZN":59.999869,"USDNAD":11.950414,"USDNGN":357.999824,"USDNIO":30.950001,"USDNOK":7.71882,"USDNPR":101.804001,"USDNZD":1.363298,"USDOMR":0.384499,"USDPAB":1,"USDPEN":3.216502,"USDPGK":3.14402,"USDPHP":51.490002,"USDPKR":110.470001,"USDPLN":3.355599,"USDPYG":5614.799805,"USDQAR":3.6398,"USDRON":3.751703,"USDRSD":95.710999,"USDRUB":56.371201,"USDRWF":835.75,"USDSAR":3.750172,"USDSBD":7.749402,"USDSCR":13.401592,"USDSDG":18.612201,"USDSEK":7.88806,"USDSGD":1.31164,"USDSHP":0.708205,"USDSLL":7680.000027,"USDSOS":563.000098,"USDSRD":7.420055,"USDSTD":19775.599609,"USDSVC":8.750086,"USDSYP":514.97998,"USDSZL":11.962052,"USDTHB":31.429733,"USDTJS":8.825499,"USDTMT":3.4,"USDTND":2.387297,"USDTOP":2.231102,"USDTRY":3.783502,"USDTTD":6.6295,"USDTWD":29.268999,"USDTZS":2246.999662,"USDUAH":27.919797,"USDUGX":3614.000248,"USDUSD":1,"USDUYU":28.330091,"USDUZS":8164.999658,"USDVEF":9.97503,"USDVND":22700,"USDVUV":102.519997,"USDWST":2.505702,"USDXAF":529.099976,"USDXAG":0.058238,"USDXAU":0.000746,"USDXCD":2.703065,"USDXDR":0.687212,"USDXOF":526.000088,"USDXPF":96.569725,"USDYER":249.899994,"USDZAR":11.954101,"USDZMK":9001.197519,"USDZMW":9.680099,"USDZWL":322.355011}}
1 change: 1 addition & 0 deletions tests/openExchangeTestData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"disclaimer":"Usage subject to terms: https:\/\/openexchangerates.org\/terms","license":"https:\/\/openexchangerates.org\/license","timestamp":1517335200,"base":"USD","rates":{"AED":3.673014,"AFN":69.49662,"ALL":107.68,"AMD":480.695,"ANG":1.786635,"AOA":203.14,"ARS":19.632,"AUD":1.237647,"AWG":1.791246,"AZN":1.7,"BAM":1.5758,"BBD":2,"BDT":83.366931,"BGN":1.576844,"BHD":0.37694,"BIF":1770,"BMD":1,"BND":1.310883,"BOB":6.910011,"BRL":3.181285,"BSD":1,"BTC":9.7754291e-5,"BTN":63.664042,"BWP":9.573877,"BYN":1.9786,"BZD":2.011848,"CAD":1.232974,"CDF":1601,"CHF":0.934988,"CLF":0.0227,"CLP":607.8,"CNH":6.3323,"CNY":6.32365,"COP":2845.155,"CRC":568.78,"CUC":1,"CUP":25.5,"CVE":88.95,"CZK":20.4,"DJF":178.52,"DKK":5.99965,"DOP":48.52,"DZD":113.480231,"EGP":17.641,"ERN":14.995,"ETB":27.55,"EUR":0.806097,"FJD":2.006008,"FKP":0.707069,"GBP":0.707069,"GEL":2.494491,"GGP":0.707069,"GHS":4.495,"GIP":0.707069,"GMD":48.51,"GNF":9035.35,"GTQ":7.361629,"GYD":207.307607,"HKD":7.81996,"HNL":23.669803,"HRK":5.980258,"HTG":64.007985,"HUF":250.51275,"IDR":13408.25919,"ILS":3.418475,"IMP":0.707069,"INR":63.775,"IQD":1183.5,"IRR":36882.905082,"ISK":100.82,"JEP":0.707069,"JMD":124.50435,"JOD":0.709001,"JPY":108.829,"KES":102.39,"KGS":68.377499,"KHR":4015,"KMF":396.195578,"KPW":900,"KRW":1074.69,"KWD":0.299686,"KYD":0.834095,"KZT":321.99,"LAK":8295,"LBP":1513.9,"LKR":154.135901,"LRD":129.199915,"LSL":11.975,"LYD":1.33,"MAD":9.1638,"MDL":16.874883,"MGA":3220,"MKD":49.644091,"MMK":1331.7,"MNT":2416.156849,"MOP":8.059894,"MRO":355.5,"MRU":35.35,"MUR":32.3,"MVR":15.409873,"MWK":725.64,"MXN":18.7069,"MYR":3.900994,"MZN":60.003288,"NAD":11.9569,"NGN":360.5,"NIO":31.1,"NOK":7.714501,"NPR":101.88,"NZD":1.363643,"OMR":0.384969,"PAB":1,"PEN":3.216002,"PGK":3.1955,"PHP":51.428,"PKR":110.76,"PLN":3.35031,"PYG":5628.25,"QAR":3.641,"RON":3.748782,"RSD":95.7545,"RUB":56.3356,"RWF":845,"SAR":3.75065,"SBD":7.752483,"SCR":13.761762,"SDG":7.023224,"SEK":7.8839,"SGD":1.311794,"SHP":0.707069,"SLL":7693.52619,"SOS":582.75,"SRD":7.468,"SSP":130.2634,"STD":19785.561496,"STN":19.895,"SVC":8.757756,"SYP":515.00999,"SZL":11.975,"THB":31.421,"TJS":8.835135,"TMT":3.499986,"TND":2.402004,"TOP":2.213605,"TRY":3.781584,"TTD":6.736134,"TWD":29.255,"TZS":2249.95,"UAH":28.5586,"UGX":3628.35,"USD":1,"UYU":28.446067,"UZS":8182.5,"VEF":10.03215,"VND":22707.741548,"VUV":104.003926,"WST":2.490067,"XAF":528.765282,"XAG":0.05833293,"XAU":0.00074698,"XCD":2.70255,"XDR":0.687212,"XOF":528.765282,"XPD":0.00094565,"XPF":96.193016,"XPT":0.00100201,"YER":250.35,"ZAR":11.983473,"ZMW":9.759,"ZWL":322.355011}}

0 comments on commit e2d9f51

Please sign in to comment.