|
| 1 | +<?php |
| 2 | + |
| 3 | +use GuzzleHttp\Client; |
| 4 | +use GuzzleHttp\Handler\MockHandler; |
| 5 | +use GuzzleHttp\Psr7\Response; |
| 6 | +use Otherguy\Currency\DriverFactory; |
| 7 | +use Otherguy\Currency\Drivers\ExchangeRatesApi; |
| 8 | +use Otherguy\Currency\Drivers\FixerIo; |
| 9 | +use Otherguy\Currency\Exceptions\ApiException; |
| 10 | +use Otherguy\Currency\Results\ConversionResult; |
| 11 | +use Otherguy\Currency\Symbol; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * RatesApiTest |
| 16 | + */ |
| 17 | +class ExchangeRatesApiTest extends TestCase |
| 18 | +{ |
| 19 | + /** @var ExchangeRatesApi */ |
| 20 | + private $exchangeRatesApi; |
| 21 | + |
| 22 | + private $mockHandler; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $this->mockHandler = new MockHandler(); |
| 27 | + $this->exchangeRatesApi = DriverFactory::make('exchangeratesapi', new Client(['handler' => $this->mockHandler])); |
| 28 | + } |
| 29 | + |
| 30 | + /** @test */ |
| 31 | + public function fails_to_set_api_key() |
| 32 | + { |
| 33 | + $this->expectException(ApiException::class); |
| 34 | + $this->expectExceptionMessage('No Access Key is required for this driver!'); |
| 35 | + $this->expectExceptionCode(400); |
| 36 | + $this->exchangeRatesApi->accessKey('test-access-key'); |
| 37 | + } |
| 38 | + |
| 39 | + /** @test */ |
| 40 | + public function can_get_latest_rates() |
| 41 | + { |
| 42 | + // Response from https://exchangeratesapi.io |
| 43 | + $this->mockHandler->append(new Response(200, [], '{"base":"EUR","rates":{"NOK":9.772,"USD":1.1289,"JPY":122.44},"date":"2019-06-13"}')); |
| 44 | + |
| 45 | + $result = $this->exchangeRatesApi->from(Symbol::EUR)->get([Symbol::NOK, Symbol::JPY, Symbol::USD]); |
| 46 | + |
| 47 | + $this->assertInstanceOf(ConversionResult::class, $result); |
| 48 | + |
| 49 | + $this->assertEquals(Symbol::EUR, $result->getBaseCurrency()); |
| 50 | + $this->assertEquals('2019-06-13', $result->getDate()); |
| 51 | + $this->assertEquals(9.772, $result->rate(Symbol::NOK)); |
| 52 | + $this->assertEquals(1.1289, $result->rate(Symbol::USD)); |
| 53 | + $this->assertEquals(122.44, $result->rate(Symbol::JPY)); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** @test */ |
| 58 | + public function can_get_historical_rates() |
| 59 | + { |
| 60 | + // Response from https://exchangeratesapi.io |
| 61 | + $this->mockHandler->append(new Response(200, [], '{"base":"GBP","rates":{"NOK":10.088752796,"CAD":1.7366601677,"USD":1.636783369,"JPY":170.6398095762,"EUR":1.1961293255},"date":"2013-12-24"}')); |
| 62 | + |
| 63 | + $result = $this->exchangeRatesApi->from(Symbol::GBP)->historical('2013-12-24', [Symbol::NOK, Symbol::CAD, Symbol::USD, Symbol::JPY, Symbol::EUR]); |
| 64 | + |
| 65 | + $this->assertInstanceOf(ConversionResult::class, $result); |
| 66 | + |
| 67 | + $this->assertEquals(Symbol::GBP, $result->getBaseCurrency()); |
| 68 | + $this->assertEquals('2013-12-24', $result->getDate()); |
| 69 | + |
| 70 | + $this->assertEquals(1.636783369, $result->rate(Symbol::USD)); |
| 71 | + $this->assertEquals(1.1961293255, $result->rate(Symbol::EUR)); |
| 72 | + $this->assertEquals(1.7366601677, $result->rate(Symbol::CAD)); |
| 73 | + $this->assertEquals(10.088752796, $result->rate(Symbol::NOK)); |
| 74 | + $this->assertEquals(170.6398095762, $result->rate(Symbol::JPY)); |
| 75 | + } |
| 76 | + |
| 77 | + /** @test */ |
| 78 | + public function fails_to_get_historical_rates_if_date_not_set() |
| 79 | + { |
| 80 | + $this->expectException(ApiException::class); |
| 81 | + $this->exchangeRatesApi->from(Symbol::USD)->to(Symbol::EUR)->historical(); |
| 82 | + } |
| 83 | + |
| 84 | + /** @test */ |
| 85 | + public function fails_to_convert_currency_amounts() |
| 86 | + { |
| 87 | + $this->expectException(ApiException::class); |
| 88 | + $this->expectExceptionMessage("Endpoint 'convert' is not supported for this driver!"); |
| 89 | + $this->expectExceptionCode(404); |
| 90 | + |
| 91 | + $result = $this->exchangeRatesApi->convert(25, Symbol::GBP, Symbol::JPY, '2018-02-22'); |
| 92 | + } |
| 93 | + |
| 94 | + /** @test */ |
| 95 | + public function can_handle_response_failures() |
| 96 | + { |
| 97 | + // Response from https://exchangeratesapi.io |
| 98 | + $this->mockHandler->append(new Response(200, [], '{"error":"Symbols \'USD,CAD,EUR,JPY,NOK,CDP\' are invalid for date 2019-06-14."}')); |
| 99 | + |
| 100 | + $this->expectException(ApiException::class); |
| 101 | + $this->expectExceptionCode(500); |
| 102 | + $this->exchangeRatesApi->from(Symbol::USD)->to(Symbol::LTL)->get(); |
| 103 | + } |
| 104 | +} |
0 commit comments