Skip to content

Commit

Permalink
Move parse results to associate array
Browse files Browse the repository at this point in the history
  • Loading branch information
dostrog committed Feb 20, 2021
1 parent f754254 commit 58b26ac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/Services/NationalBankOfUkraine.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function parseRateData(string $content, string $quoteCurrency): array
throw new RuntimeException(trans('larate::error.badfloat', ['value' => $value]));
}

return [$value, $date];
return [
'value' => $value,
'date' => $date,
];
}

/**
Expand All @@ -70,7 +73,7 @@ public function getExchangeRate(CurrencyPairContract $currencyPair, DateTimeInte
])
: $this->makeRequest(['valcode' => $quoteCurrency]);

[$value, $responseDate] = $this->parseRateData($content, $quoteCurrency);
['value' => $value, 'date' => $responseDate] = $this->parseRateData($content, $quoteCurrency);

return new ExchangeRate($currencyPair, $value, $responseDate, $this->getName());
}
Expand Down
8 changes: 6 additions & 2 deletions src/Services/RussianCentralBank.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function parseRateData(string $content, string $quoteCurrency): array
$nominalStr = (string) $quoteCurrencyData['0']->Nominal;
$nominal = $fmt->parse($nominalStr);

return [$value, $nominal, $date];
return [
'value' => $value,
'nominal' => $nominal,
'date' => $date,
];
}

/**
Expand All @@ -75,7 +79,7 @@ public function getExchangeRate(CurrencyPairContract $currencyPair, DateTimeInte
? $this->makeRequest(['date_req' => $date->format('d/m/Y')])
: $this->makeRequest();

[$value, $nominal, $responseDate] = $this->parseRateData($content, $quoteCurrency);
['value' => $value, 'nominal' => $nominal, 'date' => $responseDate] = $this->parseRateData($content, $quoteCurrency);

return new ExchangeRate($currencyPair, $value / $nominal, $responseDate, $this->getName());
}
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/NationalBankOfUkraineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dostrog\Larate\CurrencyPair;
use Dostrog\Larate\Services\NationalBankOfUkraine;
use Dostrog\Larate\Tests\TestCase;
use Exception;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -53,7 +54,10 @@ public function nbu_parse_rate_data(): void
</exchange>
CONTENT;

self::assertEquals([23.9821, Carbon::parse('16.01.2020')], $this->service->parseRateData($content, 'USD'));
self::assertEquals([
'value' => 23.9821,
'date' => Carbon::parse('16.01.2020'),
], $this->service->parseRateData($content, 'USD'));
}

/** @test */
Expand Down Expand Up @@ -207,7 +211,7 @@ public function rcb_get_exchange_rate_response_throw_exception(): void
$httpClient = $this->mock(Factory::class);
$httpClient->shouldReceive('get')
->withSomeOfArgs(['url' => NationalBankOfUkraine::URL])
->andThrow(\Exception::class);
->andThrow(Exception::class);

$rcb = new NationalBankOfUkraine($httpClient);
$pair = new CurrencyPair(self::BASE_CURRENCY, self::QUOTE_CURRENCY);
Expand Down
11 changes: 7 additions & 4 deletions tests/Unit/RussianCentralBankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dostrog\Larate\CurrencyPair;
use Dostrog\Larate\Services\RussianCentralBank;
use Dostrog\Larate\Tests\TestCase;
use Exception;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -53,7 +54,9 @@ public function rcb_parse_rate_data(): void
</ValCurs>
CONTENT;

self::assertEquals([418.1800, 1.0, Carbon::parse('16.01.1996')], $this->service->parseRateData($content, 'EEK'));
self::assertEquals([
'value' => 418.1800, 'nominal' => 1.0, 'date' => Carbon::parse('16.01.1996')
], $this->service->parseRateData($content, 'EEK'));
}

/** @test */
Expand Down Expand Up @@ -152,7 +155,7 @@ public function rcb_get_exchange_rate_for_holiday(): void
$rate1 = $this->service->getExchangeRate($pair, Carbon::parse($date1))->getValue();

self::assertEquals($expected, $rate0);
self::assertEquals($rate0, $rate1);
self::assertSame($rate0, $rate1);
}

/** @test */
Expand All @@ -179,7 +182,7 @@ public function rcb_get_exchange_rate_response_throw_exception(): void
$httpClient = $this->mock(Factory::class);
$httpClient->shouldReceive('get')
->withSomeOfArgs(['url' => RussianCentralBank::URL])
->andThrow(\Exception::class);
->andThrow(Exception::class);


$rcb = new RussianCentralBank($httpClient);
Expand Down Expand Up @@ -215,6 +218,6 @@ public function rcb_get_exchange_rate_for_past(): void
/** @test */
public function rcb_get_name(): void
{
self::assertEquals(self::PROVIDER_NAME, $this->service->getName());
self::assertSame(self::PROVIDER_NAME, $this->service->getName());
}
}

0 comments on commit 58b26ac

Please sign in to comment.