-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handler for Client Request/Response Exceptions
Fixes #14
- Loading branch information
Showing
17 changed files
with
277 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception; | ||
|
||
use Exception; | ||
use LauLamanApps\IzettleApi\Exception\IzettleApiException; | ||
|
||
class ClientException extends Exception implements IzettleApiException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception; | ||
|
||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use LauLamanApps\IzettleApi\Client\Exception\ClientException as IzettleClientException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidClient\InvalidClientIdException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\InvalidUsernameOrPasswordException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\TooManyFailedAttemptsException; | ||
|
||
final class GuzzleClientExceptionHandler | ||
{ | ||
public static function handleClientException(ClientException $exception): void | ||
{ | ||
switch ($exception->getCode()) { | ||
case 400: | ||
self::handleClient400Exception($exception); | ||
} | ||
|
||
throw new IzettleClientException($exception->getMessage()); | ||
} | ||
|
||
public static function handleRequestException(RequestException $exception): void | ||
{ | ||
$responseData = json_decode($exception->getResponse()->getBody()->getContents(), true); | ||
switch ($exception->getCode()) { | ||
case 404: | ||
throw new NotFoundException($responseData['developerMessage']); | ||
} | ||
|
||
throw new IzettleClientException($exception->getMessage()); | ||
} | ||
|
||
private static function handleClient400Exception(ClientException $exception): void | ||
{ | ||
$responseData = json_decode($exception->getResponse()->getBody()->getContents(), true); | ||
switch ($responseData['error']) { | ||
case 'invalid_grant': | ||
self::handleInvalidGrantException($responseData); | ||
case 'invalid_client': | ||
self::handleInvalidClientException($responseData); | ||
case 'unauthorized_client': | ||
throw new InvalidClientException($responseData['error_description']); | ||
default: | ||
throw new InvalidClientException($responseData['error_description']); | ||
} | ||
} | ||
|
||
private static function handleInvalidGrantException(array $responseData): void | ||
{ | ||
switch ($responseData['error_description']) { | ||
case 'INCORRECT_PASSWORD_OR_USERNAME': | ||
throw new InvalidUsernameOrPasswordException(); | ||
case 'TOO_MANY_FAILED_ATTEMPTS': | ||
throw new TooManyFailedAttemptsException(); | ||
default: | ||
throw new InvalidGrantException($responseData['error_description']); | ||
} | ||
} | ||
|
||
private static function handleInvalidClientException(array $responseData): void | ||
{ | ||
switch ($responseData['error_description']) { | ||
case 'Invalid client_id': | ||
throw new InvalidClientIdException(); | ||
default: | ||
throw new InvalidClientException($responseData['error_description']); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Client/Exception/InvalidClient/InvalidClientIdException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception\InvalidClient; | ||
|
||
use LauLamanApps\IzettleApi\Client\Exception\InvalidClientException; | ||
|
||
final class InvalidClientIdException extends InvalidClientException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception; | ||
|
||
class InvalidClientException extends ClientException | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Client/Exception/InvalidGrant/InvalidUsernameOrPasswordException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception\InvalidGrant; | ||
|
||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrantException; | ||
|
||
final class InvalidUsernameOrPasswordException extends InvalidGrantException | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Client/Exception/InvalidGrant/TooManyFailedAttemptsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception\InvalidGrant; | ||
|
||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrantException; | ||
|
||
final class TooManyFailedAttemptsException extends InvalidGrantException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Exception; | ||
|
||
class InvalidGrantException extends ClientException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Client/Purchase/Exception/PurchaseNotFoundException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Client\Purchase\Exception; | ||
|
||
use LauLamanApps\IzettleApi\Client\Exception\NotFoundException; | ||
|
||
final class PurchaseNotFoundException extends NotFoundException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/Unit/Client/Exception/GuzzleClientExceptionHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LauLamanApps\IzettleApi\Tests\Unit\Client\Exception; | ||
|
||
use GuzzleHttp\Exception\ClientException as GuzzleClientException; | ||
use GuzzleHttp\Exception\RequestException as GuzzleRequestException; | ||
use LauLamanApps\IzettleApi\Client\Exception\ClientException; | ||
use LauLamanApps\IzettleApi\Client\Exception\GuzzleClientExceptionHandler; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidClientException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidClient\InvalidClientIdException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\InvalidUsernameOrPasswordException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\TooManyFailedAttemptsException; | ||
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrantException; | ||
use LauLamanApps\IzettleApi\Client\Exception\NotFoundException; | ||
use LauLamanApps\IzettleApi\Tests\Unit\MockeryAssertionTrait; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @small | ||
*/ | ||
final class GuzzleClientExceptionHandlerTest extends TestCase | ||
{ | ||
use MockeryAssertionTrait; | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider getClientExceptions | ||
*/ | ||
public function handleClientException(GuzzleClientException $exception, string $expectedException): void | ||
{ | ||
self::expectException($expectedException); | ||
GuzzleClientExceptionHandler::handleClientException($exception); | ||
} | ||
|
||
/** | ||
* @return GuzzleClientException[] | ||
*/ | ||
public function getClientExceptions(): array | ||
{ | ||
return [ | ||
'undefined' => [new GuzzleClientException(0, Mockery::mock(RequestInterface::class)), ClientException::class], | ||
'INCORRECT_PASSWORD_OR_USERNAME' => [$this->getClientException(400, 'invalid_grant', 'INCORRECT_PASSWORD_OR_USERNAME'), InvalidUsernameOrPasswordException::class], | ||
'TOO_MANY_FAILED_ATTEMPTS' => [$this->getClientException(400, 'invalid_grant', 'TOO_MANY_FAILED_ATTEMPTS'), TooManyFailedAttemptsException::class], | ||
'InvalidGrantException' => [$this->getClientException(400, 'invalid_grant', '[fallback]'), InvalidGrantException::class], | ||
'Invalid client_id' => [$this->getClientException(400, 'invalid_client', 'Invalid client_id'), InvalidClientIdException::class], | ||
'Invalid client' => [$this->getClientException(400, 'invalid_client', '[fallback]'), InvalidClientException::class], | ||
'unauthorized_client' => [$this->getClientException(400, 'unauthorized_client', '[does not mather]'), InvalidClientException::class], | ||
'InvalidClientException' => [$this->getClientException(400, 'fallback', '[does not mather]'), InvalidClientException::class], | ||
|
||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider getRequestExceptions | ||
*/ | ||
public function handleRequestException(GuzzleRequestException $exception, string $expectedException): void | ||
{ | ||
self::expectException($expectedException); | ||
GuzzleClientExceptionHandler::handleRequestException($exception); | ||
} | ||
|
||
/** | ||
* @return GuzzleRequestException[] | ||
*/ | ||
public function getRequestExceptions(): array | ||
{ | ||
return [ | ||
'undefined' => [$this->getRequestException(0, '[fallback]'), ClientException::class], | ||
'not found' => [$this->getRequestException(404, 'not found'), NotFoundException::class], | ||
]; | ||
} | ||
|
||
private function getClientException(int $code ,string $error, string $errorDescription): GuzzleClientException | ||
{ | ||
/** @var RequestInterface|MockInterface $request */ | ||
$request = Mockery::mock(RequestInterface::class); | ||
$response = $this->getResponse($code, ['error'=> $error, 'error_description' => $errorDescription]); | ||
|
||
return new GuzzleClientException('', $request, $response); | ||
} | ||
|
||
private function getRequestException(int $code, string $developerMessage): GuzzleRequestException | ||
{ | ||
/** @var RequestInterface|MockInterface $request */ | ||
$request = Mockery::mock(RequestInterface::class); | ||
$response = $this->getResponse($code, ['developerMessage' => $developerMessage]); | ||
|
||
return new GuzzleRequestException('', $request, $response); | ||
} | ||
|
||
private function getResponse(int $code, array $returnData): ResponseInterface | ||
{ | ||
/** @var ResponseInterface|MockInterface $response */ | ||
$response = Mockery::mock(ResponseInterface::class); | ||
$response->shouldReceive('getStatusCode')->once()->andReturn($code); | ||
$response->shouldReceive('getBody')->once()->andReturnSelf(); | ||
$response->shouldReceive('getContents')->once()->andReturn(json_encode($returnData)); | ||
|
||
return $response; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters