mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 02:11:45 +01:00
SurveyService - getAllSurveys() method - catch and serve an exception while fetching all surveys
This commit is contained in:
@@ -10,8 +10,10 @@ namespace Meritoo\LimeSurvey\ApiClient\Service;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||
|
||||
/**
|
||||
* Service that serves surveys
|
||||
@@ -65,14 +67,29 @@ class SurveyService
|
||||
* Returns all surveys
|
||||
*
|
||||
* @return Collection
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getAllSurveys()
|
||||
{
|
||||
if ($this->allSurveys->isEmpty()) {
|
||||
$surveys = $this
|
||||
->client
|
||||
->run(MethodType::LIST_SURVEYS)
|
||||
->getData();
|
||||
try {
|
||||
$surveys = $this
|
||||
->client
|
||||
->run(MethodType::LIST_SURVEYS)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
$reason = $exception->getReason();
|
||||
|
||||
/*
|
||||
* Reason of the exception is different than "Oops, there is no surveys. Everything else is fine."?
|
||||
* Let's throw the exception
|
||||
*/
|
||||
if (ReasonType::NO_SURVEYS_FOUND !== $reason) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$surveys = new Collection();
|
||||
}
|
||||
|
||||
if (null !== $surveys) {
|
||||
$this->allSurveys = $surveys;
|
||||
|
||||
@@ -19,6 +19,13 @@ class ReasonType extends BaseType
|
||||
*/
|
||||
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no surveys
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NO_SURVEYS_FOUND = 'No surveys found';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no table with tokens/participants of survey
|
||||
*
|
||||
|
||||
@@ -208,7 +208,7 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
|
||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API with mocked method runMethod()
|
||||
*
|
||||
* @param int $runMethodCallCount Count of calls of the runMethod() method (who is mocked)
|
||||
* @param array $runMethodCallResults (optional) Results of calls of the runMethod() method (who is mocked)
|
||||
|
||||
@@ -8,15 +8,18 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
||||
|
||||
use Exception;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
/**
|
||||
@@ -66,6 +69,29 @@ class SurveyServiceTest extends BaseTestCase
|
||||
static::assertEquals($client, $surveyService->getClient());
|
||||
}
|
||||
|
||||
public function testGetAllSurveysWithImportantException()
|
||||
{
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
|
||||
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||
$this->serviceWithoutSurveys->getAllSurveys();
|
||||
}
|
||||
|
||||
public function testGetAllSurveysWithNoSurveysException()
|
||||
{
|
||||
$exception = new CannotProcessDataException(ReasonType::NO_SURVEYS_FOUND);
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
|
||||
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys());
|
||||
}
|
||||
|
||||
public function testGetAllSurveys()
|
||||
{
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1);
|
||||
@@ -115,7 +141,7 @@ class SurveyServiceTest extends BaseTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
|
||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API with mocked method runMethod()
|
||||
*
|
||||
* @param int $runMethodCallCount Count of calls of the runMethod() method (who is mocked)
|
||||
* @param array $runMethodCallResults (optional) Results of calls of the runMethod() method (who is mocked)
|
||||
@@ -133,6 +159,26 @@ class SurveyServiceTest extends BaseTestCase
|
||||
return $rpcClientManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API with mocked method runMethod()
|
||||
* that throws an exception
|
||||
*
|
||||
* @param int $runMethodCallCount Count of calls of the runMethod() method (who is mocked)
|
||||
* @param Exception $exception The exception that should be thrown
|
||||
* @return PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception)
|
||||
{
|
||||
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
|
||||
|
||||
$rpcClientManager
|
||||
->expects(static::exactly($runMethodCallCount))
|
||||
->method('runMethod')
|
||||
->willThrowException($exception);
|
||||
|
||||
return $rpcClientManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates instance of the tested service without surveys
|
||||
*
|
||||
|
||||
@@ -31,6 +31,7 @@ class ReasonTypeTest extends BaseTypeTestCase
|
||||
{
|
||||
return [
|
||||
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
|
||||
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||
];
|
||||
}
|
||||
@@ -63,6 +64,11 @@ class ReasonTypeTest extends BaseTypeTestCase
|
||||
true,
|
||||
];
|
||||
|
||||
yield[
|
||||
ReasonType::NO_SURVEYS_FOUND,
|
||||
true,
|
||||
];
|
||||
|
||||
yield[
|
||||
ReasonType::NO_TOKEN_TABLE,
|
||||
true,
|
||||
|
||||
Reference in New Issue
Block a user