26 Commits
0.0.7 ... 0.0.8

Author SHA1 Message Date
Meritoo
db6a6f22e2 ParticipantService - getParticipantDetails() method - returns full data of participant with given e-mail of given survey 2017-10-01 20:51:13 +02:00
Meritoo
f5334f816f Participants collection - store instances of ParticipantShort instead of Participant
Related to fetch full data of participant of given survey
2017-10-01 20:50:17 +02:00
Meritoo
e43712f218 Minor refactoring 2017-10-01 20:43:39 +02:00
Meritoo
e464ae30af SurveyService - getStartSurveyUrl() method - returns url used to start survey for given survey and participant 2017-10-01 18:34:56 +02:00
Meritoo
98cad233d9 Minor refactoring 2017-10-01 18:20:58 +02:00
Meritoo
5154f05ee4 ParticipantService - hasParticipantFilledSurvey() method - fix "Response: HTTP/1.1 500 Internal Server Error" bug while trying to get information if participant has filled survey 2017-10-01 15:01:54 +02:00
Meritoo
0c44140c89 MissingParticipantOfSurveyException - fix message 2017-10-01 13:42:42 +02:00
Meritoo
41156ed058 Participants collection - store instances of ParticipantShort instead of Participant
Required to fix bug "Call to undefined method Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort::isCompleted() in /src/Service/ParticipantService.php on line 206"
2017-10-01 13:42:35 +02:00
Meritoo
54bd0ca114 SurveyService - allow to verify if survey with given ID exists and if is active 2017-09-29 23:55:56 +02:00
Meritoo
5243294bd5 Implement Surveys class, collection of surveys, to work with surveys 2017-09-29 22:51:42 +02:00
Meritoo
6159731768 Base class for one item - add class constructor & allow to set values directly in the constructor 2017-09-29 22:10:13 +02:00
Meritoo
bbd466610c Tests - add missing tests of Participants::addParticipant() method for not existing survey 2017-09-29 19:47:42 +02:00
Meritoo
d54765b378 SurveyService - getAllSurveys() method - catch and serve an exception while fetching all surveys 2017-09-29 16:55:04 +02:00
Meritoo
ab328b96ee ParticipantService - getSurveyParticipants() method - catch and serve an exception while fetching participants of given survey 2017-09-29 15:18:31 +02:00
Meritoo
cd6dbf72bc Tests - add missing tests of Participants::addParticipant() method 2017-09-29 15:16:17 +02:00
Meritoo
beaf61d2ea Participants collection - disable method has(), because hasParticipantsOfSurvey() should be used to verify if there are participants of given survey 2017-09-29 15:07:26 +02:00
Meritoo
cacb2b3b92 ParticipantService - getParticipant() method - get all participants of survey first (to avoid problem when participants exist but are not loaded) 2017-09-29 14:20:31 +02:00
Meritoo
dfa64fee43 ParticipantService - getParticipant() & hasParticipantFilledSurvey() methods - returns participant of survey & information if participant has filled survey 2017-09-29 13:35:26 +02:00
Meritoo
6c44a62f36 Item of result - setting values - make stronger comparison for boolean values 2017-09-29 13:17:40 +02:00
Meritoo
4f26bca282 Minor refactoring 2017-09-29 10:31:04 +02:00
Meritoo
dbd0a65286 composer.json - move tests-related classes to "autoload-dev" section (used for development purposes only and avoid polluting the autoloader in production) 2017-09-29 09:24:57 +02:00
Meritoo
0562cb4d21 ParticipantService & SurveyService - allow to get a client from these services (add getters) 2017-09-28 21:34:31 +02:00
Meritoo
f8a675d0fb Tests - Client - verify null returned as raw data by JsonRpcClient 2017-09-28 20:34:33 +02:00
Meritoo
a5b534b00d Client - allow to get configuration of connection (add getter) 2017-09-28 20:29:50 +02:00
Meritoo
a6866d994c Configuration of connecting - do not allow to change (remove setters) 2017-09-28 20:13:54 +02:00
Meritoo
5061f5a295 Minor refactoring 2017-09-28 19:43:02 +02:00
57 changed files with 1902 additions and 852 deletions

View File

@@ -3,7 +3,7 @@
"description": "Client of LimeSurvey API",
"type": "library",
"license": "MIT",
"version": "0.0.7",
"version": "0.0.8",
"authors": [
{
"name": "Meritoo",
@@ -25,8 +25,12 @@
},
"autoload": {
"psr-4": {
"Meritoo\\LimeSurvey\\": "src/Meritoo/LimeSurvey/",
"Meritoo\\LimeSurvey\\Test\\": "tests/Meritoo/LimeSurvey/Test/"
"Meritoo\\LimeSurvey\\ApiClient\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Meritoo\\LimeSurvey\\Test\\ApiClient\\": "tests/"
}
}
}

View File

@@ -16,13 +16,32 @@ namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
*/
abstract class BaseItem
{
/**
* Class constructor
*
* @param array $data (optional) Data to set in properties of the item
*/
public function __construct(array $data = [])
{
$this->setValues($data);
}
/**
* Sets given value in given property
*
* @param string $property Name of property to set the value
* @param mixed $value Value to set in property
* @return mixed
*/
abstract public function setValue($property, $value);
/**
* Sets values in each property of the item
*
* @param array $data Data to set in properties of the item
* @return $this
*/
public function setValues($data)
private function setValues(array $data)
{
/*
* Oops, no data
@@ -40,13 +59,4 @@ abstract class BaseItem
return $this;
}
/**
* Sets given value in given property
*
* @param string $property Name of property to set the value
* @param mixed $value Value to set in property
* @return mixed
*/
abstract public function setValue($property, $value);
}

View File

@@ -119,6 +119,16 @@ class Client
return new Result($method, $rawData);
}
/**
* Returns configuration used while connecting to LimeSurvey's API
*
* @return ConnectionConfiguration
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
*

View File

@@ -80,12 +80,12 @@ class ConnectionConfiguration
*/
public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true)
{
$this
->setBaseUrl($baseUrl)
->setUsername($username)
->setPassword($password)
->setDebugMode($debugMode)
->setVerifySslCertificate($verifySslCertificate);
$this->setBaseUrl($baseUrl);
$this->username = $username;
$this->password = $password;
$this->debugMode = $debugMode;
$this->verifySslCertificate = $verifySslCertificate;
}
/**
@@ -98,29 +98,6 @@ class ConnectionConfiguration
return $this->baseUrl;
}
/**
* Sets the base url, protocol & domain
*
* @param string $baseUrl The base url, protocol & domain
* @return $this
*
* @throws InvalidUrlException
*/
public function setBaseUrl($baseUrl)
{
if (!Regex::isValidUrl($baseUrl)) {
throw new InvalidUrlException($baseUrl);
}
if (Regex::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
}
$this->baseUrl = $baseUrl;
return $this;
}
/**
* Returns the url of the LimeSurvey's remote control
*
@@ -154,19 +131,6 @@ class ConnectionConfiguration
return $this->username;
}
/**
* Sets the name of user used to authenticate to LimeSurvey
*
* @param string $username The name of user used to authenticate to LimeSurvey
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Returns the password used to authenticate to LimeSurvey
*
@@ -177,19 +141,6 @@ class ConnectionConfiguration
return $this->password;
}
/**
* Sets the password used to authenticate to LimeSurvey
*
* @param string $password The password used to authenticate to LimeSurvey
* @return $this
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Returns information if the "debug" mode is turned on
*
@@ -200,19 +151,6 @@ class ConnectionConfiguration
return $this->debugMode;
}
/**
* Sets information if the "debug" mode is turned on
*
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* @return $this
*/
public function setDebugMode($debugMode = false)
{
$this->debugMode = $debugMode;
return $this;
}
/**
* Returns information if the SSL certificate verification is turned on
*
@@ -223,26 +161,6 @@ class ConnectionConfiguration
return $this->verifySslCertificate;
}
/**
* Sets information if the SSL certificate verification is turned on
*
* @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned on.
* Otherwise - turned off.
* @return $this
*/
public function setVerifySslCertificate($verifySslCertificate = true)
{
$this->verifySslCertificate = $verifySslCertificate;
return $this;
}
/*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Additional / extra methods (neither getters, nor setters)
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/**
* Returns full url of the LimeSurvey's API.
* It's a base url with part related to remote control.
@@ -253,4 +171,27 @@ class ConnectionConfiguration
{
return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl);
}
/**
* Sets the base url, protocol & domain
*
* @param string $baseUrl The base url, protocol & domain
* @return $this
*
* @throws InvalidUrlException
*/
private function setBaseUrl($baseUrl)
{
if (!Regex::isValidUrl($baseUrl)) {
throw new InvalidUrlException($baseUrl);
}
if (Regex::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
}
$this->baseUrl = $baseUrl;
return $this;
}
}

View File

@@ -16,6 +16,13 @@ namespace Meritoo\LimeSurvey\ApiClient\Exception;
*/
class CannotProcessDataException extends \Exception
{
/**
* Reason why data cannot be processed, e.g. "Invalid user name or password"
*
* @var string
*/
private $reason;
/**
* Class constructor
*
@@ -23,9 +30,21 @@ class CannotProcessDataException extends \Exception
*/
public function __construct($reason)
{
$this->reason = $reason;
$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);
}
/**
* Returns reason why data cannot be processed, e.g. "Invalid user name or password"
*
* @return string
*/
public function getReason()
{
return $this->reason;
}
}

View File

@@ -0,0 +1,35 @@
<?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 Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* An exception used while class used to create instance of one item of the result, with data fetched from the
* LimeSurvey's API, is incorrect
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class IncorrectClassOfResultItemException extends \Exception
{
/**
* Class constructor
*
* @param string $className Incorrect class name used to create instance of one item
*/
public function __construct($className)
{
$template = 'Class %s used to create instance of one item of the result should extend %s, but it does not. Did'
. ' you forget to use proper base class?';
$message = sprintf($template, $className, BaseItem::class);
parent::__construct($message);
}
}

View File

@@ -0,0 +1,32 @@
<?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;
/**
* An exception used when participant of survey is missing
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MissingParticipantOfSurveyException extends \Exception
{
/**
* Class constructor
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
*/
public function __construct($surveyId, $email)
{
$template = 'Participant with e-mail %s of survey with ID %s is missing. Maybe was not added to the survey?';
$message = sprintf($template, $email, $surveyId);
parent::__construct($message);
}
}

View File

@@ -12,7 +12,8 @@ use Exception;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
/**
* An exception used while instance of one item used by result, with data fetched from the LimeSurvey's API, is unknown
* An exception used while class name used to create instance of one item of the result, with data fetched from the
* LimeSurvey's API, is unknown
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
@@ -27,10 +28,10 @@ class UnknownInstanceOfResultItem extends Exception
*/
public function __construct($method)
{
$template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper'
. ' class is not mapped in %s::%s() method. Did you forget about this?';
$template = 'Class name used to create instance of one item used by result the of \'%s\' LimeSurvey API\'s'
. ' method is unknown. Proper class is not mapped in %s::%s() method. Did you forget about this?';
$message = sprintf($template, $method, ResultProcessor::class, 'getItemInstance');
$message = sprintf($template, $method, ResultProcessor::class, 'getItemClassName');
parent::__construct($message);
}
}

View File

@@ -1,142 +0,0 @@
<?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\Service;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Serves participants
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ParticipantService
{
/**
* Client of the LimeSurvey's API
*
* @var Client
*/
private $client;
/**
* Participants of survey.
* All participants grouped per survey.
*
* @var Participants
*/
private $allParticipants;
/**
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Participants $allParticipants (optional) Participants of survey. All participants grouped per survey.
*/
public function __construct(Client $client, Participants $allParticipants = null)
{
if (null === $allParticipants) {
$allParticipants = new Participants();
}
$this->client = $client;
$this->allParticipants = $allParticipants;
}
/**
* Returns participants of given survey
*
* @param int $surveyId ID of survey
* @return Collection
*/
public function getSurveyParticipants($surveyId)
{
$hasSurvey = $this
->allParticipants
->hasParticipantsOfSurvey($surveyId);
if (!$hasSurvey) {
$arguments = [
$surveyId,
];
$participants = $this
->client
->run(MethodType::LIST_PARTICIPANTS, $arguments)
->getData();
$this
->allParticipants
->addParticipants($participants, $surveyId);
}
return $this
->allParticipants
->getBySurvey($surveyId);
}
/**
* Returns information if given survey has participant with given e-mail
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @return bool
*/
public function hasParticipant($surveyId, $email)
{
/*
* I have to get all participants of survey to avoid problem when participants exist but are not loaded
*/
$this->getSurveyParticipants($surveyId);
return null !== $this
->allParticipants
->getParticipantOfSurvey($surveyId, $email);
}
/**
* Adds participant with given data to survey with given ID
*
* @param int $surveyId ID of survey
* @param string $firstName First name of the participant to add
* @param string $lastName Last ame of the participant to add
* @param string $email E-mail address of the participant to add
* @return Participant
*/
public function addParticipant($surveyId, $firstName, $lastName, $email)
{
$participantsData = [
[
'firstname' => $firstName,
'lastname' => $lastName,
'email' => $email,
],
];
$arguments = [
$surveyId,
$participantsData,
];
$participantCollection = $this
->client
->run(MethodType::ADD_PARTICIPANTS, $arguments)
->getData();
$this
->allParticipants
->addParticipants($participantCollection, $surveyId);
return $participantCollection->getFirst();
}
}

View File

@@ -1,104 +0,0 @@
<?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\Service;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Serves surveys
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SurveyService
{
/**
* Client of the LimeSurvey's API
*
* @var Client
*/
private $client;
/**
* All surveys
*
* @var Collection
*/
private $allSurveys;
/**
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Collection $allSurveys (optional) All surveys
*/
public function __construct(Client $client, Collection $allSurveys = null)
{
if (null === $allSurveys) {
$allSurveys = new Collection();
}
$this->client = $client;
$this->allSurveys = $allSurveys;
}
/**
* Returns all surveys
*
* @return Collection
*/
public function getAllSurveys()
{
if ($this->allSurveys->isEmpty()) {
$surveys = $this
->client
->run(MethodType::LIST_SURVEYS)
->getData();
if (null !== $surveys) {
$this->allSurveys = $surveys;
}
}
return $this->allSurveys;
}
/**
* Returns information if survey with given ID exists
*
* @param int $surveyId ID of survey to verify
* @return bool
*/
public function isExistingSurvey($surveyId)
{
$allSurveys = $this->getAllSurveys();
/*
* No surveys?
* Nothing to do
*/
if ($allSurveys->isEmpty()) {
return false;
}
$surveyId = (int)$surveyId;
/* @var Survey $survey */
foreach ($allSurveys as $survey) {
if ($survey->getId() == $surveyId) {
return true;
}
}
return false;
}
}

View File

@@ -10,10 +10,10 @@ namespace Meritoo\LimeSurvey\ApiClient\Result\Collection;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Method\DisabledMethodException;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
/**
* Participants of survey.
* Collection of participants (of surveys).
* All participants grouped per survey.
*
* It's a collection of participants' collections.
@@ -40,10 +40,18 @@ class Participants extends Collection
throw new DisabledMethodException(__METHOD__, 'addParticipants');
}
/**
* {@inheritdoc}
*/
public function has($element)
{
throw new DisabledMethodException(__METHOD__, 'hasParticipantsOfSurvey');
}
/**
* Adds participants of given survey
*
* @param Collection $participants Participants to add. Collection of Participant classes.
* @param Collection $participants Participants to add. Collection of ParticipantShort classes.
* @param int $surveyId ID of survey
* @return $this
*/
@@ -67,11 +75,11 @@ class Participants extends Collection
/**
* Adds participant of given survey
*
* @param Participant $participant Participant to add
* @param int $surveyId ID of survey
* @param ParticipantShort $participant Participant to add
* @param int $surveyId ID of survey
* @return $this
*/
public function addParticipant(Participant $participant, $surveyId)
public function addParticipant(ParticipantShort $participant, $surveyId)
{
$this
->getBySurvey($surveyId)
@@ -120,7 +128,7 @@ class Participants extends Collection
*
* @param int $surveyId ID of survey
* @param string $participantEmail E-mail of searched participant
* @return Participant|null
* @return ParticipantShort|null
*/
public function getParticipantOfSurvey($surveyId, $participantEmail)
{
@@ -131,7 +139,7 @@ class Participants extends Collection
return null;
}
/* @var Participant $participant */
/* @var ParticipantShort $participant */
foreach ($participants as $participant) {
if ($participant->getEmail() == $participantEmail) {
return $participant;

View File

@@ -0,0 +1,58 @@
<?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\Result\Collection;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
/**
* Collection of surveys (the Survey class instances)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Surveys extends Collection
{
/**
* {@inheritdoc}
*/
public function add($element, $index = null)
{
if (null === $index) {
/* @var Survey $element */
$index = $element->getId();
}
return parent::add($element, $index);
}
/**
* Returns all or active only surveys
*
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
* @return $this
*/
public function getAll($onlyActive = false)
{
if ($this->isEmpty() || !$onlyActive) {
return $this;
}
$all = new static();
/* @var Survey $survey */
foreach ($this as $survey) {
if ($survey->isActive()) {
$all->add($survey);
}
}
return $all;
}
}

View File

@@ -13,7 +13,7 @@ use Meritoo\Common\Utilities\Date;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* One item of the result/data: full data of participant
* One item of the result/data: full data of one participant
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
@@ -183,15 +183,15 @@ class Participant extends BaseItem
break;
case 'blacklisted':
$this->blacklisted = 'Y' === trim($value);
$this->blacklisted = 'Y' === trim(strtoupper($value));
break;
case 'sent':
$this->sent = 'Y' === trim($value);
$this->sent = 'Y' === trim(strtoupper($value));
break;
case 'remindersent':
$this->reminderSent = 'Y' === trim($value);
$this->reminderSent = 'Y' === trim(strtoupper($value));
break;
case 'remindercount':
@@ -199,7 +199,7 @@ class Participant extends BaseItem
break;
case 'completed':
$this->completed = 'Y' === trim($value);
$this->completed = 'Y' === trim(strtoupper($value));
break;
case 'usesleft':

View File

@@ -103,4 +103,26 @@ class ParticipantShort extends BaseItem
{
return $this->email;
}
/**
* Returns short data of participant created from full data of participant
*
* @param Participant $participant Full data of participant
* @return $this
*/
public static function fromParticipant(Participant $participant)
{
$info = [
'firstname' => $participant->getFirstName(),
'lastname' => $participant->getLastName(),
'email' => $participant->getEmail(),
];
$data = [
'tid' => $participant->getId(),
'participant_info' => $info,
];
return new self($data);
}
}

View File

@@ -193,11 +193,11 @@ class QuestionShort extends BaseItem
break;
case 'other':
$this->other = 'Y' === trim($value);
$this->other = 'Y' === trim(strtoupper($value));
break;
case 'mandatory':
$this->mandatory = 'Y' === trim($value);
$this->mandatory = 'Y' === trim(strtoupper($value));
break;
case 'question_order':

View File

@@ -86,7 +86,7 @@ class Survey extends BaseItem
break;
case 'active':
$this->active = 'Y' === trim($value);
$this->active = 'Y' === trim(strtoupper($value));
break;
}
}

View File

@@ -8,7 +8,9 @@
namespace Meritoo\LimeSurvey\ApiClient\Result\Processor;
use Meritoo\Common\Utilities\Reflection;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
use Meritoo\LimeSurvey\ApiClient\Exception\IncorrectClassOfResultItemException;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
@@ -31,7 +33,9 @@ class ResultProcessor
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
* class constants.
* @param array $rawData Data returned by the LimeSurvey's API
* @return null|BaseItem|array
* @return array|BaseItem|null
*
* @throws IncorrectClassOfResultItemException
*/
public function process($method, array $rawData)
{
@@ -46,9 +50,9 @@ class ResultProcessor
}
/*
* Prepare instance of one item
* Prepare class name for instance of one item
*/
$item = $this->getItemInstance($method);
$itemClassName = $this->getItemClassName($method);
/*
* The raw data is or, actually, should be iterable?
@@ -57,49 +61,50 @@ class ResultProcessor
$items = [];
foreach ($rawData as $itemData) {
$emptyItem = clone $item;
$items[] = $emptyItem->setValues($itemData);
$items[] = new $itemClassName($itemData);
}
return $items;
}
return $item->setValues($rawData);
return new $itemClassName($rawData);
}
/**
* Returns instance of one item of the result
* Returns class name used to create instance of one item of the result
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
* class constants.
* @return BaseItem
* @return string
*
* @throws IncorrectClassOfResultItemException
* @throws UnknownInstanceOfResultItem
*/
private function getItemInstance($method)
private function getItemClassName($method)
{
$item = null;
$className = null;
$method = MethodType::getValidatedMethod($method);
switch ($method) {
case MethodType::ADD_PARTICIPANTS:
case MethodType::GET_PARTICIPANT_PROPERTIES:
$item = new Participant();
$className = Participant::class;
break;
case MethodType::GET_QUESTION_PROPERTIES:
$item = new Question();
$className = Question::class;
break;
case MethodType::LIST_PARTICIPANTS:
$item = new ParticipantShort();
$className = ParticipantShort::class;
break;
case MethodType::LIST_QUESTIONS:
$item = new QuestionShort();
$className = QuestionShort::class;
break;
case MethodType::LIST_SURVEYS:
$item = new Survey();
$className = Survey::class;
break;
/*
@@ -108,12 +113,19 @@ class ResultProcessor
}
/*
* Instance of the item is unknown?
* Oops, class name for instance of the item is unknown
*/
if (null === $item) {
if (null === $className) {
throw new UnknownInstanceOfResultItem($method);
}
return $item;
if (Reflection::isChildOfClass($className, BaseItem::class)) {
return $className;
}
/*
* Oops, class is incorrect (should extend BaseItem)
*/
throw new IncorrectClassOfResultItemException($className);
}
}

View File

@@ -128,7 +128,11 @@ class Result
->getResultProcessor()
->process($this->method, $rawData);
if (null === $processed || is_array($processed)) {
/*
* Result is unknown and it should be iterable the result is an array?
* Let's prepare and return collection
*/
if ((null === $processed && MethodType::isResultIterable($this->method)) || is_array($processed)) {
$collection = new Collection();
if (is_array($processed)) {

View File

@@ -0,0 +1,255 @@
<?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\Service;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
/**
* Service that serves participants
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ParticipantService
{
/**
* Client of the LimeSurvey's API
*
* @var Client
*/
private $client;
/**
* Collection of participants (of surveys).
* All participants grouped per survey.
*
* @var Participants
*/
private $allParticipants;
/**
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Participants $allParticipants (optional) Collection of participants (of surveys). All participants
* grouped per survey.
*/
public function __construct(Client $client, Participants $allParticipants = null)
{
if (null === $allParticipants) {
$allParticipants = new Participants();
}
$this->client = $client;
$this->allParticipants = $allParticipants;
}
/**
* Returns client of the LimeSurvey's API
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* Returns participants of given survey
*
* @param int $surveyId ID of survey
* @return Collection
*
* @throws CannotProcessDataException
*/
public function getSurveyParticipants($surveyId)
{
$hasSurvey = $this
->allParticipants
->hasParticipantsOfSurvey($surveyId);
if (!$hasSurvey) {
$arguments = [
$surveyId,
];
try {
$participants = $this
->client
->run(MethodType::LIST_PARTICIPANTS, $arguments)
->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
->allParticipants
->addParticipants($participants, $surveyId);
}
return $this
->allParticipants
->getBySurvey($surveyId);
}
/**
* Returns information if given survey has participant with given e-mail
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @return bool
*/
public function hasParticipant($surveyId, $email)
{
/*
* I have to get all participants of survey to avoid problem when participants exist but are not loaded
*/
$this->getSurveyParticipants($surveyId);
return null !== $this
->allParticipants
->getParticipantOfSurvey($surveyId, $email);
}
/**
* Adds participant with given data to survey with given ID
*
* @param int $surveyId ID of survey
* @param string $firstName First name of the participant to add
* @param string $lastName Last ame of the participant to add
* @param string $email E-mail address of the participant to add
* @return Participant
*/
public function addParticipant($surveyId, $firstName, $lastName, $email)
{
$participantsData = [
[
'firstname' => $firstName,
'lastname' => $lastName,
'email' => $email,
],
];
$arguments = [
$surveyId,
$participantsData,
];
$participantCollection = $this
->client
->run(MethodType::ADD_PARTICIPANTS, $arguments)
->getData();
/* @var Participant $addedParticipant */
$addedParticipant = $participantCollection->getFirst();
$participants = new Collection([
ParticipantShort::fromParticipant($addedParticipant),
]);
$this
->allParticipants
->addParticipants($participants, $surveyId);
return $participantCollection->getFirst();
}
/**
* Returns short data of one participant with given e-mail of given survey
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @return ParticipantShort|null
*/
public function getParticipant($surveyId, $email)
{
/*
* I have to get all participants of survey to avoid problem when participants exist but are not loaded
*/
$this->getSurveyParticipants($surveyId);
return $this
->allParticipants
->getParticipantOfSurvey($surveyId, $email);
}
/**
* Returns full data of participant with given e-mail of given survey
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @return Participant|null
*/
public function getParticipantDetails($surveyId, $email)
{
$arguments = [
$surveyId,
[
'email' => $email,
],
];
$participant = $this
->client
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
->getData();
/* @var Participant $participant */
return $participant;
}
/**
* Returns information if participant with given e-mail has filled given survey
*
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @return bool
*
* @throws MissingParticipantOfSurveyException
*/
public function hasParticipantFilledSurvey($surveyId, $email)
{
if ($this->hasParticipant($surveyId, $email)) {
$arguments = [
$surveyId,
[
'email' => $email,
],
];
/* @var Participant $participant */
$participant = $this
->client
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
->getData();
return true === $participant->isCompleted();
}
throw new MissingParticipantOfSurveyException($surveyId, $email);
}
}

View File

@@ -0,0 +1,168 @@
<?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\Service;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
/**
* Service that serves surveys
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SurveyService
{
/**
* Client of the LimeSurvey's API
*
* @var Client
*/
private $client;
/**
* All surveys.
* Collection of surveys (the Survey class instances).
*
* @var Surveys
*/
private $allSurveys;
/**
* Template of the url used to start survey
*
* Example:
* - url: https://your.limesurvey.instance/12345?token=q1w2e3r4t5y6
* - LimeSurvey frontend: https://your.limesurvey.instance
* - survey ID: 12345
* - token: q1w2e3r4t5y6
*
* @var string
*/
private $startSurveyUrlTemplate = '%s/%d?token=%s';
/**
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class instances).
*/
public function __construct(Client $client, Surveys $allSurveys = null)
{
if (null === $allSurveys) {
$allSurveys = new Surveys();
}
$this->client = $client;
$this->allSurveys = $allSurveys;
}
/**
* Returns client of the LimeSurvey's API
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* Returns all surveys
*
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
* @return Surveys
*
* @throws CannotProcessDataException
*/
public function getAllSurveys($onlyActive = false)
{
if ($this->allSurveys->isEmpty()) {
$surveys = new Surveys();
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;
}
}
if (null !== $surveys && $surveys instanceof Collection) {
$this->allSurveys = new Surveys($surveys->toArray());
}
}
return $this->allSurveys->getAll($onlyActive);
}
/**
* Returns information if survey with given ID exists
*
* @param int $surveyId ID of survey to verify
* @param bool $shouldBeActive (optional) If is set to true, survey should be active. If it's not, it shouldn't
* be returned, even if exists. Otherwise - it doesn't matter.
* @return bool
*/
public function isExistingSurvey($surveyId, $shouldBeActive = false)
{
$allSurveys = $this->getAllSurveys($shouldBeActive);
/*
* No surveys?
* Nothing to do
*/
if ($allSurveys->isEmpty()) {
return false;
}
$surveyId = (int)$surveyId;
/* @var Survey $survey */
foreach ($allSurveys as $survey) {
if ($survey->getId() == $surveyId) {
return true;
}
}
return false;
}
/**
* Returns url used to start survey for given survey and participant
*
* @param int $surveyId ID of survey to start
* @param Participant $participant Participant who would like to start survey
* @return string
*/
public function getStartSurveyUrl($surveyId, Participant $participant)
{
$baseUrl = $this
->client
->getConfiguration()
->getBaseUrl();
return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participant->getToken());
}
}

42
src/Type/ReasonType.php Normal file
View File

@@ -0,0 +1,42 @@
<?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 survey with given ID
*
* @var string
*/
const NOT_EXISTING_SURVEY_ID = 'Error: Invalid survey ID';
/**
* 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 surveys
*
* @var string
*/
const NO_SURVEYS_FOUND = 'No surveys found';
/**
* Reason of exception when there is no table with tokens/participants of survey
*
* @var string
*/
const NO_TOKEN_TABLE = 'Error: No token table';
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Base\Result;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
@@ -21,31 +22,11 @@ class BaseItemTest extends BaseTestCase
{
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(BaseItem::class);
static::assertConstructorVisibilityAndArguments(BaseItem::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testSetValues()
public function testSetValuesVisibilityAndArguments()
{
$mock = $this->getBaseItemMock();
static::assertInstanceOf(BaseItem::class, $mock->setValues([]));
static::assertInstanceOf(BaseItem::class, $mock->setValues(['lorem']));
}
/**
* Returns mock of the tested class
*
* @return BaseItem
*/
private function getBaseItemMock()
{
$mock = $this->getMockForAbstractClass(BaseItem::class);
$mock
->expects(static::any())
->method('setValue')
->willReturn(null);
return $mock;
static::assertMethodVisibilityAndArguments(BaseItem::class, 'setValues', OopVisibilityType::IS_PRIVATE, 1, 1);
}
}

View File

@@ -52,13 +52,14 @@ class ClientTest extends BaseTestCase
}
/**
* @param string $method Name of method to call
* @param array $arguments Arguments of the method to call
* @param bool $debugMode If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* @param string $method Name of method to call
* @param array $arguments Arguments of the method to call
* @param bool $debugMode If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* @param mixed $expectedRawData Expected raw data returned by JsonRpcClient
*
* @dataProvider provideMethod
*/
public function testRun($method, $arguments, $debugMode)
public function testRun($method, $arguments, $debugMode, $expectedRawData)
{
$sessionManager = $this->createMock(SessionManager::class);
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
@@ -66,14 +67,26 @@ class ClientTest extends BaseTestCase
$rpcClientManager
->expects(static::any())
->method('runMethod')
->willReturn([]);
->willReturn($expectedRawData);
$this->configuration->setDebugMode($debugMode);
$client = new Client($this->configuration, $rpcClientManager, $sessionManager);
$configuration = new ConnectionConfiguration(
$this->configuration->getBaseUrl(),
$this->configuration->getUsername(),
$this->configuration->getPassword(),
$debugMode,
$this->configuration->isVerifySslCertificateOn()
);
$client = new Client($configuration, $rpcClientManager, $sessionManager);
static::assertInstanceOf(Result::class, $client->run($method, $arguments));
}
public function testGetConfiguration()
{
$client = new Client($this->configuration);
static::assertEquals($this->configuration, $client->getConfiguration());
}
public function testGetRpcClientManagerVisibilityAndArguments()
{
static::assertMethodVisibilityAndArguments(Client::class, 'getRpcClientManager', OopVisibilityType::IS_PRIVATE);
@@ -115,18 +128,21 @@ class ClientTest extends BaseTestCase
MethodType::GET_PARTICIPANT_PROPERTIES,
[],
true,
[],
];
yield[
MethodType::LIST_SURVEYS,
[],
false,
[],
];
yield[
MethodType::LIST_PARTICIPANTS,
[],
false,
null,
];
/*

View File

@@ -76,73 +76,13 @@ class ConnectionConfigurationTest extends BaseTestCase
static::assertFalse($this->configurationAnother->isVerifySslCertificateOn());
}
public function testSetBaseUrl()
public function testGetRemoteControlUrl()
{
$this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum');
static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl());
$this->configurationWithDefaults->setRemoteControlUrl('lorem/ipsum');
static::assertEquals('lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl());
$this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum/');
static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl());
}
public function testSetRemoteControlUrl()
{
$this->configurationWithDefaults->setRemoteControlUrl('/lorem/ipsum');
static::assertEquals('/lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl());
}
public function testSetUsername()
{
$this->configurationWithDefaults->setUsername('lorem');
static::assertEquals('lorem', $this->configurationWithDefaults->getUsername());
}
public function testSetPassword()
{
$this->configurationWithDefaults->setPassword('ipsum');
static::assertEquals('ipsum', $this->configurationWithDefaults->getPassword());
}
public function testSetDebugMode()
{
$this->configurationWithDefaults->setDebugMode();
$this->configurationAnother->setDebugMode();
static::assertFalse($this->configurationWithDefaults->isDebugModeOn());
static::assertFalse($this->configurationAnother->isDebugModeOn());
$this->configurationWithDefaults->setDebugMode(false);
$this->configurationAnother->setDebugMode(false);
static::assertFalse($this->configurationWithDefaults->isDebugModeOn());
static::assertFalse($this->configurationAnother->isDebugModeOn());
$this->configurationWithDefaults->setDebugMode(true);
$this->configurationAnother->setDebugMode(true);
static::assertTrue($this->configurationWithDefaults->isDebugModeOn());
static::assertTrue($this->configurationAnother->isDebugModeOn());
}
public function testSetVerifySslCertificate()
{
$this->configurationWithDefaults->setVerifySslCertificate();
$this->configurationAnother->setVerifySslCertificate();
static::assertTrue($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertTrue($this->configurationAnother->isVerifySslCertificateOn());
$this->configurationWithDefaults->setVerifySslCertificate(false);
$this->configurationAnother->setVerifySslCertificate(false);
static::assertFalse($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertFalse($this->configurationAnother->isVerifySslCertificateOn());
$this->configurationWithDefaults->setVerifySslCertificate(true);
$this->configurationAnother->setVerifySslCertificate(true);
static::assertTrue($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertTrue($this->configurationAnother->isVerifySslCertificateOn());
$this->configurationAnother->setRemoteControlUrl('dolor/sit');
static::assertEquals('dolor/sit', $this->configurationAnother->getRemoteControlUrl());
}
public function testGetFullUrl()
@@ -195,6 +135,6 @@ class ConnectionConfigurationTest extends BaseTestCase
parent::setUp();
$this->configurationWithDefaults = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
$this->configurationAnother = new ConnectionConfiguration('http://lets-test.com', 'test11', 'test22', true, false);
$this->configurationAnother = new ConnectionConfiguration('http://lets-test.com/', 'test11', 'test22', true, false);
}
}

View File

@@ -0,0 +1,58 @@
<?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 Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
use Meritoo\LimeSurvey\ApiClient\Exception\IncorrectClassOfResultItemException;
use stdClass;
/**
* Test case of an exception used while class used to create instance of one item of the result is incorrect
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class IncorrectClassOfResultItemExceptionTest extends BaseTestCase
{
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(IncorrectClassOfResultItemException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
}
/**
* @param string $className Incorrect class name used to create instance of one item
* @param string $expectedMessage Expected exception's message
*
* @dataProvider provideIncorrectClassName
*/
public function testConstructorMessage($className, $expectedMessage)
{
$exception = new IncorrectClassOfResultItemException($className);
static::assertEquals($expectedMessage, $exception->getMessage());
}
/**
* Provides incorrect class name used to create instance of one item
*
* @return Generator
*/
public function provideIncorrectClassName()
{
$template = 'Class %s used to create instance of one item of the result should extend %s, but it does not. Did'
. ' you forget to use proper base class?';
yield[
stdClass::class,
sprintf($template, stdClass::class, BaseItem::class),
];
}
}

View File

@@ -0,0 +1,57 @@
<?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 Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
/**
* Test case of an exception used when participant of survey is missing
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MissingParticipantOfSurveyExceptionTest extends BaseTestCase
{
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(MissingParticipantOfSurveyException::class, OopVisibilityType::IS_PUBLIC, 2, 2);
}
/**
* @param int $surveyId ID of survey
* @param string $email E-mail address of the participant
* @param string $expectedMessage Expected exception's message
*
* @dataProvider provideSurveyIdAndEmail
*/
public function testConstructorMessage($surveyId, $email, $expectedMessage)
{
$exception = new MissingParticipantOfSurveyException($surveyId, $email);
static::assertEquals($expectedMessage, $exception->getMessage());
}
public function provideSurveyIdAndEmail()
{
$template = 'Participant with e-mail %s of survey with ID %s is missing. Maybe was not added to the survey?';
yield[
1,
'lorem@ipsum.com',
sprintf($template, 'lorem@ipsum.com', 1),
];
yield[
1234,
'another@email.comm',
sprintf($template, 'another@email.comm', 1234),
];
}
}

View File

@@ -49,17 +49,17 @@ class UnknownInstanceOfResultItemTest extends BaseTestCase
*/
public function provideMethodName()
{
$template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper'
. ' class is not mapped in %s::%s() method. Did you forget about this?';
$template = 'Class name used to create instance of one item used by result the of \'%s\' LimeSurvey API\'s'
. ' method is unknown. Proper class is not mapped in %s::%s() method. Did you forget about this?';
yield[
MethodType::LIST_SURVEYS,
sprintf($template, MethodType::LIST_SURVEYS, ResultProcessor::class, 'getItemInstance'),
sprintf($template, MethodType::LIST_SURVEYS, ResultProcessor::class, 'getItemClassName'),
];
yield[
MethodType::ADD_PARTICIPANTS,
sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemInstance'),
sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemClassName'),
];
}
}

View File

@@ -1,186 +0,0 @@
<?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\Service;
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\Manager\JsonRpcClientManager;
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
use PHPUnit_Framework_MockObject_MockObject;
/**
* Test case of the service that serves participants
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ParticipantServiceTest extends BaseTestCase
{
/**
* Serves participants.
* Service without participants.
*
* @var ParticipantService
*/
private $serviceWithoutParticipants;
/**
* Serves participants.
* Service with participants.
*
* @var ParticipantService
*/
private $serviceWithParticipants;
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(ParticipantService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
}
public function testGetSurveyParticipantsFromEmptyParticipants()
{
$rpcClientManager = $this->getJsonRpcClientManager(3);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(1));
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(2));
static::assertCount(2, $this->serviceWithParticipants->getSurveyParticipants(1));
static::assertCount(1, $this->serviceWithParticipants->getSurveyParticipants(2));
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
}
public function testHasParticipant()
{
$rpcClientManager = $this->getJsonRpcClientManager(3);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
static::assertTrue($this->serviceWithParticipants->hasParticipant(1, 'john@scott.com'));
static::assertFalse($this->serviceWithParticipants->hasParticipant(2, 'john@scott.com'));
static::assertFalse($this->serviceWithParticipants->hasParticipant(3, 'john@scott.com'));
}
public function testAddParticipant()
{
$firstName = 'John';
$lastName = 'Scott';
$email = 'john@scott.com';
$runMethodCallCount = 1;
$runMethodCallResults = [
[
'firstname' => $firstName,
'lastname' => $lastName,
'email' => $email,
],
];
$rpcClientManager = $this->getJsonRpcClientManager($runMethodCallCount, $runMethodCallResults);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$result = $this->serviceWithoutParticipants->addParticipant(1, $firstName, $lastName, $email);
static::assertInstanceOf(Participant::class, $result);
}
/**
* Returns configuration used while connecting to LimeSurvey's API
*
* @return ConnectionConfiguration
*/
private function getConnectionConfiguration()
{
return new ConnectionConfiguration('http://test.com', 'test', 'test');
}
/**
* Returns manager of session started while connecting to LimeSurvey's API
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getSessionManager()
{
return $this->createMock(SessionManager::class);
}
/**
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @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)
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
$rpcClientManager
->expects(static::exactly($runMethodCallCount))
->method('runMethod')
->will(static::returnValue($runMethodCallResults));
return $rpcClientManager;
}
/**
* Creates instance of the tested service without participants
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithoutParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$this->serviceWithoutParticipants = new ParticipantService($client);
}
/**
* Creates instance of the tested service with participants
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$allParticipants = new Participants([
1 => new Collection([
(new Participant())->setValues([
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
]),
new Participant(),
]),
2 => new Collection([
new Participant(),
]),
]);
$this->serviceWithParticipants = new ParticipantService($client, $allParticipants);
}
}

View File

@@ -1,155 +0,0 @@
<?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\Service;
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\Manager\JsonRpcClientManager;
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
use PHPUnit_Framework_MockObject_MockObject;
/**
* Test case of the service that serves surveys
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SurveyServiceTest extends BaseTestCase
{
/**
* Serves surveys.
* Service without surveys.
*
* @var SurveyService
*/
private $serviceWithoutSurveys;
/**
* Serves surveys.
* Service with surveys.
*
* @var SurveyService
*/
private $serviceWithSurveys;
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(SurveyService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
}
public function testGetAllSurveys()
{
$rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys());
static::assertCount(2, $this->serviceWithSurveys->getAllSurveys());
}
public function testIsExistingSurvey()
{
$rpcClientManager = $this->getJsonRpcClientManager(2);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1));
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2));
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1));
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2));
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(3));
}
/**
* Returns configuration used while connecting to LimeSurvey's API
*
* @return ConnectionConfiguration
*/
private function getConnectionConfiguration()
{
return new ConnectionConfiguration('http://test.com', 'test', 'test');
}
/**
* Returns manager of session started while connecting to LimeSurvey's API
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getSessionManager()
{
return $this->createMock(SessionManager::class);
}
/**
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @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)
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
$rpcClientManager
->expects(static::exactly($runMethodCallCount))
->method('runMethod')
->will(static::returnValue($runMethodCallResults));
return $rpcClientManager;
}
/**
* Creates instance of the tested service without surveys
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithoutSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$this->serviceWithoutSurveys = new SurveyService($client);
}
/**
* Creates instance of the tested service with surveys
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$allSurveys = new Collection([
(new Survey())->setValues([
'sid' => 1,
'surveyls_title' => 'Test',
]),
(new Survey())->setValues([
'sid' => 2,
'surveyls_title' => 'Another Test',
]),
]);
$this->serviceWithSurveys = new SurveyService($client, $allSurveys);
}
}

View File

@@ -14,9 +14,10 @@ use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
/**
* Test case of the participants of survey
* Test case of the collection of participants (of surveys)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
@@ -24,21 +25,21 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
class ParticipantsTest extends BaseTestCase
{
/**
* An empty collection of participants
* An empty collection of participants (of surveys)
*
* @var Participants
*/
private $participantsEmpty;
/**
* Participants of 1 survey
* Collection of participants of 1 survey
*
* @var Participants
*/
private $participantsOfOneSurvey;
/**
* Participants of more than 1 survey
* Collection of participants of more than 1 survey
*
* @var Participants
*/
@@ -61,7 +62,13 @@ class ParticipantsTest extends BaseTestCase
(new Participants())->addMultiple([]);
}
public function testAddParticipantsWithoutParticipants()
public function testHas()
{
$this->expectException(DisabledMethodException::class);
(new Participants())->has(new Participant());
}
public function testAddParticipantsUsingEmptyCollection()
{
$surveyId = 1;
@@ -115,6 +122,55 @@ class ParticipantsTest extends BaseTestCase
static::assertCount(2, $this->participantsOfOneSurvey->getBySurvey($surveyId));
}
public function testAddParticipantFirstParticipant()
{
$surveyId = 1;
$email = 'john@scott.com';
$participant = new ParticipantShort([
'tid' => 1,
'participant_info' => [
'firstname' => 'John',
'lastname' => 'Scott',
'email' => $email,
],
]);
$participants = new Participants();
$result = $participants->addParticipant($participant, $surveyId);
static::assertEquals($participants, $result);
static::assertEquals($participant, $participants->getParticipantOfSurvey($surveyId, $email));
static::assertTrue($participants->hasParticipantsOfSurvey($surveyId));
static::assertFalse($participants->hasParticipantsOfSurvey(2));
}
public function testAddParticipantNotFirstParticipant()
{
$surveyId = 1;
$email = 'john@scott.com';
$participant = new ParticipantShort([
'tid' => 1,
'participant_info' => [
'firstname' => 'John',
'lastname' => 'Scott',
'email' => $email,
],
]);
$result = $this
->participantsOfOneSurvey
->addParticipant($participant, $surveyId);
static::assertEquals($this->participantsOfOneSurvey, $result);
static::assertEquals($participant, $this->participantsOfOneSurvey->getParticipantOfSurvey($surveyId, $email));
static::assertTrue($this->participantsOfOneSurvey->hasParticipantsOfSurvey($surveyId));
static::assertFalse($this->participantsOfOneSurvey->hasParticipantsOfSurvey(2));
}
/**
* {@inheritdoc}
*/

View File

@@ -0,0 +1,108 @@
<?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\Result\Collection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
/**
* Test case of the collection of surveys (the Survey class instances)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SurveysTest extends BaseTestCase
{
/**
* An empty collection of surveys
*
* @var Surveys
*/
private $surveysEmpty;
/**
* Not empty collection of surveys
*
* @var Surveys
*/
private $surveysNotEmpty;
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(Surveys::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testAddWithoutIndex()
{
$survey1 = new Survey([
'sid' => 3,
'surveyls_title' => 'Test Test Test',
]);
$survey2 = new Survey([
'sid' => 4,
'surveyls_title' => 'Another Test Test Test',
]);
$this
->surveysEmpty
->add($survey1)
->add($survey2);
$this
->surveysNotEmpty
->add($survey1)
->add($survey2);
static::assertEquals($survey1, $this->surveysEmpty[3]);
static::assertEquals($survey2, $this->surveysEmpty[4]);
static::assertEquals($survey1, $this->surveysNotEmpty[3]);
static::assertEquals($survey2, $this->surveysNotEmpty[4]);
}
public function testGetAll()
{
static::assertCount(0, $this->surveysEmpty->getAll());
static::assertCount(0, $this->surveysEmpty->getAll(true));
static::assertCount(3, $this->surveysNotEmpty->getAll());
static::assertCount(2, $this->surveysNotEmpty->getAll(true));
}
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$surveys = [
new Survey([
'sid' => 1,
'surveyls_title' => 'Test',
'active' => 'Y',
]),
new Survey([
'sid' => 2,
'surveyls_title' => 'Another Test',
'active' => 'Y',
]),
new Survey([
'sid' => 3,
'surveyls_title' => 'I am inactive',
]),
];
$this->surveysEmpty = new Surveys();
$this->surveysNotEmpty = new Surveys($surveys);
}
}

View File

@@ -9,6 +9,8 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +46,7 @@ class ParticipantShortTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(ParticipantShort::class);
static::assertConstructorVisibilityAndArguments(ParticipantShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheParticipant()
@@ -79,6 +81,52 @@ class ParticipantShortTest extends BaseTestCase
static::assertEquals('dolor@sit.com', $this->participant2ndInstance->getEmail());
}
public function testFromParticipantUsingEmptyParticipant()
{
$participant = new Participant();
$participantShort = ParticipantShort::fromParticipant($participant);
static::assertEquals(0, $participantShort->getId());
static::assertEquals('', $participantShort->getFirstName());
static::assertEquals('', $participantShort->getLastName());
static::assertEquals('', $participantShort->getEmail());
static::assertEquals($participant->getId(), $participantShort->getId());
static::assertEquals($participant->getFirstName(), $participantShort->getFirstName());
static::assertEquals($participant->getLastName(), $participantShort->getLastName());
static::assertEquals($participant->getEmail(), $participantShort->getEmail());
}
public function testFromParticipant()
{
$participant1 = new Participant([
'tid' => $this->rawData[0]['tid'],
'firstname' => $this->rawData[0]['participant_info']['firstname'],
'lastname' => $this->rawData[0]['participant_info']['lastname'],
'email' => $this->rawData[0]['participant_info']['email'],
]);
$participant2 = new Participant([
'tid' => $this->rawData[1]['tid'],
'firstname' => $this->rawData[1]['participant_info']['firstname'],
'lastname' => $this->rawData[1]['participant_info']['lastname'],
'email' => $this->rawData[1]['participant_info']['email'],
]);
$participantShort1 = ParticipantShort::fromParticipant($participant1);
$participantShort2 = ParticipantShort::fromParticipant($participant2);
static::assertEquals($participant1->getId(), $participantShort1->getId());
static::assertEquals($participant1->getFirstName(), $participantShort1->getFirstName());
static::assertEquals($participant1->getLastName(), $participantShort1->getLastName());
static::assertEquals($participant1->getEmail(), $participantShort1->getEmail());
static::assertEquals($participant2->getId(), $participantShort2->getId());
static::assertEquals($participant2->getFirstName(), $participantShort2->getFirstName());
static::assertEquals($participant2->getLastName(), $participantShort2->getLastName());
static::assertEquals($participant2->getEmail(), $participantShort2->getEmail());
}
/**
* Returns raw data of participants
*
@@ -89,7 +137,6 @@ class ParticipantShortTest extends BaseTestCase
return [
[
'tid' => '123',
'token' => uniqid(),
'participant_info' => [
'firstname' => 'Lorem',
'lastname' => 'Ipsum',
@@ -98,7 +145,6 @@ class ParticipantShortTest extends BaseTestCase
],
[
'tid' => '456',
'token' => uniqid(),
'participant_info' => [
'firstname' => 'Dolor',
'lastname' => 'Sit',
@@ -116,7 +162,7 @@ class ParticipantShortTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getParticipantsRawData();
$this->participant1stInstance = (new ParticipantShort())->setValues($this->rawData[0]);
$this->participant2ndInstance = (new ParticipantShort())->setValues($this->rawData[1]);
$this->participant1stInstance = new ParticipantShort($this->rawData[0]);
$this->participant2ndInstance = new ParticipantShort($this->rawData[1]);
}
}

View File

@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use DateTime;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -45,7 +46,7 @@ class ParticipantTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Participant::class);
static::assertConstructorVisibilityAndArguments(Participant::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheParticipant()
@@ -215,7 +216,7 @@ class ParticipantTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getParticipantsRawData();
$this->participant1stInstance = (new Participant())->setValues($this->rawData[0]);
$this->participant2ndInstance = (new Participant())->setValues($this->rawData[1]);
$this->participant1stInstance = new Participant($this->rawData[0]);
$this->participant2ndInstance = new Participant($this->rawData[1]);
}
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\QuestionShort;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +45,7 @@ class QuestionShortTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(QuestionShort::class);
static::assertConstructorVisibilityAndArguments(QuestionShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheQuestionShort()
@@ -222,7 +223,7 @@ class QuestionShortTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getQuestionsRawData();
$this->question1stInstance = (new QuestionShort())->setValues($this->rawData[0]);
$this->question2ndInstance = (new QuestionShort())->setValues($this->rawData[1]);
$this->question1stInstance = new QuestionShort($this->rawData[0]);
$this->question2ndInstance = new QuestionShort($this->rawData[1]);
}
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Question;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +45,7 @@ class QuestionTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Question::class);
static::assertConstructorVisibilityAndArguments(Question::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheQuestionShort()
@@ -284,7 +285,7 @@ class QuestionTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getQuestionsRawData();
$this->question1stInstance = (new Question())->setValues($this->rawData[0]);
$this->question2ndInstance = (new Question())->setValues($this->rawData[1]);
$this->question1stInstance = new Question($this->rawData[0]);
$this->question2ndInstance = new Question($this->rawData[1]);
}
}

View File

@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use DateTime;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -45,7 +46,7 @@ class SurveyTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Survey::class);
static::assertConstructorVisibilityAndArguments(Survey::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheSurvey()
@@ -119,7 +120,7 @@ class SurveyTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getSurveysRawData();
$this->survey1stInstance = (new Survey())->setValues($this->rawData[0]);
$this->survey2ndInstance = (new Survey())->setValues($this->rawData[1]);
$this->survey1stInstance = new Survey($this->rawData[0]);
$this->survey2ndInstance = new Survey($this->rawData[1]);
}
}

View File

@@ -75,9 +75,9 @@ class ResultProcessorTest extends BaseTestCase
static::assertInstanceOf(BaseItem::class, $processed);
}
public function testGetItemInstanceVisibilityAndArguments()
public function testGetItemClassNameVisibilityAndArguments()
{
static::assertMethodVisibilityAndArguments(ResultProcessor::class, 'getItemInstance', OopVisibilityType::IS_PRIVATE, 1, 1);
static::assertMethodVisibilityAndArguments(ResultProcessor::class, 'getItemClassName', OopVisibilityType::IS_PRIVATE, 1, 1);
}
public function testRunWithUnknownResultClass()

View File

@@ -0,0 +1,377 @@
<?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\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\Exception\MissingParticipantOfSurveyException;
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
use PHPUnit_Framework_MockObject_MockObject;
/**
* Test case of the service that serves participants
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ParticipantServiceTest extends BaseTestCase
{
/**
* Service that serves participants.
* Without participants.
*
* @var ParticipantService
*/
private $serviceWithoutParticipants;
/**
* Service that serves participants.
* With participants.
*
* @var ParticipantService
*/
private $serviceWithParticipants;
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(ParticipantService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
}
public function testGetClient()
{
$rpcClientManager = $this->getJsonRpcClientManager(0);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertInstanceOf(Client::class, $this->serviceWithoutParticipants->getClient());
static::assertInstanceOf(Client::class, $this->serviceWithParticipants->getClient());
$connectionConfiguration = $this->getConnectionConfiguration();
$client = new Client($connectionConfiguration);
$participantService = new ParticipantService($client);
static::assertEquals($client, $participantService->getClient());
}
public function testGetSurveyParticipants()
{
$rpcClientManager = $this->getJsonRpcClientManager(3);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(1));
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(2));
static::assertCount(2, $this->serviceWithParticipants->getSurveyParticipants(1));
static::assertCount(1, $this->serviceWithParticipants->getSurveyParticipants(2));
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()
{
$rpcClientManager = $this->getJsonRpcClientManager(3);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
static::assertTrue($this->serviceWithParticipants->hasParticipant(1, 'john@scott.com'));
static::assertFalse($this->serviceWithParticipants->hasParticipant(2, 'john@scott.com'));
static::assertFalse($this->serviceWithParticipants->hasParticipant(3, 'john@scott.com'));
}
public function testAddParticipantForNotExistingSurvey()
{
$this->expectException(CannotProcessDataException::class);
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
$surveyId = 1;
$firstName = 'John';
$lastName = 'Scott';
$email = 'john@scott.com';
$this->serviceWithoutParticipants->addParticipant($surveyId, $firstName, $lastName, $email);
$this->serviceWithParticipants->addParticipant($surveyId, $firstName, $lastName, $email);
}
public function testAddParticipant()
{
$surveyId = 1;
$firstName = 'John';
$lastName = 'Scott';
$email = 'john@scott.com';
$runMethodCallCount = 1;
$runMethodCallResults = [
[
'firstname' => $firstName,
'lastname' => $lastName,
'email' => $email,
],
];
$rpcClientManager = $this->getJsonRpcClientManager($runMethodCallCount, $runMethodCallResults);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$result = $this->serviceWithoutParticipants->addParticipant($surveyId, $firstName, $lastName, $email);
static::assertInstanceOf(Participant::class, $result);
static::assertEquals($firstName, $result->getFirstName());
static::assertEquals($lastName, $result->getLastName());
static::assertEquals($email, $result->getEmail());
}
public function testGetParticipant()
{
$rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertNull($this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com'));
$participant = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com');
static::assertInstanceOf(ParticipantShort::class, $participant);
static::assertEquals('John', $participant->getFirstName());
static::assertEquals('Scott', $participant->getLastName());
static::assertEquals('john@scott.com', $participant->getEmail());
}
public function testGetParticipantDetails()
{
$sessionManager = $this->getSessionManager();
$rpcClientManager = $this->getJsonRpcClientManager(1);
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$runMethodCallResults = [
'tid' => 1,
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
'token' => uniqid(),
'sent' => 'N',
'completed' => 'N',
];
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertNull($this->serviceWithoutParticipants->getParticipantDetails(1, 'john@scott.com'));
$participant = $this->serviceWithParticipants->getParticipantDetails(1, 'john@scott.com');
static::assertInstanceOf(Participant::class, $participant);
static::assertEquals($runMethodCallResults['tid'], $participant->getId());
static::assertEquals($runMethodCallResults['firstname'], $participant->getFirstName());
static::assertEquals($runMethodCallResults['lastname'], $participant->getLastName());
static::assertEquals($runMethodCallResults['email'], $participant->getEmail());
static::assertEquals($runMethodCallResults['token'], $participant->getToken());
static::assertFalse($participant->isSent());
static::assertFalse($participant->isCompleted());
static::assertNull($participant->isBlacklisted());
static::assertNull($participant->getValidFrom());
}
public function testHasParticipantFilledSurveyWithException()
{
$this->expectException(MissingParticipantOfSurveyException::class);
$rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->serviceWithoutParticipants->hasParticipantFilledSurvey(1, 'john@scott.com');
}
public function testHasParticipantFilledSurveyUsingExistingParticipant()
{
$runMethodCallResults = [
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
'completed' => 'Y',
];
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
$sessionManager = $this->getSessionManager();
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertTrue($this->serviceWithParticipants->hasParticipantFilledSurvey(1, 'john@scott.com'));
}
public function testHasParticipantFilledSurveyUsingNotExistingParticipant()
{
$this->expectException(MissingParticipantOfSurveyException::class);
$rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager();
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
$this->serviceWithParticipants->hasParticipantFilledSurvey(3, 'mary@jane.com');
}
/**
* Returns configuration used while connecting to LimeSurvey's API
*
* @return ConnectionConfiguration
*/
private function getConnectionConfiguration()
{
return new ConnectionConfiguration('http://test.com', 'test', 'test');
}
/**
* Returns manager of session started while connecting to LimeSurvey's API
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getSessionManager()
{
return $this->createMock(SessionManager::class);
}
/**
* 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)
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
$rpcClientManager
->expects(static::exactly($runMethodCallCount))
->method('runMethod')
->will(static::returnValue($runMethodCallResults));
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
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithoutParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$this->serviceWithoutParticipants = new ParticipantService($client);
}
/**
* Creates instance of the tested service with participants
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$allParticipants = new Participants([
1 => new Collection([
new ParticipantShort([
'tid' => 1,
'participant_info' => [
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
],
]),
new ParticipantShort([
'tid' => 2,
'participant_info' => [
'firstname' => 'Mary',
'lastname' => 'Jane',
'email' => 'mary@jane.com',
],
]),
]),
2 => new Collection([
new ParticipantShort(),
]),
]);
$this->serviceWithParticipants = new ParticipantService($client, $allParticipants);
}
}

View File

@@ -0,0 +1,290 @@
<?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\Service;
use Exception;
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\Collection\Surveys;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
use PHPUnit_Framework_MockObject_MockObject;
/**
* Test case of the service that serves surveys
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SurveyServiceTest extends BaseTestCase
{
/**
* Service that serves surveys.
* Without surveys.
*
* @var SurveyService
*/
private $serviceWithoutSurveys;
/**
* Service that serves surveys.
* With surveys.
*
* @var SurveyService
*/
private $serviceWithSurveys;
/**
* Base url of LimeSurvey's instance.
* Used to prepare configuration of connection.
*
* @var string
*/
private $connectionBaseUrl = 'http://test.com';
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(SurveyService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
}
public function testGetClient()
{
$rpcClientManager = $this->getJsonRpcClientManager(0);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
static::assertInstanceOf(Client::class, $this->serviceWithoutSurveys->getClient());
static::assertInstanceOf(Client::class, $this->serviceWithSurveys->getClient());
$connectionConfiguration = $this->getConnectionConfiguration();
$client = new Client($connectionConfiguration);
$surveyService = new SurveyService($client);
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(2);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
/*
* If there are no surveys, count of all or active only surveys is the same
*/
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys());
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys(true));
/*
* If there are surveys, here we've got difference between count of all or active only surveys
*/
static::assertCount(3, $this->serviceWithSurveys->getAllSurveys());
static::assertCount(2, $this->serviceWithSurveys->getAllSurveys(true));
}
public function testIsExistingSurvey()
{
$rpcClientManager = $this->getJsonRpcClientManager(4);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
/*
* If there are no surveys, verification of existence any survey always return false
*/
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1));
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1, true));
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2));
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2, true));
/*
* If there are surveys, verification of existence active survey always return true
*/
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1));
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1, true));
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2));
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2, true));
/*
* If there are surveys, verification of existence of non-active survey shows difference
*/
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(3));
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(3, true));
/*
* If there are surveys, verification of existence non-existing survey always return false
*/
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4));
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true));
}
public function testGetStartSurveyUrl()
{
$rpcClientManager = $this->getJsonRpcClientManager(0);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
$surveyId = 123;
$token = uniqid();
$expectedUrl = sprintf('%s/%d?token=%s', $this->connectionBaseUrl, $surveyId, $token);
$participant = new Participant([
'tid' => 1,
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
'token' => $token,
]);
static::assertEquals($expectedUrl, $this->serviceWithoutSurveys->getStartSurveyUrl($surveyId, $participant));
static::assertEquals($expectedUrl, $this->serviceWithSurveys->getStartSurveyUrl($surveyId, $participant));
}
/**
* Returns configuration used while connecting to LimeSurvey's API
*
* @return ConnectionConfiguration
*/
private function getConnectionConfiguration()
{
return new ConnectionConfiguration($this->connectionBaseUrl, 'test', 'test');
}
/**
* Returns manager of session started while connecting to LimeSurvey's API
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getSessionManager()
{
return $this->createMock(SessionManager::class);
}
/**
* 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)
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
$rpcClientManager
->expects(static::exactly($runMethodCallCount))
->method('runMethod')
->will(static::returnValue($runMethodCallResults));
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
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithoutSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$this->serviceWithoutSurveys = new SurveyService($client);
}
/**
* Creates instance of the tested service with surveys
*
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
*/
private function createServiceWithSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
{
$configuration = $this->getConnectionConfiguration();
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$allSurveys = new Surveys([
new Survey([
'sid' => 1,
'surveyls_title' => 'Test',
'active' => 'Y',
]),
new Survey([
'sid' => 2,
'surveyls_title' => 'Another Test',
'active' => 'Y',
]),
new Survey([
'sid' => 3,
'surveyls_title' => 'I am inactive',
]),
]);
$this->serviceWithSurveys = new SurveyService($client, $allSurveys);
}
}

View File

@@ -0,0 +1,83 @@
<?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 [
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_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::NOT_EXISTING_SURVEY_ID,
true,
];
yield[
ReasonType::NO_PARTICIPANTS_FOUND,
true,
];
yield[
ReasonType::NO_SURVEYS_FOUND,
true,
];
yield[
ReasonType::NO_TOKEN_TABLE,
true,
];
}
}