5 Commits

15 changed files with 111 additions and 67 deletions

View File

@@ -1,4 +1,4 @@
FROM php:5.6-cli FROM php:5.5-cli
# #
# Tools & libraries # Tools & libraries

View File

@@ -5,10 +5,22 @@ Client of the [LimeSurvey's API](https://manual.limesurvey.org/RemoteControl_2_A
## Installation ## Installation
In your `composer.json` add address of repository into `repositories` section:
```json
"repositories": [
(...)
{
"type": "vcs",
"url": "https://github.com/wiosna-dev/limesurvey-api-client"
}
]
```
Run [Composer](https://getcomposer.org) to install this package in your project: Run [Composer](https://getcomposer.org) to install this package in your project:
```bash ```bash
$ composer require meritoo/limesurvey-api-client $ composer require wiosna-dev/limesurvey-api-client
``` ```
> How to install Composer: https://getcomposer.org/download > How to install Composer: https://getcomposer.org/download

View File

@@ -1,9 +1,9 @@
{ {
"name": "meritoo/limesurvey-api-client", "name": "wiosna-dev/limesurvey-api-client",
"description": "Client of LimeSurvey API", "description": "Client of LimeSurvey API",
"type": "library", "type": "library",
"license": "MIT", "license": "MIT",
"version": "0.0.10", "version": "0.0.11",
"authors": [ "authors": [
{ {
"name": "Meritoo", "name": "Meritoo",
@@ -11,18 +11,18 @@
} }
], ],
"require": { "require": {
"php": ">=5.6", "php": ">=5.5.9",
"fguillot/json-rpc": "^1.2", "fguillot/json-rpc": "^1.2",
"meritoo/common-library": "~0.0.1" "wiosna-dev/common-library": "0.0.17"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7", "friendsofphp/php-cs-fixer": "^2.2",
"squizlabs/php_codesniffer": "^2.9",
"phpmd/phpmd": "^2.6",
"sebastian/phpcpd": "^3.0",
"pdepend/pdepend": "^2.5", "pdepend/pdepend": "^2.5",
"phploc/phploc": "^4.0", "phploc/phploc": "^2.1",
"friendsofphp/php-cs-fixer": "^2.6" "phpmd/phpmd": "^2.6",
"phpunit/phpunit": "^4.8",
"sebastian/phpcpd": "^2.0",
"squizlabs/php_codesniffer": "^2.9"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@@ -33,5 +33,14 @@
"psr-4": { "psr-4": {
"Meritoo\\LimeSurvey\\Test\\ApiClient\\": "tests/" "Meritoo\\LimeSurvey\\Test\\ApiClient\\": "tests/"
} }
} },
"config": {
"sort-packages": true
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/wiosna-dev/common-library"
}
]
} }

View File

@@ -189,6 +189,23 @@ class SurveyService
return false; return false;
} }
/**
* Returns url used to start survey for given survey and participant's token
*
* @param int $surveyId ID of survey to start
* @param string $participantToken Token of participant who would like to start survey
* @return string
*/
public function getStartSurveyUrlByToken($surveyId, $participantToken)
{
$baseUrl = $this
->client
->getConfiguration()
->getBaseUrl();
return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participantToken);
}
/** /**
* Returns url used to start survey for given survey and participant * Returns url used to start survey for given survey and participant
* *
@@ -198,12 +215,7 @@ class SurveyService
*/ */
public function getStartSurveyUrl($surveyId, Participant $participant) public function getStartSurveyUrl($surveyId, Participant $participant)
{ {
$baseUrl = $this return $this->getStartSurveyUrlByToken($surveyId, $participant->getToken());
->client
->getConfiguration()
->getBaseUrl();
return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participant->getToken());
} }
/** /**

View File

@@ -45,7 +45,7 @@ class ClientTest extends BaseTestCase
*/ */
public function testRunWithIncorrectMethod($incorrectMethod) public function testRunWithIncorrectMethod($incorrectMethod)
{ {
$this->expectException(UnknownMethodException::class); $this->setExpectedException(UnknownMethodException::class);
$client = new Client($this->configuration); $client = new Client($this->configuration);
$client->run($incorrectMethod); $client->run($incorrectMethod);
@@ -61,8 +61,8 @@ class ClientTest extends BaseTestCase
*/ */
public function testRun($method, $arguments, $debugMode, $expectedRawData) public function testRun($method, $arguments, $debugMode, $expectedRawData)
{ {
$sessionManager = $this->createMock(SessionManager::class); $sessionManager = $this->getMock(SessionManager::class, [], [], '', false);
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$rpcClientManager $rpcClientManager
->expects(static::any()) ->expects(static::any())

View File

@@ -47,7 +47,7 @@ class ConnectionConfigurationTest extends BaseTestCase
*/ */
public function testConstructorWithEmptyBaseUrl($emptyBaseUrl) public function testConstructorWithEmptyBaseUrl($emptyBaseUrl)
{ {
$this->expectException(InvalidUrlException::class); $this->setExpectedException(InvalidUrlException::class);
new ConnectionConfiguration($emptyBaseUrl, '', ''); new ConnectionConfiguration($emptyBaseUrl, '', '');
} }
@@ -57,7 +57,7 @@ class ConnectionConfigurationTest extends BaseTestCase
*/ */
public function testConstructorWithInvalidBaseUrl($invalidBaseUrl) public function testConstructorWithInvalidBaseUrl($invalidBaseUrl)
{ {
$this->expectException(InvalidUrlException::class); $this->setExpectedException(InvalidUrlException::class);
new ConnectionConfiguration($invalidBaseUrl, '', ''); new ConnectionConfiguration($invalidBaseUrl, '', '');
} }

View File

@@ -40,7 +40,7 @@ class JsonRpcClientManagerTest extends BaseTestCase
public function testRunMethodWithEmptyArrayReturned() public function testRunMethodWithEmptyArrayReturned()
{ {
$rpcClient = $this->createMock(RpcClient::class); $rpcClient = $this->getMock(RpcClient::class);
$manager = $this $manager = $this
->getMockBuilder(JsonRpcClientManager::class) ->getMockBuilder(JsonRpcClientManager::class)
@@ -68,8 +68,8 @@ class JsonRpcClientManagerTest extends BaseTestCase
public function testRunMethodWithRawDataReturned() public function testRunMethodWithRawDataReturned()
{ {
$rpcClient = $this->createMock(RpcClient::class); $rpcClient = $this->getMock(RpcClient::class);
$manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']); $manager = $this->getMock(JsonRpcClientManager::class, ['getRpcClient'], [], '', false);
$rpcClient $rpcClient
->expects(static::once()) ->expects(static::once())
@@ -87,10 +87,10 @@ class JsonRpcClientManagerTest extends BaseTestCase
public function testRunMethodWithException() public function testRunMethodWithException()
{ {
$this->expectException(InvalidResultOfMethodRunException::class); $this->setExpectedException(InvalidResultOfMethodRunException::class);
$manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']); $manager = $this->getMock(JsonRpcClientManager::class, ['getRpcClient'], [], '', false);
$rpcClient = $this->createMock(RpcClient::class); $rpcClient = $this->getMock(RpcClient::class);
$rpcClient $rpcClient
->expects(self::once()) ->expects(self::once())

View File

@@ -29,10 +29,9 @@ class SessionManagerTest extends BaseTestCase
public function testGetSessionKeyWhenFailedWithoutReason() public function testGetSessionKeyWhenFailedWithoutReason()
{ {
$this->expectException(CreateSessionKeyFailedException::class); $this->setExpectedException(CreateSessionKeyFailedException::class, 'Create of the session key has failed');
$this->expectExceptionMessage('Create of the session key has failed');
$clientManager = $this->createMock(JsonRpcClientManager::class); $clientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$clientManager $clientManager
->expects(static::any()) ->expects(static::any())
@@ -46,10 +45,10 @@ class SessionManagerTest extends BaseTestCase
{ {
$reason = 'Invalid credentials'; $reason = 'Invalid credentials';
$this->expectException(CreateSessionKeyFailedException::class); $message = sprintf('Create of the session key has failed. Reason: \'%s\'.', $reason);
$this->expectExceptionMessage(sprintf('Create of the session key has failed. Reason: \'%s\'.', $reason)); $this->setExpectedException(CreateSessionKeyFailedException::class, $message);
$clientManager = $this->createMock(JsonRpcClientManager::class); $clientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$clientManager $clientManager
->expects(static::any()) ->expects(static::any())
@@ -63,7 +62,7 @@ class SessionManagerTest extends BaseTestCase
public function testGetSessionKey() public function testGetSessionKey()
{ {
$clientManager = $this->createMock(JsonRpcClientManager::class); $clientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$clientManager $clientManager
->expects(static::any()) ->expects(static::any())
@@ -76,7 +75,7 @@ class SessionManagerTest extends BaseTestCase
public function testReleaseSessionKey() public function testReleaseSessionKey()
{ {
$clientManager = $this->createMock(JsonRpcClientManager::class); $clientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$clientManager $clientManager
->expects(static::any()) ->expects(static::any())

View File

@@ -52,19 +52,19 @@ class ParticipantsTest extends BaseTestCase
public function testAdd() public function testAdd()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new Participants())->add(''); (new Participants())->add('');
} }
public function testAddMultiple() public function testAddMultiple()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new Participants())->addMultiple([]); (new Participants())->addMultiple([]);
} }
public function testHas() public function testHas()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new Participants())->has(new Participant()); (new Participants())->has(new Participant());
} }

View File

@@ -44,19 +44,19 @@ class SurveysSummariesTest extends BaseTestCase
public function testAdd() public function testAdd()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new SurveysSummaries())->add(''); (new SurveysSummaries())->add('');
} }
public function testAddMultiple() public function testAddMultiple()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new SurveysSummaries())->addMultiple([]); (new SurveysSummaries())->addMultiple([]);
} }
public function testHas() public function testHas()
{ {
$this->expectException(DisabledMethodException::class); $this->setExpectedException(DisabledMethodException::class);
(new SurveysSummaries())->has(new SurveySummary()); (new SurveysSummaries())->has(new SurveySummary());
} }

View File

@@ -82,7 +82,7 @@ class ResultProcessorTest extends BaseTestCase
public function testRunWithUnknownResultClass() public function testRunWithUnknownResultClass()
{ {
$this->expectException(UnknownInstanceOfResultItem::class); $this->setExpectedException(UnknownInstanceOfResultItem::class);
$rawData = [ $rawData = [
'lorem' => 'ipsum', 'lorem' => 'ipsum',

View File

@@ -128,7 +128,7 @@ class ResultTest extends BaseTestCase
public function testGetDataUsingProcessedDataWhoCannotBeProcessed() public function testGetDataUsingProcessedDataWhoCannotBeProcessed()
{ {
$this->expectException(CannotProcessDataException::class); $this->setExpectedException(CannotProcessDataException::class);
$this->statusInsteadDataResult->getData(); $this->statusInsteadDataResult->getData();
} }

View File

@@ -112,9 +112,7 @@ class ParticipantServiceTest extends BaseTestCase
public function testGetParticipantDetailsWithException() public function testGetParticipantDetailsWithException()
{ {
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID); $exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
$this->setExpectedException(CannotProcessDataException::class, $exception->getMessage());
$this->expectException(CannotProcessDataException::class);
$this->expectExceptionMessage($exception->getMessage());
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception); $rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
$sessionManager = $this->getSessionManager(); $sessionManager = $this->getSessionManager();
@@ -158,7 +156,7 @@ class ParticipantServiceTest extends BaseTestCase
public function testHasParticipantFilledSurveyWithoutParticipants() public function testHasParticipantFilledSurveyWithoutParticipants()
{ {
$this->expectException(MissingParticipantOfSurveyException::class); $this->setExpectedException(MissingParticipantOfSurveyException::class);
$rpcClientManager = $this->getJsonRpcClientManager(1); $rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager(); $sessionManager = $this->getSessionManager();
@@ -178,7 +176,7 @@ class ParticipantServiceTest extends BaseTestCase
public function testHasParticipantFilledSurveyUsingNotExistingParticipant() public function testHasParticipantFilledSurveyUsingNotExistingParticipant()
{ {
$this->expectException(MissingParticipantOfSurveyException::class); $this->setExpectedException(MissingParticipantOfSurveyException::class);
$rpcClientManager = $this->getJsonRpcClientManager(1); $rpcClientManager = $this->getJsonRpcClientManager(1);
$sessionManager = $this->getSessionManager(); $sessionManager = $this->getSessionManager();
@@ -253,7 +251,7 @@ class ParticipantServiceTest extends BaseTestCase
*/ */
private function getSessionManager() private function getSessionManager()
{ {
return $this->createMock(SessionManager::class); return $this->getMock(SessionManager::class, [], [], '', false);
} }
/** /**
@@ -265,7 +263,7 @@ class ParticipantServiceTest extends BaseTestCase
*/ */
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = []) private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{ {
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$mocker = $rpcClientManager $mocker = $rpcClientManager
->expects(static::exactly($runMethodCallCount)) ->expects(static::exactly($runMethodCallCount))
@@ -297,7 +295,7 @@ class ParticipantServiceTest extends BaseTestCase
*/ */
private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception) private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception)
{ {
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$rpcClientManager $rpcClientManager
->expects(static::exactly($runMethodCallCount)) ->expects(static::exactly($runMethodCallCount))

View File

@@ -100,7 +100,7 @@ class SurveyServiceTest extends BaseTestCase
public function testGetAllSurveysWithNoTableException() public function testGetAllSurveysWithNoTableException()
{ {
$this->expectException(CannotProcessDataException::class); $this->setExpectedException(CannotProcessDataException::class);
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE); $exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception); $rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
@@ -181,6 +181,22 @@ class SurveyServiceTest extends BaseTestCase
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true)); static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true));
} }
public function testGetStartSurveyUrlByToken()
{
$rpcClientManager = $this->getJsonRpcClientManager(0);
$sessionManager = $this->getSessionManager();
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
$surveyId = 123;
$token = uniqid();
$expectedUrl = sprintf('%s/%d?token=%s', $this->connectionBaseUrl, $surveyId, $token);
static::assertEquals($expectedUrl, $this->serviceWithoutSurveys->getStartSurveyUrlByToken($surveyId, $token));
static::assertEquals($expectedUrl, $this->serviceWithSurveys->getStartSurveyUrlByToken($surveyId, $token));
}
public function testGetStartSurveyUrl() public function testGetStartSurveyUrl()
{ {
$rpcClientManager = $this->getJsonRpcClientManager(0); $rpcClientManager = $this->getJsonRpcClientManager(0);
@@ -208,9 +224,7 @@ class SurveyServiceTest extends BaseTestCase
public function testGetSurveyParticipantsWithNotExistingSurveyException() public function testGetSurveyParticipantsWithNotExistingSurveyException()
{ {
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID); $exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
$this->setExpectedException(CannotProcessDataException::class, $exception->getMessage());
$this->expectException(CannotProcessDataException::class);
$this->expectExceptionMessage($exception->getMessage());
$runMethodCallResults = [ $runMethodCallResults = [
[ [
@@ -316,7 +330,7 @@ class SurveyServiceTest extends BaseTestCase
public function testGetSurveyParticipantsWithNoTableException() public function testGetSurveyParticipantsWithNoTableException()
{ {
$this->expectException(CannotProcessDataException::class); $this->setExpectedException(CannotProcessDataException::class);
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE); $exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception); $rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
@@ -328,7 +342,7 @@ class SurveyServiceTest extends BaseTestCase
public function testGetSurveyParticipantsWithNoParticipantsException() public function testGetSurveyParticipantsWithNoParticipantsException()
{ {
$this->expectException(CannotProcessDataException::class); $this->setExpectedException(CannotProcessDataException::class);
$exception = new CannotProcessDataException(ReasonType::NO_PARTICIPANTS_FOUND); $exception = new CannotProcessDataException(ReasonType::NO_PARTICIPANTS_FOUND);
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception); $rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
@@ -340,7 +354,7 @@ class SurveyServiceTest extends BaseTestCase
public function testAddParticipantForNotExistingSurvey() public function testAddParticipantForNotExistingSurvey()
{ {
$this->expectException(CannotProcessDataException::class); $this->setExpectedException(CannotProcessDataException::class);
$exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID); $exception = new CannotProcessDataException(ReasonType::NOT_EXISTING_SURVEY_ID);
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception); $rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
@@ -450,7 +464,7 @@ class SurveyServiceTest extends BaseTestCase
public function testGetSurveyTokenCountWithException() public function testGetSurveyTokenCountWithException()
{ {
$this->expectException(MissingSurveySummaryException::class); $this->setExpectedException(MissingSurveySummaryException::class);
$runMethodCallResults = [ $runMethodCallResults = [
null, null,
@@ -480,7 +494,7 @@ class SurveyServiceTest extends BaseTestCase
*/ */
private function getSessionManager() private function getSessionManager()
{ {
return $this->createMock(SessionManager::class); return $this->getMock(SessionManager::class, [], [], '', false);
} }
/** /**
@@ -492,7 +506,7 @@ class SurveyServiceTest extends BaseTestCase
*/ */
private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = []) private function getJsonRpcClientManager($runMethodCallCount, array $runMethodCallResults = [])
{ {
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$mocker = $rpcClientManager $mocker = $rpcClientManager
->expects(static::exactly($runMethodCallCount)) ->expects(static::exactly($runMethodCallCount))
@@ -524,7 +538,7 @@ class SurveyServiceTest extends BaseTestCase
*/ */
private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception) private function getJsonRpcClientManagerWithException($runMethodCallCount, Exception $exception)
{ {
$rpcClientManager = $this->createMock(JsonRpcClientManager::class); $rpcClientManager = $this->getMock(JsonRpcClientManager::class, [], [], '', false);
$rpcClientManager $rpcClientManager
->expects(static::exactly($runMethodCallCount)) ->expects(static::exactly($runMethodCallCount))

View File

@@ -33,7 +33,7 @@ class MethodTypeTest extends BaseTypeTestCase
*/ */
public function testGetValidatedMethodWithIncorrectMethod($incorrectMethod) public function testGetValidatedMethodWithIncorrectMethod($incorrectMethod)
{ {
$this->expectException(UnknownMethodException::class); $this->setExpectedException(UnknownMethodException::class);
MethodType::getValidatedMethod($incorrectMethod); MethodType::getValidatedMethod($incorrectMethod);
} }
@@ -52,7 +52,7 @@ class MethodTypeTest extends BaseTypeTestCase
*/ */
public function testIsResultIterableWithIncorrectMethod($incorrectMethod) public function testIsResultIterableWithIncorrectMethod($incorrectMethod)
{ {
$this->expectException(UnknownMethodException::class); $this->setExpectedException(UnknownMethodException::class);
MethodType::isResultIterable($incorrectMethod); MethodType::isResultIterable($incorrectMethod);
} }