Skip to content

Commit

Permalink
some fixes plus test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjul17 committed Dec 20, 2015
1 parent c9e4dfe commit bd49932
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/Exception/SellsyError.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class SellsyError extends Exception {
*/
protected $more;

/**
* @var string
*/
protected $sellsyCode;

/**
* SellsyError constructor.
*
Expand All @@ -21,8 +26,16 @@ class SellsyError extends Exception {
* @param Exception $previous
*/
public function __construct ($message, $code, $more = NULL, Exception $previous = NULL) {
parent::__construct($message, $code, $previous);
$this->more = $more;
parent::__construct($message, 0, $previous);
$this->more = $more;
$this->sellsyCode = $code;
}

/**
* @return string
*/
public function getSellsyCode () {
return $this->sellsyCode;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;
use SellsyApi\Exception\OAuthException;

class Request implements AsyncRequestInterface {

Expand Down Expand Up @@ -193,20 +194,20 @@ protected function getOAuthHeader () {
* @param mixed $response
*
* @return mixed
* @throws \OAuthException
* @throws \SellsyApi\Exception\OAuthException
* @throws \SellsyApi\Exception\SellsyError
* @throws \UnexpectedValueException
*/
protected function handleResponse ($response) {

if (strstr($response, 'oauth_problem')) {
throw new \OAuthException($response);
throw new OAuthException($response);
}

$array = json_decode($response, TRUE);

if (!is_array($array)) {
throw new \UnexpectedValueException(sprintf('Unable to decode JSON Sellsy\'s response (%s)', $response));
throw new \UnexpectedValueException(sprintf('Unable to decode JSON Sellsy\'s response (%s)', $response));
}

if (!array_key_exists('status', $array)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Exception/SellsyErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class SellsyErrorTest extends \PHPUnit_Framework_TestCase {

public function testConstructor () {
$prevE = new \Exception();
$code = 123;
$code = 'E_CUSTOM';
$message = 'message';
$more = 'more';
$e = new SellsyError($message, $code, $more, $prevE);

$this->assertInstanceOf(SellsyError::class, $e);
$this->assertSame($prevE, $e->getPrevious());
$this->assertSame($code, $e->getCode());
$this->assertSame($code, $e->getSellsyCode());
$this->assertSame($message, $e->getMessage());
$this->assertSame($more, $e->getMore());
}
Expand Down
93 changes: 91 additions & 2 deletions tests/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,103 @@ public function testCancelPromise () {
try {
$promise->cancel();
$promise->wait();
} catch (\Exception $e){
} catch (\Exception $e) {

}
$this->assertSame($promise::REJECTED, $promise->getState());
}

public function testErrorResponse() {
/**
* @expectedException \SellsyApi\Exception\SellsyError
* @expectedExceptionMessage Unexpected error
*/
public function testErrorResponse () {
$request = $this->createRequest();
$prop = new \ReflectionProperty($request, 'client');
$prop->setAccessible(TRUE);
$params = ['field' => 'value'];
$method = 'method';
$client = $this->createClientMock('post', $method, $params, new Response(200, [],
'{"status":"error","error":{"code":"E_UNKNOW ","message":"Unexpected error","more":"Fatal error"}}'));
$prop->setValue($request, $client);
$request->call($method, $params);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Unknown Sellsy error
*/
public function testInvalidErrorResponse () {
$request = $this->createRequest();
$prop = new \ReflectionProperty($request, 'client');
$prop->setAccessible(TRUE);
$params = ['field' => 'value'];
$method = 'method';
$client = $this->createClientMock('post', $method, $params,
new Response(200, [], '{"status":"error","error":"error"}'));
$prop->setValue($request, $client);
$request->call($method, $params);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Unable to decode JSON Sellsy's response (response)
*/
public function testInvalidJSONResponse () {
$request = $this->createRequest();
$prop = new \ReflectionProperty($request, 'client');
$prop->setAccessible(TRUE);
$params = ['field' => 'value'];
$method = 'method';
$client = $this->createClientMock('post', $method, $params, new Response(200, [], 'response'));
$prop->setValue($request, $client);
$request->call($method, $params);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Field response not found in Sellsy's response ({"status":"success"})
*/
public function testInvalidResponse () {
$request = $this->createRequest();
$prop = new \ReflectionProperty($request, 'client');
$prop->setAccessible(TRUE);
$params = ['field' => 'value'];
$method = 'method';
$client = $this->createClientMock('post', $method, $params, new Response(200, [], '{"status":"success"}'));
$prop->setValue($request, $client);
$request->call($method, $params);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Field status not found in Sellsy's response (["response"])
*/
public function testInvalidStatusResponse () {
$request = $this->createRequest();
$prop = new \ReflectionProperty($request, 'client');
$prop->setAccessible(TRUE);
$params = ['field' => 'value'];
$method = 'method';
$client = $this->createClientMock('post', $method, $params, new Response(200, [], '["response"]'));
$prop->setValue($request, $client);
$request->call($method, $params);
}

/**
* @expectedException \SellsyApi\Exception\OAuthException
* @expectedExceptionMessage oauth_problem=signature_invalid
*/
public function testOAuthProblem () {
$params = ['field' => 'value'];
$method = 'method';
$promise = new Promise();
$request = $this->createRequest();
$this->prepareRequest($request, 'postAsync', $method, $params, $promise);
$res = $request->callAsync($method, $params);
$this->assertInstanceOf(PromiseInterface::class, $res);
$promise->resolve(new Response(200, [], 'oauth_problem=signature_invalid'));
$res->wait();
}

}

0 comments on commit bd49932

Please sign in to comment.