|
| 1 | +<?php |
| 2 | + |
| 3 | +use Carbon\Carbon; |
| 4 | +use GuzzleHttp\Promise\Create; |
| 5 | +use GuzzleHttp\Psr7\Response; |
| 6 | +use Illuminate\Http\Client\Factory; |
| 7 | +use Illuminate\Http\Client\Request; |
| 8 | +use Illuminate\Http\Client\RequestException; |
| 9 | +use Worksome\Exchange\ExchangeRateProviders\CurrencyGEOProvider; |
| 10 | +use Worksome\Exchange\Support\Rates; |
| 11 | + |
| 12 | +it('is able to make a real call to the API', function () { |
| 13 | + $client = new Factory(); |
| 14 | + $currencyGeoProvider = new CurrencyGEOProvider($client, getenv('CURRENCY_GEO_ACCESS_KEY')); |
| 15 | + $rates = $currencyGeoProvider->getRates('EUR', currencies()); |
| 16 | + |
| 17 | + expect($rates)->toBeInstanceOf(Rates::class); |
| 18 | +}) |
| 19 | + ->skip(getenv('CURRENCY_GEO_ACCESS_KEY') === false, 'No CURRENCY_GEO_ACCESS_KEY was defined.') |
| 20 | + ->group('integration'); |
| 21 | + |
| 22 | +it('makes a HTTP request to the correct endpoint', function () { |
| 23 | + $client = new Factory(); |
| 24 | + $client->fake(['*' => [ |
| 25 | + 'timestamp' => now()->subDay()->timestamp, |
| 26 | + 'rates' => [ |
| 27 | + 'EUR' => [ |
| 28 | + 'currency_name' => 'Euro', |
| 29 | + 'rate' => 1, |
| 30 | + 'rate_for_amount' => 1, |
| 31 | + ], |
| 32 | + 'GBP' => [ |
| 33 | + 'currency_name' => 'Pound sterling', |
| 34 | + 'rate' => 2.5, |
| 35 | + 'rate_for_amount' => 2.5, |
| 36 | + ], |
| 37 | + ], |
| 38 | + ]]); |
| 39 | + |
| 40 | + $currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); |
| 41 | + $currencyGeoProvider->getRates('EUR', currencies()); |
| 42 | + |
| 43 | + $client->assertSent(function (Request $request) { |
| 44 | + return str_starts_with($request->url(), 'https://api.getgeoapi.com/v2/currency/convert'); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +it('returns floats for all rates', function () { |
| 49 | + $client = new Factory(); |
| 50 | + $client->fake(['*' => [ |
| 51 | + 'timestamp' => now()->subDay()->timestamp, |
| 52 | + 'rates' => [ |
| 53 | + 'EUR' => [ |
| 54 | + 'currency_name' => 'Euro', |
| 55 | + 'rate' => 1, |
| 56 | + 'rate_for_amount' => 1, |
| 57 | + ], |
| 58 | + 'GBP' => [ |
| 59 | + 'currency_name' => 'Pound sterling', |
| 60 | + 'rate' => 2.5, |
| 61 | + 'rate_for_amount' => 2.5, |
| 62 | + ], |
| 63 | + ], |
| 64 | + ]]); |
| 65 | + |
| 66 | + $currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); |
| 67 | + $rates = $currencyGeoProvider->getRates('EUR', currencies()); |
| 68 | + |
| 69 | + expect($rates->getRates())->each->toBeFloat(); |
| 70 | +}); |
| 71 | + |
| 72 | +it('sets the returned timestamp as the retrievedAt timestamp', function () { |
| 73 | + Carbon::setTestNow(now()); |
| 74 | + |
| 75 | + $client = new Factory(); |
| 76 | + $client->fake(['*' => [ |
| 77 | + 'timestamp' => now()->subDay()->timestamp, |
| 78 | + 'rates' => [], |
| 79 | + ]]); |
| 80 | + |
| 81 | + $currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); |
| 82 | + $rates = $currencyGeoProvider->getRates('EUR', currencies()); |
| 83 | + |
| 84 | + expect($rates->getRetrievedAt()->timestamp)->toBe(now()->startOfDay()->timestamp); |
| 85 | +}); |
| 86 | + |
| 87 | +it('throws a RequestException if a 500 error occurs', function () { |
| 88 | + $client = new Factory(); |
| 89 | + $client->fake(['*' => Create::promiseFor(new Response(500))]); |
| 90 | + |
| 91 | + $currencyGeoProvider = new CurrencyGEOProvider($client, 'password'); |
| 92 | + $currencyGeoProvider->getRates('EUR', currencies()); |
| 93 | +})->throws(RequestException::class); |
0 commit comments