From c83272e06f5894315a19cf16088972c6a1903a05 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Sat, 1 Apr 2023 16:09:06 +0200 Subject: [PATCH] Add units tests for the modelAdmin class --- Source/Config/Validate.php | 10 ++++ Source/Config/config.php | 1 + Source/Tests/TestModel/ModelAdminTest.php | 69 +++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 Source/Tests/TestModel/ModelAdminTest.php diff --git a/Source/Config/Validate.php b/Source/Config/Validate.php index a1062df..72dd570 100644 --- a/Source/Config/Validate.php +++ b/Source/Config/Validate.php @@ -106,4 +106,14 @@ class Validate return true; } + public static function categories(array $categories): bool + { + foreach ($categories as $category) { + if (strlen($category) > $GLOBALS['categoryMaxLength']) { + return false; + } + } + return true; + } + } diff --git a/Source/Config/config.php b/Source/Config/config.php index 1024d98..fdb3d96 100644 --- a/Source/Config/config.php +++ b/Source/Config/config.php @@ -32,4 +32,5 @@ $GLOBALS['titleMaxLength']=50; $GLOBALS['typeMaxLength']=50; $GLOBALS['responseMaxLength']=200; $GLOBALS['usernameMaxLength']=50; +$GLOBALS['categoriesMaxLength']=50; diff --git a/Source/Tests/TestModel/ModelAdminTest.php b/Source/Tests/TestModel/ModelAdminTest.php new file mode 100644 index 0000000..3251751 --- /dev/null +++ b/Source/Tests/TestModel/ModelAdminTest.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