From 20d7d2f50d991bd2213b4fb3ab5149e3578b2626 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Tue, 26 Sep 2017 12:49:59 +0200 Subject: [PATCH] Catch an exception while running method (e.g. "Malformed payload") & throw custom exception --- .../InvalidResultOfMethodRunException.php | 45 +++++++++++ .../Manager/JsonRpcClientManager.php | 16 +++- .../InvalidResultOfMethodRunExceptionTest.php | 75 +++++++++++++++++++ .../Manager/JsonRpcClientManagerTest.php | 23 ++++++ 4 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 src/Meritoo/LimeSurvey/ApiClient/Exception/InvalidResultOfMethodRunException.php create mode 100644 tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/InvalidResultOfMethodRunExceptionTest.php diff --git a/src/Meritoo/LimeSurvey/ApiClient/Exception/InvalidResultOfMethodRunException.php b/src/Meritoo/LimeSurvey/ApiClient/Exception/InvalidResultOfMethodRunException.php new file mode 100644 index 0000000..7b639b0 --- /dev/null +++ b/src/Meritoo/LimeSurvey/ApiClient/Exception/InvalidResultOfMethodRunException.php @@ -0,0 +1,45 @@ + + * @copyright Meritoo.pl + */ +class InvalidResultOfMethodRunException extends Exception +{ + /** + * Class constructor + * + * @param Exception $previousException The previous exception, source of an error + * @param string $methodName Name of called method + * @param array $methodArguments (optional) Arguments of the called method + */ + public function __construct(Exception $previousException, $methodName, array $methodArguments = []) + { + $template = "Oops, an error occurred while running method. Is there everything ok? Details:\n" + . "- error: %s,\n" + . "- method: %s,\n" + . '- arguments: %s.'; + + if (empty($methodArguments)) { + $methodArguments = '(no arguments)'; + } else { + $methodArguments = Arrays::valuesKeys2string($methodArguments, ', ', '=', '"'); + } + + $message = sprintf($template, $previousException->getMessage(), $methodName, $methodArguments); + parent::__construct($message, $previousException->getCode()); + } +} diff --git a/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php b/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php index 64047f3..b7ffba2 100644 --- a/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php +++ b/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php @@ -3,7 +3,9 @@ namespace Meritoo\LimeSurvey\ApiClient\Manager; use JsonRPC\Client as RpcClient; +use JsonRPC\Exception\InvalidJsonFormatException; use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration; +use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException; use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException; use Meritoo\LimeSurvey\ApiClient\Type\MethodType; @@ -47,14 +49,22 @@ class JsonRpcClientManager * @return mixed * * @throws UnknownMethodException + * @throws InvalidResultOfMethodRunException */ public function runMethod($method, $arguments = []) { + $result = null; $method = MethodType::getValidatedMethod($method); - return $this - ->getRpcClient() - ->execute($method, $arguments); + try { + $result = $this + ->getRpcClient() + ->execute($method, $arguments); + } catch (InvalidJsonFormatException $exception) { + throw new InvalidResultOfMethodRunException($exception, $method); + } + + return $result; } /** diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/InvalidResultOfMethodRunExceptionTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/InvalidResultOfMethodRunExceptionTest.php new file mode 100644 index 0000000..4179592 --- /dev/null +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/InvalidResultOfMethodRunExceptionTest.php @@ -0,0 +1,75 @@ + + * @copyright Meritoo.pl + */ +class InvalidResultOfMethodRunExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(InvalidResultOfMethodRunException::class, OopVisibilityType::IS_PUBLIC, 3, 2); + } + + /** + * @param Exception $previousException The previous exception, source of an error + * @param string $methodName Name of called method + * @param array $methodArguments Arguments of the called method + * @param string $expectedMessage Expected exception's message + * + * @dataProvider providePreviousExceptionAndMethod + */ + public function testConstructorMessage(Exception $previousException, $methodName, array $methodArguments, $expectedMessage) + { + $exception = new InvalidResultOfMethodRunException($previousException, $methodName, $methodArguments); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides previous exception, name and arguments of called method + * + * @return Generator + */ + public function providePreviousExceptionAndMethod() + { + $template = "Oops, an error occurred while running method. Is there everything ok? Details:\n" + . "- error: %s,\n" + . "- method: %s,\n" + . '- arguments: %s.'; + + yield[ + new Exception('Lorem ipsum'), + MethodType::ADD_RESPONSE, + [], + sprintf($template, 'Lorem ipsum', MethodType::ADD_RESPONSE, '(no arguments)'), + ]; + + yield[ + new Exception('Dolor sit amet'), + MethodType::LIST_SURVEYS, + [ + 'fist_name' => 'John', + 'last_name' => 'Scott', + 'email' => 'john@scott.com', + ], + sprintf($template, 'Dolor sit amet', MethodType::LIST_SURVEYS, 'fist_name="John", last_name="Scott", email="john@scott.com"'), + ]; + } +} diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Manager/JsonRpcClientManagerTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Manager/JsonRpcClientManagerTest.php index 42f7f80..30d182d 100644 --- a/tests/Meritoo/LimeSurvey/Test/ApiClient/Manager/JsonRpcClientManagerTest.php +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Manager/JsonRpcClientManagerTest.php @@ -9,9 +9,11 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Manager; use JsonRPC\Client as RpcClient; +use JsonRPC\Exception\InvalidJsonFormatException; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration; +use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException; use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager; use Meritoo\LimeSurvey\ApiClient\Type\MethodType; use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest; @@ -83,6 +85,27 @@ class JsonRpcClientManagerTest extends BaseTestCase static::assertEquals(SurveyTest::getSurveysRawData(), $manager->runMethod(MethodType::LIST_SURVEYS)); } + public function testRunMethodWithException() + { + $this->expectException(InvalidResultOfMethodRunException::class); + + $manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']); + $rpcClient = $this->createMock(RpcClient::class); + + $rpcClient + ->expects(self::once()) + ->method('execute') + ->willThrowException(new InvalidJsonFormatException('bla bla')); + + $manager + ->expects(static::once()) + ->method('getRpcClient') + ->willReturn($rpcClient); + + /* @var JsonRpcClientManager $manager */ + $manager->runMethod(MethodType::LIST_SURVEYS); + } + public function testGetRpcClientVisibilityAndArguments() { static::assertMethodVisibilityAndArguments(JsonRpcClientManager::class, 'getRpcClient', OopVisibilityType::IS_PROTECTED);