mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 02:11:45 +01:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64e0fb3152 | ||
|
|
b58c346e95 | ||
|
|
83ff76776c | ||
|
|
ac72c6bd76 | ||
|
|
bf7392853f |
@@ -3,7 +3,7 @@
|
||||
"description": "Client of LimeSurvey API",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"version": "0.0.9",
|
||||
"version": "0.0.10",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Meritoo",
|
||||
|
||||
81
src/Base/Result/BaseParticipant.php
Normal file
81
src/Base/Result/BaseParticipant.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
||||
|
||||
/**
|
||||
* Base class for participant of survey.
|
||||
* Used as a foundation for short or full participant's data.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseParticipant extends BaseItem
|
||||
{
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
}
|
||||
142
src/Base/Result/BaseParticipantsCollection.php
Normal file
142
src/Base/Result/BaseParticipantsCollection.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
|
||||
/**
|
||||
* Base class for participants' collection
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseParticipantsCollection extends Collection
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipant');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
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 ParticipantShort or Participant instances.
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipants(Collection $participants, $surveyId)
|
||||
{
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->addMultiple($participants);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* If there are no participants of given survey, adds an empty collection who will store participants.
|
||||
* So, this method will return collection always.
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBySurvey($surveyId)
|
||||
{
|
||||
/*
|
||||
* There are no participants of given survey?
|
||||
* Let's add an empty collection who will store participants
|
||||
*/
|
||||
if (!isset($this[$surveyId])) {
|
||||
$this[$surveyId] = new Collection();
|
||||
}
|
||||
|
||||
return $this[$surveyId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if there are participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantsOfSurvey($surveyId)
|
||||
{
|
||||
return false === $this
|
||||
->getBySurvey($surveyId)
|
||||
->isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participant of given survey
|
||||
*
|
||||
* @param BaseParticipant $participant Participant to add
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipant(BaseParticipant $participant, $surveyId)
|
||||
{
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->add($participant);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participant of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return BaseParticipant|null
|
||||
*/
|
||||
public function getParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
$participants = $this->getBySurvey($surveyId);
|
||||
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($participants as $participant) {
|
||||
if ($participant->getEmail() == $participantEmail) {
|
||||
return $participant;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
31
src/Exception/MissingSurveySummaryException.php
Normal file
31
src/Exception/MissingSurveySummaryException.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?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 survey's summary is missing
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MissingSurveySummaryException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
*/
|
||||
public function __construct($surveyId)
|
||||
{
|
||||
$template = 'Summary of survey with ID %d is missing. Does the survey exist?';
|
||||
$message = sprintf($template, $surveyId);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,10 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Collection;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipantsCollection;
|
||||
|
||||
/**
|
||||
* Collection of participants (of surveys).
|
||||
* Collection of participants' short data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* It's a collection of participants' collections.
|
||||
@@ -22,130 +20,6 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Participants extends Collection
|
||||
class Participants extends BaseParticipantsCollection
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipants');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
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 ParticipantShort classes.
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipants(Collection $participants, $surveyId)
|
||||
{
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->addMultiple($participants);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participant of given survey
|
||||
*
|
||||
* @param ParticipantShort $participant Participant to add
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipant(ParticipantShort $participant, $surveyId)
|
||||
{
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->add($participant);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if there are participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantsOfSurvey($surveyId)
|
||||
{
|
||||
return false === $this
|
||||
->getBySurvey($surveyId)
|
||||
->isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* If there are no participants of given survey, adds an empty collection who will store participants.
|
||||
* So, this method will return collection always.
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBySurvey($surveyId)
|
||||
{
|
||||
/*
|
||||
* There are no participants of given survey?
|
||||
* Let's add an empty collection who will store participants
|
||||
*/
|
||||
if (!isset($this[$surveyId])) {
|
||||
$this[$surveyId] = new Collection();
|
||||
}
|
||||
|
||||
return $this[$surveyId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participant of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return ParticipantShort|null
|
||||
*/
|
||||
public function getParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
/* @var Collection $participants */
|
||||
$participants = $this->getBySurvey($surveyId);
|
||||
|
||||
if ($participants->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* @var ParticipantShort $participant */
|
||||
foreach ($participants as $participant) {
|
||||
if ($participant->getEmail() == $participantEmail) {
|
||||
return $participant;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
36
src/Result/Collection/ParticipantsDetails.php
Normal file
36
src/Result/Collection/ParticipantsDetails.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?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\LimeSurvey\ApiClient\Base\Result\BaseParticipantsCollection;
|
||||
|
||||
/**
|
||||
* Collection of participants' full data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* It's a collection of participants' collections.
|
||||
* The survey ID is used as an index per each collection of participants, so they are grouped by survey.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ParticipantsDetails extends BaseParticipantsCollection
|
||||
{
|
||||
/**
|
||||
* Returns information if survey with given ID has participant with given e-mail address
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
return null !== $this->getParticipantOfSurvey($surveyId, $participantEmail);
|
||||
}
|
||||
}
|
||||
116
src/Result/Collection/SurveysSummaries.php
Normal file
116
src/Result/Collection/SurveysSummaries.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?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\Common\Exception\Method\DisabledMethodException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\SurveySummary;
|
||||
|
||||
/**
|
||||
* Collection of surveys' summaries (the SurveySummary class instances)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SurveysSummaries extends Collection
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addSurveySummary');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addSurveysSummaries');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($element)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'hasSurveySummary');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds survey's summary
|
||||
*
|
||||
* @param SurveySummary $summary Survey's summary
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addSurveySummary(SurveySummary $summary, $surveyId)
|
||||
{
|
||||
$this[$surveyId] = $summary;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds surveys' summaries
|
||||
*
|
||||
* @param array $summaries Surveys' summaries to add
|
||||
* @return $this
|
||||
*/
|
||||
public function addSurveysSummaries(array $summaries)
|
||||
{
|
||||
/*
|
||||
* No summaries?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($summaries)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
foreach ($summaries as $surveyId => $summary) {
|
||||
$this->addSurveySummary($summary, $surveyId);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if there is summary of survey with given ID
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSurveySummary($surveyId)
|
||||
{
|
||||
/*
|
||||
* There are no surveys' summaries or there is no summary of survey with given ID?
|
||||
*/
|
||||
if ($this->isEmpty() || !isset($this[$surveyId])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns summary of survey with given ID
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return SurveySummary|null
|
||||
*/
|
||||
public function getSurveySummary($surveyId)
|
||||
{
|
||||
if ($this->hasSurveySummary($surveyId)) {
|
||||
return $this[$surveyId];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Result\Item;
|
||||
|
||||
use DateTime;
|
||||
use Meritoo\Common\Utilities\Date;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipant;
|
||||
|
||||
/**
|
||||
* One item of the result/data: full data of one participant
|
||||
@@ -18,15 +18,8 @@ use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Participant extends BaseItem
|
||||
class Participant extends BaseParticipant
|
||||
{
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* Another ID of the participant?
|
||||
* Don't know where it is used.
|
||||
@@ -42,27 +35,6 @@ class Participant extends BaseItem
|
||||
*/
|
||||
private $mpId;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* Status of the e-mail
|
||||
*
|
||||
@@ -229,16 +201,6 @@ class Participant extends BaseItem
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns another ID of the participant?
|
||||
* Don't know where it is used.
|
||||
@@ -260,36 +222,6 @@ class Participant extends BaseItem
|
||||
return $this->mpId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status of the e-mail
|
||||
*
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Item;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipant;
|
||||
|
||||
/**
|
||||
* One item of the result/data: short data of one participant
|
||||
@@ -16,36 +16,8 @@ use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ParticipantShort extends BaseItem
|
||||
class ParticipantShort extends BaseParticipant
|
||||
{
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -64,46 +36,6 @@ class ParticipantShort extends BaseItem
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns short data of participant created from full data of participant
|
||||
*
|
||||
|
||||
196
src/Result/Item/SurveySummary.php
Normal file
196
src/Result/Item/SurveySummary.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?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\Item;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
|
||||
/**
|
||||
* One item of the result/data: survey's summary (contains aggregated data)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SurveySummary extends BaseItem
|
||||
{
|
||||
/**
|
||||
* Count/Amount of tokens
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $tokenCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of invalid tokens
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $tokenInvalidCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of sent tokens
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $tokenSentCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of opted out tokens
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $tokenOptedOutCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of completed tokens
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $tokenCompletedCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of complete responses
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $completeResponsesCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of incomplete responses
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $incompleteResponsesCount;
|
||||
|
||||
/**
|
||||
* Count/Amount of full responses
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $fullResponsesCount;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($property, $value)
|
||||
{
|
||||
switch ($property) {
|
||||
case 'token_count':
|
||||
$this->tokenCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'token_invalid':
|
||||
$this->tokenInvalidCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'token_sent':
|
||||
$this->tokenSentCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'token_opted_out':
|
||||
$this->tokenOptedOutCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'token_completed':
|
||||
$this->tokenCompletedCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'completed_responses':
|
||||
$this->completeResponsesCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'incomplete_responses':
|
||||
$this->incompleteResponsesCount = (int)$value;
|
||||
break;
|
||||
|
||||
case 'full_responses':
|
||||
$this->fullResponsesCount = (int)$value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of tokens
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTokenCount()
|
||||
{
|
||||
return $this->tokenCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of invalid tokens
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTokenInvalidCount()
|
||||
{
|
||||
return $this->tokenInvalidCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of sent tokens
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTokenSentCount()
|
||||
{
|
||||
return $this->tokenSentCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of opted out tokens
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTokenOptedOutCount()
|
||||
{
|
||||
return $this->tokenOptedOutCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of completed tokens
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTokenCompletedCount()
|
||||
{
|
||||
return $this->tokenCompletedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of complete responses
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCompleteResponsesCount()
|
||||
{
|
||||
return $this->completeResponsesCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of incomplete responses
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getIncompleteResponsesCount()
|
||||
{
|
||||
return $this->incompleteResponsesCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of full responses
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFullResponsesCount()
|
||||
{
|
||||
return $this->fullResponsesCount;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Question;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\QuestionShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\SurveySummary;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
/**
|
||||
@@ -95,6 +96,10 @@ class ResultProcessor
|
||||
$className = Question::class;
|
||||
break;
|
||||
|
||||
case MethodType::GET_SUMMARY:
|
||||
$className = SurveySummary::class;
|
||||
break;
|
||||
|
||||
case MethodType::LIST_PARTICIPANTS:
|
||||
$className = ParticipantShort::class;
|
||||
break;
|
||||
|
||||
@@ -8,13 +8,11 @@
|
||||
|
||||
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\Collection\ParticipantsDetails;
|
||||
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;
|
||||
|
||||
@@ -34,28 +32,30 @@ class ParticipantService
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* Collection of participants (of surveys).
|
||||
* Collection of participants' full data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* @var Participants
|
||||
* @var ParticipantsDetails
|
||||
*/
|
||||
private $allParticipants;
|
||||
private $participantsDetails;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param ParticipantsDetails $participantsDetails (optional) Collection of participants' full data. All
|
||||
* participants grouped per survey.
|
||||
*/
|
||||
public function __construct(Client $client, Participants $allParticipants = null)
|
||||
{
|
||||
if (null === $allParticipants) {
|
||||
$allParticipants = new Participants();
|
||||
public function __construct(
|
||||
Client $client,
|
||||
ParticipantsDetails $participantsDetails = null
|
||||
) {
|
||||
if (null === $participantsDetails) {
|
||||
$participantsDetails = new ParticipantsDetails();
|
||||
}
|
||||
|
||||
$this->client = $client;
|
||||
$this->allParticipants = $allParticipants;
|
||||
$this->participantsDetails = $participantsDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,54 +68,6 @@ class ParticipantService
|
||||
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
|
||||
*
|
||||
@@ -125,100 +77,54 @@ class ParticipantService
|
||||
*/
|
||||
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);
|
||||
return null !== $this->getParticipantDetails($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
|
||||
* Returns full data of participant with given e-mail (participant of given survey)
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $email E-mail address of the participant
|
||||
* @return Participant|null
|
||||
*
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getParticipantDetails($surveyId, $email)
|
||||
{
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
[
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
if (!$this->participantsDetails->hasParticipantOfSurvey($surveyId, $email)) {
|
||||
$participant = null;
|
||||
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
[
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
/* @var Participant $participant */
|
||||
$participant = $this
|
||||
->client
|
||||
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
/*
|
||||
* Oops, something is broken, because the reason is different than "participant was not found"
|
||||
*/
|
||||
if (ReasonType::NO_PARTICIPANT_PROPERTIES !== $exception->getReason()) {
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $participant) {
|
||||
$this->participantsDetails->addParticipant($participant, $surveyId);
|
||||
}
|
||||
}
|
||||
|
||||
$participant = $this
|
||||
->client
|
||||
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||
->getData();
|
||||
->participantsDetails
|
||||
->getParticipantOfSurvey($surveyId, $email);
|
||||
|
||||
/* @var Participant $participant */
|
||||
return $participant;
|
||||
}
|
||||
|
||||
@@ -234,20 +140,9 @@ class ParticipantService
|
||||
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();
|
||||
return true === $this
|
||||
->getParticipantDetails($surveyId, $email)
|
||||
->isCompleted();
|
||||
}
|
||||
|
||||
throw new MissingParticipantOfSurveyException($surveyId, $email);
|
||||
|
||||
@@ -11,14 +11,19 @@ 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\MissingSurveySummaryException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\SurveysSummaries;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\SurveySummary;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||
|
||||
/**
|
||||
* Service that serves surveys
|
||||
* Service that serves surveys and participants of surveys
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
@@ -40,6 +45,21 @@ class SurveyService
|
||||
*/
|
||||
private $allSurveys;
|
||||
|
||||
/**
|
||||
* Collection of participants' short data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* @var Participants
|
||||
*/
|
||||
private $allParticipants;
|
||||
|
||||
/**
|
||||
* Collection of surveys' summaries (the SurveySummary class instances)
|
||||
*
|
||||
* @var SurveysSummaries
|
||||
*/
|
||||
private $surveySummaries;
|
||||
|
||||
/**
|
||||
* Template of the url used to start survey
|
||||
*
|
||||
@@ -56,17 +76,36 @@ class SurveyService
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class instances).
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class
|
||||
* instances).
|
||||
* @param Participants $allParticipants (optional) Collection of participants' short data. All participants
|
||||
* grouped per survey.
|
||||
* @param SurveysSummaries $surveysSummaries (optional) Collection of surveys' summaries (the SurveySummary class
|
||||
* instances)
|
||||
*/
|
||||
public function __construct(Client $client, Surveys $allSurveys = null)
|
||||
{
|
||||
public function __construct(
|
||||
Client $client,
|
||||
Surveys $allSurveys = null,
|
||||
Participants $allParticipants = null,
|
||||
SurveysSummaries $surveysSummaries = null
|
||||
) {
|
||||
if (null === $allSurveys) {
|
||||
$allSurveys = new Surveys();
|
||||
}
|
||||
|
||||
if (null === $allParticipants) {
|
||||
$allParticipants = new Participants();
|
||||
}
|
||||
|
||||
if (null === $surveysSummaries) {
|
||||
$surveysSummaries = new SurveysSummaries();
|
||||
}
|
||||
|
||||
$this->client = $client;
|
||||
$this->allSurveys = $allSurveys;
|
||||
$this->allParticipants = $allParticipants;
|
||||
$this->surveySummaries = $surveysSummaries;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +121,8 @@ class SurveyService
|
||||
/**
|
||||
* Returns all surveys
|
||||
*
|
||||
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
|
||||
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all (default
|
||||
* behaviour).
|
||||
* @return Surveys
|
||||
*
|
||||
* @throws CannotProcessDataException
|
||||
@@ -122,7 +162,7 @@ class SurveyService
|
||||
*
|
||||
* @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.
|
||||
* be returned, even if exists. Otherwise - it doesn't matter (default behaviour).
|
||||
* @return bool
|
||||
*/
|
||||
public function isExistingSurvey($surveyId, $shouldBeActive = false)
|
||||
@@ -165,4 +205,182 @@ class SurveyService
|
||||
|
||||
return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participant->getToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param bool $onlyCompleted (optional) If is set to true, participants who completed survey are returned only.
|
||||
* Otherwise - all (default behaviour).
|
||||
* @return Collection
|
||||
*
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getSurveyParticipants($surveyId, $onlyCompleted = false)
|
||||
{
|
||||
$hasSurvey = $this
|
||||
->allParticipants
|
||||
->hasParticipantsOfSurvey($surveyId);
|
||||
|
||||
if (!$hasSurvey) {
|
||||
$offset = 0;
|
||||
$limit = $this->getSurveyTokenCount($surveyId);
|
||||
$includeUnused = !$onlyCompleted;
|
||||
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
$offset,
|
||||
$limit,
|
||||
$includeUnused,
|
||||
];
|
||||
|
||||
try {
|
||||
$participants = $this
|
||||
->client
|
||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
/*
|
||||
* Oops, something is broken, because the reason is different than "there are no participants"
|
||||
*/
|
||||
if (ReasonType::NO_PARTICIPANTS_FOUND !== $exception->getReason()) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$participants = new Collection();
|
||||
}
|
||||
|
||||
$this
|
||||
->allParticipants
|
||||
->addParticipants($participants, $surveyId);
|
||||
}
|
||||
|
||||
return $this
|
||||
->allParticipants
|
||||
->getBySurvey($surveyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (participant 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);
|
||||
|
||||
$participant = $this
|
||||
->allParticipants
|
||||
->getParticipantOfSurvey($surveyId, $email);
|
||||
|
||||
/* @var ParticipantShort $participant */
|
||||
return $participant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns count/amount of tokens of survey with given ID
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return int
|
||||
*
|
||||
* @throws MissingSurveySummaryException
|
||||
*/
|
||||
public function getSurveyTokenCount($surveyId)
|
||||
{
|
||||
$surveySummary = $this
|
||||
->surveySummaries
|
||||
->getSurveySummary($surveyId);
|
||||
|
||||
/*
|
||||
* Unknown survey's summary?
|
||||
* Let's fetch it
|
||||
*/
|
||||
if (null === $surveySummary) {
|
||||
$surveySummary = $this->getSurveySummary($surveyId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, survey's summary is missing
|
||||
*/
|
||||
if (null === $surveySummary) {
|
||||
throw new MissingSurveySummaryException($surveyId);
|
||||
}
|
||||
|
||||
return $surveySummary->getTokenCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns summary of survey with given ID
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return SurveySummary|null
|
||||
*/
|
||||
private function getSurveySummary($surveyId)
|
||||
{
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
];
|
||||
|
||||
/* @var SurveySummary $surveySummary */
|
||||
$surveySummary = $this
|
||||
->client
|
||||
->run(MethodType::GET_SUMMARY, $arguments)
|
||||
->getData();
|
||||
|
||||
if (null !== $surveySummary) {
|
||||
$this
|
||||
->surveySummaries
|
||||
->addSurveySummary($surveySummary, $surveyId);
|
||||
}
|
||||
|
||||
return $surveySummary;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,13 @@ class MethodType extends BaseType
|
||||
*/
|
||||
const GET_QUESTION_PROPERTIES = 'get_question_properties';
|
||||
|
||||
/**
|
||||
* Get survey summary, regarding token usage and survey participation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const GET_SUMMARY = 'get_summary';
|
||||
|
||||
/**
|
||||
* Return the IDs and properties of token/participants of a survey
|
||||
*
|
||||
|
||||
@@ -26,6 +26,13 @@ class ReasonType extends BaseType
|
||||
*/
|
||||
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no participant's properties/details
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NO_PARTICIPANT_PROPERTIES = 'Error: No results were found based on your attributes.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no surveys
|
||||
*
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Exception;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||
|
||||
60
tests/Exception/MissingSurveySummaryExceptionTest.php
Normal file
60
tests/Exception/MissingSurveySummaryExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\MissingSurveySummaryException;
|
||||
|
||||
/**
|
||||
* Test case of an exception used when survey's summary is missing
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MissingSurveySummaryExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(MissingSurveySummaryException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideSurveyId
|
||||
*/
|
||||
public function testConstructorMessage($surveyId, $expectedMessage)
|
||||
{
|
||||
$exception = new MissingSurveySummaryException($surveyId);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides ID of survey
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideSurveyId()
|
||||
{
|
||||
$template = 'Summary of survey with ID %d is missing. Does the survey exist?';
|
||||
|
||||
yield[
|
||||
1,
|
||||
sprintf($template, 1),
|
||||
];
|
||||
|
||||
yield[
|
||||
'123',
|
||||
sprintf($template, '123'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
|
||||
/**
|
||||
* Test case of the collection of participants (of surveys)
|
||||
* Test case of the collection of participants' short data
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
@@ -25,7 +25,7 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
class ParticipantsTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* An empty collection of participants (of surveys)
|
||||
* An empty collection of participants' short data
|
||||
*
|
||||
* @var Participants
|
||||
*/
|
||||
|
||||
174
tests/Result/Collection/SurveysSummariesTest.php
Normal file
174
tests/Result/Collection/SurveysSummariesTest.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?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 Generator;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\SurveysSummaries;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\SurveySummary;
|
||||
|
||||
/**
|
||||
* Test case of the collection of surveys' summaries (the SurveySummary class instances)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SurveysSummariesTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* Empty collection of surveys' summaries
|
||||
*
|
||||
* @var SurveysSummaries
|
||||
*/
|
||||
private $emptySurveysSummaries;
|
||||
|
||||
/**
|
||||
* Non-empty collection of surveys' summaries
|
||||
*
|
||||
* @var SurveysSummaries
|
||||
*/
|
||||
private $nonEmptySurveysSummaries;
|
||||
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(SurveysSummaries::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||
}
|
||||
|
||||
public function testAdd()
|
||||
{
|
||||
$this->expectException(DisabledMethodException::class);
|
||||
(new SurveysSummaries())->add('');
|
||||
}
|
||||
|
||||
public function testAddMultiple()
|
||||
{
|
||||
$this->expectException(DisabledMethodException::class);
|
||||
(new SurveysSummaries())->addMultiple([]);
|
||||
}
|
||||
|
||||
public function testHas()
|
||||
{
|
||||
$this->expectException(DisabledMethodException::class);
|
||||
(new SurveysSummaries())->has(new SurveySummary());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $summaries Surveys' summaries to add
|
||||
* @dataProvider provideSurveysSummaries
|
||||
*/
|
||||
public function testAddSurveysSummaries(array $summaries)
|
||||
{
|
||||
$existingSummariesCount = $this->nonEmptySurveysSummaries->count();
|
||||
|
||||
$this->emptySurveysSummaries->addSurveysSummaries($summaries);
|
||||
$this->nonEmptySurveysSummaries->addSurveysSummaries($summaries);
|
||||
|
||||
static::assertCount(count($summaries), $this->emptySurveysSummaries);
|
||||
static::assertCount(count($summaries) + $existingSummariesCount, $this->nonEmptySurveysSummaries);
|
||||
}
|
||||
|
||||
public function testHasSurveySummaryUsingNonExistingSurvey()
|
||||
{
|
||||
static::assertFalse($this->emptySurveysSummaries->hasSurveySummary(1));
|
||||
static::assertFalse($this->emptySurveysSummaries->hasSurveySummary(2));
|
||||
|
||||
static::assertFalse($this->nonEmptySurveysSummaries->hasSurveySummary(3));
|
||||
static::assertFalse($this->nonEmptySurveysSummaries->hasSurveySummary(4));
|
||||
}
|
||||
|
||||
public function testHasSurveySummaryUsingExistingSurvey()
|
||||
{
|
||||
static::assertTrue($this->nonEmptySurveysSummaries->hasSurveySummary(1));
|
||||
static::assertTrue($this->nonEmptySurveysSummaries->hasSurveySummary(2));
|
||||
}
|
||||
|
||||
public function testGetSurveySummaryUsingNonExistingSurvey()
|
||||
{
|
||||
static::assertNull($this->emptySurveysSummaries->getSurveySummary(1));
|
||||
static::assertNull($this->emptySurveysSummaries->getSurveySummary(2));
|
||||
|
||||
static::assertNull($this->nonEmptySurveysSummaries->getSurveySummary(3));
|
||||
static::assertNull($this->nonEmptySurveysSummaries->getSurveySummary(4));
|
||||
}
|
||||
|
||||
public function testGetSurveySummaryUsingExistingSurvey()
|
||||
{
|
||||
$surveySummary1 = $this->nonEmptySurveysSummaries->getSurveySummary(1);
|
||||
$surveySummary2 = $this->nonEmptySurveysSummaries->getSurveySummary(2);
|
||||
|
||||
static::assertInstanceOf(SurveySummary::class, $surveySummary1);
|
||||
static::assertInstanceOf(SurveySummary::class, $surveySummary2);
|
||||
|
||||
static::assertEquals(0, $surveySummary1->getTokenCount());
|
||||
static::assertEquals(5, $surveySummary2->getTokenCount());
|
||||
|
||||
static::assertEquals(0, $surveySummary1->getFullResponsesCount());
|
||||
static::assertEquals(3, $surveySummary2->getFullResponsesCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides surveys' summaries
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideSurveysSummaries()
|
||||
{
|
||||
yield[
|
||||
[],
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
123 => new SurveySummary(),
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
100 => new SurveySummary(),
|
||||
500 => new SurveySummary(),
|
||||
800 => new SurveySummary(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->emptySurveysSummaries = new SurveysSummaries();
|
||||
|
||||
$this->nonEmptySurveysSummaries = new SurveysSummaries([
|
||||
1 => new SurveySummary([
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
]),
|
||||
2 => new SurveySummary([
|
||||
'token_count' => '5',
|
||||
'token_invalid' => '2',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '2',
|
||||
'completed_responses' => '1',
|
||||
'incomplete_responses' => '2',
|
||||
'full_responses' => '3',
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -127,12 +127,24 @@ class ParticipantShortTest extends BaseTestCase
|
||||
static::assertEquals($participant2->getEmail(), $participantShort2->getEmail());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getParticipantsRawData();
|
||||
|
||||
$this->participant1stInstance = new ParticipantShort($this->rawData[0]);
|
||||
$this->participant2ndInstance = new ParticipantShort($this->rawData[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns raw data of participants
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getParticipantsRawData()
|
||||
private static function getParticipantsRawData()
|
||||
{
|
||||
return [
|
||||
[
|
||||
@@ -153,16 +165,4 @@ class ParticipantShortTest extends BaseTestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getParticipantsRawData();
|
||||
|
||||
$this->participant1stInstance = new ParticipantShort($this->rawData[0]);
|
||||
$this->participant2ndInstance = new ParticipantShort($this->rawData[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ 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;
|
||||
use Meritoo\LimeSurvey\Test\ApiClient\Utilities\DateUtility;
|
||||
|
||||
/**
|
||||
* Test case of the one item of the result/data: full data of participant
|
||||
@@ -159,12 +160,24 @@ class ParticipantTest extends BaseTestCase
|
||||
static::assertNull($this->participant2ndInstance->getValidUntil());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getParticipantsRawData();
|
||||
|
||||
$this->participant1stInstance = new Participant($this->rawData[0]);
|
||||
$this->participant2ndInstance = new Participant($this->rawData[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns raw data of participants
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getParticipantsRawData()
|
||||
private static function getParticipantsRawData()
|
||||
{
|
||||
return [
|
||||
[
|
||||
@@ -184,7 +197,7 @@ class ParticipantTest extends BaseTestCase
|
||||
'completed' => 'N',
|
||||
'usesleft' => 10,
|
||||
'validfrom' => null,
|
||||
'validuntil' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
'validuntil' => DateUtility::getDateTime(),
|
||||
],
|
||||
[
|
||||
'tid' => '456',
|
||||
@@ -200,23 +213,11 @@ class ParticipantTest extends BaseTestCase
|
||||
'sent' => 'Y',
|
||||
'remindersent' => 'N',
|
||||
'remindercount' => 1,
|
||||
'completed' => (new DateTime())->format('Y-m-d H:i'),
|
||||
'completed' => DateUtility::getDateTime(false),
|
||||
'usesleft' => 5,
|
||||
'validfrom' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
'validfrom' => DateUtility::getDateTime(),
|
||||
'validuntil' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getParticipantsRawData();
|
||||
|
||||
$this->participant1stInstance = new Participant($this->rawData[0]);
|
||||
$this->participant2ndInstance = new Participant($this->rawData[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,12 +158,24 @@ class QuestionShortTest extends BaseTestCase
|
||||
static::assertEquals('HR', $this->question2ndInstance->getModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getQuestionsRawData();
|
||||
|
||||
$this->question1stInstance = new QuestionShort($this->rawData[0]);
|
||||
$this->question2ndInstance = new QuestionShort($this->rawData[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns raw data of questions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getQuestionsRawData()
|
||||
private static function getQuestionsRawData()
|
||||
{
|
||||
return [
|
||||
[
|
||||
@@ -214,16 +226,4 @@ class QuestionShortTest extends BaseTestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getQuestionsRawData();
|
||||
|
||||
$this->question1stInstance = new QuestionShort($this->rawData[0]);
|
||||
$this->question2ndInstance = new QuestionShort($this->rawData[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,12 +194,24 @@ class QuestionTest extends BaseTestCase
|
||||
static::assertNull($this->question2ndInstance->getDefaultValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getQuestionsRawData();
|
||||
|
||||
$this->question1stInstance = new Question($this->rawData[0]);
|
||||
$this->question2ndInstance = new Question($this->rawData[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns raw data of questions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getQuestionsRawData()
|
||||
private static function getQuestionsRawData()
|
||||
{
|
||||
return [
|
||||
[
|
||||
@@ -276,16 +288,4 @@ class QuestionTest extends BaseTestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->rawData = static::getQuestionsRawData();
|
||||
|
||||
$this->question1stInstance = new Question($this->rawData[0]);
|
||||
$this->question2ndInstance = new Question($this->rawData[1]);
|
||||
}
|
||||
}
|
||||
|
||||
97
tests/Result/Item/SurveySummaryTest.php
Normal file
97
tests/Result/Item/SurveySummaryTest.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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\Item;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\SurveySummary;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
/**
|
||||
* Test case of the one item of the result/data: survey's summary (contains aggregated data)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SurveySummaryTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(SurveySummary::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $rawData Raw data of survey's summary
|
||||
* @dataProvider provideRawData
|
||||
*/
|
||||
public function testCreateOfTheSurveySummary($rawData)
|
||||
{
|
||||
$processor = new ResultProcessor();
|
||||
$processed = $processor->process(MethodType::GET_SUMMARY, $rawData);
|
||||
|
||||
/* @var SurveySummary $processed */
|
||||
static::assertEquals($rawData['token_count'], $processed->getTokenCount());
|
||||
static::assertEquals($rawData['token_invalid'], $processed->getTokenInvalidCount());
|
||||
static::assertEquals($rawData['token_sent'], $processed->getTokenSentCount());
|
||||
static::assertEquals($rawData['token_opted_out'], $processed->getTokenOptedOutCount());
|
||||
static::assertEquals($rawData['token_completed'], $processed->getTokenCompletedCount());
|
||||
static::assertEquals($rawData['completed_responses'], $processed->getCompleteResponsesCount());
|
||||
static::assertEquals($rawData['incomplete_responses'], $processed->getIncompleteResponsesCount());
|
||||
static::assertEquals($rawData['full_responses'], $processed->getFullResponsesCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides raw data of survey's summary
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideRawData()
|
||||
{
|
||||
yield[
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
'token_count' => '28',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '5',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '6',
|
||||
'completed_responses' => '6',
|
||||
'incomplete_responses' => '10',
|
||||
'full_responses' => '16',
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
'token_count' => '28',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '2',
|
||||
'completed_responses' => '2',
|
||||
'incomplete_responses' => '12',
|
||||
'full_responses' => '14',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ 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;
|
||||
use Meritoo\LimeSurvey\Test\ApiClient\Utilities\DateUtility;
|
||||
|
||||
/**
|
||||
* Test case of the one item of the result/data: survey
|
||||
@@ -99,13 +100,13 @@ class SurveyTest extends BaseTestCase
|
||||
'sid' => '123',
|
||||
'surveyls_title' => 'Test',
|
||||
'startdate' => null,
|
||||
'expires' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
'expires' => DateUtility::getDateTime(),
|
||||
'active' => 'N',
|
||||
],
|
||||
[
|
||||
'sid' => '456',
|
||||
'surveyls_title' => 'Another Test',
|
||||
'startdate' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
'startdate' => DateUtility::getDateTime(),
|
||||
'expires' => null,
|
||||
'active' => 'Y',
|
||||
],
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Result;
|
||||
|
||||
use DateTime;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Result;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
use Meritoo\LimeSurvey\Test\ApiClient\Utilities\DateUtility;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
/**
|
||||
@@ -179,7 +179,7 @@ class ResultTest extends BaseTestCase
|
||||
[
|
||||
'sid' => '456',
|
||||
'surveyls_title' => 'Another Test',
|
||||
'startdate' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
'startdate' => DateUtility::getDateTime(),
|
||||
'expires' => null,
|
||||
'active' => 'Y',
|
||||
],
|
||||
|
||||
@@ -18,11 +18,11 @@ 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\Collection\ParticipantsDetails;
|
||||
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 Meritoo\LimeSurvey\Test\ApiClient\Utilities\DateUtility;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
/**
|
||||
@@ -33,6 +33,13 @@ use PHPUnit_Framework_MockObject_MockObject;
|
||||
*/
|
||||
class ParticipantServiceTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* Raw data of participants
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $participantsRawData;
|
||||
|
||||
/**
|
||||
* Service that serves participants.
|
||||
* Without participants.
|
||||
@@ -72,124 +79,48 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
static::assertEquals($client, $participantService->getClient());
|
||||
}
|
||||
|
||||
public function testGetSurveyParticipants()
|
||||
public function testHasParticipantUsingServiceWithoutParticipants()
|
||||
{
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2);
|
||||
$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));
|
||||
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
|
||||
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
|
||||
}
|
||||
|
||||
public function testHasParticipant()
|
||||
{
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
null,
|
||||
],
|
||||
[
|
||||
null,
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2, $runMethodCallResults);
|
||||
$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()
|
||||
public function testGetParticipantDetailsWithException()
|
||||
{
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
|
||||
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$this->expectExceptionMessage($exception->getMessage());
|
||||
|
||||
$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());
|
||||
$this->serviceWithParticipants->getParticipantDetails(1, 'lorem@ipsum.com');
|
||||
}
|
||||
|
||||
public function testGetParticipantDetails()
|
||||
@@ -199,35 +130,33 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
$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);
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(0);
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
static::assertNull($this->serviceWithoutParticipants->getParticipantDetails(1, 'john@scott.com'));
|
||||
$participant = $this->serviceWithParticipants->getParticipantDetails(1, 'john@scott.com');
|
||||
$participant1 = $this->serviceWithoutParticipants->getParticipantDetails(1, 'john@scott.com');
|
||||
$participant2 = $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());
|
||||
$rawData = $this->participantsRawData[0];
|
||||
$id = $rawData['tid'];
|
||||
$firstName = $rawData['firstname'];
|
||||
$lastName = $rawData['lastname'];
|
||||
$email = $rawData['email'];
|
||||
$token = $rawData['token'];
|
||||
|
||||
static::assertNull($participant1);
|
||||
static::assertInstanceOf(Participant::class, $participant2);
|
||||
static::assertEquals($id, $participant2->getId());
|
||||
static::assertEquals($firstName, $participant2->getFirstName());
|
||||
static::assertEquals($lastName, $participant2->getLastName());
|
||||
static::assertEquals($email, $participant2->getEmail());
|
||||
static::assertEquals($token, $participant2->getToken());
|
||||
static::assertTrue($participant2->isSent());
|
||||
static::assertTrue($participant2->isCompleted());
|
||||
static::assertFalse($participant2->isBlacklisted());
|
||||
static::assertNull($participant2->getValidFrom());
|
||||
}
|
||||
|
||||
public function testHasParticipantFilledSurveyWithException()
|
||||
public function testHasParticipantFilledSurveyWithoutParticipants()
|
||||
{
|
||||
$this->expectException(MissingParticipantOfSurveyException::class);
|
||||
|
||||
@@ -240,14 +169,7 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
|
||||
public function testHasParticipantFilledSurveyUsingExistingParticipant()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'completed' => 'Y',
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(0);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
@@ -265,6 +187,55 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
$this->serviceWithParticipants->hasParticipantFilledSurvey(3, 'mary@jane.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->participantsRawData = [
|
||||
[
|
||||
'tid' => 1,
|
||||
'participant_id' => null,
|
||||
'mpid' => null,
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'emailstatus' => 'OK',
|
||||
'token' => uniqid(),
|
||||
'language' => 'pl',
|
||||
'blacklisted' => 'N',
|
||||
'sent' => 'Y',
|
||||
'remindersent' => 'N',
|
||||
'remindercount' => 0,
|
||||
'completed' => DateUtility::getDateTime(),
|
||||
'usesleft' => 10,
|
||||
'validfrom' => null,
|
||||
'validuntil' => DateUtility::getDateTime(),
|
||||
],
|
||||
[
|
||||
'tid' => 2,
|
||||
'participant_id' => null,
|
||||
'mpid' => null,
|
||||
'firstname' => 'Mary',
|
||||
'lastname' => 'Jane',
|
||||
'email' => 'mary@jane.com',
|
||||
'emailstatus' => 'OK',
|
||||
'token' => uniqid(),
|
||||
'language' => 'pl',
|
||||
'blacklisted' => 'N',
|
||||
'sent' => 'Y',
|
||||
'remindersent' => 'N',
|
||||
'remindercount' => 0,
|
||||
'completed' => 'N',
|
||||
'usesleft' => 10,
|
||||
'validfrom' => null,
|
||||
'validuntil' => DateUtility::getDateTime(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns configuration used while connecting to LimeSurvey's API
|
||||
*
|
||||
@@ -296,10 +267,22 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
{
|
||||
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
|
||||
|
||||
$rpcClientManager
|
||||
$mocker = $rpcClientManager
|
||||
->expects(static::exactly($runMethodCallCount))
|
||||
->method('runMethod')
|
||||
->will(static::returnValue($runMethodCallResults));
|
||||
->method('runMethod');
|
||||
|
||||
if (!empty($runMethodCallResults)) {
|
||||
$function = [
|
||||
$mocker,
|
||||
'willReturnOnConsecutiveCalls',
|
||||
];
|
||||
|
||||
/*
|
||||
* I have to use the call_user_func_array() function to pass elements of $runMethodCallResults array as
|
||||
* arguments of the willReturnOnConsecutiveCalls() method
|
||||
*/
|
||||
call_user_func_array($function, $runMethodCallResults);
|
||||
}
|
||||
|
||||
return $rpcClientManager;
|
||||
}
|
||||
@@ -348,30 +331,16 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
$configuration = $this->getConnectionConfiguration();
|
||||
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
||||
|
||||
$allParticipants = new Participants([
|
||||
$participantsDetails = new ParticipantsDetails([
|
||||
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',
|
||||
],
|
||||
]),
|
||||
new Participant($this->participantsRawData[0]),
|
||||
new Participant($this->participantsRawData[1]),
|
||||
]),
|
||||
2 => new Collection([
|
||||
new ParticipantShort(),
|
||||
new Participant(),
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->serviceWithParticipants = new ParticipantService($client, $allParticipants);
|
||||
$this->serviceWithParticipants = new ParticipantService($client, $participantsDetails);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,22 +9,26 @@
|
||||
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\MissingSurveySummaryException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
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
|
||||
* Test case of the service that serves surveys and participants of surveys
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
@@ -32,7 +36,7 @@ use PHPUnit_Framework_MockObject_MockObject;
|
||||
class SurveyServiceTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* Service that serves surveys.
|
||||
* Service that serves surveys and participants of surveys.
|
||||
* Without surveys.
|
||||
*
|
||||
* @var SurveyService
|
||||
@@ -40,13 +44,29 @@ class SurveyServiceTest extends BaseTestCase
|
||||
private $serviceWithoutSurveys;
|
||||
|
||||
/**
|
||||
* Service that serves surveys.
|
||||
* Service that serves surveys and participants of surveys.
|
||||
* With surveys.
|
||||
*
|
||||
* @var SurveyService
|
||||
*/
|
||||
private $serviceWithSurveys;
|
||||
|
||||
/**
|
||||
* Service that serves surveys and participants of surveys.
|
||||
* Without participants.
|
||||
*
|
||||
* @var SurveyService
|
||||
*/
|
||||
private $serviceWithoutParticipants;
|
||||
|
||||
/**
|
||||
* Service that serves surveys and participants of surveys.
|
||||
* With participants.
|
||||
*
|
||||
* @var SurveyService
|
||||
*/
|
||||
private $serviceWithParticipants;
|
||||
|
||||
/**
|
||||
* Base url of LimeSurvey's instance.
|
||||
* Used to prepare configuration of connection.
|
||||
@@ -57,7 +77,7 @@ class SurveyServiceTest extends BaseTestCase
|
||||
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(SurveyService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
|
||||
static::assertConstructorVisibilityAndArguments(SurveyService::class, OopVisibilityType::IS_PUBLIC, 4, 1);
|
||||
}
|
||||
|
||||
public function testGetClient()
|
||||
@@ -78,7 +98,7 @@ class SurveyServiceTest extends BaseTestCase
|
||||
static::assertEquals($client, $surveyService->getClient());
|
||||
}
|
||||
|
||||
public function testGetAllSurveysWithImportantException()
|
||||
public function testGetAllSurveysWithNoTableException()
|
||||
{
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
|
||||
@@ -185,6 +205,264 @@ class SurveyServiceTest extends BaseTestCase
|
||||
static::assertEquals($expectedUrl, $this->serviceWithSurveys->getStartSurveyUrl($surveyId, $participant));
|
||||
}
|
||||
|
||||
public function testGetSurveyParticipantsWithNotExistingSurveyException()
|
||||
{
|
||||
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
|
||||
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$this->expectExceptionMessage($exception->getMessage());
|
||||
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
[
|
||||
'status' => ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2, $runMethodCallResults);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
$this->serviceWithParticipants->getSurveyParticipants(3);
|
||||
}
|
||||
|
||||
public function testGetSurveyParticipantsWithNoParticipantsFoundException()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
[
|
||||
'status' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2, $runMethodCallResults);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
$participants = $this->serviceWithParticipants->getSurveyParticipants(3);
|
||||
|
||||
static::assertInstanceOf(Collection::class, $participants);
|
||||
static::assertCount(0, $participants);
|
||||
}
|
||||
|
||||
public function testGetSurveyParticipants()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
null,
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
null,
|
||||
[
|
||||
'token_count' => '2',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(6, $runMethodCallResults);
|
||||
$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 testGetSurveyParticipantsWithNoTableException()
|
||||
{
|
||||
$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()
|
||||
{
|
||||
$this->expectException(CannotProcessDataException::class);
|
||||
$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 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()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'token_count' => '0',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
null,
|
||||
[
|
||||
[
|
||||
'tid' => 1,
|
||||
'participant_info' => [
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
],
|
||||
],
|
||||
[
|
||||
'tid' => 2,
|
||||
'participant_info' => [
|
||||
'firstname' => 'Mary',
|
||||
'lastname' => 'Jane',
|
||||
'email' => 'mary@jane.com',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'token_count' => '2',
|
||||
'token_invalid' => '0',
|
||||
'token_sent' => '0',
|
||||
'token_opted_out' => '0',
|
||||
'token_completed' => '0',
|
||||
'completed_responses' => '0',
|
||||
'incomplete_responses' => '0',
|
||||
'full_responses' => '0',
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2, $runMethodCallResults);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
|
||||
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
$participant1 = $this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com');
|
||||
$participant2 = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com');
|
||||
|
||||
static::assertNull($participant1);
|
||||
static::assertInstanceOf(ParticipantShort::class, $participant2);
|
||||
static::assertEquals('John', $participant2->getFirstName());
|
||||
static::assertEquals('Scott', $participant2->getLastName());
|
||||
static::assertEquals('john@scott.com', $participant2->getEmail());
|
||||
}
|
||||
|
||||
public function testGetSurveyTokenCountWithException()
|
||||
{
|
||||
$this->expectException(MissingSurveySummaryException::class);
|
||||
|
||||
$runMethodCallResults = [
|
||||
null,
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||
|
||||
$this->serviceWithoutSurveys->getSurveyTokenCount(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns configuration used while connecting to LimeSurvey's API
|
||||
*
|
||||
@@ -216,10 +494,22 @@ class SurveyServiceTest extends BaseTestCase
|
||||
{
|
||||
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
|
||||
|
||||
$rpcClientManager
|
||||
$mocker = $rpcClientManager
|
||||
->expects(static::exactly($runMethodCallCount))
|
||||
->method('runMethod')
|
||||
->will(static::returnValue($runMethodCallResults));
|
||||
->method('runMethod');
|
||||
|
||||
if (!empty($runMethodCallResults)) {
|
||||
$function = [
|
||||
$mocker,
|
||||
'willReturnOnConsecutiveCalls',
|
||||
];
|
||||
|
||||
/*
|
||||
* I have to use the call_user_func_array() function to pass elements of $runMethodCallResults array as
|
||||
* arguments of the willReturnOnConsecutiveCalls() method
|
||||
*/
|
||||
call_user_func_array($function, $runMethodCallResults);
|
||||
}
|
||||
|
||||
return $rpcClientManager;
|
||||
}
|
||||
@@ -287,4 +577,55 @@ class SurveyServiceTest extends BaseTestCase
|
||||
|
||||
$this->serviceWithSurveys = new SurveyService($client, $allSurveys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 SurveyService($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 SurveyService($client, null, $allParticipants);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,7 @@ class MethodTypeTest extends BaseTypeTestCase
|
||||
'EXPORT_STATISTICS' => MethodType::EXPORT_STATISTICS,
|
||||
'GET_PARTICIPANT_PROPERTIES' => MethodType::GET_PARTICIPANT_PROPERTIES,
|
||||
'GET_QUESTION_PROPERTIES' => MethodType::GET_QUESTION_PROPERTIES,
|
||||
'GET_SUMMARY' => MethodType::GET_SUMMARY,
|
||||
'LIST_PARTICIPANTS' => MethodType::LIST_PARTICIPANTS,
|
||||
'LIST_QUESTIONS' => MethodType::LIST_QUESTIONS,
|
||||
'LIST_SURVEYS' => MethodType::LIST_SURVEYS,
|
||||
|
||||
@@ -30,10 +30,11 @@ class ReasonTypeTest extends BaseTypeTestCase
|
||||
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,
|
||||
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||
'NO_PARTICIPANT_PROPERTIES' => ReasonType::NO_PARTICIPANT_PROPERTIES,
|
||||
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
|
||||
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
38
tests/Utilities/DateUtility.php
Normal file
38
tests/Utilities/DateUtility.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Utilities;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Date-related utility
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DateUtility
|
||||
{
|
||||
/**
|
||||
* Returns date formatted with long or medium format
|
||||
*
|
||||
* @param bool $useLongFormat (optional) If is set to true, long format will be used (default behaviour).
|
||||
* Otherwise - medium format.
|
||||
* @return string
|
||||
*/
|
||||
public static function getDateTime($useLongFormat = true)
|
||||
{
|
||||
$format = 'Y-m-d H:i';
|
||||
|
||||
if ($useLongFormat) {
|
||||
$format = 'Y-m-d H:i:s';
|
||||
}
|
||||
|
||||
return (new DateTime())->format($format);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user