Result - store status provided by the LimeSurvey's API (instead of raw data) & do not process the data (because it's unknown)

This commit is contained in:
Meritoo
2017-09-26 21:40:25 +02:00
parent fd1ec32e1a
commit 07bc4ab4af
4 changed files with 188 additions and 2 deletions

View 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'),
];
}
}

View File

@@ -12,6 +12,7 @@ use DateTime;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
use Meritoo\LimeSurvey\ApiClient\Result\Result;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
use PHPUnit_Framework_MockObject_MockObject;
@@ -45,6 +46,14 @@ class ResultTest extends BaseTestCase
*/
private $notIterableData;
/**
* Status provided instead of real data.
* An array with one key: "status".
*
* @var array
*/
private $statusInsteadData;
/**
* Result with empty data returned by the LimeSurvey's API.
* Mock of the tested class.
@@ -69,6 +78,13 @@ class ResultTest extends BaseTestCase
*/
private $notIterableDataResult;
/**
* Result with status provided instead of real data
*
* @var Result
*/
private $statusInsteadDataResult;
public function testConstructorVisibilityAndArguments()
{
static::assertConstructorVisibilityAndArguments(Result::class, OopVisibilityType::IS_PUBLIC, 2, 2);
@@ -110,6 +126,12 @@ class ResultTest extends BaseTestCase
static::assertEquals($this->iterableData, $iterableData);
}
public function testGetDataUsingProcessedDataWhoCannotBeProcessed()
{
$this->expectException(CannotProcessDataException::class);
$this->statusInsteadDataResult->getData();
}
public function testGetProcessedDataVisibilityAndArguments()
{
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);
}
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{
*/
@@ -149,6 +185,10 @@ class ResultTest extends BaseTestCase
],
];
$this->statusInsteadData = [
'status' => 'Invalid data',
];
$emptyData = [
MethodType::LIST_SURVEYS,
$this->emptyData,
@@ -167,6 +207,7 @@ class ResultTest extends BaseTestCase
$this->emptyDataResult = $this->getResultMock($emptyData);
$this->iterableDataResult = $this->getResultMock($iterableData);
$this->notIterableDataResult = $this->getResultMock($notIterableData);
$this->statusInsteadDataResult = new Result(MethodType::LIST_PARTICIPANTS, $this->statusInsteadData);
}
/**