From f3dd5a2169499aa733236a411058aba6d6260a6a Mon Sep 17 00:00:00 2001 From: David Marland Date: Wed, 27 Jul 2016 17:01:32 +0100 Subject: [PATCH] Handle asking for the Base currency and fix error in Readme --- README.md | 2 +- src/Result.php | 8 ++++++++ tests/ExchangeTest.php | 4 ++-- tests/ResultTest.php | 5 +++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 552df7b..1f8d37b 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ print $rates->GBP; The last option is to return the response as a `Result` class. This allows access to the full set of properties returned from the feed. ```php -$result = (new Exchange())->symbols(Currency::USD, Currency::GBP)->getAsObject(); +$result = (new Exchange())->symbols(Currency::USD, Currency::GBP)->getResult(); $date = $result->getDate(); // The date the data is from $rates = $result->getRates(); // Array of rates as above diff --git a/src/Result.php b/src/Result.php index 68507a8..cb912c9 100644 --- a/src/Result.php +++ b/src/Result.php @@ -71,6 +71,14 @@ public function getRates() */ public function getRate($code) { + // the result won't have the base code in it, + // because that would always be 1. But to make + // dynamic code easier this prevents null if + // the base code is asked for + if ($code == $this->getBase()) { + return 1.0; + } + if (isset($this->rates[$code])) { return $this->rates[$code]; } diff --git a/tests/ExchangeTest.php b/tests/ExchangeTest.php index a4f754f..ab8f7da 100644 --- a/tests/ExchangeTest.php +++ b/tests/ExchangeTest.php @@ -116,7 +116,7 @@ public function testResponseAsResult() { $response = m::mock('StdClass'); $response->shouldReceive('getBody')->once()->andReturn(json_encode([ - 'base' => 'USD', + 'base' => 'EUR', 'date' => '2016-01-02', 'rates' => [ 'GBP' => 1.01, @@ -132,7 +132,7 @@ public function testResponseAsResult() $result = $exchange->getResult(); $this->assertInstanceOf('\Fadion\Fixerio\Result', $result); - $this->assertEquals(1.02, $result->getRate(Currency::USD)); + $this->assertEquals(1.01, $result->getRate(Currency::GBP)); } /** diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 0f01114..f1c9f75 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -22,6 +22,11 @@ public function testGetters() $this->assertEquals(1.23, $result->getRate(Currency::USD)); $this->assertEquals(1.01, $result->getRate(Currency::GBP)); + + // Check that asking for the Base will return 1 + $this->assertEquals(1, $result->getRate(Currency::EUR)); + + // Null if currency not in result $this->assertNull($result->getRate(Currency::HKD)); $this->assertNull($result->getRate('invalid')); }