mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 18:11:50 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3b0e66fb3 | ||
|
|
0fbfc9780d | ||
|
|
92315bd853 | ||
|
|
c3e6935dd8 | ||
|
|
07bc4ab4af | ||
|
|
fd1ec32e1a |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,6 +11,7 @@
|
|||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
### Composer
|
### Composer
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
|
/composer.lock
|
||||||
/composer.phar
|
/composer.phar
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -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.3",
|
"version": "0.0.6",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Meritoo",
|
"name": "Meritoo",
|
||||||
|
|||||||
3581
composer.lock
generated
3581
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -59,15 +59,26 @@ 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)
|
||||||
@@ -201,6 +212,30 @@ class ConnectionConfiguration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns information if the SSL certificate verification is turned on
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isVerifySslCertificateOn()
|
||||||
|
{
|
||||||
|
return $this->verifySslCertificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets information if the SSL certificate verification is turned on
|
||||||
|
*
|
||||||
|
* @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned on.
|
||||||
|
* Otherwise - turned off.
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setVerifySslCertificate($verifySslCertificate = true)
|
||||||
|
{
|
||||||
|
$this->verifySslCertificate = $verifySslCertificate;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
* Additional / extra methods (neither getters, nor setters)
|
* Additional / extra methods (neither getters, nor setters)
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?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
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $reason Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||||
|
*/
|
||||||
|
public function __construct($reason)
|
||||||
|
{
|
||||||
|
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||||
|
$message = sprintf($template, $reason);
|
||||||
|
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -85,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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ class ResultProcessor
|
|||||||
*/
|
*/
|
||||||
if (MethodType::isResultIterable($method)) {
|
if (MethodType::isResultIterable($method)) {
|
||||||
$items = [];
|
$items = [];
|
||||||
$emptyItem = clone $item;
|
|
||||||
|
|
||||||
foreach ($rawData as $itemData) {
|
foreach ($rawData as $itemData) {
|
||||||
|
$emptyItem = clone $item;
|
||||||
$items[] = $emptyItem->setValues($itemData);
|
$items[] = $emptyItem->setValues($itemData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,16 +79,43 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Status is unknown?
|
||||||
|
* Let's process the raw data
|
||||||
|
*/
|
||||||
|
if (empty($this->status)) {
|
||||||
return $this->getProcessedData($this->rawData);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns processed data based on the raw data returned by the LimeSurvey's API
|
* Returns processed data based on the raw data returned by the LimeSurvey's API
|
||||||
*
|
*
|
||||||
@@ -119,4 +154,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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,16 @@ use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
|||||||
*/
|
*/
|
||||||
class ConnectionConfigurationTest extends BaseTestCase
|
class ConnectionConfigurationTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Simple instance of the configuration
|
||||||
|
*
|
||||||
|
* @var ConnectionConfiguration
|
||||||
|
*/
|
||||||
|
private $simpleConfiguration;
|
||||||
|
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
static::assertConstructorVisibilityAndArguments(ConnectionConfiguration::class, OopVisibilityType::IS_PUBLIC, 4, 3);
|
static::assertConstructorVisibilityAndArguments(ConnectionConfiguration::class, OopVisibilityType::IS_PUBLIC, 5, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,69 +56,67 @@ class ConnectionConfigurationTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testConstructor()
|
public function testConstructor()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
static::assertEquals('http://test.com', $this->simpleConfiguration->getBaseUrl());
|
||||||
|
static::assertEquals('test1', $this->simpleConfiguration->getUsername());
|
||||||
static::assertEquals('http://test.com', $configuration->getBaseUrl());
|
static::assertEquals('test2', $this->simpleConfiguration->getPassword());
|
||||||
static::assertEquals('test1', $configuration->getUsername());
|
static::assertFalse($this->simpleConfiguration->isDebugModeOn());
|
||||||
static::assertEquals('test2', $configuration->getPassword());
|
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetBaseUrl()
|
public function testSetBaseUrl()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setBaseUrl('http://lorem.ipsum');
|
||||||
|
static::assertEquals('http://lorem.ipsum', $this->simpleConfiguration->getBaseUrl());
|
||||||
|
|
||||||
$configuration->setBaseUrl('http://lorem.ipsum');
|
$this->simpleConfiguration->setBaseUrl('http://lorem.ipsum/');
|
||||||
static::assertEquals('http://lorem.ipsum', $configuration->getBaseUrl());
|
static::assertEquals('http://lorem.ipsum', $this->simpleConfiguration->getBaseUrl());
|
||||||
|
|
||||||
$configuration->setBaseUrl('http://lorem.ipsum/');
|
|
||||||
static::assertEquals('http://lorem.ipsum', $configuration->getBaseUrl());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetRemoteControlUrl()
|
public function testSetRemoteControlUrl()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setRemoteControlUrl('/lorem/ipsum');
|
||||||
$configuration->setRemoteControlUrl('/lorem/ipsum');
|
static::assertEquals('/lorem/ipsum', $this->simpleConfiguration->getRemoteControlUrl());
|
||||||
|
|
||||||
static::assertEquals('/lorem/ipsum', $configuration->getRemoteControlUrl());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetUsername()
|
public function testSetUsername()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setUsername('lorem');
|
||||||
$configuration->setUsername('lorem');
|
static::assertEquals('lorem', $this->simpleConfiguration->getUsername());
|
||||||
|
|
||||||
static::assertEquals('lorem', $configuration->getUsername());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetPassword()
|
public function testSetPassword()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setPassword('ipsum');
|
||||||
$configuration->setPassword('ipsum');
|
static::assertEquals('ipsum', $this->simpleConfiguration->getPassword());
|
||||||
|
|
||||||
static::assertEquals('ipsum', $configuration->getPassword());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetDebugMode()
|
public function testSetDebugMode()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setDebugMode();
|
||||||
|
static::assertFalse($this->simpleConfiguration->isDebugModeOn());
|
||||||
|
|
||||||
$configuration->setDebugMode();
|
$this->simpleConfiguration->setDebugMode(false);
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
static::assertFalse($this->simpleConfiguration->isDebugModeOn());
|
||||||
|
|
||||||
$configuration->setDebugMode(false);
|
$this->simpleConfiguration->setDebugMode(true);
|
||||||
static::assertFalse($configuration->isDebugModeOn());
|
static::assertTrue($this->simpleConfiguration->isDebugModeOn());
|
||||||
|
}
|
||||||
|
|
||||||
$configuration->setDebugMode(true);
|
public function testSetVerifySslCertificate()
|
||||||
static::assertTrue($configuration->isDebugModeOn());
|
{
|
||||||
|
$this->simpleConfiguration->setVerifySslCertificate();
|
||||||
|
static::assertTrue($this->simpleConfiguration->isVerifySslCertificateOn());
|
||||||
|
|
||||||
|
$this->simpleConfiguration->setVerifySslCertificate(false);
|
||||||
|
static::assertFalse($this->simpleConfiguration->isVerifySslCertificateOn());
|
||||||
|
|
||||||
|
$this->simpleConfiguration->setVerifySslCertificate(true);
|
||||||
|
static::assertTrue($this->simpleConfiguration->isVerifySslCertificateOn());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetFullUrl()
|
public function testGetFullUrl()
|
||||||
{
|
{
|
||||||
$configuration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
$this->simpleConfiguration->setRemoteControlUrl('lorem/ipsum');
|
||||||
$configuration->setRemoteControlUrl('lorem/ipsum');
|
static::assertEquals('http://test.com/lorem/ipsum', $this->simpleConfiguration->getFullUrl());
|
||||||
|
|
||||||
static::assertEquals('http://test.com/lorem/ipsum', $configuration->getFullUrl());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,4 +154,13 @@ class ConnectionConfigurationTest extends BaseTestCase
|
|||||||
'htp:/dolor.com',
|
'htp:/dolor.com',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->simpleConfiguration = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ 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;
|
||||||
@@ -39,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()
|
||||||
|
|||||||
@@ -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,28 +47,43 @@ 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()
|
||||||
{
|
{
|
||||||
@@ -76,15 +92,15 @@ class ResultTest extends BaseTestCase
|
|||||||
|
|
||||||
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,6 +126,12 @@ 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()
|
||||||
{
|
{
|
||||||
static::assertMethodVisibilityAndArguments(Result::class, 'getProcessedData', OopVisibilityType::IS_PRIVATE, 1, 1);
|
static::assertMethodVisibilityAndArguments(Result::class, 'getProcessedData', OopVisibilityType::IS_PRIVATE, 1, 1);
|
||||||
@@ -120,6 +142,20 @@ class ResultTest extends BaseTestCase
|
|||||||
static::assertMethodVisibilityAndArguments(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));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc{
|
* {@inheritdoc{
|
||||||
*/
|
*/
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user