From f5334f816f735e7f9a96143e0c2f7d67efb76e4d Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sun, 1 Oct 2017 20:46:31 +0200 Subject: [PATCH] Participants collection - store instances of ParticipantShort instead of Participant Related to fetch full data of participant of given survey --- src/Result/Collection/Participants.php | 2 +- src/Result/Item/ParticipantShort.php | 22 ++++++++++ src/Service/ParticipantService.php | 9 ++++- tests/Result/Item/ParticipantShortTest.php | 47 ++++++++++++++++++++++ tests/Service/ParticipantServiceTest.php | 28 ++++++++----- 5 files changed, 95 insertions(+), 13 deletions(-) diff --git a/src/Result/Collection/Participants.php b/src/Result/Collection/Participants.php index 6d0921b..577435f 100644 --- a/src/Result/Collection/Participants.php +++ b/src/Result/Collection/Participants.php @@ -51,7 +51,7 @@ class Participants extends Collection /** * Adds participants of given survey * - * @param Collection $participants Participants to add. Collection of Participant classes. + * @param Collection $participants Participants to add. Collection of ParticipantShort classes. * @param int $surveyId ID of survey * @return $this */ diff --git a/src/Result/Item/ParticipantShort.php b/src/Result/Item/ParticipantShort.php index e676877..67d739a 100644 --- a/src/Result/Item/ParticipantShort.php +++ b/src/Result/Item/ParticipantShort.php @@ -103,4 +103,26 @@ class ParticipantShort extends BaseItem { return $this->email; } + + /** + * Returns short data of participant created from full data of participant + * + * @param Participant $participant Full data of participant + * @return $this + */ + public static function fromParticipant(Participant $participant) + { + $info = [ + 'firstname' => $participant->getFirstName(), + 'lastname' => $participant->getLastName(), + 'email' => $participant->getEmail(), + ]; + + $data = [ + 'tid' => $participant->getId(), + 'participant_info' => $info, + ]; + + return new self($data); + } } diff --git a/src/Service/ParticipantService.php b/src/Service/ParticipantService.php index 260d91b..79fa24f 100644 --- a/src/Service/ParticipantService.php +++ b/src/Service/ParticipantService.php @@ -164,9 +164,16 @@ class ParticipantService ->run(MethodType::ADD_PARTICIPANTS, $arguments) ->getData(); + /* @var Participant $addedParticipant */ + $addedParticipant = $participantCollection->getFirst(); + + $participants = new Collection([ + ParticipantShort::fromParticipant($addedParticipant), + ]); + $this ->allParticipants - ->addParticipants($participantCollection, $surveyId); + ->addParticipants($participants, $surveyId); return $participantCollection->getFirst(); } diff --git a/tests/Result/Item/ParticipantShortTest.php b/tests/Result/Item/ParticipantShortTest.php index 44fd9bf..d21e1df 100644 --- a/tests/Result/Item/ParticipantShortTest.php +++ b/tests/Result/Item/ParticipantShortTest.php @@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; +use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant; use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort; use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor; use Meritoo\LimeSurvey\ApiClient\Type\MethodType; @@ -80,6 +81,52 @@ class ParticipantShortTest extends BaseTestCase static::assertEquals('dolor@sit.com', $this->participant2ndInstance->getEmail()); } + public function testFromParticipantUsingEmptyParticipant() + { + $participant = new Participant(); + $participantShort = ParticipantShort::fromParticipant($participant); + + static::assertEquals(0, $participantShort->getId()); + static::assertEquals('', $participantShort->getFirstName()); + static::assertEquals('', $participantShort->getLastName()); + static::assertEquals('', $participantShort->getEmail()); + + static::assertEquals($participant->getId(), $participantShort->getId()); + static::assertEquals($participant->getFirstName(), $participantShort->getFirstName()); + static::assertEquals($participant->getLastName(), $participantShort->getLastName()); + static::assertEquals($participant->getEmail(), $participantShort->getEmail()); + } + + public function testFromParticipant() + { + $participant1 = new Participant([ + 'tid' => $this->rawData[0]['tid'], + 'firstname' => $this->rawData[0]['participant_info']['firstname'], + 'lastname' => $this->rawData[0]['participant_info']['lastname'], + 'email' => $this->rawData[0]['participant_info']['email'], + ]); + + $participant2 = new Participant([ + 'tid' => $this->rawData[1]['tid'], + 'firstname' => $this->rawData[1]['participant_info']['firstname'], + 'lastname' => $this->rawData[1]['participant_info']['lastname'], + 'email' => $this->rawData[1]['participant_info']['email'], + ]); + + $participantShort1 = ParticipantShort::fromParticipant($participant1); + $participantShort2 = ParticipantShort::fromParticipant($participant2); + + static::assertEquals($participant1->getId(), $participantShort1->getId()); + static::assertEquals($participant1->getFirstName(), $participantShort1->getFirstName()); + static::assertEquals($participant1->getLastName(), $participantShort1->getLastName()); + static::assertEquals($participant1->getEmail(), $participantShort1->getEmail()); + + static::assertEquals($participant2->getId(), $participantShort2->getId()); + static::assertEquals($participant2->getFirstName(), $participantShort2->getFirstName()); + static::assertEquals($participant2->getLastName(), $participantShort2->getLastName()); + static::assertEquals($participant2->getEmail(), $participantShort2->getEmail()); + } + /** * Returns raw data of participants * diff --git a/tests/Service/ParticipantServiceTest.php b/tests/Service/ParticipantServiceTest.php index 7625079..f95a38f 100644 --- a/tests/Service/ParticipantServiceTest.php +++ b/tests/Service/ParticipantServiceTest.php @@ -20,6 +20,7 @@ use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager; use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager; use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants; use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant; +use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort; use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService; use Meritoo\LimeSurvey\ApiClient\Type\ReasonType; use PHPUnit_Framework_MockObject_MockObject; @@ -185,7 +186,7 @@ class ParticipantServiceTest extends BaseTestCase static::assertNull($this->serviceWithoutParticipants->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('Scott', $participant->getLastName()); static::assertEquals('john@scott.com', $participant->getEmail()); @@ -314,20 +315,25 @@ class ParticipantServiceTest extends BaseTestCase $allParticipants = new Participants([ 1 => new Collection([ - new Participant([ - 'firstname' => 'John', - 'lastname' => 'Scott', - 'email' => 'john@scott.com', - 'completed' => 'Y', + new ParticipantShort([ + 'tid' => 1, + 'participant_info' => [ + 'firstname' => 'John', + 'lastname' => 'Scott', + 'email' => 'john@scott.com', + ], ]), - new Participant([ - 'firstname' => 'Mary', - 'lastname' => 'Jane', - 'email' => 'mary@jane.com', + new ParticipantShort([ + 'tid' => 2, + 'participant_info' => [ + 'firstname' => 'Mary', + 'lastname' => 'Jane', + 'email' => 'mary@jane.com', + ], ]), ]), 2 => new Collection([ - new Participant(), + new ParticipantShort(), ]), ]);