Skip to content

Commit

Permalink
fix: abstract api historical dates
Browse files Browse the repository at this point in the history
  • Loading branch information
florianv committed Jun 30, 2021
1 parent 9481ff0 commit baa98b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Service/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate

if (isset($data['exchange_rates'][$currencyPair->getQuoteCurrency()])) {
$date = new \DateTime();
$date->setTimestamp($data['last_updated']);
$date->setTimezone(new \DateTimeZone('UTC'));

if (isset($data['last_updated'])) {
$date->setTimestamp($data['last_updated']);
}

if (isset($data['date'])) {
$date = \DateTime::createFromFormat('Y-m-d', $data['date'], new \DateTimeZone('UTC'));
}

$rate = $data['exchange_rates'][$currencyPair->getQuoteCurrency()];

return $this->createRate($currencyPair, (float) $rate, $date);
Expand Down
38 changes: 38 additions & 0 deletions tests/Fixtures/Service/AbstractApi/historical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"base": "USD",
"date": "2021-05-14",
"exchange_rates": {
"EUR": 0.824878,
"JPY": 109.28813,
"BGN": 1.613297,
"CZK": 21.027798,
"DKK": 6.13396,
"GBP": 0.71008,
"HUF": 293.260744,
"PLN": 3.729935,
"RON": 4.063268,
"SEK": 8.35313,
"CHF": 0.903407,
"ISK": 124.47414,
"NOK": 8.254887,
"HRK": 6.20226,
"RUB": 73.926916,
"TRY": 8.428112,
"AUD": 1.289615,
"BRL": 5.274437,
"CAD": 1.212571,
"CNY": 6.436031,
"HKD": 7.766312,
"IDR": 14296.749979,
"ILS": 3.279469,
"INR": 73.25497,
"KRW": 1128.491298,
"MXN": 19.816217,
"MYR": 4.125464,
"NZD": 1.383403,
"PHP": 47.733234,
"SGD": 1.333086,
"THB": 31.355275,
"ZAR": 14.082488
}
}
2 changes: 1 addition & 1 deletion tests/Tests/Service/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function it_fetches_a_historical_rate()
{
$pair = CurrencyPair::createFromString('USD/GBP');
$url = 'https://exchange-rates.abstractapi.com/v1/historical?api_key=secret&base=USD&date=2000-01-03';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/AbstractApi/success.json');
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/AbstractApi/historical.json');
$date = new \DateTime('2000-01-03');

$service = new AbstractApi($this->getHttpAdapterMock($url, $content), null, ['api_key' => 'secret']);
Expand Down

0 comments on commit baa98b9

Please sign in to comment.