Skip to content

Commit

Permalink
Merge pull request #10 from graze/error-codes
Browse files Browse the repository at this point in the history
error code constants
  • Loading branch information
Harry Bragg committed Oct 7, 2015
2 parents 4d1b7bd + 1d09e4f commit 5fc5258
Show file tree
Hide file tree
Showing 9 changed files with 1,067 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ $ composer require graze/gigya-client

``` php
$client = new \Graze\Gigya\Gigya($key, $secret, Gigya::DC_EU);
$account = $client->accounts()->getAccountInfo(['uid' => $uid]);
$response = $client->accounts()->getAccountInfo(['uid' => $uid]);
$account = $response->getData();
```

## Change log
Expand Down
10 changes: 10 additions & 0 deletions src/Endpoints/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class Client
*/
protected $options;

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

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

/**
* @param string $namespace
* @param array $params [:apiKey,:secret,:userKey]
Expand Down
45 changes: 45 additions & 0 deletions src/Exceptions/ResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Graze\Gigya\Exceptions;

use Exception;
use Graze\Gigya\Response\ResponseInterface;
use RuntimeException;

/**
* Class ResponseException
*
* Generic Response Exception
*
* @package Graze\Gigya\Exceptions
*/
class ResponseException extends RuntimeException
{
/**
* @var ResponseInterface
*/
protected $response;

/**
* @param ResponseInterface $response
* @param string $message
* @param Exception|null $previous
*/
public function __construct(ResponseInterface $response, $message = '', Exception $previous = null)
{
$this->response = $response;

$message = (($message) ? $message . "\n" : '') .
$response;

parent::__construct($message, $response->getErrorCode(), $previous);
}

/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}
876 changes: 876 additions & 0 deletions src/Response/ErrorCode.php

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class Response implements ResponseInterface
*/
protected $data;

/**
* @var GuzzleResponseInterface
*/
protected $response;

/**
* @param GuzzleResponseInterface $response
*/
Expand Down Expand Up @@ -164,4 +169,21 @@ public function getOriginalResponse()
{
return $this->response;
}

/**
* @return string
*/
public function __toString()
{
return sprintf(
"Response: %d: %s - %d: %s\n%s\n%s\n%s",
$this->getStatusCode(),
$this->getStatusReason(),
$this->getErrorCode(),
ErrorCode::getName($this->getErrorCode()),
ErrorCode::getDescription($this->getErrorCode()),
$this->getErrorMessage(),
$this->getErrorDetails()
);
}
}
5 changes: 5 additions & 0 deletions src/Response/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public function getData();
* @return GuzzleResponseInterface
*/
public function getOriginalResponse();

/**
* @return string
*/
public function __toString();
}
49 changes: 49 additions & 0 deletions tests/unit/Exception/ResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Graze\Gigya\Test\Unit\Exception;

use Graze\Gigya\Exceptions\ResponseException;
use Graze\Gigya\Response\ResponseInterface;
use Graze\Gigya\Test\TestCase;
use Mockery as m;

class ResponseExceptionTest extends TestCase
{
public function testInstanceOfRuntimeException()
{
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getErrorCode')
->andReturn(0);
$exception = new ResponseException($response);

static::assertInstanceOf('RuntimeException', $exception);
}

public function testExceptionIncludeResponseStringAndCode()
{
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getErrorCode')
->andReturn(100001);
$response->shouldReceive('__toString')
->andReturn('some description from the response');
$exception = new ResponseException($response);

static::setExpectedException(
ResponseException::class,
'some description from the response',
100001
);

throw $exception;
}

public function testGetResponse()
{
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getErrorCode')
->andReturn(100001);
$exception = new ResponseException($response);

static::assertSame($response, $exception->getResponse());
}
}
37 changes: 37 additions & 0 deletions tests/unit/Response/ErrorCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Graze\Gigya\Test\Unit\Response;

use Graze\Gigya\Response\ErrorCode;
use Graze\Gigya\Test\TestCase;

class ErrorCodeTest extends TestCase
{
public function testGetName()
{
static::assertEquals('Session migration error', ErrorCode::getName(ErrorCode::ERROR_SESSION_MIGRATION_ERROR));
static::assertEquals('Invalid Secret', ErrorCode::getName(ErrorCode::ERROR_INVALID_SECRET));
}

public function testGetNameWithUnknownErrorCodeWillReturnNull()
{
static::assertNull(ErrorCode::getName(3271863217367182));
}

public function testGetDescription()
{
static::assertEquals(
'When accounts.login, accounts.socialLogin, accounts.finalizeRegistration, socialize.notifyLogin, or socialize.login is called and the policy (in the site Policies) requires 2-factor authentication, and the device is not already in the verified device list for the account. The first time the method is called, the device needs to be registered, and for the following calls, the device needs to be verified.',
ErrorCode::getDescription(ErrorCode::ERROR_ACCOUNT_PENDING_TFA_VERIFICATION)
);
static::assertEquals(
'If Protect Against Account Harvesting policy is enabled and neither Email Validation nor CAPTCHA Validation policies are enabled.',
ErrorCode::getDescription(ErrorCode::ERROR_INVALID_POLICY_CONFIGURATION)
);
}

public function testGetDescriptionWithUnknownErrorCodeWillReturnNull()
{
static::assertNull(ErrorCode::getDescription(3271863217367182));
}
}
21 changes: 21 additions & 0 deletions tests/unit/Response/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use DateTime;
use Graze\Gigya\Response\Response;
use Graze\Gigya\Test\TestCase;
use Graze\Gigya\Test\TestFixtures;
use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface;
use Mockery as m;

class ResponseTest extends TestCase
{
Expand Down Expand Up @@ -42,4 +45,22 @@ public function testOtherTimeZoneFormat()
static::assertEquals("943000", $time->format('u'));
static::assertEquals("+02:00", $time->getTimezone()->getName());
}

public function testToString()
{
$guzzleResponse = m::mock(GuzzleResponseInterface::class);
$guzzleResponse->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('accounts.getAccountInfo'));
$response = new Response($guzzleResponse);

static::assertRegExp('/Response: \d+: \w+ - \d+: .+\n.+/s', $response->__toString());
}

public function testToStringForFailure()
{
$guzzleResponse = m::mock(GuzzleResponseInterface::class);
$guzzleResponse->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('failure_403'));
$response = new Response($guzzleResponse);

static::assertRegExp('/Response: \d+: \w+ - \d+: .+\n.+\n.+\n.+/s', $response->__toString());
}
}

0 comments on commit 5fc5258

Please sign in to comment.