mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
Catch an exception while running method (e.g. "Malformed payload") & throw custom exception
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\LimeSurvey\ApiClient\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Meritoo\Common\Utilities\Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used when an error occurred while running method
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,9 @@
|
|||||||
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
||||||
|
|
||||||
use JsonRPC\Client as RpcClient;
|
use JsonRPC\Client as RpcClient;
|
||||||
|
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
@@ -47,14 +49,22 @@ class JsonRpcClientManager
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @throws UnknownMethodException
|
* @throws UnknownMethodException
|
||||||
|
* @throws InvalidResultOfMethodRunException
|
||||||
*/
|
*/
|
||||||
public function runMethod($method, $arguments = [])
|
public function runMethod($method, $arguments = [])
|
||||||
{
|
{
|
||||||
|
$result = null;
|
||||||
$method = MethodType::getValidatedMethod($method);
|
$method = MethodType::getValidatedMethod($method);
|
||||||
|
|
||||||
return $this
|
try {
|
||||||
|
$result = $this
|
||||||
->getRpcClient()
|
->getRpcClient()
|
||||||
->execute($method, $arguments);
|
->execute($method, $arguments);
|
||||||
|
} catch (InvalidJsonFormatException $exception) {
|
||||||
|
throw new InvalidResultOfMethodRunException($exception, $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used when an error occurred while running method
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @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"'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,9 +9,11 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Manager;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Manager;
|
||||||
|
|
||||||
use JsonRPC\Client as RpcClient;
|
use JsonRPC\Client as RpcClient;
|
||||||
|
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest;
|
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));
|
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()
|
public function testGetRpcClientVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
static::assertMethodVisibilityAndArguments(JsonRpcClientManager::class, 'getRpcClient', OopVisibilityType::IS_PROTECTED);
|
static::assertMethodVisibilityAndArguments(JsonRpcClientManager::class, 'getRpcClient', OopVisibilityType::IS_PROTECTED);
|
||||||
|
|||||||
Reference in New Issue
Block a user