Participants collection - store instances of ParticipantShort instead of Participant

Related to fetch full data of participant of given survey
This commit is contained in:
Meritoo
2017-10-01 20:46:31 +02:00
parent e43712f218
commit f5334f816f
5 changed files with 95 additions and 13 deletions

View File

@@ -51,7 +51,7 @@ class Participants extends Collection
/** /**
* Adds participants of given survey * Adds participants of given survey
* *
* @param Collection $participants Participants to add. Collection of Participant classes. * @param Collection $participants Participants to add. Collection of ParticipantShort classes.
* @param int $surveyId ID of survey * @param int $surveyId ID of survey
* @return $this * @return $this
*/ */

View File

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

View File

@@ -164,9 +164,16 @@ class ParticipantService
->run(MethodType::ADD_PARTICIPANTS, $arguments) ->run(MethodType::ADD_PARTICIPANTS, $arguments)
->getData(); ->getData();
/* @var Participant $addedParticipant */
$addedParticipant = $participantCollection->getFirst();
$participants = new Collection([
ParticipantShort::fromParticipant($addedParticipant),
]);
$this $this
->allParticipants ->allParticipants
->addParticipants($participantCollection, $surveyId); ->addParticipants($participants, $surveyId);
return $participantCollection->getFirst(); return $participantCollection->getFirst();
} }

View File

@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType; use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort; use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor; use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType; use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -80,6 +81,52 @@ class ParticipantShortTest extends BaseTestCase
static::assertEquals('dolor@sit.com', $this->participant2ndInstance->getEmail()); static::assertEquals('dolor@sit.com', $this->participant2ndInstance->getEmail());
} }
public function testFromParticipantUsingEmptyParticipant()
{
$participant = new Participant();
$participantShort = ParticipantShort::fromParticipant($participant);
static::assertEquals(0, $participantShort->getId());
static::assertEquals('', $participantShort->getFirstName());
static::assertEquals('', $participantShort->getLastName());
static::assertEquals('', $participantShort->getEmail());
static::assertEquals($participant->getId(), $participantShort->getId());
static::assertEquals($participant->getFirstName(), $participantShort->getFirstName());
static::assertEquals($participant->getLastName(), $participantShort->getLastName());
static::assertEquals($participant->getEmail(), $participantShort->getEmail());
}
public function testFromParticipant()
{
$participant1 = new Participant([
'tid' => $this->rawData[0]['tid'],
'firstname' => $this->rawData[0]['participant_info']['firstname'],
'lastname' => $this->rawData[0]['participant_info']['lastname'],
'email' => $this->rawData[0]['participant_info']['email'],
]);
$participant2 = new Participant([
'tid' => $this->rawData[1]['tid'],
'firstname' => $this->rawData[1]['participant_info']['firstname'],
'lastname' => $this->rawData[1]['participant_info']['lastname'],
'email' => $this->rawData[1]['participant_info']['email'],
]);
$participantShort1 = ParticipantShort::fromParticipant($participant1);
$participantShort2 = ParticipantShort::fromParticipant($participant2);
static::assertEquals($participant1->getId(), $participantShort1->getId());
static::assertEquals($participant1->getFirstName(), $participantShort1->getFirstName());
static::assertEquals($participant1->getLastName(), $participantShort1->getLastName());
static::assertEquals($participant1->getEmail(), $participantShort1->getEmail());
static::assertEquals($participant2->getId(), $participantShort2->getId());
static::assertEquals($participant2->getFirstName(), $participantShort2->getFirstName());
static::assertEquals($participant2->getLastName(), $participantShort2->getLastName());
static::assertEquals($participant2->getEmail(), $participantShort2->getEmail());
}
/** /**
* Returns raw data of participants * Returns raw data of participants
* *

View File

@@ -20,6 +20,7 @@ use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager; use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants; use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant; use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService; use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType; use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
@@ -185,7 +186,7 @@ class ParticipantServiceTest extends BaseTestCase
static::assertNull($this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com')); static::assertNull($this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com'));
$participant = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com'); $participant = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com');
static::assertInstanceOf(Participant::class, $participant); static::assertInstanceOf(ParticipantShort::class, $participant);
static::assertEquals('John', $participant->getFirstName()); static::assertEquals('John', $participant->getFirstName());
static::assertEquals('Scott', $participant->getLastName()); static::assertEquals('Scott', $participant->getLastName());
static::assertEquals('john@scott.com', $participant->getEmail()); static::assertEquals('john@scott.com', $participant->getEmail());
@@ -314,20 +315,25 @@ class ParticipantServiceTest extends BaseTestCase
$allParticipants = new Participants([ $allParticipants = new Participants([
1 => new Collection([ 1 => new Collection([
new Participant([ new ParticipantShort([
'firstname' => 'John', 'tid' => 1,
'lastname' => 'Scott', 'participant_info' => [
'email' => 'john@scott.com', 'firstname' => 'John',
'completed' => 'Y', 'lastname' => 'Scott',
'email' => 'john@scott.com',
],
]), ]),
new Participant([ new ParticipantShort([
'firstname' => 'Mary', 'tid' => 2,
'lastname' => 'Jane', 'participant_info' => [
'email' => 'mary@jane.com', 'firstname' => 'Mary',
'lastname' => 'Jane',
'email' => 'mary@jane.com',
],
]), ]),
]), ]),
2 => new Collection([ 2 => new Collection([
new Participant(), new ParticipantShort(),
]), ]),
]); ]);