diff --git a/.gitignore b/.gitignore index 2c1fc0c..9c10d38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor composer.phar composer.lock -.DS_Store \ No newline at end of file +.DS_Store +.idea diff --git a/src/Exchange.php b/src/Exchange.php index 0f2e730..eeebad3 100644 --- a/src/Exchange.php +++ b/src/Exchange.php @@ -262,19 +262,29 @@ private function makeRequest($url) /** * @param string $body * @throws ResponseException if the response is malformed - * @return array + * @return array|\stdClass */ private function prepareResponse($body) { $response = json_decode($body, true); - if (isset($response['rates']) and is_array($response['rates'])) { - return ($this->asObject) ? (object) $response['rates'] : $response['rates']; - } else if (isset($response['error'])) { - throw new ResponseException($response['error']); - } else { + if (json_last_error() !== JSON_ERROR_NONE) { + throw new ResponseException(json_last_error_msg()); + } + + if ($response['success'] === false) { + throw new ResponseException($response['error']['info'], $response['error']['code']); + } + + if (!is_array($response['rates'])) { throw new ResponseException('Response body is malformed.'); } + + if ($this->asObject) { + return (object) $response['rates']; + } + + return $response['rates']; } /** @@ -286,6 +296,14 @@ private function prepareResponseResult($body) { $response = json_decode($body, true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new ResponseException(json_last_error_msg()); + } + + if ($response['success'] === false) { + throw new ResponseException($response['error']['info'], $response['error']['code']); + } + if (isset($response['rates']) and is_array($response['rates']) and isset($response['base']) and isset($response['date'])) { return new Result( @@ -293,11 +311,9 @@ private function prepareResponseResult($body) new DateTime($response['date']), $response['rates'] ); - } else if (isset($response['error'])) { - throw new ResponseException($response['error']); - } else { - throw new ResponseException('Response body is malformed.'); } + + throw new ResponseException('Response body is malformed.'); } } diff --git a/tests/ExchangeTest.php b/tests/ExchangeTest.php index 4eb9d94..d4e2146 100644 --- a/tests/ExchangeTest.php +++ b/tests/ExchangeTest.php @@ -8,6 +8,24 @@ class ExchangeTest extends PHPUnit_Framework_TestCase { private $url = 'http://data.fixer.io/api'; + private static $errorResponse = [ + 'success' => false, + 'error' => [ + 'code' => 999, + 'info' => 'Some error message', + ], + ]; + + private static $successfulResponse = [ + 'success' => true, + 'base' => 'EUR', + 'date' => '2016-01-02', + 'rates' => [ + 'GBP' => 1.01, + 'USD' => 1.02 + ], + ]; + public function tearDown() { m::close(); @@ -81,7 +99,7 @@ public function testFullExample() public function testResponse() { $response = m::mock('StdClass'); - $response->shouldReceive('getBody')->once()->andReturn(json_encode(['rates' => ['GBP', 'USD']])); + $response->shouldReceive('getBody')->once()->andReturn(json_encode(self::$successfulResponse)); $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('request')->once()->andReturn($response); @@ -90,7 +108,7 @@ public function testResponse() $exchange->symbols('GBP', 'USD'); $rates = $exchange->get(); - $expected = ['GBP', 'USD']; + $expected = self::$successfulResponse['rates']; $this->assertEquals($rates, $expected); } @@ -98,7 +116,7 @@ public function testResponse() public function testResponseAsObject() { $response = m::mock('StdClass'); - $response->shouldReceive('getBody')->once()->andReturn(json_encode(['rates' => ['GBP', 'USD']])); + $response->shouldReceive('getBody')->once()->andReturn(json_encode(self::$successfulResponse)); $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('request')->once()->andReturn($response); @@ -107,7 +125,7 @@ public function testResponseAsObject() $exchange->symbols('GBP', 'USD'); $rates = $exchange->getAsObject(); - $expected = (object) ['GBP', 'USD']; + $expected = (object) self::$successfulResponse['rates']; $this->assertEquals($rates, $expected); } @@ -115,14 +133,7 @@ public function testResponseAsObject() public function testResponseAsResult() { $response = m::mock('StdClass'); - $response->shouldReceive('getBody')->once()->andReturn(json_encode([ - 'base' => 'EUR', - 'date' => '2016-01-02', - 'rates' => [ - 'GBP' => 1.01, - 'USD' => 1.02 - ], - ])); + $response->shouldReceive('getBody')->once()->andReturn(json_encode(self::$successfulResponse)); $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('request')->once()->andReturn($response); @@ -142,7 +153,7 @@ public function testResponseAsResult() public function testResponseException() { $response = m::mock('StdClass'); - $response->shouldReceive('getBody')->once()->andReturn(json_encode(['error' => 'Some error message'])); + $response->shouldReceive('getBody')->once()->andReturn(json_encode(self::$errorResponse)); $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('request')->once()->andReturn($response); @@ -159,7 +170,7 @@ public function testResponseException() public function testResponseResultException() { $response = m::mock('StdClass'); - $response->shouldReceive('getBody')->once()->andReturn(json_encode(['error' => 'Some error message'])); + $response->shouldReceive('getBody')->once()->andReturn(json_encode(self::$errorResponse)); $client = m::mock('GuzzleHttp\Client'); $client->shouldReceive('request')->once()->andReturn($response); @@ -169,4 +180,4 @@ public function testResponseResultException() $exchange->getResult(); } -} \ No newline at end of file +}