From 377307362cdff5ce6e0f7f36cea38feec40b821c Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Sat, 1 Apr 2023 16:01:20 +0200 Subject: [PATCH] Add unitary tests for the modelAdmin class --- Source/Config/Validate.php | 3 +- Source/Config/config.php | 1 + Source/Tests/TestModel/testModelAdmin.php | 69 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 Source/Tests/TestModel/testModelAdmin.php diff --git a/Source/Config/Validate.php b/Source/Config/Validate.php index 3f0f7bf..f3ac599 100644 --- a/Source/Config/Validate.php +++ b/Source/Config/Validate.php @@ -123,9 +123,8 @@ class Validate public static function categories(array $categories): bool { - global $categoryMaxLength; foreach ($categories as $category) { - if (strlen($category) > $categoryMaxLength) { + if (strlen($category) > $GLOBALS['categoryMaxLength']) { return false; } } diff --git a/Source/Config/config.php b/Source/Config/config.php index def087e..d3ce9cc 100644 --- a/Source/Config/config.php +++ b/Source/Config/config.php @@ -42,4 +42,5 @@ $GLOBALS['titleMaxLength']=50; $GLOBALS['typeMaxLength']=50; $GLOBALS['responseMaxLength']=200; $GLOBALS['usernameMaxLength']=50; +$GLOBALS["categoriesMaxLength"]=50; diff --git a/Source/Tests/TestModel/testModelAdmin.php b/Source/Tests/TestModel/testModelAdmin.php new file mode 100644 index 0000000..3251751 --- /dev/null +++ b/Source/Tests/TestModel/testModelAdmin.php @@ -0,0 +1,69 @@ +assertInstanceOf(ModelAdmin::class, $model); + } + + public function testAddQuestionInvalidType() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage("Type de question invalide"); + $_POST['type']="JeSuisUnTypeInvalideDePlusDe50CaracteresEtJeSuisTropLongPourEtreUnTypeDeQuestionValide"; + $_POST['question']="Suis-je une question valide ?"; + $model = (new ModelAdmin())->addQuestion(); + } + + public function testDeleteQuestionInvalidType() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage("Type de question invalide"); + $_POST['type']="JeSuisUnTypeInvalideDePlusDe50CaracteresEtJeSuisTropLongPourEtreUnTypeDeQuestionValide"; + $_POST['question']="Suis-je une question valide ?"; + $model = (new ModelAdmin())->deleteQuestion(); + } + + public function testAddResponseInvalidCategories() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Categories invalides'); + $_POST['categories']=['JeSuisUneCategorieInvalideDePlusDe50CaracteresEtJeSuisTropLonguePourEtreUneCategorieValide']; + $_POST['response']="Suis-je une réponse valide ?"; + $model = (new ModelAdmin())->addResponse(); + } + + public function testAddKeywordInvalidKeyword() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Mot clé invalide'); + $_POST['keyword']="JeSuisUnMotCleInvalideDePlusDe50CaracteresEtJeSuisTropLonguePourEtreUnMotCleValide"; + $model = (new ModelAdmin())->addKeyword(); + } + + public function testGetCategories() + { + $categories = (new ModelAdmin())->getCategories(); + $this->assertIsArray($categories); + } + + public function testGetQuestions() + { + $questions = (new ModelAdmin())->getQuestions(); + $this->assertIsArray($questions); + } + + public function testGetResponsesCandidate() + { + $responses = (new ModelAdmin())->getResponsesCandidate(); + $this->assertIsArray($responses); + } +} \ No newline at end of file