mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e902568a91 | ||
|
|
626174953b | ||
|
|
ddb568adf7 | ||
|
|
835c4325b8 | ||
|
|
24a3d478b5 | ||
|
|
e38df1a8f2 | ||
|
|
db6a6f22e2 | ||
|
|
f5334f816f | ||
|
|
e43712f218 | ||
|
|
e464ae30af | ||
|
|
98cad233d9 | ||
|
|
5154f05ee4 | ||
|
|
0c44140c89 | ||
|
|
41156ed058 | ||
|
|
54bd0ca114 | ||
|
|
5243294bd5 | ||
|
|
6159731768 | ||
|
|
bbd466610c | ||
|
|
d54765b378 | ||
|
|
ab328b96ee | ||
|
|
cd6dbf72bc | ||
|
|
beaf61d2ea | ||
|
|
cacb2b3b92 | ||
|
|
dfa64fee43 | ||
|
|
6c44a62f36 | ||
|
|
4f26bca282 | ||
|
|
dbd0a65286 | ||
|
|
0562cb4d21 | ||
|
|
f8a675d0fb | ||
|
|
a5b534b00d | ||
|
|
a6866d994c | ||
|
|
5061f5a295 | ||
|
|
f5de59f50b | ||
|
|
6e54d39972 | ||
|
|
b3b0e66fb3 | ||
|
|
0fbfc9780d | ||
|
|
92315bd853 | ||
|
|
c3e6935dd8 | ||
|
|
07bc4ab4af | ||
|
|
fd1ec32e1a | ||
|
|
72480ecc27 | ||
|
|
20d7d2f50d | ||
|
|
f5bafdc969 | ||
|
|
0eb6cc85a2 | ||
|
|
e803d82844 | ||
|
|
01439aaa1f | ||
|
|
844f04c877 | ||
|
|
959e9481fd | ||
|
|
af2df98d14 | ||
|
|
d0badf1ec6 | ||
|
|
2dc215a3e4 | ||
|
|
10c0bf8e41 |
64
.docker/config/Dockerfile
Normal file
64
.docker/config/Dockerfile
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
FROM php:5.6-cli
|
||||||
|
|
||||||
|
#
|
||||||
|
# Tools & libraries
|
||||||
|
#
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends \
|
||||||
|
vim \
|
||||||
|
git \
|
||||||
|
zip \
|
||||||
|
unzip \
|
||||||
|
zlib1g-dev \
|
||||||
|
libicu-dev \
|
||||||
|
&& apt-get clean \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||||
|
|
||||||
|
#
|
||||||
|
# PHP extensions
|
||||||
|
#
|
||||||
|
RUN docker-php-ext-install \
|
||||||
|
zip \
|
||||||
|
intl \
|
||||||
|
mbstring
|
||||||
|
|
||||||
|
#
|
||||||
|
# PHP configuration:
|
||||||
|
# - default configuration
|
||||||
|
# - timezone
|
||||||
|
#
|
||||||
|
COPY php.ini /usr/local/etc/php/php.ini
|
||||||
|
ARG TIMEZONE
|
||||||
|
RUN echo "\n""date.timezone = $TIMEZONE""\n" >> /usr/local/etc/php/php.ini
|
||||||
|
|
||||||
|
#
|
||||||
|
# Xdebug
|
||||||
|
#
|
||||||
|
RUN pecl install xdebug \
|
||||||
|
&& docker-php-ext-enable xdebug
|
||||||
|
COPY xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
|
||||||
|
|
||||||
|
#
|
||||||
|
# Phing
|
||||||
|
#
|
||||||
|
RUN pear channel-discover pear.phing.info \
|
||||||
|
&& pear install [--alldeps] phing/phing
|
||||||
|
|
||||||
|
#
|
||||||
|
# Composer + https://packagist.org/packages/hirak/prestissimo package
|
||||||
|
#
|
||||||
|
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
|
||||||
|
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === \
|
||||||
|
'544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo \
|
||||||
|
'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
|
||||||
|
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
|
||||||
|
&& php -r "unlink('composer-setup.php');" \
|
||||||
|
&& composer global require --no-plugins --no-scripts hirak/prestissimo \
|
||||||
|
&& rm -rf /root/.composer/cache/*
|
||||||
|
|
||||||
|
#
|
||||||
|
# Bash
|
||||||
|
#
|
||||||
|
RUN sed -i 's/^# export/export/g' /root/.bashrc \
|
||||||
|
&& sed -i 's/^# alias/alias/g' /root/.bashrc \
|
||||||
|
&& echo "\n"'export PATH=/project/vendor/bin:$PATH'"\n" >> /root/.bashrc
|
||||||
3
.docker/config/php.ini
Normal file
3
.docker/config/php.ini
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
display_errors = On
|
||||||
|
display_startup_errors = On
|
||||||
|
error_reporting = E_ALL
|
||||||
6
.docker/config/xdebug.ini
Normal file
6
.docker/config/xdebug.ini
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[xdebug]
|
||||||
|
zend_extension=xdebug.so
|
||||||
|
|
||||||
|
xdebug.remote_enable=1
|
||||||
|
xdebug.remote_port=9001
|
||||||
|
xdebug.remote_host=10.254.254.254
|
||||||
11
.gitignore
vendored
11
.gitignore
vendored
@@ -11,12 +11,13 @@
|
|||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
### Composer
|
### Composer
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
|
/composer.lock
|
||||||
/composer.phar
|
/composer.phar
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
### Phing
|
### Phing
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
/phing/properties
|
/.phing/properties
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
### PHPUnit
|
### PHPUnit
|
||||||
@@ -28,10 +29,16 @@
|
|||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
/.php_cs.cache
|
/.php_cs.cache
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
|
### Build files
|
||||||
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
|
/.build/
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
### Generated databases
|
### Generated databases
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
/data/tmp
|
/.data/tmp
|
||||||
*.sql
|
*.sql
|
||||||
*.sqlite
|
*.sqlite
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
||||||
<!-- Properties -->
|
<!-- Properties -->
|
||||||
<if>
|
<if>
|
||||||
<available file="phing/properties" property="custom.properties.available"/>
|
<available file=".phing/properties" property="custom.properties.available"/>
|
||||||
<then>
|
<then>
|
||||||
<property file="phing/properties" />
|
<property file=".phing/properties" />
|
||||||
</then>
|
</then>
|
||||||
<else>
|
<else>
|
||||||
<property file="phing/properties.dist" />
|
<property file=".phing/properties.dist" />
|
||||||
</else>
|
</else>
|
||||||
</if>
|
</if>
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ composer.validate = false
|
|||||||
|
|
||||||
# System directories
|
# System directories
|
||||||
#
|
#
|
||||||
dir.data = ${project.basedir}/data
|
dir.data = ${project.basedir}/.data
|
||||||
dir.src = ${project.basedir}/src
|
dir.src = ${project.basedir}/src
|
||||||
dir.tests = ${project.basedir}/tests
|
dir.tests = ${project.basedir}/tests
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ dir.tests = ${project.basedir}/tests
|
|||||||
# Build directories
|
# Build directories
|
||||||
# --------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------
|
||||||
|
|
||||||
dir.build = ${project.basedir}/build
|
dir.build = ${project.basedir}/.build
|
||||||
dir.reports = ${dir.build}/logs
|
dir.reports = ${dir.build}/logs
|
||||||
dir.reports.pdepend = ${dir.reports}/pdepend
|
dir.reports.pdepend = ${dir.reports}/pdepend
|
||||||
dir.reports.coverage = ${dir.reports}/phpunit_coverage
|
dir.reports.coverage = ${dir.reports}/phpunit_coverage
|
||||||
@@ -11,12 +11,12 @@
|
|||||||
|
|
||||||
<!-- Properties -->
|
<!-- Properties -->
|
||||||
<if>
|
<if>
|
||||||
<available file="phing/properties" property="custom.properties.available"/>
|
<available file=".phing/properties" property="custom.properties.available"/>
|
||||||
<then>
|
<then>
|
||||||
<property file="phing/properties" />
|
<property file=".phing/properties" />
|
||||||
</then>
|
</then>
|
||||||
<else>
|
<else>
|
||||||
<property file="phing/properties.dist" />
|
<property file=".phing/properties.dist" />
|
||||||
</else>
|
</else>
|
||||||
</if>
|
</if>
|
||||||
|
|
||||||
12
build.xml
12
build.xml
@@ -2,12 +2,12 @@
|
|||||||
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
||||||
<!-- Properties -->
|
<!-- Properties -->
|
||||||
<if>
|
<if>
|
||||||
<available file="phing/properties" property="custom.properties.available"/>
|
<available file=".phing/properties" property="custom.properties.available"/>
|
||||||
<then>
|
<then>
|
||||||
<property file="phing/properties" />
|
<property file=".phing/properties" />
|
||||||
</then>
|
</then>
|
||||||
<else>
|
<else>
|
||||||
<property file="phing/properties.dist" />
|
<property file=".phing/properties.dist" />
|
||||||
</else>
|
</else>
|
||||||
</if>
|
</if>
|
||||||
|
|
||||||
@@ -18,12 +18,12 @@
|
|||||||
|
|
||||||
<!-- Build app -->
|
<!-- Build app -->
|
||||||
<target name="build:app" description="Prepares app to build and tests">
|
<target name="build:app" description="Prepares app to build and tests">
|
||||||
<phing phingfile="phing/app.xml" haltonfailure="true" />
|
<phing phingfile=".phing/app.xml" haltonfailure="true" />
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<!-- Build tests -->
|
<!-- Build tests -->
|
||||||
<target name="build:tests" description="Runs all tests, checks and creates docs">
|
<target name="build:tests" description="Runs all tests, checks and creates docs">
|
||||||
<phing phingfile="phing/tests.xml" haltonfailure="true" />
|
<phing phingfile=".phing/tests.xml" haltonfailure="true" />
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Conditional running of tests.
|
Conditional running of tests.
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
<if>
|
<if>
|
||||||
<equals arg1="${env}" arg2="test" />
|
<equals arg1="${env}" arg2="test" />
|
||||||
<then>
|
<then>
|
||||||
<phing phingfile="phing/tests.xml" haltonfailure="true" />
|
<phing phingfile=".phing/tests.xml" haltonfailure="true" />
|
||||||
</then>
|
</then>
|
||||||
<else>
|
<else>
|
||||||
<echo message="[Skipped] Running tests, checks and creating docs, because it's a not 'test' environment..." />
|
<echo message="[Skipped] Running tests, checks and creating docs, because it's a not 'test' environment..." />
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "Client of LimeSurvey API",
|
"description": "Client of LimeSurvey API",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "0.0.1",
|
"version": "0.0.9",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Meritoo",
|
"name": "Meritoo",
|
||||||
@@ -11,22 +11,27 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
|
"php": ">=5.6",
|
||||||
"fguillot/json-rpc": "^1.2",
|
"fguillot/json-rpc": "^1.2",
|
||||||
"meritoo/common-library": "~0.0.1"
|
"meritoo/common-library": "~0.0.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^4.8.35 || ^5.4.3",
|
"phpunit/phpunit": "^5.7",
|
||||||
"squizlabs/php_codesniffer": "^2.8",
|
"squizlabs/php_codesniffer": "^2.9",
|
||||||
"phpmd/phpmd": "^2.6",
|
"phpmd/phpmd": "^2.6",
|
||||||
"sebastian/phpcpd": "^3.0",
|
"sebastian/phpcpd": "^3.0",
|
||||||
"pdepend/pdepend": "^2.5",
|
"pdepend/pdepend": "^2.5",
|
||||||
"phploc/phploc": "^4.0",
|
"phploc/phploc": "^4.0",
|
||||||
"friendsofphp/php-cs-fixer": "^2.5"
|
"friendsofphp/php-cs-fixer": "^2.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Meritoo\\LimeSurvey\\": "src/Meritoo/LimeSurvey/",
|
"Meritoo\\LimeSurvey\\ApiClient\\": "src/"
|
||||||
"Meritoo\\LimeSurvey\\Test\\": "tests/Meritoo/LimeSurvey/Test/"
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Meritoo\\LimeSurvey\\Test\\ApiClient\\": "tests/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3581
composer.lock
generated
3581
composer.lock
generated
File diff suppressed because it is too large
Load Diff
15
docker-compose.yml
Normal file
15
docker-compose.yml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
php-cli:
|
||||||
|
image: meritoo/limesurvey-api-client
|
||||||
|
container_name: meritoo-limesurvey-api-client
|
||||||
|
working_dir: /project
|
||||||
|
entrypoint: php
|
||||||
|
command: -S 0.0.0.0:9999
|
||||||
|
build:
|
||||||
|
context: ./.docker/config
|
||||||
|
args:
|
||||||
|
- TIMEZONE=$TIMEZONE
|
||||||
|
volumes:
|
||||||
|
- .:/project
|
||||||
@@ -30,6 +30,6 @@
|
|||||||
</groups>
|
</groups>
|
||||||
|
|
||||||
<logging>
|
<logging>
|
||||||
<log type="coverage-html" target="./build/logs/phpunit_coverage/html" />
|
<log type="coverage-html" target="./.build/logs/phpunit_coverage/html" />
|
||||||
</logging>
|
</logging>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
@@ -16,13 +16,32 @@ namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
|||||||
*/
|
*/
|
||||||
abstract class BaseItem
|
abstract class BaseItem
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param array $data (optional) Data to set in properties of the item
|
||||||
|
*/
|
||||||
|
public function __construct(array $data = [])
|
||||||
|
{
|
||||||
|
$this->setValues($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets given value in given property
|
||||||
|
*
|
||||||
|
* @param string $property Name of property to set the value
|
||||||
|
* @param mixed $value Value to set in property
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
abstract public function setValue($property, $value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets values in each property of the item
|
* Sets values in each property of the item
|
||||||
*
|
*
|
||||||
* @param array $data Data to set in properties of the item
|
* @param array $data Data to set in properties of the item
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setValues($data)
|
private function setValues(array $data)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Oops, no data
|
* Oops, no data
|
||||||
@@ -40,13 +59,4 @@ abstract class BaseItem
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets given value in given property
|
|
||||||
*
|
|
||||||
* @param string $property Name of property to set the value
|
|
||||||
* @param mixed $value Value to set in property
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
abstract public function setValue($property, $value);
|
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,8 @@ class Client
|
|||||||
public function run($method, $arguments = [])
|
public function run($method, $arguments = [])
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Let's validate method
|
* Let's validate method.
|
||||||
|
* It's called in the JsonRpcClientManager::runMethod() too, but I want to verify it before getting session key.
|
||||||
*/
|
*/
|
||||||
$method = MethodType::getValidatedMethod($method);
|
$method = MethodType::getValidatedMethod($method);
|
||||||
|
|
||||||
@@ -92,7 +93,7 @@ class Client
|
|||||||
->getSessionKey($username, $password);
|
->getSessionKey($username, $password);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Use the session's key as of the method's arguments
|
* Use the session's key as one of the method's arguments
|
||||||
*/
|
*/
|
||||||
array_unshift($arguments, $sessionKey);
|
array_unshift($arguments, $sessionKey);
|
||||||
|
|
||||||
@@ -103,9 +104,31 @@ class Client
|
|||||||
->getRpcClientManager()
|
->getRpcClientManager()
|
||||||
->runMethod($method, $arguments);
|
->runMethod($method, $arguments);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Raw data is unknown?
|
||||||
|
* Let's use an empty array instead
|
||||||
|
*
|
||||||
|
* Required to avoid bug:
|
||||||
|
* Argument 2 passed to Meritoo\LimeSurvey\ApiClient\Result\Result::__construct() must be of the type array,
|
||||||
|
* null given
|
||||||
|
*/
|
||||||
|
if (null === $rawData) {
|
||||||
|
$rawData = [];
|
||||||
|
}
|
||||||
|
|
||||||
return new Result($method, $rawData);
|
return new Result($method, $rawData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns configuration used while connecting to LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @return ConnectionConfiguration
|
||||||
|
*/
|
||||||
|
public function getConfiguration()
|
||||||
|
{
|
||||||
|
return $this->configuration;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
|
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
|
||||||
*
|
*
|
||||||
@@ -59,21 +59,33 @@ class ConnectionConfiguration
|
|||||||
*/
|
*/
|
||||||
private $debugMode = false;
|
private $debugMode = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If is set to true, the SSL certificate verification is turned on. Otherwise - turned off.
|
||||||
|
* It's useful while running application with custom, non-official SSL certificate, e.g. while development process.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $verifySslCertificate = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
* @param string $baseUrl Base url. Protocol & domain.
|
* @param string $baseUrl Base url. Protocol & domain.
|
||||||
* @param string $username Name of user used to authenticate to LimeSurvey
|
* @param string $username Name of user used to authenticate to LimeSurvey
|
||||||
* @param string $password Password used to authenticate to LimeSurvey
|
* @param string $password Password used to authenticate to LimeSurvey
|
||||||
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
|
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise -
|
||||||
|
* turned off.
|
||||||
|
* @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned
|
||||||
|
* on. Otherwise - turned off.
|
||||||
*/
|
*/
|
||||||
public function __construct($baseUrl, $username, $password, $debugMode = false)
|
public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true)
|
||||||
{
|
{
|
||||||
$this
|
$this->setBaseUrl($baseUrl);
|
||||||
->setBaseUrl($baseUrl)
|
|
||||||
->setUsername($username)
|
$this->username = $username;
|
||||||
->setPassword($password)
|
$this->password = $password;
|
||||||
->setDebugMode($debugMode);
|
$this->debugMode = $debugMode;
|
||||||
|
$this->verifySslCertificate = $verifySslCertificate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,29 +98,6 @@ class ConnectionConfiguration
|
|||||||
return $this->baseUrl;
|
return $this->baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the base url, protocol & domain
|
|
||||||
*
|
|
||||||
* @param string $baseUrl The base url, protocol & domain
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @throws InvalidUrlException
|
|
||||||
*/
|
|
||||||
public function setBaseUrl($baseUrl)
|
|
||||||
{
|
|
||||||
if (!Regex::isValidUrl($baseUrl)) {
|
|
||||||
throw new InvalidUrlException($baseUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Regex::endsWith($baseUrl, '/')) {
|
|
||||||
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->baseUrl = $baseUrl;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the url of the LimeSurvey's remote control
|
* Returns the url of the LimeSurvey's remote control
|
||||||
*
|
*
|
||||||
@@ -142,19 +131,6 @@ class ConnectionConfiguration
|
|||||||
return $this->username;
|
return $this->username;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the name of user used to authenticate to LimeSurvey
|
|
||||||
*
|
|
||||||
* @param string $username The name of user used to authenticate to LimeSurvey
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setUsername($username)
|
|
||||||
{
|
|
||||||
$this->username = $username;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the password used to authenticate to LimeSurvey
|
* Returns the password used to authenticate to LimeSurvey
|
||||||
*
|
*
|
||||||
@@ -165,19 +141,6 @@ class ConnectionConfiguration
|
|||||||
return $this->password;
|
return $this->password;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the password used to authenticate to LimeSurvey
|
|
||||||
*
|
|
||||||
* @param string $password The password used to authenticate to LimeSurvey
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setPassword($password)
|
|
||||||
{
|
|
||||||
$this->password = $password;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns information if the "debug" mode is turned on
|
* Returns information if the "debug" mode is turned on
|
||||||
*
|
*
|
||||||
@@ -189,24 +152,15 @@ class ConnectionConfiguration
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets information if the "debug" mode is turned on
|
* Returns information if the SSL certificate verification is turned on
|
||||||
*
|
*
|
||||||
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
|
* @return bool
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
public function setDebugMode($debugMode = false)
|
public function isVerifySslCertificateOn()
|
||||||
{
|
{
|
||||||
$this->debugMode = $debugMode;
|
return $this->verifySslCertificate;
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
||||||
* Additional / extra methods (neither getters, nor setters)
|
|
||||||
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns full url of the LimeSurvey's API.
|
* Returns full url of the LimeSurvey's API.
|
||||||
* It's a base url with part related to remote control.
|
* It's a base url with part related to remote control.
|
||||||
@@ -217,4 +171,27 @@ class ConnectionConfiguration
|
|||||||
{
|
{
|
||||||
return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl);
|
return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the base url, protocol & domain
|
||||||
|
*
|
||||||
|
* @param string $baseUrl The base url, protocol & domain
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws InvalidUrlException
|
||||||
|
*/
|
||||||
|
private function setBaseUrl($baseUrl)
|
||||||
|
{
|
||||||
|
if (!Regex::isValidUrl($baseUrl)) {
|
||||||
|
throw new InvalidUrlException($baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Regex::endsWith($baseUrl, '/')) {
|
||||||
|
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->baseUrl = $baseUrl;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
50
src/Exception/CannotProcessDataException.php
Normal file
50
src/Exception/CannotProcessDataException.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while raw data returned by the LimeSurvey's API cannot be processed
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class CannotProcessDataException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $reason;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $reason Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*/
|
||||||
|
public function __construct($reason)
|
||||||
|
{
|
||||||
|
$this->reason = $reason;
|
||||||
|
|
||||||
|
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||||
|
$message = sprintf($template, $this->reason);
|
||||||
|
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getReason()
|
||||||
|
{
|
||||||
|
return $this->reason;
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/Exception/IncorrectClassOfResultItemException.php
Normal file
35
src/Exception/IncorrectClassOfResultItemException.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while class used to create instance of one item of the result, with data fetched from the
|
||||||
|
* LimeSurvey's API, is incorrect
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class IncorrectClassOfResultItemException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $className Incorrect class name used to create instance of one item
|
||||||
|
*/
|
||||||
|
public function __construct($className)
|
||||||
|
{
|
||||||
|
$template = 'Class %s used to create instance of one item of the result should extend %s, but it does not. Did'
|
||||||
|
. ' you forget to use proper base class?';
|
||||||
|
|
||||||
|
$message = sprintf($template, $className, BaseItem::class);
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/Exception/InvalidResultOfMethodRunException.php
Normal file
45
src/Exception/InvalidResultOfMethodRunException.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Meritoo\Common\Utilities\Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used when an error occurred while running method
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class InvalidResultOfMethodRunException extends Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param Exception $previousException The previous exception, source of an error
|
||||||
|
* @param string $methodName Name of called method
|
||||||
|
* @param array $methodArguments (optional) Arguments of the called method
|
||||||
|
*/
|
||||||
|
public function __construct(Exception $previousException, $methodName, array $methodArguments = [])
|
||||||
|
{
|
||||||
|
$template = "Oops, an error occurred while running method. Is there everything ok? Details:\n"
|
||||||
|
. "- error: %s,\n"
|
||||||
|
. "- method: %s,\n"
|
||||||
|
. '- arguments: %s.';
|
||||||
|
|
||||||
|
if (empty($methodArguments)) {
|
||||||
|
$methodArguments = '(no arguments)';
|
||||||
|
} else {
|
||||||
|
$methodArguments = Arrays::valuesKeys2string($methodArguments, ', ', '=', '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = sprintf($template, $previousException->getMessage(), $methodName, $methodArguments);
|
||||||
|
parent::__construct($message, $previousException->getCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/Exception/MissingParticipantOfSurveyException.php
Normal file
32
src/Exception/MissingParticipantOfSurveyException.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used when participant of survey is missing
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class MissingParticipantOfSurveyException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
*/
|
||||||
|
public function __construct($surveyId, $email)
|
||||||
|
{
|
||||||
|
$template = 'Participant with e-mail %s of survey with ID %s is missing. Maybe was not added to the survey?';
|
||||||
|
$message = sprintf($template, $email, $surveyId);
|
||||||
|
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,19 @@
|
|||||||
<?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\ApiClient\Exception;
|
namespace Meritoo\LimeSurvey\ApiClient\Exception;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An exception used while instance of one item used by result, with data fetched from the LimeSurvey's API, is unknown
|
* An exception used while class name used to create instance of one item of the result, with data fetched from the
|
||||||
|
* LimeSurvey's API, is unknown
|
||||||
*
|
*
|
||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
@@ -21,10 +28,10 @@ class UnknownInstanceOfResultItem extends Exception
|
|||||||
*/
|
*/
|
||||||
public function __construct($method)
|
public function __construct($method)
|
||||||
{
|
{
|
||||||
$template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper'
|
$template = 'Class name used to create instance of one item used by result the of \'%s\' LimeSurvey API\'s'
|
||||||
. ' class is not mapped in %s::%s() method. Did you forget about this?';
|
. ' method is unknown. Proper class is not mapped in %s::%s() method. Did you forget about this?';
|
||||||
|
|
||||||
$message = sprintf($template, $method, ResultProcessor::class, 'getItemInstance');
|
$message = sprintf($template, $method, ResultProcessor::class, 'getItemClassName');
|
||||||
parent::__construct($message);
|
parent::__construct($message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,9 @@
|
|||||||
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
||||||
|
|
||||||
use JsonRPC\Client as RpcClient;
|
use JsonRPC\Client as RpcClient;
|
||||||
|
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
@@ -47,14 +49,22 @@ class JsonRpcClientManager
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @throws UnknownMethodException
|
* @throws UnknownMethodException
|
||||||
|
* @throws InvalidResultOfMethodRunException
|
||||||
*/
|
*/
|
||||||
public function runMethod($method, $arguments = [])
|
public function runMethod($method, $arguments = [])
|
||||||
{
|
{
|
||||||
|
$result = null;
|
||||||
$method = MethodType::getValidatedMethod($method);
|
$method = MethodType::getValidatedMethod($method);
|
||||||
|
|
||||||
return $this
|
try {
|
||||||
->getRpcClient()
|
$result = $this
|
||||||
->execute($method, $arguments);
|
->getRpcClient()
|
||||||
|
->execute($method, $arguments);
|
||||||
|
} catch (InvalidJsonFormatException $exception) {
|
||||||
|
throw new InvalidResultOfMethodRunException($exception, $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,7 +85,20 @@ class JsonRpcClientManager
|
|||||||
* The "debug" mode is turned on?
|
* The "debug" mode is turned on?
|
||||||
*/
|
*/
|
||||||
if ($this->connectionConfiguration->isDebugModeOn()) {
|
if ($this->connectionConfiguration->isDebugModeOn()) {
|
||||||
$this->rpcClient->getHttpClient()->withDebug();
|
$this
|
||||||
|
->rpcClient
|
||||||
|
->getHttpClient()
|
||||||
|
->withDebug();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The SSL certificate verification is turned off?
|
||||||
|
*/
|
||||||
|
if (!$this->connectionConfiguration->isVerifySslCertificateOn()) {
|
||||||
|
$this
|
||||||
|
->rpcClient
|
||||||
|
->getHttpClient()
|
||||||
|
->withoutSslVerification();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
151
src/Result/Collection/Participants.php
Normal file
151
src/Result/Collection/Participants.php
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
<?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\Common\Collection\Collection;
|
||||||
|
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of participants (of surveys).
|
||||||
|
* 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 Participants extends Collection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
58
src/Result/Collection/Surveys.php
Normal file
58
src/Result/Collection/Surveys.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?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\Common\Collection\Collection;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of surveys (the Survey class instances)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class Surveys extends Collection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function add($element, $index = null)
|
||||||
|
{
|
||||||
|
if (null === $index) {
|
||||||
|
/* @var Survey $element */
|
||||||
|
$index = $element->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::add($element, $index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all or active only surveys
|
||||||
|
*
|
||||||
|
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function getAll($onlyActive = false)
|
||||||
|
{
|
||||||
|
if ($this->isEmpty() || !$onlyActive) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$all = new static();
|
||||||
|
|
||||||
|
/* @var Survey $survey */
|
||||||
|
foreach ($this as $survey) {
|
||||||
|
if ($survey->isActive()) {
|
||||||
|
$all->add($survey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ use Meritoo\Common\Utilities\Date;
|
|||||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One item of the result/data: full data of participant
|
* One item of the result/data: full data of one participant
|
||||||
*
|
*
|
||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
@@ -183,15 +183,15 @@ class Participant extends BaseItem
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'blacklisted':
|
case 'blacklisted':
|
||||||
$this->blacklisted = 'Y' === trim($value);
|
$this->blacklisted = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sent':
|
case 'sent':
|
||||||
$this->sent = 'Y' === trim($value);
|
$this->sent = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'remindersent':
|
case 'remindersent':
|
||||||
$this->reminderSent = 'Y' === trim($value);
|
$this->reminderSent = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'remindercount':
|
case 'remindercount':
|
||||||
@@ -199,7 +199,12 @@ class Participant extends BaseItem
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'completed':
|
case 'completed':
|
||||||
$this->completed = 'Y' === trim($value);
|
if ('N' === trim(strtoupper($value))) {
|
||||||
|
$this->completed = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->completed = Date::isValidDate($value, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'usesleft':
|
case 'usesleft':
|
||||||
@@ -211,7 +216,7 @@ class Participant extends BaseItem
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->validFrom = Date::getDateTime($value, false, 'Y-m-d H:i:s');
|
$this->validFrom = Date::getDateTime($value, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'validuntil':
|
case 'validuntil':
|
||||||
@@ -219,7 +224,7 @@ class Participant extends BaseItem
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->validUntil = Date::getDateTime($value, false, 'Y-m-d H:i:s');
|
$this->validUntil = Date::getDateTime($value, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -193,11 +193,11 @@ class QuestionShort extends BaseItem
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'other':
|
case 'other':
|
||||||
$this->other = 'Y' === trim($value);
|
$this->other = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'mandatory':
|
case 'mandatory':
|
||||||
$this->mandatory = 'Y' === trim($value);
|
$this->mandatory = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'question_order':
|
case 'question_order':
|
||||||
@@ -74,7 +74,7 @@ class Survey extends BaseItem
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->startsAt = Date::getDateTime($value, false, 'Y-m-d H:i:s');
|
$this->startsAt = Date::getDateTime($value, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'expires':
|
case 'expires':
|
||||||
@@ -82,11 +82,11 @@ class Survey extends BaseItem
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->expiresAt = Date::getDateTime($value, false, 'Y-m-d H:i:s');
|
$this->expiresAt = Date::getDateTime($value, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'active':
|
case 'active':
|
||||||
$this->active = 'Y' === trim($value);
|
$this->active = 'Y' === trim(strtoupper($value));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,9 @@
|
|||||||
|
|
||||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Processor;
|
namespace Meritoo\LimeSurvey\ApiClient\Result\Processor;
|
||||||
|
|
||||||
|
use Meritoo\Common\Utilities\Reflection;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\IncorrectClassOfResultItemException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
||||||
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;
|
||||||
@@ -31,7 +33,9 @@ class ResultProcessor
|
|||||||
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
||||||
* class constants.
|
* class constants.
|
||||||
* @param array $rawData Data returned by the LimeSurvey's API
|
* @param array $rawData Data returned by the LimeSurvey's API
|
||||||
* @return null|BaseItem|array
|
* @return array|BaseItem|null
|
||||||
|
*
|
||||||
|
* @throws IncorrectClassOfResultItemException
|
||||||
*/
|
*/
|
||||||
public function process($method, array $rawData)
|
public function process($method, array $rawData)
|
||||||
{
|
{
|
||||||
@@ -46,59 +50,61 @@ class ResultProcessor
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare instance of one item
|
* Prepare class name for instance of one item
|
||||||
*/
|
*/
|
||||||
$item = $this->getItemInstance($method);
|
$itemClassName = $this->getItemClassName($method);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The raw data is or, actually, should be iterable?
|
* The raw data is or, actually, should be iterable?
|
||||||
*/
|
*/
|
||||||
if (MethodType::isResultIterable($method)) {
|
if (MethodType::isResultIterable($method)) {
|
||||||
$items = [];
|
$items = [];
|
||||||
$emptyItem = clone $item;
|
|
||||||
|
|
||||||
foreach ($rawData as $itemData) {
|
foreach ($rawData as $itemData) {
|
||||||
$items[] = $emptyItem->setValues($itemData);
|
$items[] = new $itemClassName($itemData);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $items;
|
return $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $item->setValues($rawData);
|
return new $itemClassName($rawData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns instance of one item of the result
|
* Returns class name used to create instance of one item of the result
|
||||||
*
|
*
|
||||||
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
||||||
* class constants.
|
* class constants.
|
||||||
* @return BaseItem
|
* @return string
|
||||||
|
*
|
||||||
|
* @throws IncorrectClassOfResultItemException
|
||||||
* @throws UnknownInstanceOfResultItem
|
* @throws UnknownInstanceOfResultItem
|
||||||
*/
|
*/
|
||||||
private function getItemInstance($method)
|
private function getItemClassName($method)
|
||||||
{
|
{
|
||||||
$item = null;
|
$className = null;
|
||||||
$method = MethodType::getValidatedMethod($method);
|
$method = MethodType::getValidatedMethod($method);
|
||||||
|
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
|
case MethodType::ADD_PARTICIPANTS:
|
||||||
case MethodType::GET_PARTICIPANT_PROPERTIES:
|
case MethodType::GET_PARTICIPANT_PROPERTIES:
|
||||||
$item = new Participant();
|
$className = Participant::class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MethodType::GET_QUESTION_PROPERTIES:
|
case MethodType::GET_QUESTION_PROPERTIES:
|
||||||
$item = new Question();
|
$className = Question::class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MethodType::LIST_PARTICIPANTS:
|
case MethodType::LIST_PARTICIPANTS:
|
||||||
$item = new ParticipantShort();
|
$className = ParticipantShort::class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MethodType::LIST_QUESTIONS:
|
case MethodType::LIST_QUESTIONS:
|
||||||
$item = new QuestionShort();
|
$className = QuestionShort::class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MethodType::LIST_SURVEYS:
|
case MethodType::LIST_SURVEYS:
|
||||||
$item = new Survey();
|
$className = Survey::class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -107,12 +113,19 @@ class ResultProcessor
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Instance of the item is unknown?
|
* Oops, class name for instance of the item is unknown
|
||||||
*/
|
*/
|
||||||
if (null === $item) {
|
if (null === $className) {
|
||||||
throw new UnknownInstanceOfResultItem($method);
|
throw new UnknownInstanceOfResultItem($method);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $item;
|
if (Reflection::isChildOfClass($className, BaseItem::class)) {
|
||||||
|
return $className;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Oops, class is incorrect (should extend BaseItem)
|
||||||
|
*/
|
||||||
|
throw new IncorrectClassOfResultItemException($className);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Result;
|
|||||||
|
|
||||||
use Meritoo\Common\Collection\Collection;
|
use Meritoo\Common\Collection\Collection;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
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;
|
||||||
|
|
||||||
@@ -35,6 +36,13 @@ class Result
|
|||||||
*/
|
*/
|
||||||
private $rawData;
|
private $rawData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Status, information returned instead of usual/normal result
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processor of the raw data fetched while talking to the LimeSurvey's API
|
* Processor of the raw data fetched while talking to the LimeSurvey's API
|
||||||
*
|
*
|
||||||
@@ -52,7 +60,7 @@ class Result
|
|||||||
public function __construct($method, array $rawData)
|
public function __construct($method, array $rawData)
|
||||||
{
|
{
|
||||||
$this->method = MethodType::getValidatedMethod($method);
|
$this->method = MethodType::getValidatedMethod($method);
|
||||||
$this->rawData = $rawData;
|
$this->setRawDataAndStatus($rawData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,14 +79,41 @@ class Result
|
|||||||
* @param bool $raw (optional) If is set to true, raw data provided by the LimeSurvey's API will be returned.
|
* @param bool $raw (optional) If is set to true, raw data provided by the LimeSurvey's API will be returned.
|
||||||
* Otherwise - prepared/processed.
|
* Otherwise - prepared/processed.
|
||||||
* @return array|Collection|BaseItem
|
* @return array|Collection|BaseItem
|
||||||
|
* @throws CannotProcessDataException
|
||||||
*/
|
*/
|
||||||
public function getData($raw = false)
|
public function getData($raw = false)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* Raw data should be returned only?
|
||||||
|
* Let's do it
|
||||||
|
*/
|
||||||
if ($raw) {
|
if ($raw) {
|
||||||
return $this->rawData;
|
return $this->rawData;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getProcessedData($this->rawData);
|
/*
|
||||||
|
* Status is unknown?
|
||||||
|
* Let's process the raw data
|
||||||
|
*/
|
||||||
|
if (empty($this->status)) {
|
||||||
|
return $this->getProcessedData($this->rawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Oops, the raw data returned by the LimeSurvey's API cannot be processed, because status was provided.
|
||||||
|
* Well, probably something is broken and... there is no data.
|
||||||
|
*/
|
||||||
|
throw new CannotProcessDataException($this->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns status, information returned instead of usual/normal result
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getStatus()
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,7 +128,11 @@ class Result
|
|||||||
->getResultProcessor()
|
->getResultProcessor()
|
||||||
->process($this->method, $rawData);
|
->process($this->method, $rawData);
|
||||||
|
|
||||||
if (null === $processed || is_array($processed)) {
|
/*
|
||||||
|
* Result is unknown and it should be iterable the result is an array?
|
||||||
|
* Let's prepare and return collection
|
||||||
|
*/
|
||||||
|
if ((null === $processed && MethodType::isResultIterable($this->method)) || is_array($processed)) {
|
||||||
$collection = new Collection();
|
$collection = new Collection();
|
||||||
|
|
||||||
if (is_array($processed)) {
|
if (is_array($processed)) {
|
||||||
@@ -119,4 +158,23 @@ class Result
|
|||||||
|
|
||||||
return $this->resultProcessor;
|
return $this->resultProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets status, information returned instead of usual/normal result and raw data returned by the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @param array $rawData Raw data returned by the LimeSurvey's API
|
||||||
|
*/
|
||||||
|
private function setRawDataAndStatus(array $rawData)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Status was provided?
|
||||||
|
* Well, probably something is broken and... there is no data
|
||||||
|
*/
|
||||||
|
if (isset($rawData['status'])) {
|
||||||
|
$this->status = trim($rawData['status']);
|
||||||
|
$rawData = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rawData = $rawData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
255
src/Service/ParticipantService.php
Normal file
255
src/Service/ParticipantService.php
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
<?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\Service;
|
||||||
|
|
||||||
|
use Meritoo\Common\Collection\Collection;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||||
|
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\Type\MethodType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service that serves participants
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ParticipantService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Client of the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @var Client
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of participants (of surveys).
|
||||||
|
* All participants grouped per survey.
|
||||||
|
*
|
||||||
|
* @var Participants
|
||||||
|
*/
|
||||||
|
private $allParticipants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param Client $client Client of the LimeSurvey's API
|
||||||
|
* @param Participants $allParticipants (optional) Collection of participants (of surveys). All participants
|
||||||
|
* grouped per survey.
|
||||||
|
*/
|
||||||
|
public function __construct(Client $client, Participants $allParticipants = null)
|
||||||
|
{
|
||||||
|
if (null === $allParticipants) {
|
||||||
|
$allParticipants = new Participants();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->client = $client;
|
||||||
|
$this->allParticipants = $allParticipants;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns client of the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @return Client
|
||||||
|
*/
|
||||||
|
public function getClient()
|
||||||
|
{
|
||||||
|
return $this->client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns participants of given survey
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @return Collection
|
||||||
|
*
|
||||||
|
* @throws CannotProcessDataException
|
||||||
|
*/
|
||||||
|
public function getSurveyParticipants($surveyId)
|
||||||
|
{
|
||||||
|
$hasSurvey = $this
|
||||||
|
->allParticipants
|
||||||
|
->hasParticipantsOfSurvey($surveyId);
|
||||||
|
|
||||||
|
if (!$hasSurvey) {
|
||||||
|
$arguments = [
|
||||||
|
$surveyId,
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$participants = $this
|
||||||
|
->client
|
||||||
|
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||||
|
->getData();
|
||||||
|
} catch (CannotProcessDataException $exception) {
|
||||||
|
$reason = $exception->getReason();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reason of the exception is different than "Oops, there is no participants. Everything else is fine."?
|
||||||
|
* Let's throw the exception
|
||||||
|
*/
|
||||||
|
if (ReasonType::NO_PARTICIPANTS_FOUND !== $reason) {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
$participants = new Collection();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this
|
||||||
|
->allParticipants
|
||||||
|
->addParticipants($participants, $surveyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this
|
||||||
|
->allParticipants
|
||||||
|
->getBySurvey($surveyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns information if given survey has participant with given e-mail
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasParticipant($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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds participant with given data to survey with given ID
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $firstName First name of the participant to add
|
||||||
|
* @param string $lastName Last ame of the participant to add
|
||||||
|
* @param string $email E-mail address of the participant to add
|
||||||
|
* @return Participant
|
||||||
|
*/
|
||||||
|
public function addParticipant($surveyId, $firstName, $lastName, $email)
|
||||||
|
{
|
||||||
|
$participantsData = [
|
||||||
|
[
|
||||||
|
'firstname' => $firstName,
|
||||||
|
'lastname' => $lastName,
|
||||||
|
'email' => $email,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$arguments = [
|
||||||
|
$surveyId,
|
||||||
|
$participantsData,
|
||||||
|
];
|
||||||
|
|
||||||
|
$participantCollection = $this
|
||||||
|
->client
|
||||||
|
->run(MethodType::ADD_PARTICIPANTS, $arguments)
|
||||||
|
->getData();
|
||||||
|
|
||||||
|
/* @var Participant $addedParticipant */
|
||||||
|
$addedParticipant = $participantCollection->getFirst();
|
||||||
|
|
||||||
|
$participants = new Collection([
|
||||||
|
ParticipantShort::fromParticipant($addedParticipant),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->allParticipants
|
||||||
|
->addParticipants($participants, $surveyId);
|
||||||
|
|
||||||
|
return $participantCollection->getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns short data of one participant with given e-mail of given survey
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
* @return ParticipantShort|null
|
||||||
|
*/
|
||||||
|
public function getParticipant($surveyId, $email)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* I have to get all participants of survey to avoid problem when participants exist but are not loaded
|
||||||
|
*/
|
||||||
|
$this->getSurveyParticipants($surveyId);
|
||||||
|
|
||||||
|
return $this
|
||||||
|
->allParticipants
|
||||||
|
->getParticipantOfSurvey($surveyId, $email);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns full data of participant with given e-mail of given survey
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
* @return Participant|null
|
||||||
|
*/
|
||||||
|
public function getParticipantDetails($surveyId, $email)
|
||||||
|
{
|
||||||
|
$arguments = [
|
||||||
|
$surveyId,
|
||||||
|
[
|
||||||
|
'email' => $email,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$participant = $this
|
||||||
|
->client
|
||||||
|
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||||
|
->getData();
|
||||||
|
|
||||||
|
/* @var Participant $participant */
|
||||||
|
return $participant;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns information if participant with given e-mail has filled given survey
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @throws MissingParticipantOfSurveyException
|
||||||
|
*/
|
||||||
|
public function hasParticipantFilledSurvey($surveyId, $email)
|
||||||
|
{
|
||||||
|
if ($this->hasParticipant($surveyId, $email)) {
|
||||||
|
$arguments = [
|
||||||
|
$surveyId,
|
||||||
|
[
|
||||||
|
'email' => $email,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
/* @var Participant $participant */
|
||||||
|
$participant = $this
|
||||||
|
->client
|
||||||
|
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||||
|
->getData();
|
||||||
|
|
||||||
|
return true === $participant->isCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new MissingParticipantOfSurveyException($surveyId, $email);
|
||||||
|
}
|
||||||
|
}
|
||||||
168
src/Service/SurveyService.php
Normal file
168
src/Service/SurveyService.php
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
<?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\Service;
|
||||||
|
|
||||||
|
use Meritoo\Common\Collection\Collection;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service that serves surveys
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class SurveyService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Client of the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @var Client
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All surveys.
|
||||||
|
* Collection of surveys (the Survey class instances).
|
||||||
|
*
|
||||||
|
* @var Surveys
|
||||||
|
*/
|
||||||
|
private $allSurveys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template of the url used to start survey
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* - url: https://your.limesurvey.instance/12345?token=q1w2e3r4t5y6
|
||||||
|
* - LimeSurvey frontend: https://your.limesurvey.instance
|
||||||
|
* - survey ID: 12345
|
||||||
|
* - token: q1w2e3r4t5y6
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $startSurveyUrlTemplate = '%s/%d?token=%s';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param Client $client Client of the LimeSurvey's API
|
||||||
|
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class instances).
|
||||||
|
*/
|
||||||
|
public function __construct(Client $client, Surveys $allSurveys = null)
|
||||||
|
{
|
||||||
|
if (null === $allSurveys) {
|
||||||
|
$allSurveys = new Surveys();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->client = $client;
|
||||||
|
$this->allSurveys = $allSurveys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns client of the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @return Client
|
||||||
|
*/
|
||||||
|
public function getClient()
|
||||||
|
{
|
||||||
|
return $this->client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all surveys
|
||||||
|
*
|
||||||
|
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
|
||||||
|
* @return Surveys
|
||||||
|
*
|
||||||
|
* @throws CannotProcessDataException
|
||||||
|
*/
|
||||||
|
public function getAllSurveys($onlyActive = false)
|
||||||
|
{
|
||||||
|
if ($this->allSurveys->isEmpty()) {
|
||||||
|
$surveys = new Surveys();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$surveys = $this
|
||||||
|
->client
|
||||||
|
->run(MethodType::LIST_SURVEYS)
|
||||||
|
->getData();
|
||||||
|
} catch (CannotProcessDataException $exception) {
|
||||||
|
$reason = $exception->getReason();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reason of the exception is different than "Oops, there is no surveys. Everything else is fine."?
|
||||||
|
* Let's throw the exception
|
||||||
|
*/
|
||||||
|
if (ReasonType::NO_SURVEYS_FOUND !== $reason) {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $surveys && $surveys instanceof Collection) {
|
||||||
|
$this->allSurveys = new Surveys($surveys->toArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->allSurveys->getAll($onlyActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns information if survey with given ID exists
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey to verify
|
||||||
|
* @param bool $shouldBeActive (optional) If is set to true, survey should be active. If it's not, it shouldn't
|
||||||
|
* be returned, even if exists. Otherwise - it doesn't matter.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isExistingSurvey($surveyId, $shouldBeActive = false)
|
||||||
|
{
|
||||||
|
$allSurveys = $this->getAllSurveys($shouldBeActive);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* No surveys?
|
||||||
|
* Nothing to do
|
||||||
|
*/
|
||||||
|
if ($allSurveys->isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$surveyId = (int)$surveyId;
|
||||||
|
|
||||||
|
/* @var Survey $survey */
|
||||||
|
foreach ($allSurveys as $survey) {
|
||||||
|
if ($survey->getId() == $surveyId) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns url used to start survey for given survey and participant
|
||||||
|
*
|
||||||
|
* @param int $surveyId ID of survey to start
|
||||||
|
* @param Participant $participant Participant who would like to start survey
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getStartSurveyUrl($surveyId, Participant $participant)
|
||||||
|
{
|
||||||
|
$baseUrl = $this
|
||||||
|
->client
|
||||||
|
->getConfiguration()
|
||||||
|
->getBaseUrl();
|
||||||
|
|
||||||
|
return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participant->getToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,16 @@ use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
|||||||
*/
|
*/
|
||||||
class MethodType extends BaseType
|
class MethodType extends BaseType
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Add participants to the tokens collection of the survey
|
||||||
|
*
|
||||||
|
* Returns the inserted data including additional new information like the Token entry ID and the token string.
|
||||||
|
* In case of errors in some data, return it in errors.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const ADD_PARTICIPANTS = 'add_participants';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a response to the survey responses collection.
|
* Add a response to the survey responses collection.
|
||||||
* Returns the id of the inserted survey response.
|
* Returns the id of the inserted survey response.
|
||||||
@@ -86,7 +96,7 @@ class MethodType extends BaseType
|
|||||||
*/
|
*/
|
||||||
public static function getValidatedMethod($method)
|
public static function getValidatedMethod($method)
|
||||||
{
|
{
|
||||||
if ((new static())->isCorrectType($method)) {
|
if ((new static())->isCorrectType($method) || (new SystemMethodType())->isCorrectType($method)) {
|
||||||
return $method;
|
return $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +114,7 @@ class MethodType extends BaseType
|
|||||||
$method = static::getValidatedMethod($method);
|
$method = static::getValidatedMethod($method);
|
||||||
|
|
||||||
return in_array($method, [
|
return in_array($method, [
|
||||||
|
static::ADD_PARTICIPANTS,
|
||||||
static::LIST_PARTICIPANTS,
|
static::LIST_PARTICIPANTS,
|
||||||
static::LIST_QUESTIONS,
|
static::LIST_QUESTIONS,
|
||||||
static::LIST_SURVEYS,
|
static::LIST_SURVEYS,
|
||||||
42
src/Type/ReasonType.php
Normal file
42
src/Type/ReasonType.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Meritoo\LimeSurvey\ApiClient\Type;
|
||||||
|
|
||||||
|
use Meritoo\Common\Type\Base\BaseType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of reason used by LimeSurvey's exception
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ReasonType extends BaseType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no survey with given ID
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NOT_EXISTING_SURVEY_ID = 'Error: Invalid survey ID';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no participants of survey
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no surveys
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NO_SURVEYS_FOUND = 'No surveys found';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reason of exception when there is no table with tokens/participants of survey
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const NO_TOKEN_TABLE = 'Error: No token table';
|
||||||
|
}
|
||||||
32
tests/Base/Result/BaseItemTest.php
Normal file
32
tests/Base/Result/BaseItemTest.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?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\Base\Result;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the base class for one item of result/data fetched while talking to the LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class BaseItemTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(BaseItem::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetValuesVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertMethodVisibilityAndArguments(BaseItem::class, 'setValues', OopVisibilityType::IS_PRIVATE, 1, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,6 +34,11 @@ class ClientTest extends BaseTestCase
|
|||||||
*/
|
*/
|
||||||
private $configuration;
|
private $configuration;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Client::class, OopVisibilityType::IS_PUBLIC, 3, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $incorrectMethod Incorrect name of method to call
|
* @param string $incorrectMethod Incorrect name of method to call
|
||||||
* @dataProvider provideIncorrectMethod
|
* @dataProvider provideIncorrectMethod
|
||||||
@@ -47,13 +52,14 @@ class ClientTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method Name of method to call
|
* @param string $method Name of method to call
|
||||||
* @param array $arguments Arguments of the method to call
|
* @param array $arguments Arguments of the method to call
|
||||||
* @param bool $debugMode If is set to true, the "debug" mode is turned on. Otherwise - turned off.
|
* @param bool $debugMode If is set to true, the "debug" mode is turned on. Otherwise - turned off.
|
||||||
|
* @param mixed $expectedRawData Expected raw data returned by JsonRpcClient
|
||||||
*
|
*
|
||||||
* @dataProvider provideMethod
|
* @dataProvider provideMethod
|
||||||
*/
|
*/
|
||||||
public function testRun($method, $arguments, $debugMode)
|
public function testRun($method, $arguments, $debugMode, $expectedRawData)
|
||||||
{
|
{
|
||||||
$sessionManager = $this->createMock(SessionManager::class);
|
$sessionManager = $this->createMock(SessionManager::class);
|
||||||
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
|
$rpcClientManager = $this->createMock(JsonRpcClientManager::class);
|
||||||
@@ -61,22 +67,34 @@ class ClientTest extends BaseTestCase
|
|||||||
$rpcClientManager
|
$rpcClientManager
|
||||||
->expects(static::any())
|
->expects(static::any())
|
||||||
->method('runMethod')
|
->method('runMethod')
|
||||||
->willReturn([]);
|
->willReturn($expectedRawData);
|
||||||
|
|
||||||
$this->configuration->setDebugMode($debugMode);
|
$configuration = new ConnectionConfiguration(
|
||||||
$client = new Client($this->configuration, $rpcClientManager, $sessionManager);
|
$this->configuration->getBaseUrl(),
|
||||||
|
$this->configuration->getUsername(),
|
||||||
|
$this->configuration->getPassword(),
|
||||||
|
$debugMode,
|
||||||
|
$this->configuration->isVerifySslCertificateOn()
|
||||||
|
);
|
||||||
|
|
||||||
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
||||||
static::assertInstanceOf(Result::class, $client->run($method, $arguments));
|
static::assertInstanceOf(Result::class, $client->run($method, $arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetConfiguration()
|
||||||
|
{
|
||||||
|
$client = new Client($this->configuration);
|
||||||
|
static::assertEquals($this->configuration, $client->getConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetRpcClientManagerVisibilityAndArguments()
|
public function testGetRpcClientManagerVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(Client::class, 'getRpcClientManager', OopVisibilityType::IS_PRIVATE);
|
static::assertMethodVisibilityAndArguments(Client::class, 'getRpcClientManager', OopVisibilityType::IS_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSessionManagerVisibilityAndArguments()
|
public function testGetSessionManagerVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(Client::class, 'getRpcClientManager', OopVisibilityType::IS_PRIVATE);
|
static::assertMethodVisibilityAndArguments(Client::class, 'getRpcClientManager', OopVisibilityType::IS_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,18 +128,21 @@ class ClientTest extends BaseTestCase
|
|||||||
MethodType::GET_PARTICIPANT_PROPERTIES,
|
MethodType::GET_PARTICIPANT_PROPERTIES,
|
||||||
[],
|
[],
|
||||||
true,
|
true,
|
||||||
|
[],
|
||||||
];
|
];
|
||||||
|
|
||||||
yield[
|
yield[
|
||||||
MethodType::LIST_SURVEYS,
|
MethodType::LIST_SURVEYS,
|
||||||
[],
|
[],
|
||||||
false,
|
false,
|
||||||
|
[],
|
||||||
];
|
];
|
||||||
|
|
||||||
yield[
|
yield[
|
||||||
MethodType::LIST_PARTICIPANTS,
|
MethodType::LIST_PARTICIPANTS,
|
||||||
[],
|
[],
|
||||||
false,
|
false,
|
||||||
|
null,
|
||||||
];
|
];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
140
tests/Configuration/ConnectionConfigurationTest.php
Normal file
140
tests/Configuration/ConnectionConfigurationTest.php
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
<?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\Configuration;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Exception\Regex\InvalidUrlException;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the configuration used while connecting to LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ConnectionConfigurationTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Configuration with default values of optional constructor's arguments
|
||||||
|
*
|
||||||
|
* @var ConnectionConfiguration
|
||||||
|
*/
|
||||||
|
private $configurationWithDefaults;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration without default values of optional constructor's arguments
|
||||||
|
*
|
||||||
|
* @var ConnectionConfiguration
|
||||||
|
*/
|
||||||
|
private $configurationAnother;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(ConnectionConfiguration::class, OopVisibilityType::IS_PUBLIC, 5, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $emptyBaseUrl Empty base url
|
||||||
|
* @dataProvider provideEmptyBaseUrl
|
||||||
|
*/
|
||||||
|
public function testConstructorWithEmptyBaseUrl($emptyBaseUrl)
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidUrlException::class);
|
||||||
|
new ConnectionConfiguration($emptyBaseUrl, '', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $invalidBaseUrl Invalid base url
|
||||||
|
* @dataProvider provideInvalidBaseUrl
|
||||||
|
*/
|
||||||
|
public function testConstructorWithInvalidBaseUrl($invalidBaseUrl)
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidUrlException::class);
|
||||||
|
new ConnectionConfiguration($invalidBaseUrl, '', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstructor()
|
||||||
|
{
|
||||||
|
static::assertEquals('http://test.com', $this->configurationWithDefaults->getBaseUrl());
|
||||||
|
static::assertEquals('test1', $this->configurationWithDefaults->getUsername());
|
||||||
|
static::assertEquals('test2', $this->configurationWithDefaults->getPassword());
|
||||||
|
static::assertFalse($this->configurationWithDefaults->isDebugModeOn());
|
||||||
|
static::assertTrue($this->configurationWithDefaults->isVerifySslCertificateOn());
|
||||||
|
|
||||||
|
static::assertEquals('http://lets-test.com', $this->configurationAnother->getBaseUrl());
|
||||||
|
static::assertEquals('test11', $this->configurationAnother->getUsername());
|
||||||
|
static::assertEquals('test22', $this->configurationAnother->getPassword());
|
||||||
|
static::assertTrue($this->configurationAnother->isDebugModeOn());
|
||||||
|
static::assertFalse($this->configurationAnother->isVerifySslCertificateOn());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetRemoteControlUrl()
|
||||||
|
{
|
||||||
|
$this->configurationWithDefaults->setRemoteControlUrl('lorem/ipsum');
|
||||||
|
static::assertEquals('lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl());
|
||||||
|
|
||||||
|
$this->configurationAnother->setRemoteControlUrl('dolor/sit');
|
||||||
|
static::assertEquals('dolor/sit', $this->configurationAnother->getRemoteControlUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetFullUrl()
|
||||||
|
{
|
||||||
|
$this->configurationWithDefaults->setRemoteControlUrl('lorem/ipsum');
|
||||||
|
static::assertEquals('http://test.com/lorem/ipsum', $this->configurationWithDefaults->getFullUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides empty base url
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideEmptyBaseUrl()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides invalid base url
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideInvalidBaseUrl()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'lorem',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'ipsum',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'htp:/dolor.com',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->configurationWithDefaults = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
||||||
|
$this->configurationAnother = new ConnectionConfiguration('http://lets-test.com/', 'test11', 'test22', true, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
60
tests/Exception/CannotProcessDataExceptionTest.php
Normal file
60
tests/Exception/CannotProcessDataExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while raw data returned by the LimeSurvey's API cannot be processed
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class CannotProcessDataExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(CannotProcessDataException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $reason Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideReason
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($reason, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new CannotProcessDataException($reason);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides reason why data cannot be processed
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideReason()
|
||||||
|
{
|
||||||
|
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'unknown',
|
||||||
|
sprintf($template, 'unknown'),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'Invalid user name or password',
|
||||||
|
sprintf($template, 'Invalid user name or password'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
61
tests/Exception/CreateSessionKeyFailedExceptionTest.php
Normal file
61
tests/Exception/CreateSessionKeyFailedExceptionTest.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CreateSessionKeyFailedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while create of the session key has failed
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class CreateSessionKeyFailedExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(CreateSessionKeyFailedException::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $reason Reason of failure, e.g. "Invalid user name or password"
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideReason
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($reason, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new CreateSessionKeyFailedException($reason);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides reason of failure
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideReason()
|
||||||
|
{
|
||||||
|
$shortMessage = 'Create of the session key has failed';
|
||||||
|
$longMessageTemplate = sprintf('%s. Reason: \'%s\'.', $shortMessage, '%s');
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'',
|
||||||
|
$shortMessage,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'Invalid user name or password',
|
||||||
|
sprintf($longMessageTemplate, 'Invalid user name or password'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
58
tests/Exception/IncorrectClassOfResultItemExceptionTest.php
Normal file
58
tests/Exception/IncorrectClassOfResultItemExceptionTest.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\IncorrectClassOfResultItemException;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while class used to create instance of one item of the result is incorrect
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class IncorrectClassOfResultItemExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(IncorrectClassOfResultItemException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $className Incorrect class name used to create instance of one item
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideIncorrectClassName
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($className, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new IncorrectClassOfResultItemException($className);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides incorrect class name used to create instance of one item
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideIncorrectClassName()
|
||||||
|
{
|
||||||
|
$template = 'Class %s used to create instance of one item of the result should extend %s, but it does not. Did'
|
||||||
|
. ' you forget to use proper base class?';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
stdClass::class,
|
||||||
|
sprintf($template, stdClass::class, BaseItem::class),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
75
tests/Exception/InvalidResultOfMethodRunExceptionTest.php
Normal file
75
tests/Exception/InvalidResultOfMethodRunExceptionTest.php
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used when an error occurred while running method
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class InvalidResultOfMethodRunExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(InvalidResultOfMethodRunException::class, OopVisibilityType::IS_PUBLIC, 3, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Exception $previousException The previous exception, source of an error
|
||||||
|
* @param string $methodName Name of called method
|
||||||
|
* @param array $methodArguments Arguments of the called method
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider providePreviousExceptionAndMethod
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage(Exception $previousException, $methodName, array $methodArguments, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new InvalidResultOfMethodRunException($previousException, $methodName, $methodArguments);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides previous exception, name and arguments of called method
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function providePreviousExceptionAndMethod()
|
||||||
|
{
|
||||||
|
$template = "Oops, an error occurred while running method. Is there everything ok? Details:\n"
|
||||||
|
. "- error: %s,\n"
|
||||||
|
. "- method: %s,\n"
|
||||||
|
. '- arguments: %s.';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
new Exception('Lorem ipsum'),
|
||||||
|
MethodType::ADD_RESPONSE,
|
||||||
|
[],
|
||||||
|
sprintf($template, 'Lorem ipsum', MethodType::ADD_RESPONSE, '(no arguments)'),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
new Exception('Dolor sit amet'),
|
||||||
|
MethodType::LIST_SURVEYS,
|
||||||
|
[
|
||||||
|
'fist_name' => 'John',
|
||||||
|
'last_name' => 'Scott',
|
||||||
|
'email' => 'john@scott.com',
|
||||||
|
],
|
||||||
|
sprintf($template, 'Dolor sit amet', MethodType::LIST_SURVEYS, 'fist_name="John", last_name="Scott", email="john@scott.com"'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
62
tests/Exception/MissingParticipantOfSurveyExceptionTest.php
Normal file
62
tests/Exception/MissingParticipantOfSurveyExceptionTest.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used when participant of survey is missing
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class MissingParticipantOfSurveyExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(MissingParticipantOfSurveyException::class, OopVisibilityType::IS_PUBLIC, 2, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $surveyId ID of survey
|
||||||
|
* @param string $email E-mail address of the participant
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideSurveyIdAndEmail
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($surveyId, $email, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new MissingParticipantOfSurveyException($surveyId, $email);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides ID of survey and e-mail address of the participant
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideSurveyIdAndEmail()
|
||||||
|
{
|
||||||
|
$template = 'Participant with e-mail %s of survey with ID %s is missing. Maybe was not added to the survey?';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
1,
|
||||||
|
'lorem@ipsum.com',
|
||||||
|
sprintf($template, 'lorem@ipsum.com', 1),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
1234,
|
||||||
|
'another@email.comm',
|
||||||
|
sprintf($template, 'another@email.comm', 1234),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
65
tests/Exception/UnknownInstanceOfResultItemTest.php
Normal file
65
tests/Exception/UnknownInstanceOfResultItemTest.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while instance of one item used by result, with data fetched from the LimeSurvey's
|
||||||
|
* API, is unknown
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class UnknownInstanceOfResultItemTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(UnknownInstanceOfResultItem::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the
|
||||||
|
* MethodType class constants.
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideMethodName
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($method, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new UnknownInstanceOfResultItem($method);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides name of called method
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideMethodName()
|
||||||
|
{
|
||||||
|
$template = 'Class name used to create instance of one item used by result the of \'%s\' LimeSurvey API\'s'
|
||||||
|
. ' method is unknown. Proper class is not mapped in %s::%s() method. Did you forget about this?';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
MethodType::LIST_SURVEYS,
|
||||||
|
sprintf($template, MethodType::LIST_SURVEYS, ResultProcessor::class, 'getItemClassName'),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
MethodType::ADD_PARTICIPANTS,
|
||||||
|
sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemClassName'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
64
tests/Exception/UnknownMethodExceptionTest.php
Normal file
64
tests/Exception/UnknownMethodExceptionTest.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?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\Exception;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while name of method used while talking to the LimeSurvey's API is unknown
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class UnknownMethodExceptionTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(UnknownMethodException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $unknownType The unknown type of something (value of constant)
|
||||||
|
* @param string $expectedMessage Expected exception's message
|
||||||
|
*
|
||||||
|
* @dataProvider provideUnknownType
|
||||||
|
*/
|
||||||
|
public function testConstructorMessage($unknownType, $expectedMessage)
|
||||||
|
{
|
||||||
|
$exception = new UnknownMethodException($unknownType);
|
||||||
|
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides name of called method
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideUnknownType()
|
||||||
|
{
|
||||||
|
$allMethods = implode(', ', (new MethodType())->getAll());
|
||||||
|
|
||||||
|
$template = 'The \'%s\' type of name of method used while talking to the LimeSurvey\'s API is unknown. Probably'
|
||||||
|
. ' doesn\'t exist or there is a typo. You should use one of these types: %s.';
|
||||||
|
|
||||||
|
yield[
|
||||||
|
MethodType::ADD_PARTICIPANTS,
|
||||||
|
sprintf($template, MethodType::ADD_PARTICIPANTS, $allMethods),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
MethodType::ADD_PARTICIPANTS,
|
||||||
|
sprintf($template, MethodType::ADD_PARTICIPANTS, $allMethods),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,9 +9,11 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Manager;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Manager;
|
||||||
|
|
||||||
use JsonRPC\Client as RpcClient;
|
use JsonRPC\Client as RpcClient;
|
||||||
|
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||||
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\Configuration\ConnectionConfiguration;
|
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest;
|
use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest;
|
||||||
@@ -33,10 +35,10 @@ class JsonRpcClientManagerTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyConstructorVisibilityAndArguments(JsonRpcClientManager::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
static::assertConstructorVisibilityAndArguments(JsonRpcClientManager::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRunMethod()
|
public function testRunMethodWithEmptyArrayReturned()
|
||||||
{
|
{
|
||||||
$rpcClient = $this->createMock(RpcClient::class);
|
$rpcClient = $this->createMock(RpcClient::class);
|
||||||
|
|
||||||
@@ -51,12 +53,12 @@ class JsonRpcClientManagerTest extends BaseTestCase
|
|||||||
->getMock();
|
->getMock();
|
||||||
|
|
||||||
$rpcClient
|
$rpcClient
|
||||||
->expects(static::any())
|
->expects(static::once())
|
||||||
->method('execute')
|
->method('execute')
|
||||||
->willReturn([]);
|
->willReturn([]);
|
||||||
|
|
||||||
$manager
|
$manager
|
||||||
->expects(static::any())
|
->expects(static::once())
|
||||||
->method('getRpcClient')
|
->method('getRpcClient')
|
||||||
->willReturn($rpcClient);
|
->willReturn($rpcClient);
|
||||||
|
|
||||||
@@ -64,18 +66,18 @@ class JsonRpcClientManagerTest extends BaseTestCase
|
|||||||
static::assertEquals([], $manager->runMethod(MethodType::LIST_SURVEYS));
|
static::assertEquals([], $manager->runMethod(MethodType::LIST_SURVEYS));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRunMethodWithMockedRpcClient()
|
public function testRunMethodWithRawDataReturned()
|
||||||
{
|
{
|
||||||
$rpcClient = $this->createMock(RpcClient::class);
|
$rpcClient = $this->createMock(RpcClient::class);
|
||||||
$manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']);
|
$manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']);
|
||||||
|
|
||||||
$rpcClient
|
$rpcClient
|
||||||
->expects(static::any())
|
->expects(static::once())
|
||||||
->method('execute')
|
->method('execute')
|
||||||
->willReturn(SurveyTest::getSurveysRawData());
|
->willReturn(SurveyTest::getSurveysRawData());
|
||||||
|
|
||||||
$manager
|
$manager
|
||||||
->expects(static::any())
|
->expects(static::once())
|
||||||
->method('getRpcClient')
|
->method('getRpcClient')
|
||||||
->willReturn($rpcClient);
|
->willReturn($rpcClient);
|
||||||
|
|
||||||
@@ -83,9 +85,30 @@ class JsonRpcClientManagerTest extends BaseTestCase
|
|||||||
static::assertEquals(SurveyTest::getSurveysRawData(), $manager->runMethod(MethodType::LIST_SURVEYS));
|
static::assertEquals(SurveyTest::getSurveysRawData(), $manager->runMethod(MethodType::LIST_SURVEYS));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRunMethodWithException()
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidResultOfMethodRunException::class);
|
||||||
|
|
||||||
|
$manager = $this->createPartialMock(JsonRpcClientManager::class, ['getRpcClient']);
|
||||||
|
$rpcClient = $this->createMock(RpcClient::class);
|
||||||
|
|
||||||
|
$rpcClient
|
||||||
|
->expects(self::once())
|
||||||
|
->method('execute')
|
||||||
|
->willThrowException(new InvalidJsonFormatException('bla bla'));
|
||||||
|
|
||||||
|
$manager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('getRpcClient')
|
||||||
|
->willReturn($rpcClient);
|
||||||
|
|
||||||
|
/* @var JsonRpcClientManager $manager */
|
||||||
|
$manager->runMethod(MethodType::LIST_SURVEYS);
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetRpcClientVisibilityAndArguments()
|
public function testGetRpcClientVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(JsonRpcClientManager::class, 'getRpcClient', OopVisibilityType::IS_PROTECTED);
|
static::assertMethodVisibilityAndArguments(JsonRpcClientManager::class, 'getRpcClient', OopVisibilityType::IS_PROTECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,7 +24,7 @@ class SessionManagerTest extends BaseTestCase
|
|||||||
{
|
{
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyConstructorVisibilityAndArguments(SessionManager::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
static::assertConstructorVisibilityAndArguments(SessionManager::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSessionKeyWhenFailedWithoutReason()
|
public function testGetSessionKeyWhenFailedWithoutReason()
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
<?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\Base\Result;
|
|
||||||
|
|
||||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
use ReflectionClass;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test case of the base class for one item of result/data fetched while talking to the LimeSurvey's API
|
|
||||||
*
|
|
||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
|
||||||
* @copyright Meritoo.pl
|
|
||||||
*/
|
|
||||||
class BaseItemTest extends PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
public function testConstructorVisibilityAndArguments()
|
|
||||||
{
|
|
||||||
$reflection = new ReflectionClass(BaseItem::class);
|
|
||||||
$constructor = $reflection->getConstructor();
|
|
||||||
|
|
||||||
static::assertNull($constructor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetValues()
|
|
||||||
{
|
|
||||||
$mock = $this->getBaseItemMock();
|
|
||||||
|
|
||||||
static::assertInstanceOf(BaseItem::class, $mock->setValues([]));
|
|
||||||
static::assertInstanceOf(BaseItem::class, $mock->setValues(['lorem']));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns mock of the tested class
|
|
||||||
*
|
|
||||||
* @return BaseItem
|
|
||||||
*/
|
|
||||||
private function getBaseItemMock()
|
|
||||||
{
|
|
||||||
$mock = $this->getMockForAbstractClass(BaseItem::class);
|
|
||||||
|
|
||||||
$mock
|
|
||||||
->expects(static::any())
|
|
||||||
->method('setValue')
|
|
||||||
->willReturn(null);
|
|
||||||
|
|
||||||
return $mock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
<?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\Configuration;
|
|
||||||
|
|
||||||
use Generator;
|
|
||||||
use Meritoo\Common\Exception\Regex\InvalidUrlException;
|
|
||||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
|
||||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test case of the configuration used while connecting to LimeSurvey's API
|
|
||||||
*
|
|
||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
|
||||||
* @copyright Meritoo.pl
|
|
||||||
*/
|
|
||||||
class ConnectionConfigurationTest extends BaseTestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param mixed $emptyBaseUrl Empty base url
|
|
||||||
* @dataProvider provideEmptyBaseUrl
|
|
||||||
*/
|
|
||||||
public function testConstructorWithEmptyBaseUrl($emptyBaseUrl)
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidUrlException::class);
|
|
||||||
new ConnectionConfiguration($emptyBaseUrl, '', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $invalidBaseUrl Invalid base url
|
|
||||||
* @dataProvider provideInvalidBaseUrl
|
|
||||||
*/
|
|
||||||
public function testConstructorWithInvalidBaseUrl($invalidBaseUrl)
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidUrlException::class);
|
|
||||||
new ConnectionConfiguration($invalidBaseUrl, '', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testConstructor()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
|
|
||||||
static::assertEquals('http://test.com', $configuration->getBaseUrl());
|
|
||||||
static::assertEquals('test1', $configuration->getUsername());
|
|
||||||
static::assertEquals('test2', $configuration->getPassword());
|
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetBaseUrl()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
|
|
||||||
$configuration->setBaseUrl('http://lorem.ipsum');
|
|
||||||
static::assertEquals('http://lorem.ipsum', $configuration->getBaseUrl());
|
|
||||||
|
|
||||||
$configuration->setBaseUrl('http://lorem.ipsum/');
|
|
||||||
static::assertEquals('http://lorem.ipsum', $configuration->getBaseUrl());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetRemoteControlUrl()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
$configuration->setRemoteControlUrl('/lorem/ipsum');
|
|
||||||
|
|
||||||
static::assertEquals('/lorem/ipsum', $configuration->getRemoteControlUrl());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetUsername()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
$configuration->setUsername('lorem');
|
|
||||||
|
|
||||||
static::assertEquals('lorem', $configuration->getUsername());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetPassword()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
$configuration->setPassword('ipsum');
|
|
||||||
|
|
||||||
static::assertEquals('ipsum', $configuration->getPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetDebugMode()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
|
|
||||||
$configuration->setDebugMode();
|
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
|
||||||
|
|
||||||
$configuration->setDebugMode(false);
|
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
|
||||||
|
|
||||||
$configuration->setDebugMode(true);
|
|
||||||
static::assertTrue($configuration->isDebugModeOn());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetFullUrl()
|
|
||||||
{
|
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
|
||||||
$configuration->setRemoteControlUrl('lorem/ipsum');
|
|
||||||
|
|
||||||
static::assertEquals('http://test.com/lorem/ipsum', $configuration->getFullUrl());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides empty base url
|
|
||||||
*
|
|
||||||
* @return Generator
|
|
||||||
*/
|
|
||||||
public function provideEmptyBaseUrl()
|
|
||||||
{
|
|
||||||
yield[
|
|
||||||
'',
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
null,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides invalid base url
|
|
||||||
*
|
|
||||||
* @return Generator
|
|
||||||
*/
|
|
||||||
public function provideInvalidBaseUrl()
|
|
||||||
{
|
|
||||||
yield[
|
|
||||||
'lorem',
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
'ipsum',
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
'htp:/dolor.com',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
199
tests/Result/Collection/ParticipantsTest.php
Normal file
199
tests/Result/Collection/ParticipantsTest.php
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
<?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\Result\Collection;
|
||||||
|
|
||||||
|
use Meritoo\Common\Collection\Collection;
|
||||||
|
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the collection of participants (of surveys)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ParticipantsTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An empty collection of participants (of surveys)
|
||||||
|
*
|
||||||
|
* @var Participants
|
||||||
|
*/
|
||||||
|
private $participantsEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of participants of 1 survey
|
||||||
|
*
|
||||||
|
* @var Participants
|
||||||
|
*/
|
||||||
|
private $participantsOfOneSurvey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of participants of more than 1 survey
|
||||||
|
*
|
||||||
|
* @var Participants
|
||||||
|
*/
|
||||||
|
private $participantsOfManySurvey;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Participants::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdd()
|
||||||
|
{
|
||||||
|
$this->expectException(DisabledMethodException::class);
|
||||||
|
(new Participants())->add('');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddMultiple()
|
||||||
|
{
|
||||||
|
$this->expectException(DisabledMethodException::class);
|
||||||
|
(new Participants())->addMultiple([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHas()
|
||||||
|
{
|
||||||
|
$this->expectException(DisabledMethodException::class);
|
||||||
|
(new Participants())->has(new Participant());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddParticipantsUsingEmptyCollection()
|
||||||
|
{
|
||||||
|
$surveyId = 1;
|
||||||
|
|
||||||
|
$participants = new Participants();
|
||||||
|
$result = $participants->addParticipants(new Collection(), $surveyId);
|
||||||
|
|
||||||
|
static::assertFalse($participants->hasParticipantsOfSurvey($surveyId));
|
||||||
|
static::assertFalse($participants->hasParticipantsOfSurvey(2));
|
||||||
|
|
||||||
|
static::assertEquals($participants, $result);
|
||||||
|
static::assertCount(0, $participants->getBySurvey($surveyId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddParticipantsFirstParticipants()
|
||||||
|
{
|
||||||
|
$surveyId = 1;
|
||||||
|
|
||||||
|
$participantsData = new Collection([
|
||||||
|
new Participant(),
|
||||||
|
new Participant(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $this
|
||||||
|
->participantsEmpty
|
||||||
|
->addParticipants($participantsData, $surveyId);
|
||||||
|
|
||||||
|
static::assertTrue($this->participantsEmpty->hasParticipantsOfSurvey($surveyId));
|
||||||
|
static::assertFalse($this->participantsEmpty->hasParticipantsOfSurvey(2));
|
||||||
|
|
||||||
|
static::assertEquals($this->participantsEmpty, $result);
|
||||||
|
static::assertCount(2, $this->participantsEmpty->getBySurvey($surveyId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddParticipantsMoreParticipants()
|
||||||
|
{
|
||||||
|
$surveyId = 2;
|
||||||
|
|
||||||
|
$participantsData = new Collection([
|
||||||
|
new Participant(),
|
||||||
|
new Participant(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $this
|
||||||
|
->participantsOfOneSurvey
|
||||||
|
->addParticipants($participantsData, $surveyId);
|
||||||
|
|
||||||
|
static::assertTrue($this->participantsOfOneSurvey->hasParticipantsOfSurvey($surveyId));
|
||||||
|
static::assertFalse($this->participantsOfOneSurvey->hasParticipantsOfSurvey(3));
|
||||||
|
|
||||||
|
static::assertEquals($this->participantsOfOneSurvey, $result);
|
||||||
|
static::assertCount(2, $this->participantsOfOneSurvey->getBySurvey($surveyId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddParticipantFirstParticipant()
|
||||||
|
{
|
||||||
|
$surveyId = 1;
|
||||||
|
$email = 'john@scott.com';
|
||||||
|
|
||||||
|
$participant = new ParticipantShort([
|
||||||
|
'tid' => 1,
|
||||||
|
'participant_info' => [
|
||||||
|
'firstname' => 'John',
|
||||||
|
'lastname' => 'Scott',
|
||||||
|
'email' => $email,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$participants = new Participants();
|
||||||
|
$result = $participants->addParticipant($participant, $surveyId);
|
||||||
|
|
||||||
|
static::assertEquals($participants, $result);
|
||||||
|
static::assertEquals($participant, $participants->getParticipantOfSurvey($surveyId, $email));
|
||||||
|
|
||||||
|
static::assertTrue($participants->hasParticipantsOfSurvey($surveyId));
|
||||||
|
static::assertFalse($participants->hasParticipantsOfSurvey(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddParticipantNotFirstParticipant()
|
||||||
|
{
|
||||||
|
$surveyId = 1;
|
||||||
|
$email = 'john@scott.com';
|
||||||
|
|
||||||
|
$participant = new ParticipantShort([
|
||||||
|
'tid' => 1,
|
||||||
|
'participant_info' => [
|
||||||
|
'firstname' => 'John',
|
||||||
|
'lastname' => 'Scott',
|
||||||
|
'email' => $email,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $this
|
||||||
|
->participantsOfOneSurvey
|
||||||
|
->addParticipant($participant, $surveyId);
|
||||||
|
|
||||||
|
static::assertEquals($this->participantsOfOneSurvey, $result);
|
||||||
|
static::assertEquals($participant, $this->participantsOfOneSurvey->getParticipantOfSurvey($surveyId, $email));
|
||||||
|
|
||||||
|
static::assertTrue($this->participantsOfOneSurvey->hasParticipantsOfSurvey($surveyId));
|
||||||
|
static::assertFalse($this->participantsOfOneSurvey->hasParticipantsOfSurvey(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->participantsEmpty = new Participants();
|
||||||
|
$this->participantsOfOneSurvey = new Participants();
|
||||||
|
$this->participantsOfManySurvey = new Participants();
|
||||||
|
|
||||||
|
$participants1Survey = new Collection();
|
||||||
|
$participants2Survey = new Collection();
|
||||||
|
$participants3Survey = new Collection();
|
||||||
|
|
||||||
|
$this
|
||||||
|
->participantsOfOneSurvey
|
||||||
|
->addParticipants($participants1Survey, 1);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->participantsOfManySurvey
|
||||||
|
->addParticipants($participants1Survey, 2)
|
||||||
|
->addParticipants($participants2Survey, 3)
|
||||||
|
->addParticipants($participants3Survey, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
108
tests/Result/Collection/SurveysTest.php
Normal file
108
tests/Result/Collection/SurveysTest.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?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\Result\Collection;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the collection of surveys (the Survey class instances)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class SurveysTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An empty collection of surveys
|
||||||
|
*
|
||||||
|
* @var Surveys
|
||||||
|
*/
|
||||||
|
private $surveysEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Not empty collection of surveys
|
||||||
|
*
|
||||||
|
* @var Surveys
|
||||||
|
*/
|
||||||
|
private $surveysNotEmpty;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Surveys::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddWithoutIndex()
|
||||||
|
{
|
||||||
|
$survey1 = new Survey([
|
||||||
|
'sid' => 3,
|
||||||
|
'surveyls_title' => 'Test Test Test',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$survey2 = new Survey([
|
||||||
|
'sid' => 4,
|
||||||
|
'surveyls_title' => 'Another Test Test Test',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->surveysEmpty
|
||||||
|
->add($survey1)
|
||||||
|
->add($survey2);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->surveysNotEmpty
|
||||||
|
->add($survey1)
|
||||||
|
->add($survey2);
|
||||||
|
|
||||||
|
static::assertEquals($survey1, $this->surveysEmpty[3]);
|
||||||
|
static::assertEquals($survey2, $this->surveysEmpty[4]);
|
||||||
|
|
||||||
|
static::assertEquals($survey1, $this->surveysNotEmpty[3]);
|
||||||
|
static::assertEquals($survey2, $this->surveysNotEmpty[4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAll()
|
||||||
|
{
|
||||||
|
static::assertCount(0, $this->surveysEmpty->getAll());
|
||||||
|
static::assertCount(0, $this->surveysEmpty->getAll(true));
|
||||||
|
|
||||||
|
static::assertCount(3, $this->surveysNotEmpty->getAll());
|
||||||
|
static::assertCount(2, $this->surveysNotEmpty->getAll(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$surveys = [
|
||||||
|
new Survey([
|
||||||
|
'sid' => 1,
|
||||||
|
'surveyls_title' => 'Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 2,
|
||||||
|
'surveyls_title' => 'Another Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 3,
|
||||||
|
'surveyls_title' => 'I am inactive',
|
||||||
|
]),
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->surveysEmpty = new Surveys();
|
||||||
|
$this->surveysNotEmpty = new Surveys($surveys);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,10 +8,12 @@
|
|||||||
|
|
||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
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\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;
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the one item of the result/data: short data of participant
|
* Test case of the one item of the result/data: short data of participant
|
||||||
@@ -19,7 +21,7 @@ use PHPUnit_Framework_TestCase;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
class ParticipantShortTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Raw data of participants
|
* Raw data of participants
|
||||||
@@ -42,6 +44,11 @@ class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $participant2ndInstance;
|
private $participant2ndInstance;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(ParticipantShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateOfTheParticipant()
|
public function testCreateOfTheParticipant()
|
||||||
{
|
{
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
@@ -74,6 +81,52 @@ class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
|||||||
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
|
||||||
*
|
*
|
||||||
@@ -84,7 +137,6 @@ class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
|||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
'tid' => '123',
|
'tid' => '123',
|
||||||
'token' => uniqid(),
|
|
||||||
'participant_info' => [
|
'participant_info' => [
|
||||||
'firstname' => 'Lorem',
|
'firstname' => 'Lorem',
|
||||||
'lastname' => 'Ipsum',
|
'lastname' => 'Ipsum',
|
||||||
@@ -93,7 +145,6 @@ class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'tid' => '456',
|
'tid' => '456',
|
||||||
'token' => uniqid(),
|
|
||||||
'participant_info' => [
|
'participant_info' => [
|
||||||
'firstname' => 'Dolor',
|
'firstname' => 'Dolor',
|
||||||
'lastname' => 'Sit',
|
'lastname' => 'Sit',
|
||||||
@@ -111,7 +162,7 @@ class ParticipantShortTest extends PHPUnit_Framework_TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->rawData = static::getParticipantsRawData();
|
$this->rawData = static::getParticipantsRawData();
|
||||||
|
|
||||||
$this->participant1stInstance = (new ParticipantShort())->setValues($this->rawData[0]);
|
$this->participant1stInstance = new ParticipantShort($this->rawData[0]);
|
||||||
$this->participant2ndInstance = (new ParticipantShort())->setValues($this->rawData[1]);
|
$this->participant2ndInstance = new ParticipantShort($this->rawData[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,10 +9,11 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
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;
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the one item of the result/data: full data of participant
|
* Test case of the one item of the result/data: full data of participant
|
||||||
@@ -20,7 +21,7 @@ use PHPUnit_Framework_TestCase;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class ParticipantTest extends PHPUnit_Framework_TestCase
|
class ParticipantTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Raw data of participants
|
* Raw data of participants
|
||||||
@@ -43,6 +44,11 @@ class ParticipantTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $participant2ndInstance;
|
private $participant2ndInstance;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Participant::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateOfTheParticipant()
|
public function testCreateOfTheParticipant()
|
||||||
{
|
{
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
@@ -194,7 +200,7 @@ class ParticipantTest extends PHPUnit_Framework_TestCase
|
|||||||
'sent' => 'Y',
|
'sent' => 'Y',
|
||||||
'remindersent' => 'N',
|
'remindersent' => 'N',
|
||||||
'remindercount' => 1,
|
'remindercount' => 1,
|
||||||
'completed' => 'Y',
|
'completed' => (new DateTime())->format('Y-m-d H:i'),
|
||||||
'usesleft' => 5,
|
'usesleft' => 5,
|
||||||
'validfrom' => (new DateTime())->format('Y-m-d H:i:s'),
|
'validfrom' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||||
'validuntil' => null,
|
'validuntil' => null,
|
||||||
@@ -210,7 +216,7 @@ class ParticipantTest extends PHPUnit_Framework_TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->rawData = static::getParticipantsRawData();
|
$this->rawData = static::getParticipantsRawData();
|
||||||
|
|
||||||
$this->participant1stInstance = (new Participant())->setValues($this->rawData[0]);
|
$this->participant1stInstance = new Participant($this->rawData[0]);
|
||||||
$this->participant2ndInstance = (new Participant())->setValues($this->rawData[1]);
|
$this->participant2ndInstance = new Participant($this->rawData[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
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\LimeSurvey\ApiClient\Result\Item\QuestionShort;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\QuestionShort;
|
||||||
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;
|
||||||
@@ -42,6 +43,11 @@ class QuestionShortTest extends BaseTestCase
|
|||||||
*/
|
*/
|
||||||
private $question2ndInstance;
|
private $question2ndInstance;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(QuestionShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateOfTheQuestionShort()
|
public function testCreateOfTheQuestionShort()
|
||||||
{
|
{
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
@@ -217,7 +223,7 @@ class QuestionShortTest extends BaseTestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->rawData = static::getQuestionsRawData();
|
$this->rawData = static::getQuestionsRawData();
|
||||||
|
|
||||||
$this->question1stInstance = (new QuestionShort())->setValues($this->rawData[0]);
|
$this->question1stInstance = new QuestionShort($this->rawData[0]);
|
||||||
$this->question2ndInstance = (new QuestionShort())->setValues($this->rawData[1]);
|
$this->question2ndInstance = new QuestionShort($this->rawData[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,10 +8,11 @@
|
|||||||
|
|
||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Question;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Question;
|
||||||
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;
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the one item of the result/data: full data of one question of survey
|
* Test case of the one item of the result/data: full data of one question of survey
|
||||||
@@ -19,7 +20,7 @@ use PHPUnit_Framework_TestCase;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class QuestionTest extends PHPUnit_Framework_TestCase
|
class QuestionTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Raw data of questions
|
* Raw data of questions
|
||||||
@@ -42,6 +43,11 @@ class QuestionTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $question2ndInstance;
|
private $question2ndInstance;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Question::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateOfTheQuestionShort()
|
public function testCreateOfTheQuestionShort()
|
||||||
{
|
{
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
@@ -279,7 +285,7 @@ class QuestionTest extends PHPUnit_Framework_TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->rawData = static::getQuestionsRawData();
|
$this->rawData = static::getQuestionsRawData();
|
||||||
|
|
||||||
$this->question1stInstance = (new Question())->setValues($this->rawData[0]);
|
$this->question1stInstance = new Question($this->rawData[0]);
|
||||||
$this->question2ndInstance = (new Question())->setValues($this->rawData[1]);
|
$this->question2ndInstance = new Question($this->rawData[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,10 +9,11 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
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;
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the one item of the result/data: survey
|
* Test case of the one item of the result/data: survey
|
||||||
@@ -20,7 +21,7 @@ use PHPUnit_Framework_TestCase;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class SurveyTest extends PHPUnit_Framework_TestCase
|
class SurveyTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Raw data of surveys
|
* Raw data of surveys
|
||||||
@@ -43,6 +44,11 @@ class SurveyTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $survey2ndInstance;
|
private $survey2ndInstance;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Survey::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateOfTheSurvey()
|
public function testCreateOfTheSurvey()
|
||||||
{
|
{
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
@@ -114,7 +120,7 @@ class SurveyTest extends PHPUnit_Framework_TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->rawData = static::getSurveysRawData();
|
$this->rawData = static::getSurveysRawData();
|
||||||
|
|
||||||
$this->survey1stInstance = (new Survey())->setValues($this->rawData[0]);
|
$this->survey1stInstance = new Survey($this->rawData[0]);
|
||||||
$this->survey2ndInstance = (new Survey())->setValues($this->rawData[1]);
|
$this->survey2ndInstance = new Survey($this->rawData[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,10 +12,10 @@ use Meritoo\Common\Test\Base\BaseTestCase;
|
|||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
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;
|
||||||
use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest;
|
use Meritoo\LimeSurvey\Test\ApiClient\Result\Item\SurveyTest;
|
||||||
use ReflectionClass;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the processor of the raw data fetched while talking to the LimeSurvey's API
|
* Test case of the processor of the raw data fetched while talking to the LimeSurvey's API
|
||||||
@@ -27,10 +27,7 @@ class ResultProcessorTest extends BaseTestCase
|
|||||||
{
|
{
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$reflection = new ReflectionClass(ResultProcessor::class);
|
static::assertHasNoConstructor(ResultProcessor::class);
|
||||||
$constructor = $reflection->getConstructor();
|
|
||||||
|
|
||||||
static::assertNull($constructor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testProcessWithEmptyRawData()
|
public function testProcessWithEmptyRawData()
|
||||||
@@ -43,12 +40,24 @@ class ResultProcessorTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testProcessWithIterableData()
|
public function testProcessWithIterableData()
|
||||||
{
|
{
|
||||||
|
$surveysRawData = SurveyTest::getSurveysRawData();
|
||||||
$processor = new ResultProcessor();
|
$processor = new ResultProcessor();
|
||||||
$processed = $processor->process(MethodType::LIST_SURVEYS, SurveyTest::getSurveysRawData());
|
$processed = $processor->process(MethodType::LIST_SURVEYS, $surveysRawData);
|
||||||
|
|
||||||
static::assertNotEmpty($processed);
|
|
||||||
static::assertTrue(is_array($processed));
|
static::assertTrue(is_array($processed));
|
||||||
static::assertCount(2, $processed);
|
static::assertCount(2, $processed);
|
||||||
|
|
||||||
|
/* @var Survey $firstSurvey */
|
||||||
|
$firstSurvey = $processed[0];
|
||||||
|
|
||||||
|
/* @var Survey $secondSurvey */
|
||||||
|
$secondSurvey = $processed[1];
|
||||||
|
|
||||||
|
static::assertEquals($surveysRawData[0]['sid'], $firstSurvey->getId());
|
||||||
|
static::assertEquals($surveysRawData[1]['sid'], $secondSurvey->getId());
|
||||||
|
|
||||||
|
static::assertEquals($surveysRawData[0]['surveyls_title'], $firstSurvey->getTitle());
|
||||||
|
static::assertEquals($surveysRawData[1]['surveyls_title'], $secondSurvey->getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testProcessWithNotIterableData()
|
public function testProcessWithNotIterableData()
|
||||||
@@ -66,9 +75,9 @@ class ResultProcessorTest extends BaseTestCase
|
|||||||
static::assertInstanceOf(BaseItem::class, $processed);
|
static::assertInstanceOf(BaseItem::class, $processed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetItemInstanceVisibilityAndArguments()
|
public function testGetItemClassNameVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(ResultProcessor::class, 'getItemInstance', OopVisibilityType::IS_PRIVATE, 1, 1);
|
static::assertMethodVisibilityAndArguments(ResultProcessor::class, 'getItemClassName', OopVisibilityType::IS_PRIVATE, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRunWithUnknownResultClass()
|
public function testRunWithUnknownResultClass()
|
||||||
@@ -12,6 +12,7 @@ use DateTime;
|
|||||||
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\Base\Result\BaseItem;
|
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Result;
|
use Meritoo\LimeSurvey\ApiClient\Result\Result;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
use PHPUnit_Framework_MockObject_MockObject;
|
use PHPUnit_Framework_MockObject_MockObject;
|
||||||
@@ -46,45 +47,60 @@ class ResultTest extends BaseTestCase
|
|||||||
private $notIterableData;
|
private $notIterableData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock of the tested class.
|
* Status provided instead of real data.
|
||||||
* With empty data returned by the LimeSurvey's API.
|
* An array with one key: "status".
|
||||||
*
|
*
|
||||||
* @var PHPUnit_Framework_MockObject_MockObject
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $emptyDataMock;
|
private $statusInsteadData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Result with empty data returned by the LimeSurvey's API.
|
||||||
* Mock of the tested class.
|
* Mock of the tested class.
|
||||||
* With iterable, not empty data.
|
|
||||||
*
|
*
|
||||||
* @var PHPUnit_Framework_MockObject_MockObject
|
* @var PHPUnit_Framework_MockObject_MockObject
|
||||||
*/
|
*/
|
||||||
private $iterableDataMock;
|
private $emptyDataResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Result with iterable, not empty data.
|
||||||
* Mock of the tested class.
|
* Mock of the tested class.
|
||||||
* With not iterable, not empty data.
|
|
||||||
*
|
*
|
||||||
* @var PHPUnit_Framework_MockObject_MockObject
|
* @var PHPUnit_Framework_MockObject_MockObject
|
||||||
*/
|
*/
|
||||||
private $notIterableDataMock;
|
private $iterableDataResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result with not iterable, not empty data.
|
||||||
|
* Mock of the tested class.
|
||||||
|
*
|
||||||
|
* @var PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $notIterableDataResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Result with status provided instead of real data
|
||||||
|
*
|
||||||
|
* @var Result
|
||||||
|
*/
|
||||||
|
private $statusInsteadDataResult;
|
||||||
|
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyConstructorVisibilityAndArguments(Result::class, OopVisibilityType::IS_PUBLIC, 2, 2);
|
static::assertConstructorVisibilityAndArguments(Result::class, OopVisibilityType::IS_PUBLIC, 2, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsEmpty()
|
public function testIsEmpty()
|
||||||
{
|
{
|
||||||
static::assertTrue($this->emptyDataMock->isEmpty());
|
static::assertTrue($this->emptyDataResult->isEmpty());
|
||||||
static::assertFalse($this->iterableDataMock->isEmpty());
|
static::assertFalse($this->iterableDataResult->isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetDataUsingProcessedData()
|
public function testGetDataUsingProcessedData()
|
||||||
{
|
{
|
||||||
$emptyData = $this->emptyDataMock->getData();
|
$emptyData = $this->emptyDataResult->getData();
|
||||||
$iterableData = $this->iterableDataMock->getData();
|
$iterableData = $this->iterableDataResult->getData();
|
||||||
$notIterableData = $this->notIterableDataMock->getData();
|
$notIterableData = $this->notIterableDataResult->getData();
|
||||||
|
|
||||||
static::assertEmpty($emptyData);
|
static::assertEmpty($emptyData);
|
||||||
static::assertNotEmpty($iterableData);
|
static::assertNotEmpty($iterableData);
|
||||||
@@ -97,8 +113,8 @@ class ResultTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testGetDataUsingRawData()
|
public function testGetDataUsingRawData()
|
||||||
{
|
{
|
||||||
$emptyData = $this->emptyDataMock->getData(true);
|
$emptyData = $this->emptyDataResult->getData(true);
|
||||||
$iterableData = $this->iterableDataMock->getData(true);
|
$iterableData = $this->iterableDataResult->getData(true);
|
||||||
|
|
||||||
static::assertEmpty($emptyData);
|
static::assertEmpty($emptyData);
|
||||||
static::assertNotEmpty($iterableData);
|
static::assertNotEmpty($iterableData);
|
||||||
@@ -110,14 +126,34 @@ class ResultTest extends BaseTestCase
|
|||||||
static::assertEquals($this->iterableData, $iterableData);
|
static::assertEquals($this->iterableData, $iterableData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetDataUsingProcessedDataWhoCannotBeProcessed()
|
||||||
|
{
|
||||||
|
$this->expectException(CannotProcessDataException::class);
|
||||||
|
$this->statusInsteadDataResult->getData();
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetProcessedDataVisibilityAndArguments()
|
public function testGetProcessedDataVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(Result::class, 'getProcessedData', OopVisibilityType::IS_PRIVATE, 1, 1);
|
static::assertMethodVisibilityAndArguments(Result::class, 'getProcessedData', OopVisibilityType::IS_PRIVATE, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetResultProcessorVisibilityAndArguments()
|
public function testGetResultProcessorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
$this->verifyMethodVisibilityAndArguments(Result::class, 'getResultProcessor', OopVisibilityType::IS_PRIVATE);
|
static::assertMethodVisibilityAndArguments(Result::class, 'getResultProcessor', OopVisibilityType::IS_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetStatusWhenIsNotProvided()
|
||||||
|
{
|
||||||
|
$result = new Result(MethodType::ADD_PARTICIPANTS, []);
|
||||||
|
|
||||||
|
static::assertEquals(null, $result->getStatus());
|
||||||
|
static::assertEquals([], $result->getData(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetStatusWhenIsProvided()
|
||||||
|
{
|
||||||
|
static::assertEquals($this->statusInsteadData['status'], $this->statusInsteadDataResult->getStatus());
|
||||||
|
static::assertEquals([], $this->statusInsteadDataResult->getData(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,24 +185,29 @@ class ResultTest extends BaseTestCase
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$emptyDataArguments = [
|
$this->statusInsteadData = [
|
||||||
|
'status' => 'Invalid data',
|
||||||
|
];
|
||||||
|
|
||||||
|
$emptyData = [
|
||||||
MethodType::LIST_SURVEYS,
|
MethodType::LIST_SURVEYS,
|
||||||
$this->emptyData,
|
$this->emptyData,
|
||||||
];
|
];
|
||||||
|
|
||||||
$iterableDataArguments = [
|
$iterableData = [
|
||||||
MethodType::LIST_SURVEYS,
|
MethodType::LIST_SURVEYS,
|
||||||
$this->iterableData,
|
$this->iterableData,
|
||||||
];
|
];
|
||||||
|
|
||||||
$notIterableDataArguments = [
|
$notIterableData = [
|
||||||
MethodType::GET_PARTICIPANT_PROPERTIES,
|
MethodType::GET_PARTICIPANT_PROPERTIES,
|
||||||
$this->notIterableData,
|
$this->notIterableData,
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->emptyDataMock = $this->getResultMock($emptyDataArguments);
|
$this->emptyDataResult = $this->getResultMock($emptyData);
|
||||||
$this->iterableDataMock = $this->getResultMock($iterableDataArguments);
|
$this->iterableDataResult = $this->getResultMock($iterableData);
|
||||||
$this->notIterableDataMock = $this->getResultMock($notIterableDataArguments);
|
$this->notIterableDataResult = $this->getResultMock($notIterableData);
|
||||||
|
$this->statusInsteadDataResult = new Result(MethodType::LIST_PARTICIPANTS, $this->statusInsteadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
377
tests/Service/ParticipantServiceTest.php
Normal file
377
tests/Service/ParticipantServiceTest.php
Normal file
@@ -0,0 +1,377 @@
|
|||||||
|
<?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::class, 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::class, $this->serviceWithoutParticipants->getClient());
|
||||||
|
static::assertInstanceOf(Client::class, $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->expectException(CannotProcessDataException::class);
|
||||||
|
$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->expectException(CannotProcessDataException::class);
|
||||||
|
$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::class, $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);
|
||||||
|
|
||||||
|
static::assertNull($this->serviceWithoutParticipants->getParticipant(1, 'john@scott.com'));
|
||||||
|
$participant = $this->serviceWithParticipants->getParticipant(1, 'john@scott.com');
|
||||||
|
|
||||||
|
static::assertInstanceOf(ParticipantShort::class, $participant);
|
||||||
|
static::assertEquals('John', $participant->getFirstName());
|
||||||
|
static::assertEquals('Scott', $participant->getLastName());
|
||||||
|
static::assertEquals('john@scott.com', $participant->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);
|
||||||
|
|
||||||
|
static::assertNull($this->serviceWithoutParticipants->getParticipantDetails(1, 'john@scott.com'));
|
||||||
|
$participant = $this->serviceWithParticipants->getParticipantDetails(1, 'john@scott.com');
|
||||||
|
|
||||||
|
static::assertInstanceOf(Participant::class, $participant);
|
||||||
|
static::assertEquals($runMethodCallResults['tid'], $participant->getId());
|
||||||
|
static::assertEquals($runMethodCallResults['firstname'], $participant->getFirstName());
|
||||||
|
static::assertEquals($runMethodCallResults['lastname'], $participant->getLastName());
|
||||||
|
static::assertEquals($runMethodCallResults['email'], $participant->getEmail());
|
||||||
|
static::assertEquals($runMethodCallResults['token'], $participant->getToken());
|
||||||
|
static::assertFalse($participant->isSent());
|
||||||
|
static::assertFalse($participant->isCompleted());
|
||||||
|
static::assertNull($participant->isBlacklisted());
|
||||||
|
static::assertNull($participant->getValidFrom());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHasParticipantFilledSurveyWithException()
|
||||||
|
{
|
||||||
|
$this->expectException(MissingParticipantOfSurveyException::class);
|
||||||
|
|
||||||
|
$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->expectException(MissingParticipantOfSurveyException::class);
|
||||||
|
|
||||||
|
$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::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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::class);
|
||||||
|
|
||||||
|
$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::class);
|
||||||
|
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
290
tests/Service/SurveyServiceTest.php
Normal file
290
tests/Service/SurveyServiceTest.php
Normal file
@@ -0,0 +1,290 @@
|
|||||||
|
<?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\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\Manager\JsonRpcClientManager;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
use PHPUnit_Framework_MockObject_MockObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the service that serves surveys
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class SurveyServiceTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Service that serves surveys.
|
||||||
|
* Without surveys.
|
||||||
|
*
|
||||||
|
* @var SurveyService
|
||||||
|
*/
|
||||||
|
private $serviceWithoutSurveys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service that serves surveys.
|
||||||
|
* With surveys.
|
||||||
|
*
|
||||||
|
* @var SurveyService
|
||||||
|
*/
|
||||||
|
private $serviceWithSurveys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base url of LimeSurvey's instance.
|
||||||
|
* Used to prepare configuration of connection.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $connectionBaseUrl = 'http://test.com';
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(SurveyService::class, OopVisibilityType::IS_PUBLIC, 2, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetClient()
|
||||||
|
{
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManager(0);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||||
|
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Client::class, $this->serviceWithoutSurveys->getClient());
|
||||||
|
static::assertInstanceOf(Client::class, $this->serviceWithSurveys->getClient());
|
||||||
|
|
||||||
|
$connectionConfiguration = $this->getConnectionConfiguration();
|
||||||
|
$client = new Client($connectionConfiguration);
|
||||||
|
$surveyService = new SurveyService($client);
|
||||||
|
|
||||||
|
static::assertEquals($client, $surveyService->getClient());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAllSurveysWithImportantException()
|
||||||
|
{
|
||||||
|
$this->expectException(CannotProcessDataException::class);
|
||||||
|
$exception = new CannotProcessDataException(ReasonType::NO_TOKEN_TABLE);
|
||||||
|
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||||
|
$this->serviceWithoutSurveys->getAllSurveys();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAllSurveysWithNoSurveysException()
|
||||||
|
{
|
||||||
|
$exception = new CannotProcessDataException(ReasonType::NO_SURVEYS_FOUND);
|
||||||
|
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManagerWithException(1, $exception);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||||
|
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAllSurveys()
|
||||||
|
{
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManager(2);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||||
|
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are no surveys, count of all or active only surveys is the same
|
||||||
|
*/
|
||||||
|
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys());
|
||||||
|
static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys(true));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are surveys, here we've got difference between count of all or active only surveys
|
||||||
|
*/
|
||||||
|
static::assertCount(3, $this->serviceWithSurveys->getAllSurveys());
|
||||||
|
static::assertCount(2, $this->serviceWithSurveys->getAllSurveys(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsExistingSurvey()
|
||||||
|
{
|
||||||
|
$rpcClientManager = $this->getJsonRpcClientManager(4);
|
||||||
|
$sessionManager = $this->getSessionManager();
|
||||||
|
|
||||||
|
$this->createServiceWithoutSurveys($rpcClientManager, $sessionManager);
|
||||||
|
$this->createServiceWithSurveys($rpcClientManager, $sessionManager);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are no surveys, verification of existence any survey always return false
|
||||||
|
*/
|
||||||
|
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1));
|
||||||
|
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1, true));
|
||||||
|
|
||||||
|
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2));
|
||||||
|
static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2, true));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are surveys, verification of existence active survey always return true
|
||||||
|
*/
|
||||||
|
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1));
|
||||||
|
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1, true));
|
||||||
|
|
||||||
|
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2));
|
||||||
|
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2, true));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are surveys, verification of existence of non-active survey shows difference
|
||||||
|
*/
|
||||||
|
static::assertTrue($this->serviceWithSurveys->isExistingSurvey(3));
|
||||||
|
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(3, true));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are surveys, verification of existence non-existing survey always return false
|
||||||
|
*/
|
||||||
|
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4));
|
||||||
|
static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetStartSurveyUrl()
|
||||||
|
{
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$participant = new Participant([
|
||||||
|
'tid' => 1,
|
||||||
|
'firstname' => 'John',
|
||||||
|
'lastname' => 'Scott',
|
||||||
|
'email' => 'john@scott.com',
|
||||||
|
'token' => $token,
|
||||||
|
]);
|
||||||
|
|
||||||
|
static::assertEquals($expectedUrl, $this->serviceWithoutSurveys->getStartSurveyUrl($surveyId, $participant));
|
||||||
|
static::assertEquals($expectedUrl, $this->serviceWithSurveys->getStartSurveyUrl($surveyId, $participant));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns configuration used while connecting to LimeSurvey's API
|
||||||
|
*
|
||||||
|
* @return ConnectionConfiguration
|
||||||
|
*/
|
||||||
|
private function getConnectionConfiguration()
|
||||||
|
{
|
||||||
|
return new ConnectionConfiguration($this->connectionBaseUrl, '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::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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::class);
|
||||||
|
|
||||||
|
$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::class);
|
||||||
|
|
||||||
|
$rpcClientManager
|
||||||
|
->expects(static::exactly($runMethodCallCount))
|
||||||
|
->method('runMethod')
|
||||||
|
->willThrowException($exception);
|
||||||
|
|
||||||
|
return $rpcClientManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates instance of the tested service without surveys
|
||||||
|
*
|
||||||
|
* @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 createServiceWithoutSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
|
||||||
|
{
|
||||||
|
$configuration = $this->getConnectionConfiguration();
|
||||||
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
||||||
|
$this->serviceWithoutSurveys = new SurveyService($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates instance of the tested service with surveys
|
||||||
|
*
|
||||||
|
* @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 createServiceWithSurveys(PHPUnit_Framework_MockObject_MockObject $rpcClientManager, PHPUnit_Framework_MockObject_MockObject $sessionManager)
|
||||||
|
{
|
||||||
|
$configuration = $this->getConnectionConfiguration();
|
||||||
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
||||||
|
|
||||||
|
$allSurveys = new Surveys([
|
||||||
|
new Survey([
|
||||||
|
'sid' => 1,
|
||||||
|
'surveyls_title' => 'Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 2,
|
||||||
|
'surveyls_title' => 'Another Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 3,
|
||||||
|
'surveyls_title' => 'I am inactive',
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->serviceWithSurveys = new SurveyService($client, $allSurveys);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ use Generator;
|
|||||||
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\SystemMethodType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the type of method used while talking with LimeSurvey's API
|
* Test case of the type of method used while talking with LimeSurvey's API
|
||||||
@@ -21,6 +22,11 @@ use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
|||||||
*/
|
*/
|
||||||
class MethodTypeTest extends BaseTypeTestCase
|
class MethodTypeTest extends BaseTypeTestCase
|
||||||
{
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertHasNoConstructor(MethodType::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $incorrectMethod Type of method to verify
|
* @param string $incorrectMethod Type of method to verify
|
||||||
* @dataProvider provideIncorrectMethod
|
* @dataProvider provideIncorrectMethod
|
||||||
@@ -83,6 +89,14 @@ class MethodTypeTest extends BaseTypeTestCase
|
|||||||
yield[
|
yield[
|
||||||
MethodType::LIST_SURVEYS,
|
MethodType::LIST_SURVEYS,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
SystemMethodType::GET_SESSION_KEY,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
SystemMethodType::RELEASE_SESSION_KEY,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,6 +171,7 @@ class MethodTypeTest extends BaseTypeTestCase
|
|||||||
protected function getAllExpectedTypes()
|
protected function getAllExpectedTypes()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'ADD_PARTICIPANTS' => MethodType::ADD_PARTICIPANTS,
|
||||||
'ADD_RESPONSE' => MethodType::ADD_RESPONSE,
|
'ADD_RESPONSE' => MethodType::ADD_RESPONSE,
|
||||||
'EXPORT_STATISTICS' => MethodType::EXPORT_STATISTICS,
|
'EXPORT_STATISTICS' => MethodType::EXPORT_STATISTICS,
|
||||||
'GET_PARTICIPANT_PROPERTIES' => MethodType::GET_PARTICIPANT_PROPERTIES,
|
'GET_PARTICIPANT_PROPERTIES' => MethodType::GET_PARTICIPANT_PROPERTIES,
|
||||||
83
tests/Type/ReasonTypeTest.php
Normal file
83
tests/Type/ReasonTypeTest.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?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\Type;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the type of reason used by LimeSurvey's exception
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class ReasonTypeTest extends BaseTypeTestCase
|
||||||
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertHasNoConstructor(ReasonType::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getAllExpectedTypes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||||
|
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||||
|
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
|
||||||
|
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getTestedTypeInstance()
|
||||||
|
{
|
||||||
|
return new ReasonType();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function provideTypeToVerify()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'',
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'lorem',
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NO_PARTICIPANTS_FOUND,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NO_SURVEYS_FOUND,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
ReasonType::NO_TOKEN_TABLE,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,11 @@ use Meritoo\LimeSurvey\ApiClient\Type\SystemMethodType;
|
|||||||
*/
|
*/
|
||||||
class SystemMethodTypeTest extends BaseTypeTestCase
|
class SystemMethodTypeTest extends BaseTypeTestCase
|
||||||
{
|
{
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertHasNoConstructor(SystemMethodType::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
Reference in New Issue
Block a user