Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
.idea
36 changes: 26 additions & 10 deletions src/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand All @@ -286,18 +296,24 @@ 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(
$response['base'],
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.');
}

}
41 changes: 26 additions & 15 deletions tests/ExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -90,15 +108,15 @@ public function testResponse()
$exchange->symbols('GBP', 'USD');

$rates = $exchange->get();
$expected = ['GBP', 'USD'];
$expected = self::$successfulResponse['rates'];

$this->assertEquals($rates, $expected);
}

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);
Expand All @@ -107,22 +125,15 @@ public function testResponseAsObject()
$exchange->symbols('GBP', 'USD');

$rates = $exchange->getAsObject();
$expected = (object) ['GBP', 'USD'];
$expected = (object) self::$successfulResponse['rates'];

$this->assertEquals($rates, $expected);
}

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -169,4 +180,4 @@ public function testResponseResultException()
$exchange->getResult();
}

}
}