You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.2 KiB
101 lines
3.2 KiB
<?php
|
|
|
|
namespace TestController;
|
|
|
|
|
|
use InvalidArgumentException;
|
|
use Model\ModelAdmin;
|
|
use PHPUnit\Framework\MockObject\Exception;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Controller\ControllerAdmin;
|
|
use stdClass;
|
|
|
|
class ControllerAdminTest extends TestCase
|
|
{
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function testAddQuestionThrowsExceptionWhenTypeIsMissing()
|
|
{
|
|
$controller = new ControllerAdmin();
|
|
$_POST['type'] = '';
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$controller->addQuestion();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws \Exception
|
|
*/
|
|
public function testAddQuestionCallsModelAdminAndGoesToQuestionsWhenTypeIsTextQuestion()
|
|
{
|
|
$modelMock = $this->createMock(ModelAdmin::class);
|
|
$modelMock->expects($this->once())->method('addQuestion');
|
|
$controller = new ControllerAdmin();
|
|
$controller->goToQuestions = $this->createMock(stdClass::class); // mock the method
|
|
$_POST['type'] = 'BusinessClass\TextQuestion';
|
|
$controller->addQuestion();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws \Exception
|
|
*/
|
|
public function testAddQuestionCallsModelAdminAndRequiresPossibleResponsesFormWhenTypeIsNotTextQuestion()
|
|
{
|
|
$modelMock = $this->createMock(ModelAdmin::class);
|
|
$modelMock->expects($this->once())->method('addQuestion');
|
|
$modelMock->expects($this->once())->method('getCategories');
|
|
$controller = new ControllerAdmin();
|
|
$controller->goToQuestions = $this->createMock(stdClass::class); // mock the method
|
|
$_POST['type'] = 'BusinessClass\OtherQuestion';
|
|
$GLOBALS['rep'] = 'path/to/';
|
|
$GLOBALS['views'] = ['possibleResponsesForm' => 'path/to/possibleResponsesForm.php'];
|
|
$this->expectOutputString(file_get_contents('path/to/possibleResponsesForm.php'));
|
|
$controller->addQuestion();
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function testAddResponseThrowsExceptionWhenParametersAreMissing()
|
|
{
|
|
$controller = new ControllerAdmin();
|
|
$_POST = [];
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$controller->addResponse();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws \Exception
|
|
*/
|
|
public function testAddResponseCallsModelAdminAndRequiresContinueWhenParametersAreValid()
|
|
{
|
|
$modelMock = $this->createMock(ModelAdmin::class);
|
|
$modelMock->expects($this->once())->method('addResponse');
|
|
$modelMock->expects($this->once())->method('getCategories');
|
|
$controller = new ControllerAdmin();
|
|
$GLOBALS['rep'] = 'path/to/';
|
|
$GLOBALS['views'] = ['continue' => 'path/to/continue.php'];
|
|
$_POST['idQuestion'] = '123';
|
|
$_POST['question'] = 'What is the meaning of life?';
|
|
$_POST['type'] = 'BusinessClass\OtherQuestion';
|
|
$this->expectOutputString(file_get_contents('path/to/continue.php'));
|
|
$controller->addResponse();
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function testContinueResponseThrowsExceptionWhenParametersAreMissing()
|
|
{
|
|
$controller = new ControllerAdmin();
|
|
$_POST = [];
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$controller->continueResponse();
|
|
|
|
}
|
|
}
|
|
|