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.
SAE4.01_FORMULAIRE/Source/Tests/TestController/ControllerCandidateTest.php

105 lines
3.0 KiB

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