Add coverage.xml generation file 2
continuous-integration/drone/push Build is passing Details

unit_test
dorian.hodin 2 years ago
parent d875f89f4a
commit f2a588f494

@ -1,100 +0,0 @@
<?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();
}
}

@ -1,104 +0,0 @@
<?php
namespace TestController;
use PHPUnit\Framework\TestCase;
use Controller\ControllerCandidate;
use Model\ModelCandidate;
use Exception;
class ControllerCandidateTest extends TestCase
{
protected ControllerCandidate $controller;
protected function setUp(): void
{
parent::setUp();
$this->controller = new ControllerCandidate();
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws Exception
*/
public function testGoToForm(): void
{
$modelMock = $this->createMock(ModelCandidate::class);
$modelMock->expects($this->once())
->method('getForm')
->willReturn('<form></form>');
$this->controller->goToForm();
$this->expectOutputRegex('/<form>.*<\/form>/s');
}
public function testGoToAdminLogin(): void
{
global $rep, $views;
$this->controller->goToAdminLogin();
$this->expectOutputString(file_get_contents($rep . $views['adminLogin']));
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws Exception
*/
public function testSubmitForm(): void
{
$modelMock = $this->createMock(ModelCandidate::class);
$modelMock->expects($this->once())
->method('submitForm');
$this->controller->submitForm();
$this->expectOutputRegex('/' . preg_quote('Location: ?thanks', '/') . '/');
}
public function testGoToThanks(): void
{
global $rep, $views;
$this->controller->goToThanks();
$this->expectOutputString(file_get_contents($rep . $views['thanks']));
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testLoginAsAdmin(): void
{
global $rep, $views;
$_SESSION['role'] = 'Admin';
$modelMock = $this->createMock(ModelCandidate::class);
$modelMock->expects($this->once())
->method('login');
$this->controller->login();
$this->expectOutputString(file_get_contents($rep . $views['admin']));
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testLoginAsNonAdmin(): void
{
global $rep, $views;
$_SESSION['role'] = 'NonAdmin';
$modelMock = $this->createMock(ModelCandidate::class);
$modelMock->expects($this->once())
->method('login');
$this->controller->login();
$this->expectOutputString(file_get_contents($rep . $views['adminLogin']));
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testLoginWithException(): void
{
global $rep, $views;
$_SESSION['role'] = 'NonAdmin';
$modelMock = $this->createMock(ModelCandidate::class);
$modelMock->expects($this->once())
->method('login')
->willThrowException(new Exception('Invalid credentials'));
$this->controller->login();
$this->expectOutputString(file_get_contents($rep . $views['adminLogin']));
}
}

@ -1,35 +0,0 @@
<?php
namespace TestController;
use PHPUnit\Framework\TestCase;
use Controller\FrontController;
class FrontControllerTest extends TestCase
{
private FrontController $frontController;
public function setUp(): void
{
$this->frontController = new FrontController();
}
public function testRouterInstance(): void
{
$this->assertInstanceOf('\Config\AltoRouter', $this->frontController->getRouter());
}
public function testRightsInstance(): void
{
$this->assertIsArray($this->frontController->getRights());
}
public function testRunMethod(): void
{
$this->expectOutputString('');
$_SERVER['BASE_URI'] = '/';
$_SESSION['role'] = 'Candidate';
$this->frontController->run();
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save