mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
380 lines
16 KiB
PHP
380 lines
16 KiB
PHP
<?php
|
|
|
|
/**
|
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
|
|
|
use Exception;
|
|
use Meritoo\Common\Collection\Collection;
|
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
|
use Meritoo\Common\Type\OopVisibilityType;
|
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
|
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
|
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
|
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
|
use Meritoo\LimeSurvey\ApiClient\Service\ParticipantService;
|
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
|
|
|
/**
|
|
* Test case of the service that serves participants
|
|
*
|
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
|
* @copyright Meritoo.pl
|
|
*/
|
|
class ParticipantServiceTest extends BaseTestCase
|
|
{
|
|
/**
|
|
* Service that serves participants.
|
|
* Without participants.
|
|
*
|
|
* @var ParticipantService
|
|
*/
|
|
private $serviceWithoutParticipants;
|
|
|
|
/**
|
|
* Service that serves participants.
|
|
* With participants.
|
|
*
|
|
* @var ParticipantService
|
|
*/
|
|
private $serviceWithParticipants;
|
|
|
|
public function testConstructorVisibilityAndArguments()
|
|
{
|
|
static::assertConstructorVisibilityAndArguments(ParticipantService::className, OopVisibilityType::IS_PUBLIC, 2, 1);
|
|
}
|
|
|
|
public function testGetClient()
|
|
{
|
|
$rpcClientManager = $this->getJsonRpcClientManager(0);
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
static::assertInstanceOf(Client::className, $this->serviceWithoutParticipants->getClient());
|
|
static::assertInstanceOf(Client::className, $this->serviceWithParticipants->getClient());
|
|
|
|
$connectionConfiguration = $this->getConnectionConfiguration();
|
|
$client = new Client($connectionConfiguration);
|
|
$participantService = new ParticipantService($client);
|
|
|
|
static::assertEquals($client, $participantService->getClient());
|
|
}
|
|
|
|
public function testGetSurveyParticipants()
|
|
{
|
|
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(1));
|
|
static::assertCount(0, $this->serviceWithoutParticipants->getSurveyParticipants(2));
|
|
|
|
static::assertCount(2, $this->serviceWithParticipants->getSurveyParticipants(1));
|
|
static::assertCount(1, $this->serviceWithParticipants->getSurveyParticipants(2));
|
|
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
|
}
|
|
|
|
public function testGetSurveyParticipantsWithImportantException()
|
|
{
|
|
$this->setExpectedException(CannotProcessDataException::className);
|
|
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
$this->serviceWithParticipants->getSurveyParticipants(3);
|
|
}
|
|
|
|
public function testGetSurveyParticipantsWithNoParticipantsException()
|
|
{
|
|
$exception = new CannotProcessDataException(ReasonType::NO_PARTICIPANTS_FOUND);
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
|
}
|
|
|
|
public function testHasParticipant()
|
|
{
|
|
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
|
|
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
|
|
|
|
static::assertTrue($this->serviceWithParticipants->hasParticipant(1, 'john@scott.com'));
|
|
static::assertFalse($this->serviceWithParticipants->hasParticipant(2, 'john@scott.com'));
|
|
static::assertFalse($this->serviceWithParticipants->hasParticipant(3, 'john@scott.com'));
|
|
}
|
|
|
|
public function testAddParticipantForNotExistingSurvey()
|
|
{
|
|
$this->setExpectedException(CannotProcessDataException::className);
|
|
$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::className, $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);
|
|
|
|
$participant1 = $this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com');
|
|
$participant2 = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com');
|
|
|
|
static::assertNull($participant1);
|
|
static::assertInstanceOf(ParticipantShort::className, $participant2);
|
|
static::assertEquals('John', $participant2->getFirstName());
|
|
static::assertEquals('Scott', $participant2->getLastName());
|
|
static::assertEquals('john@scott.com', $participant2->getEmail());
|
|
}
|
|
|
|
public function testGetParticipantDetails()
|
|
{
|
|
$sessionManager = $this->getSessionManager();
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManager(1);
|
|
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
|
|
|
$runMethodCallResults = [
|
|
'tid' => 1,
|
|
'firstname' => 'John',
|
|
'lastname' => 'Scott',
|
|
'email' => 'john@scott.com',
|
|
'token' => uniqid(),
|
|
'sent' => 'N',
|
|
'completed' => 'N',
|
|
];
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
$participant1 = $this->serviceWithoutParticipants->getParticipantDetails(1, 'john@scott.com');
|
|
$participant2 = $this->serviceWithParticipants->getParticipantDetails(1, 'john@scott.com');
|
|
|
|
static::assertNull($participant1);
|
|
static::assertInstanceOf(Participant::className, $participant2);
|
|
static::assertEquals($runMethodCallResults['tid'], $participant2->getId());
|
|
static::assertEquals($runMethodCallResults['firstname'], $participant2->getFirstName());
|
|
static::assertEquals($runMethodCallResults['lastname'], $participant2->getLastName());
|
|
static::assertEquals($runMethodCallResults['email'], $participant2->getEmail());
|
|
static::assertEquals($runMethodCallResults['token'], $participant2->getToken());
|
|
static::assertFalse($participant2->isSent());
|
|
static::assertFalse($participant2->isCompleted());
|
|
static::assertNull($participant2->isBlacklisted());
|
|
static::assertNull($participant2->getValidFrom());
|
|
}
|
|
|
|
public function testHasParticipantFilledSurveyWithoutParticipants()
|
|
{
|
|
$this->setExpectedException(MissingParticipantOfSurveyException::className);
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManager(1);
|
|
$sessionManager = $this->getSessionManager();
|
|
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
|
|
|
$this->serviceWithoutParticipants->hasParticipantFilledSurvey(1, 'john@scott.com');
|
|
}
|
|
|
|
public function testHasParticipantFilledSurveyUsingExistingParticipant()
|
|
{
|
|
$runMethodCallResults = [
|
|
'firstname' => 'John',
|
|
'lastname' => 'Scott',
|
|
'email' => 'john@scott.com',
|
|
'completed' => 'Y',
|
|
];
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
|
$sessionManager = $this->getSessionManager();
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
static::assertTrue($this->serviceWithParticipants->hasParticipantFilledSurvey(1, 'john@scott.com'));
|
|
}
|
|
|
|
public function testHasParticipantFilledSurveyUsingNotExistingParticipant()
|
|
{
|
|
$this->setExpectedException(MissingParticipantOfSurveyException::className);
|
|
|
|
$rpcClientManager = $this->getJsonRpcClientManager(1);
|
|
$sessionManager = $this->getSessionManager();
|
|
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
|
|
|
$this->serviceWithParticipants->hasParticipantFilledSurvey(3, 'mary@jane.com');
|
|
}
|
|
|
|
/**
|
|
* Returns configuration used while connecting to LimeSurvey's API
|
|
*
|
|
* @return ConnectionConfiguration
|
|
*/
|
|
private function getConnectionConfiguration()
|
|
{
|
|
return new ConnectionConfiguration('http://test.com', 'test', 'test');
|
|
}
|
|
|
|
/**
|
|
* Returns manager of session started while connecting to LimeSurvey's API
|
|
*
|
|
* @return PHPUnit_Framework_MockObject_MockObject
|
|
*/
|
|
private function getSessionManager()
|
|
{
|
|
return $this->createMock(SessionManager::className);
|
|
}
|
|
|
|
/**
|
|
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API with mocked method runMethod()
|
|
*
|
|
* @param int $runMethodCallCount Count of calls of the runMethod() method (who is mocked)
|
|
* @param array $runMethodCallResults (optional) Results of calls of the runMethod() method (who is mocked)
|
|
* @return PHPUnit_Framework_MockObject_MockObject
|
|
*/
|
|
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
|
|
{
|
|
$rpcClientManager = $this->createMock(JsonRpcClientManager::className);
|
|
|
|
$rpcClientManager
|
|
->expects(static::exactly($runMethodCallCount))
|
|
->method('runMethod')
|
|
->will(static::returnValue($runMethodCallResults));
|
|
|
|
return $rpcClientManager;
|
|
}
|
|
|
|
/**
|
|
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API with mocked method runMethod()
|
|
* that throws an exception
|
|
*
|
|
* @param int $runMethodCallCount Count of calls of the runMethod() method (who is mocked)
|
|
* @param Exception $exception The exception that should be thrown
|
|
* @return PHPUnit_Framework_MockObject_MockObject
|
|
*/
|
|
private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception)
|
|
{
|
|
$rpcClientManager = $this->createMock(JsonRpcClientManager::className);
|
|
|
|
$rpcClientManager
|
|
->expects(static::exactly($runMethodCallCount))
|
|
->method('runMethod')
|
|
->willThrowException($exception);
|
|
|
|
return $rpcClientManager;
|
|
}
|
|
|
|
/**
|
|
* Creates instance of the tested service without participants
|
|
*
|
|
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
|
|
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
|
|
*/
|
|
private function createServiceWithoutParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
|
|
{
|
|
$configuration = $this->getConnectionConfiguration();
|
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
|
$this->serviceWithoutParticipants = new ParticipantService($client);
|
|
}
|
|
|
|
/**
|
|
* Creates instance of the tested service with participants
|
|
*
|
|
* @param PHPUnit_Framework_MockObject_MockObject $rpcClientManager Manager of the JsonRPC client used while connecting to LimeSurvey's API
|
|
* @param PHPUnit_Framework_MockObject_MockObject $sessionManager Manager of session started while connecting to LimeSurvey's API
|
|
*/
|
|
private function createServiceWithParticipants(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
|
|
{
|
|
$configuration = $this->getConnectionConfiguration();
|
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
|
|
|
$allParticipants = new Participants([
|
|
1 => new Collection([
|
|
new ParticipantShort([
|
|
'tid' => 1,
|
|
'participant_info' => [
|
|
'firstname' => 'John',
|
|
'lastname' => 'Scott',
|
|
'email' => 'john@scott.com',
|
|
],
|
|
]),
|
|
new ParticipantShort([
|
|
'tid' => 2,
|
|
'participant_info' => [
|
|
'firstname' => 'Mary',
|
|
'lastname' => 'Jane',
|
|
'email' => 'mary@jane.com',
|
|
],
|
|
]),
|
|
]),
|
|
2 => new Collection([
|
|
new ParticipantShort(),
|
|
]),
|
|
]);
|
|
|
|
$this->serviceWithParticipants = new ParticipantService($client, $allParticipants);
|
|
}
|
|
}
|