diff --git a/src/Service/SurveyService.php b/src/Service/SurveyService.php index ab59713..fa0ffba 100644 --- a/src/Service/SurveyService.php +++ b/src/Service/SurveyService.php @@ -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; diff --git a/src/Type/ReasonType.php b/src/Type/ReasonType.php index 3bbc045..e50fea3 100644 --- a/src/Type/ReasonType.php +++ b/src/Type/ReasonType.php @@ -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 * diff --git a/tests/Service/ParticipantServiceTest.php b/tests/Service/ParticipantServiceTest.php index 05f8332..65052ed 100644 --- a/tests/Service/ParticipantServiceTest.php +++ b/tests/Service/ParticipantServiceTest.php @@ -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) diff --git a/tests/Service/SurveyServiceTest.php b/tests/Service/SurveyServiceTest.php index bcdac6a..a672972 100644 --- a/tests/Service/SurveyServiceTest.php +++ b/tests/Service/SurveyServiceTest.php @@ -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 * diff --git a/tests/Type/ReasonTypeTest.php b/tests/Type/ReasonTypeTest.php index 1755a8d..cf3af64 100644 --- a/tests/Type/ReasonTypeTest.php +++ b/tests/Type/ReasonTypeTest.php @@ -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,