Skip to content

Commit ac2dab1

Browse files
committed
Test coverage for Fixer.io
1 parent 34f3d02 commit ac2dab1

File tree

8 files changed

+214
-101
lines changed

8 files changed

+214
-101
lines changed

src/Drivers/BaseCurrencyDriver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public function date($date): CurrencyDriverContract
117117
}
118118

119119
/**
120-
* Retrieves the date.
120+
* Returns the date in 'YYYY-mm-dd' format or null if not set.
121121
*
122-
* @return string
122+
* @return string|null
123123
*/
124-
public function getDate(): string
124+
public function getDate(): ?string
125125
{
126126
return $this->date;
127127
}
@@ -194,26 +194,27 @@ public function accessKey(string $accessKey): CurrencyDriverContract
194194
* Performs an HTTP request.
195195
*
196196
* @param string $endpoint The API endpoint.
197+
* @param array $params The query parameters for this request.
197198
* @param string $method The HTTP method (defaults to 'GET').
198199
*
199200
* @return array|bool The response as decoded JSON.
200201
*
201202
* @throws ApiException
202203
*/
203-
public function apiRequest(string $endpoint, string $method = 'GET')
204+
function apiRequest(string $endpoint, array $params = [], string $method = 'GET')
204205
{
205206
$url = sprintf('%s://%s/%s', $this->getProtocol(), $this->apiURL, $endpoint);
206207

207208
try {
208-
$response = $this->httpClient->request($method, $url, ['query' => $this->httpParams])->getBody();
209+
$response = $this->httpClient->request($method, $url, ['query' => array_merge($this->httpParams, $params)])->getBody();
209210
} catch (GuzzleException $e ) {
210211
throw new ApiException($e->getMessage(), $e->getCode(), $e);
211212
}
212213

213214
$data = json_decode($response->getContents(), true);
214215

215216
// Check for JSON errors
216-
if( json_last_error() !== JSON_ERROR_NONE ) {
217+
if(json_last_error() !== JSON_ERROR_NONE || ! is_array($data)) {
217218
throw new ApiException(json_last_error_msg(), json_last_error());
218219
}
219220

src/Drivers/CurrencyDriverContract.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ function amount($amount): CurrencyDriverContract;
4141
function date($date): CurrencyDriverContract;
4242

4343
/**
44-
* Retrieve the date in a 'YYYY-mm-dd' format.
44+
* Returns the date in 'YYYY-mm-dd' format or null if not set.
4545
*
46-
* @return string
46+
* @return string|null
4747
*/
48-
function getDate(): string;
48+
public function getDate(): ?string;
4949

5050
/**
5151
* @return array
@@ -62,13 +62,14 @@ function get($forCurrency = []): ConversionResult;
6262
/**
6363
* Converts any amount in a given currency to another currency.
6464
*
65-
* @param float $amount The amount to convert.
66-
* @param string $fromCurrency The base currency.
67-
* @param string $toCurrency The target currency.
65+
* @param float $amount The amount to convert.
66+
* @param string $fromCurrency The base currency.
67+
* @param string $toCurrency The target currency.
68+
* @param int|string|DateTime $date The date to get the conversion rate for.
6869
*
6970
* @return float The conversion result.
7071
*/
71-
function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null): float;
72+
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null, $date = null): float;
7273

7374
/**
7475
* @param int|string|DateTime $date
@@ -126,9 +127,10 @@ function getProtocol(): string;
126127
* Performs an HTTP request.
127128
*
128129
* @param string $endpoint The API endpoint.
130+
* @param array $params The query parameters for this request.
129131
* @param string $method The HTTP method (defaults to 'GET').
130132
*
131133
* @return array|bool The response as decoded JSON.
132134
*/
133-
function apiRequest(string $endpoint, string $method = 'GET');
135+
function apiRequest(string $endpoint, array $params = [], string $method = 'GET');
134136
}

src/Drivers/CurrencyLayer.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Otherguy\Currency\Exceptions\ApiException;
55
use Otherguy\Currency\Exceptions\CurrencyException;
66
use Otherguy\Currency\Results\ConversionResult;
7+
use Otherguy\Currency\Symbol;
78

89
/**
910
* Class CurrencyLayer
@@ -16,7 +17,7 @@ class CurrencyLayer extends BaseCurrencyDriver implements CurrencyDriverContract
1617
protected $apiURL = 'apilayer.net/api/';
1718

1819
/** @var string $baseCurrency CurrencyLayer's Free Plan base currency is 'USD' */
19-
protected $baseCurrency = 'USD';
20+
protected $baseCurrency = Symbol::USD;
2021

2122
protected $httpParams = [
2223
'format' => 1
@@ -54,16 +55,19 @@ public function get($forCurrency = []): ConversionResult
5455
/**
5556
* Converts any amount in a given currency to another currency.
5657
*
57-
* @param float $amount The amount to convert.
58-
* @param string $fromCurrency The base currency.
59-
* @param string $toCurrency The target currency.
58+
* @param float $amount The amount to convert.
59+
* @param string $fromCurrency The base currency.
60+
* @param string $toCurrency The target currency.
61+
* @param int|string|DateTime $date The date to get the conversion rate for.
6062
*
6163
* @return float The conversion result.
6264
*
63-
* @throws CurrencyException
65+
* @throws ApiException
6466
*/
65-
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null): float
67+
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null, $date = null): float
6668
{
69+
$this->date($date);
70+
6771
// Overwrite/set params
6872
if($amount !== null) {
6973
$this->amount = $amount;
@@ -122,25 +126,22 @@ public function historical($date = null, $forCurrency = []): ConversionResult
122126
return new ConversionResult($response['source'], $response['timestamp'], $rates);
123127
}
124128

129+
125130
/**
126131
* Performs an HTTP request.
127132
*
128133
* @param string $endpoint The API endpoint.
134+
* @param array $params The query parameters for this request.
129135
* @param string $method The HTTP method (defaults to 'GET').
130136
*
131137
* @return array|bool The response as decoded JSON.
132138
*
133-
* @throws CurrencyException
139+
* @throws ApiException
134140
*/
135-
public function apiRequest(string $endpoint, string $method = 'GET')
141+
function apiRequest(string $endpoint, array $params = [], string $method = 'GET')
136142
{
137143
// Perform actual API request.
138-
$response = parent::apiRequest($endpoint, $method);
139-
140-
// If the response is not an array, something went wrong.
141-
if(! is_array($response)) {
142-
throw new ApiException('Unexpected API response!');
143-
}
144+
$response = parent::apiRequest($endpoint, $params, $method);
144145

145146
// Handle response exceptions.
146147
if ($response['success'] == false) {

src/Drivers/FixerIo.php

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Otherguy\Currency\Exceptions\ApiException;
55
use Otherguy\Currency\Exceptions\CurrencyException;
66
use Otherguy\Currency\Results\ConversionResult;
7+
use Otherguy\Currency\Symbol;
78

89
/**
910
* Class FixerIo
@@ -16,7 +17,7 @@ class FixerIo extends BaseCurrencyDriver implements CurrencyDriverContract
1617
protected $apiURL = 'data.fixer.io/api';
1718

1819
/** @var string $baseCurrency Fixer.io's Free Plan base currency is 'EUR' */
19-
protected $baseCurrency = 'EUR';
20+
protected $baseCurrency = Symbol::EUR;
2021

2122
/**
2223
* @param string|array $forCurrency
@@ -34,94 +35,100 @@ public function get($forCurrency = []): ConversionResult
3435
// Get API response
3536
$response = $this->apiRequest('latest', [
3637
'base' => $this->getBaseCurrency(),
37-
'symbols' => join(',', $this->getSymbols())
38+
'symbols' => join(',', $this->getSymbols()),
3839
]);
3940

4041
return new ConversionResult($response['base'], $response['date'], $response['rates']);
4142
}
4243

4344
/**
44-
* Converts any amount in a given currency to another currency.
45-
*
46-
* @param float $amount The amount to convert.
47-
* @param string $fromCurrency The base currency.
48-
* @param string $toCurrency The target currency.
45+
* @param int|string|DateTime $date
46+
* @param string|array $forCurrency
4947
*
50-
* @return float The conversion result.
48+
* @return ConversionResult
5149
*
5250
* @throws CurrencyException
5351
*/
54-
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null): float
52+
public function historical($date = null, $forCurrency = []): ConversionResult
5553
{
56-
// Overwrite/set params
57-
if($amount !== null) {
58-
$this->amount = $amount;
59-
}
60-
61-
if($fromCurrency !== null) {
62-
$this->baseCurrency = $fromCurrency;
63-
}
54+
// Set date
55+
$this->date($date);
6456

65-
if($toCurrency !== null) {
66-
$this->currencies = [$toCurrency];
57+
if (!empty((array)$forCurrency)) {
58+
$this->currencies((array)$forCurrency);
6759
}
6860

6961
// Get API response
70-
$response = $this->apiRequest('convert', [
71-
'from' => $this->getBaseCurrency(),
72-
'to' => reset($this->currencies),
73-
'amount' => $this->amount
62+
$response = $this->apiRequest($this->date, [
63+
'base' => $this->getBaseCurrency(),
64+
'symbols' => join(',', $this->getSymbols()),
7465
]);
7566

76-
// Return the rate as a float
77-
return floatval($response['info']['rate']);
67+
return new ConversionResult($response['base'], $response['date'], $response['rates']);
7868
}
7969

8070
/**
81-
* @param int|string|DateTime $date
82-
* @param string|array $forCurrency
71+
* Converts any amount in a given currency to another currency.
8372
*
84-
* @return ConversionResult
73+
* @param float $amount The amount to convert.
74+
* @param string $fromCurrency The base currency.
75+
* @param string $toCurrency The target currency.
76+
* @param int|string|DateTime $date The date to get the conversion rate for.
8577
*
86-
* @throws CurrencyException
78+
* @return float The conversion result.
79+
*
80+
* @throws ApiException
8781
*/
88-
public function historical($date = null, $forCurrency = []): ConversionResult
82+
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null, $date = null): float
8983
{
9084
// Set date
9185
$this->date($date);
9286

93-
if (!empty((array)$forCurrency)) {
94-
$this->currencies((array)$forCurrency);
87+
// Overwrite/set params
88+
if ($amount !== null) {
89+
$this->amount = $amount;
90+
}
91+
92+
if ($fromCurrency !== null) {
93+
$this->baseCurrency = $fromCurrency;
94+
}
95+
96+
if ($toCurrency !== null) {
97+
$this->currencies = [$toCurrency];
98+
}
99+
100+
$params = [
101+
'from' => $this->getBaseCurrency(),
102+
'to' => reset($this->currencies),
103+
'amount' => $this->amount,
104+
];
105+
106+
if(null !== $this->getDate()) {
107+
$params['date'] = $this->getDate();
95108
}
96109

97110
// Get API response
98-
$response = $this->apiRequest($this->date, [
99-
'base' => $this->getBaseCurrency(),
100-
'symbols' => join(',', $this->getSymbols())
101-
]);
111+
$response = $this->apiRequest('convert', $params);
102112

103-
return new ConversionResult($response['base'], $response['date'], $response['rates']);
113+
// Return the rate as a float
114+
return floatval($response['result']);
104115
}
105116

106117
/**
107118
* Performs an HTTP request.
108119
*
109120
* @param string $endpoint The API endpoint.
121+
* @param array $params The query parameters for this request.
110122
* @param string $method The HTTP method (defaults to 'GET').
111123
*
112124
* @return array|bool The response as decoded JSON.
113125
*
114-
* @throws CurrencyException
126+
* @throws ApiException
115127
*/
116-
public function apiRequest(string $endpoint, string $method = 'GET')
128+
function apiRequest(string $endpoint, array $params = [], string $method = 'GET')
117129
{
118130
// Perform actual API request.
119-
$response = parent::apiRequest($endpoint, $method);
120-
121-
// If the response is not an array, something went wrong.
122-
if(! is_array($response)) {
123-
throw new ApiException('Unexpected API response!');
124-
}
131+
$response = parent::apiRequest($endpoint, $params, $method);
125132

126133
// Handle response exceptions.
127134
if ($response['success'] == false) {

src/Drivers/MockCurrencyDriver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ function get($forCurrency = []): ConversionResult
2525
/**
2626
* Converts any amount in a given currency to another currency.
2727
*
28-
* @param float $amount The amount to convert.
29-
* @param string $fromCurrency The base currency.
30-
* @param string $toCurrency The target currency.
28+
* @param float $amount The amount to convert.
29+
* @param string $fromCurrency The base currency.
30+
* @param string $toCurrency The target currency.
31+
* @param int|string|DateTime $date The date to get the conversion rate for.
3132
*
3233
* @return float The conversion result.
3334
*/
34-
function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null): float
35+
public function convert(float $amount = null, string $fromCurrency = null, string $toCurrency = null, $date = null): float
3536
{
3637
return 12.34;
3738
}

0 commit comments

Comments
 (0)