Base class for one item - add class constructor & allow to set values directly in the constructor

This commit is contained in:
Meritoo
2017-09-29 20:56:39 +02:00
parent bbd466610c
commit 6159731768
17 changed files with 190 additions and 84 deletions

View File

@@ -9,6 +9,7 @@
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;
/**
@@ -21,31 +22,11 @@ class BaseItemTest extends BaseTestCase
{
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(BaseItem::class);
static::assertConstructorVisibilityAndArguments(BaseItem::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testSetValues()
public function testSetValuesVisibilityAndArguments()
{
$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;
static::assertMethodVisibilityAndArguments(BaseItem::class, 'setValues', OopVisibilityType::IS_PRIVATE, 1, 1);
}
}

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

View File

@@ -49,17 +49,17 @@ class UnknownInstanceOfResultItemTest extends BaseTestCase
*/
public function provideMethodName()
{
$template = '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?';
$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, 'getItemInstance'),
sprintf($template, MethodType::LIST_SURVEYS, ResultProcessor::class, 'getItemClassName'),
];
yield[
MethodType::ADD_PARTICIPANTS,
sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemInstance'),
sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemClassName'),
];
}
}

View File

@@ -125,9 +125,8 @@ class ParticipantsTest extends BaseTestCase
{
$surveyId = 1;
$email = 'john@scott.com';
$participant = new Participant();
$participant->setValues([
$participant = new Participant([
'firstname' => 'John',
'lastname' => 'Scott',
'email' => $email,
@@ -148,9 +147,8 @@ class ParticipantsTest extends BaseTestCase
{
$surveyId = 1;
$email = 'john@scott.com';
$participant = new Participant();
$participant->setValues([
$participant = new Participant([
'firstname' => 'John',
'lastname' => 'Scott',
'email' => $email,

View File

@@ -9,6 +9,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +45,7 @@ class ParticipantShortTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(ParticipantShort::class);
static::assertConstructorVisibilityAndArguments(ParticipantShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheParticipant()
@@ -116,7 +117,7 @@ class ParticipantShortTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getParticipantsRawData();
$this->participant1stInstance = (new ParticipantShort())->setValues($this->rawData[0]);
$this->participant2ndInstance = (new ParticipantShort())->setValues($this->rawData[1]);
$this->participant1stInstance = new ParticipantShort($this->rawData[0]);
$this->participant2ndInstance = new ParticipantShort($this->rawData[1]);
}
}

View File

@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
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\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -45,7 +46,7 @@ class ParticipantTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Participant::class);
static::assertConstructorVisibilityAndArguments(Participant::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheParticipant()
@@ -215,7 +216,7 @@ class ParticipantTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getParticipantsRawData();
$this->participant1stInstance = (new Participant())->setValues($this->rawData[0]);
$this->participant2ndInstance = (new Participant())->setValues($this->rawData[1]);
$this->participant1stInstance = new Participant($this->rawData[0]);
$this->participant2ndInstance = new Participant($this->rawData[1]);
}
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\LimeSurvey\ApiClient\Result\Item\QuestionShort;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +45,7 @@ class QuestionShortTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(QuestionShort::class);
static::assertConstructorVisibilityAndArguments(QuestionShort::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheQuestionShort()
@@ -222,7 +223,7 @@ class QuestionShortTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getQuestionsRawData();
$this->question1stInstance = (new QuestionShort())->setValues($this->rawData[0]);
$this->question2ndInstance = (new QuestionShort())->setValues($this->rawData[1]);
$this->question1stInstance = new QuestionShort($this->rawData[0]);
$this->question2ndInstance = new QuestionShort($this->rawData[1]);
}
}

View File

@@ -9,6 +9,7 @@
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\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -44,7 +45,7 @@ class QuestionTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Question::class);
static::assertConstructorVisibilityAndArguments(Question::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheQuestionShort()
@@ -284,7 +285,7 @@ class QuestionTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getQuestionsRawData();
$this->question1stInstance = (new Question())->setValues($this->rawData[0]);
$this->question2ndInstance = (new Question())->setValues($this->rawData[1]);
$this->question1stInstance = new Question($this->rawData[0]);
$this->question2ndInstance = new Question($this->rawData[1]);
}
}

View File

@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\Test\ApiClient\Result\Item;
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\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
@@ -45,7 +46,7 @@ class SurveyTest extends BaseTestCase
public function testConstructorVisibilityAndArguments()
{
static::assertHasNoConstructor(Survey::class);
static::assertConstructorVisibilityAndArguments(Survey::class, OopVisibilityType::IS_PUBLIC, 1, 0);
}
public function testCreateOfTheSurvey()
@@ -119,7 +120,7 @@ class SurveyTest extends BaseTestCase
parent::setUp();
$this->rawData = static::getSurveysRawData();
$this->survey1stInstance = (new Survey())->setValues($this->rawData[0]);
$this->survey2ndInstance = (new Survey())->setValues($this->rawData[1]);
$this->survey1stInstance = new Survey($this->rawData[0]);
$this->survey2ndInstance = new Survey($this->rawData[1]);
}
}

View File

@@ -75,9 +75,9 @@ class ResultProcessorTest extends BaseTestCase
static::assertInstanceOf(BaseItem::class, $processed);
}
public function testGetItemInstanceVisibilityAndArguments()
public function testGetItemClassNameVisibilityAndArguments()
{
static::assertMethodVisibilityAndArguments(ResultProcessor::class, 'getItemInstance', OopVisibilityType::IS_PRIVATE, 1, 1);
static::assertMethodVisibilityAndArguments(ResultProcessor::class, 'getItemClassName', OopVisibilityType::IS_PRIVATE, 1, 1);
}
public function testRunWithUnknownResultClass()

View File

@@ -297,13 +297,13 @@ class ParticipantServiceTest extends BaseTestCase
$allParticipants = new Participants([
1 => new Collection([
(new Participant())->setValues([
new Participant([
'firstname' => 'John',
'lastname' => 'Scott',
'email' => 'john@scott.com',
'completed' => 'Y',
]),
(new Participant())->setValues([
new Participant([
'firstname' => 'Mary',
'lastname' => 'Jane',
'email' => 'mary@jane.com',

View File

@@ -204,11 +204,11 @@ class SurveyServiceTest extends BaseTestCase
$client = new Client($configuration, $rpcClientManager, $sessionManager);
$allSurveys = new Collection([
(new Survey())->setValues([
new Survey([
'sid' => 1,
'surveyls_title' => 'Test',
]),
(new Survey())->setValues([
new Survey([
'sid' => 2,
'surveyls_title' => 'Another Test',
]),