mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 18:11:50 +01:00
ParticipantService - getSurveyParticipants() method - catch and serve an exception while fetching participants of given survey
This commit is contained in:
@@ -16,6 +16,13 @@ namespace Meritoo\LimeSurvey\ApiClient\Exception;
|
|||||||
*/
|
*/
|
||||||
class CannotProcessDataException extends \Exception
|
class CannotProcessDataException extends \Exception
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $reason;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
@@ -23,9 +30,21 @@ class CannotProcessDataException extends \Exception
|
|||||||
*/
|
*/
|
||||||
public function __construct($reason)
|
public function __construct($reason)
|
||||||
{
|
{
|
||||||
|
$this->reason = $reason;
|
||||||
|
|
||||||
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||||
$message = sprintf($template, $reason);
|
$message = sprintf($template, $this->reason);
|
||||||
|
|
||||||
parent::__construct($message);
|
parent::__construct($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getReason()
|
||||||
|
{
|
||||||
|
return $this->reason;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,12 @@ namespace Meritoo\LimeSurvey\ApiClient\Service;
|
|||||||
|
|
||||||
use Meritoo\Common\Collection\Collection;
|
use Meritoo\Common\Collection\Collection;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service that serves participants
|
* Service that serves participants
|
||||||
@@ -69,6 +71,8 @@ class ParticipantService
|
|||||||
*
|
*
|
||||||
* @param int $surveyId ID of survey
|
* @param int $surveyId ID of survey
|
||||||
* @return Collection
|
* @return Collection
|
||||||
|
*
|
||||||
|
* @throws CannotProcessDataException
|
||||||
*/
|
*/
|
||||||
public function getSurveyParticipants($surveyId)
|
public function getSurveyParticipants($surveyId)
|
||||||
{
|
{
|
||||||
@@ -81,10 +85,24 @@ class ParticipantService
|
|||||||
$surveyId,
|
$surveyId,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
$participants = $this
|
$participants = $this
|
||||||
->client
|
->client
|
||||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||||
->getData();
|
->getData();
|
||||||
|
} catch (CannotProcessDataException $exception) {
|
||||||
|
$reason = $exception->getReason();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reason of the exception is different than "Oops, there is no participants. Everything else is fine."?
|
||||||
|
* Let's throw the exception
|
||||||
|
*/
|
||||||
|
if (ReasonType::NO_PARTICIPANTS_FOUND !== $reason) {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
$participants = new Collection();
|
||||||
|
}
|
||||||
|
|
||||||
$this
|
$this
|
||||||
->allParticipants
|
->allParticipants
|
||||||
|
|||||||
28
src/Type/ReasonType.php
Normal file
28
src/Type/ReasonType.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Meritoo\LimeSurvey\ApiClient\Type;
|
||||||
|
|
||||||
|
use Meritoo\Common\Type\Base\BaseType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of reason used by LimeSurvey's exception
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ReasonType extends BaseType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no participants of survey
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no table with tokens/participants of survey
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NO_TOKEN_TABLE = 'Error: No token table';
|
||||||
|
}
|
||||||
@@ -8,17 +8,20 @@
|
|||||||
|
|
||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Meritoo\Common\Collection\Collection;
|
use Meritoo\Common\Collection\Collection;
|
||||||
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\Client\Client;
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
|
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
use PHPUnit_Framework_MockObject_MockObject;
|
use PHPUnit_Framework_MockObject_MockObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,7 +71,7 @@ class ParticipantServiceTest extends BaseTestCase
|
|||||||
static::assertEquals($client, $participantService->getClient());
|
static::assertEquals($client, $participantService->getClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSurveyParticipantsFromEmptyParticipants()
|
public function testGetSurveyParticipants()
|
||||||
{
|
{
|
||||||
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
||||||
$sessionManager = $this->getSessionManager();
|
$sessionManager = $this->getSessionManager();
|
||||||
@@ -84,6 +87,29 @@ class ParticipantServiceTest extends BaseTestCase
|
|||||||
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetSurveyParticipantsWithImportantException()
|
||||||
|
{
|
||||||
|
$this->expectException(CannotProcessDataException::class);
|
||||||
|
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
|
||||||
|
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||||
|
$this->serviceWithParticipants->getSurveyParticipants(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetSurveyParticipantsWithNoParticipantsException()
|
||||||
|
{
|
||||||
|
$exception = new CannotProcessDataException(ReasonType::NO_PARTICIPANTS_FOUND);
|
||||||
|
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||||
|
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
||||||
|
}
|
||||||
|
|
||||||
public function testHasParticipant()
|
public function testHasParticipant()
|
||||||
{
|
{
|
||||||
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
||||||
@@ -200,6 +226,26 @@ class ParticipantServiceTest extends BaseTestCase
|
|||||||
return $rpcClientManager;
|
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 participants
|
* Creates instance of the tested service without participants
|
||||||
*
|
*
|
||||||
|
|||||||
71
tests/Type/ReasonTypeTest.php
Normal file
71
tests/Type/ReasonTypeTest.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?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\Type;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the type of reason used by LimeSurvey's exception
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ReasonTypeTest extends BaseTypeTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertHasNoConstructor(ReasonType::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getAllExpectedTypes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||||
|
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getTestedTypeInstance()
|
||||||
|
{
|
||||||
|
return new ReasonType();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function provideTypeToVerify()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'',
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'lorem',
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NO_PARTICIPANTS_FOUND,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NO_TOKEN_TABLE,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user