Do not fetch all participants of given survey to get information if participant has filled the survey

This commit is contained in:
Meritoo
2017-10-25 11:28:13 +02:00
parent bf7392853f
commit ac72c6bd76
10 changed files with 429 additions and 342 deletions

View 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;
}
}

View 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;
}
}

View File

@@ -8,9 +8,7 @@
namespace Meritoo\LimeSurvey\ApiClient\Result\Collection; namespace Meritoo\LimeSurvey\ApiClient\Result\Collection;
use Meritoo\Common\Collection\Collection; use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipantsCollection;
use Meritoo\Common\Exception\Method\DisabledMethodException;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
/** /**
* Collection of participants' short data. * Collection of participants' short data.
@@ -22,130 +20,6 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl> * @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright 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;
}
} }

View 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);
}
}

View File

@@ -10,7 +10,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Result\Item;
use DateTime; use DateTime;
use Meritoo\Common\Utilities\Date; 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 * 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> * @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright 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? * Another ID of the participant?
* Don't know where it is used. * Don't know where it is used.
@@ -42,27 +35,6 @@ class Participant extends BaseItem
*/ */
private $mpId; 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 * 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? * Returns another ID of the participant?
* Don't know where it is used. * Don't know where it is used.
@@ -260,36 +222,6 @@ class Participant extends BaseItem
return $this->mpId; 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 * Returns status of the e-mail
* *

View File

@@ -8,7 +8,7 @@
namespace Meritoo\LimeSurvey\ApiClient\Result\Item; 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 * 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> * @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright 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} * {@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 * Returns short data of participant created from full data of participant
* *

View File

@@ -13,6 +13,7 @@ use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException; use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException; use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants; use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\ParticipantsDetails;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant; use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort; use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType; use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -41,21 +42,39 @@ class ParticipantService
*/ */
private $allParticipants; private $allParticipants;
/**
* Collection of participants' full data.
* All participants grouped per survey.
*
* @var Participants
*/
private $participantsDetails;
/** /**
* Class constructor * Class constructor
* *
* @param Client $client Client of the LimeSurvey's API * @param Client $client Client of the LimeSurvey's API
* @param Participants $allParticipants (optional) Collection of participants' short data. All participants * @param Participants $allParticipants (optional) Collection of participants' short data. All participants
* grouped per survey. * grouped per survey.
* @param ParticipantsDetails $participantsDetails (optional) Collection of participants' full data. All
* participants grouped per survey.
*/ */
public function __construct(Client $client, Participants $allParticipants = null) public function __construct(
{ Client $client,
Participants $allParticipants = null,
ParticipantsDetails $participantsDetails = null
) {
if (null === $allParticipants) { if (null === $allParticipants) {
$allParticipants = new Participants(); $allParticipants = new Participants();
} }
if (null === $participantsDetails) {
$participantsDetails = new ParticipantsDetails();
}
$this->client = $client; $this->client = $client;
$this->allParticipants = $allParticipants; $this->allParticipants = $allParticipants;
$this->participantsDetails = $participantsDetails;
} }
/** /**
@@ -93,13 +112,10 @@ class ParticipantService
->run(MethodType::LIST_PARTICIPANTS, $arguments) ->run(MethodType::LIST_PARTICIPANTS, $arguments)
->getData(); ->getData();
} catch (CannotProcessDataException $exception) { } catch (CannotProcessDataException $exception) {
$reason = $exception->getReason();
/* /*
* Reason of the exception is different than "Oops, there is no participants. Everything else is fine."? * Oops, something is broken, because the reason is different than "there are no participants"
* Let's throw the exception
*/ */
if (ReasonType::NO_PARTICIPANTS_FOUND !== $reason) { if (ReasonType::NO_PARTICIPANTS_FOUND !== $exception->getReason()) {
throw $exception; throw $exception;
} }
@@ -125,14 +141,7 @@ class ParticipantService
*/ */
public function hasParticipant($surveyId, $email) public function hasParticipant($surveyId, $email)
{ {
/* return null !== $this->getParticipantDetails($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);
} }
/** /**
@@ -192,9 +201,12 @@ class ParticipantService
*/ */
$this->getSurveyParticipants($surveyId); $this->getSurveyParticipants($surveyId);
return $this $participant = $this
->allParticipants ->allParticipants
->getParticipantOfSurvey($surveyId, $email); ->getParticipantOfSurvey($surveyId, $email);
/* @var ParticipantShort $participant */
return $participant;
} }
/** /**
@@ -203,9 +215,14 @@ class ParticipantService
* @param int $surveyId ID of survey * @param int $surveyId ID of survey
* @param string $email E-mail address of the participant * @param string $email E-mail address of the participant
* @return Participant|null * @return Participant|null
*
* @throws CannotProcessDataException
*/ */
public function getParticipantDetails($surveyId, $email) public function getParticipantDetails($surveyId, $email)
{ {
if (!$this->participantsDetails->hasParticipantOfSurvey($surveyId, $email)) {
$participant = null;
$arguments = [ $arguments = [
$surveyId, $surveyId,
[ [
@@ -213,12 +230,30 @@ class ParticipantService
], ],
]; ];
try {
/* @var Participant $participant */
$participant = $this $participant = $this
->client ->client
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments) ->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
->getData(); ->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
->participantsDetails
->getParticipantOfSurvey($surveyId, $email);
/* @var Participant $participant */
return $participant; return $participant;
} }
@@ -234,20 +269,9 @@ class ParticipantService
public function hasParticipantFilledSurvey($surveyId, $email) public function hasParticipantFilledSurvey($surveyId, $email)
{ {
if ($this->hasParticipant($surveyId, $email)) { if ($this->hasParticipant($surveyId, $email)) {
$arguments = [ return true === $this
$surveyId, ->getParticipantDetails($surveyId, $email)
[ ->isCompleted();
'email' => $email,
],
];
/* @var Participant $participant */
$participant = $this
->client
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
->getData();
return true === $participant->isCompleted();
} }
throw new MissingParticipantOfSurveyException($surveyId, $email); throw new MissingParticipantOfSurveyException($surveyId, $email);

View File

@@ -26,6 +26,13 @@ class ReasonType extends BaseType
*/ */
const NO_PARTICIPANTS_FOUND = 'No survey participants found.'; 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 * Reason of exception when there is no surveys
* *

View File

@@ -8,6 +8,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Service; namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
use DateTime;
use Exception; use Exception;
use Meritoo\Common\Collection\Collection; use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
@@ -51,7 +52,7 @@ class ParticipantServiceTest extends BaseTestCase
public function testConstructorVisibilityAndArguments() public function testConstructorVisibilityAndArguments()
{ {
static::assertConstructorVisibilityAndArguments(ParticipantService::class, OopVisibilityType::IS_PUBLIC, 2, 1); static::assertConstructorVisibilityAndArguments(ParticipantService::class, OopVisibilityType::IS_PUBLIC, 3, 1);
} }
public function testGetClient() public function testGetClient()
@@ -111,16 +112,49 @@ class ParticipantServiceTest extends BaseTestCase
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3)); static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
} }
public function testHasParticipant() public function testHasParticipantUsingServiceWithoutParticipants()
{ {
$rpcClientManager = $this->getJsonRpcClientManager(3); $rpcClientManager = $this->getJsonRpcClientManager(2);
$sessionManager = $this->getSessionManager(); $sessionManager = $this->getSessionManager();
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager); $this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com')); static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com')); static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
}
public function testHasParticipant()
{
$runMethodCallResults = [
[
'tid' => '123',
'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' => 'N',
'usesleft' => 10,
'validfrom' => null,
'validuntil' => (new DateTime())->format('Y-m-d H:i:s'),
],
[
null,
],
[
null,
],
];
$rpcClientManager = $this->getJsonRpcClientManager(3, $runMethodCallResults);
$sessionManager = $this->getSessionManager();
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
static::assertTrue($this->serviceWithParticipants->hasParticipant(1, '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(2, 'john@scott.com'));
@@ -156,11 +190,13 @@ class ParticipantServiceTest extends BaseTestCase
$runMethodCallCount = 1; $runMethodCallCount = 1;
$runMethodCallResults = [ $runMethodCallResults = [
[
[ [
'firstname' => $firstName, 'firstname' => $firstName,
'lastname' => $lastName, 'lastname' => $lastName,
'email' => $email, 'email' => $email,
], ],
],
]; ];
$rpcClientManager = $this->getJsonRpcClientManager($runMethodCallCount, $runMethodCallResults); $rpcClientManager = $this->getJsonRpcClientManager($runMethodCallCount, $runMethodCallResults);
@@ -200,14 +236,22 @@ class ParticipantServiceTest extends BaseTestCase
$rpcClientManager = $this->getJsonRpcClientManager(1); $rpcClientManager = $this->getJsonRpcClientManager(1);
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager); $this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
$id = 1;
$firstName = 'John';
$lastName = 'Scott';
$email = 'john@scott.com';
$token = uniqid();
$runMethodCallResults = [ $runMethodCallResults = [
'tid' => 1, [
'firstname' => 'John', 'tid' => $id,
'lastname' => 'Scott', 'firstname' => $firstName,
'email' => 'john@scott.com', 'lastname' => $lastName,
'token' => uniqid(), 'email' => $email,
'token' => $token,
'sent' => 'N', 'sent' => 'N',
'completed' => 'N', 'completed' => 'N',
],
]; ];
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults); $rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
@@ -218,11 +262,11 @@ class ParticipantServiceTest extends BaseTestCase
static::assertNull($participant1); static::assertNull($participant1);
static::assertInstanceOf(Participant::class, $participant2); static::assertInstanceOf(Participant::class, $participant2);
static::assertEquals($runMethodCallResults['tid'], $participant2->getId()); static::assertEquals($id, $participant2->getId());
static::assertEquals($runMethodCallResults['firstname'], $participant2->getFirstName()); static::assertEquals($firstName, $participant2->getFirstName());
static::assertEquals($runMethodCallResults['lastname'], $participant2->getLastName()); static::assertEquals($lastName, $participant2->getLastName());
static::assertEquals($runMethodCallResults['email'], $participant2->getEmail()); static::assertEquals($email, $participant2->getEmail());
static::assertEquals($runMethodCallResults['token'], $participant2->getToken()); static::assertEquals($token, $participant2->getToken());
static::assertFalse($participant2->isSent()); static::assertFalse($participant2->isSent());
static::assertFalse($participant2->isCompleted()); static::assertFalse($participant2->isCompleted());
static::assertNull($participant2->isBlacklisted()); static::assertNull($participant2->isBlacklisted());
@@ -243,10 +287,12 @@ class ParticipantServiceTest extends BaseTestCase
public function testHasParticipantFilledSurveyUsingExistingParticipant() public function testHasParticipantFilledSurveyUsingExistingParticipant()
{ {
$runMethodCallResults = [ $runMethodCallResults = [
[
'firstname' => 'John', 'firstname' => 'John',
'lastname' => 'Scott', 'lastname' => 'Scott',
'email' => 'john@scott.com', 'email' => 'john@scott.com',
'completed' => 'Y', 'completed' => (new DateTime())->format('Y-m-d H:i'),
],
]; ];
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults); $rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
@@ -298,10 +344,22 @@ class ParticipantServiceTest extends BaseTestCase
{ {
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->createMock(JsonRpcClientManager::class);
$rpcClientManager $mocker = $rpcClientManager
->expects(static::exactly($runMethodCallCount)) ->expects(static::exactly($runMethodCallCount))
->method('runMethod') ->method('runMethod');
->will(static::returnValue($runMethodCallResults));
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; return $rpcClientManager;
} }

View File

@@ -32,6 +32,7 @@ class ReasonTypeTest extends BaseTypeTestCase
return [ return [
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID, 'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND, 'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
'NO_PARTICIPANT_PROPERTIES' => ReasonType::NO_PARTICIPANT_PROPERTIES,
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND, 'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE, 'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
]; ];