diff --git a/Source/Config/config.php b/Source/Config/config.php index 25eebd7..1024d98 100644 --- a/Source/Config/config.php +++ b/Source/Config/config.php @@ -11,6 +11,7 @@ $views['categories'] = 'Views/HTML/categories.php'; $views['questions'] = 'Views/HTML/questions.php'; $views['responses'] = 'Views/HTML/responses.php'; $views['thanks'] = 'Views/HTML/thanks.php'; +$views['error']='Views/HTML/error.php'; $_SERVER['BASE_URI'] = ''; diff --git a/Source/Controller/FrontController.php b/Source/Controller/FrontController.php index ebb37d0..3d3c80c 100644 --- a/Source/Controller/FrontController.php +++ b/Source/Controller/FrontController.php @@ -3,8 +3,6 @@ namespace Controller; use Exception; -use PDOException; -use Config\Validate; use Config\Clean; use Config\AltoRouter; @@ -13,9 +11,12 @@ use Config\AltoRouter; */ class FrontController { - private $router; - private $rights; - + private AltoRouter $router; + private array $rights; + + /** + * @throws Exception + */ public function __construct() { $this->router = new AltoRouter(); $this->router->setBasePath($_SERVER['BASE_URI']); @@ -26,7 +27,23 @@ class FrontController { ); } - public function run() { + /** + * @return array + */ + public function getRights(): array + { + return $this->rights; + } + + /** + * @return AltoRouter + */ + public function getRouter(): AltoRouter + { + return $this->router; + } + public function run(): void + { global $error,$rep,$views; $exists=false; $match = $this->router->match(); @@ -56,7 +73,11 @@ class FrontController { } } - private function mapRoutes() { + /** + * @throws Exception + */ + protected function mapRoutes(): void + { global $controller; $this->router->map('GET', '/', array($controller['Candidate'], 'goToForm'), 'goToForm'); $this->router->map('POST', '/submitForm', array($controller['Candidate'], 'submitForm'), 'submitForm'); @@ -73,4 +94,4 @@ class FrontController { $this->router->map('GET','/goToQuestions',array($controller['Admin'],'goToQuestions'),'goToQuestions'); $this->router->map('GET','/goToResponses',array($controller['Admin'],'goToResponses'),'goToResponses'); } -} +} \ No newline at end of file diff --git a/Source/Exceptions/InvalidUsernameOrPasswordException.php b/Source/Exceptions/InvalidUsernameOrPasswordException.php index d536f7e..8e89b8d 100644 --- a/Source/Exceptions/InvalidUsernameOrPasswordException.php +++ b/Source/Exceptions/InvalidUsernameOrPasswordException.php @@ -6,7 +6,7 @@ use Exception; class InvalidUsernameOrPasswordException extends Exception { - public function __construct($message = "nom d'utilisateur ou mot de passe invalide", $code = 0, Exception $previous = null) + public function __construct($message = "Nom d'utilisateur ou mot de passe invalide", $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/Source/Tests/TestController/.gitkeep b/Source/Tests/TestController/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Source/Tests/TestController/ControllerAdminTest.php b/Source/Tests/TestController/ControllerAdminTest.php new file mode 100644 index 0000000..2346825 --- /dev/null +++ b/Source/Tests/TestController/ControllerAdminTest.php @@ -0,0 +1,100 @@ +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(); + + } +} + diff --git a/Source/Tests/TestController/ControllerCandidateTest.php b/Source/Tests/TestController/ControllerCandidateTest.php new file mode 100644 index 0000000..3dd6f76 --- /dev/null +++ b/Source/Tests/TestController/ControllerCandidateTest.php @@ -0,0 +1,104 @@ +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('
'); + $this->controller->goToForm(); + $this->expectOutputRegex('/