From 3df98e7767d8beb435c68473633ba83c8c3c7f28 Mon Sep 17 00:00:00 2001 From: tombrain Date: Sun, 5 Nov 2023 23:22:15 +0100 Subject: [PATCH] new testmethods --- tests/uts/formhandler_ConstructorTest.php | 48 ++++++++++++++++++++++ tests/uts/formhandler_DataHandlingTest.php | 9 ++++ tests/uts/formhandler_RadiobuttonTest.php | 8 ++++ tests/uts/formhandler_SelectFieldTest.php | 9 ++++ tests/uts/formhandler_TimeFieldTest.php | 12 ++++++ tests/uts/formhandler_UploadFieldTest.php | 38 +++++++++++++++++ 6 files changed, 124 insertions(+) diff --git a/tests/uts/formhandler_ConstructorTest.php b/tests/uts/formhandler_ConstructorTest.php index ad4b438..76c9155 100644 --- a/tests/uts/formhandler_ConstructorTest.php +++ b/tests/uts/formhandler_ConstructorTest.php @@ -116,4 +116,52 @@ public function testNoField(): void $this->assertTriggertError('Try to get the value of an unknown field "notexist"!', E_USER_WARNING); } + + public function testNoName(): void + { + // three different versions + $form = new FormHandler(); + $this->assertEquals(FH_DEFAULT_FORM_NAME, $form->getName()); + $form2 = new FormHandler(); + $this->assertEquals(FH_DEFAULT_FORM_NAME . "1", $form2->getName()); + $form3 = new FormHandler(); + $this->assertEquals(FH_DEFAULT_FORM_NAME . "2", $form3->getName()); + } + + public function testSetAction(): void + { + // three different versions + $form = new FormHandler(null, "action"); + + $a = $this->getPrivateProperty($form, "_action"); + $this->assertEquals("action", $a); + } + + public function testSetActionFromQueryString(): void + { + $_SERVER['PHP_SELF'] = "phpself.php"; + + // three different versions + $form = new FormHandler(); + + $a = $this->getPrivateProperty($form, "_action"); + $this->assertEquals("phpself.php", $a); + + $_SERVER['QUERY_STRING'] = "querystring"; + + // three different versions + $form2 = new FormHandler(); + + $a = $this->getPrivateProperty($form2, "_action"); + $this->assertEquals("phpself.php?querystring", $a); + } + + public function testSetExtra(): void + { + // three different versions + $form = new FormHandler(null, null, "extra"); + + $a = $this->getPrivateProperty($form, "_extra"); + $this->assertEquals("extra", $a); + } } diff --git a/tests/uts/formhandler_DataHandlingTest.php b/tests/uts/formhandler_DataHandlingTest.php index cee1665..7137ca4 100644 --- a/tests/uts/formhandler_DataHandlingTest.php +++ b/tests/uts/formhandler_DataHandlingTest.php @@ -28,6 +28,15 @@ public function testGetAsArray(): void $this->assertEquals("21", $day); } + public function testGetAsArray_expectedError(): void + { + $form = new FormHandler(); + + $this->assertFalse($form->getAsArray('date')); + + $this->assertTriggertError('The datefield "date" does not exists!', E_USER_NOTICE); + } + public function testGetValue(): void { $_POST['FormHandler_submit'] = "1"; diff --git a/tests/uts/formhandler_RadiobuttonTest.php b/tests/uts/formhandler_RadiobuttonTest.php index 2f1e841..755e28a 100644 --- a/tests/uts/formhandler_RadiobuttonTest.php +++ b/tests/uts/formhandler_RadiobuttonTest.php @@ -246,4 +246,12 @@ public function test_validator(): void $t['radiobutton'] ); } + + public function test_expected_error_options_no_array(): void + { + $form = new FormHandler(); + + $form->radioButton("Radiobutton", "radiobutton", "noarray"); + $this->assertTriggertError("You have to give an array as value with the radiobutton 'radiobutton'", E_USER_WARNING); + } }; diff --git a/tests/uts/formhandler_SelectFieldTest.php b/tests/uts/formhandler_SelectFieldTest.php index d9057af..08a000b 100644 --- a/tests/uts/formhandler_SelectFieldTest.php +++ b/tests/uts/formhandler_SelectFieldTest.php @@ -196,4 +196,13 @@ public function test_new_extra(): void 'error_selectfield' ]); } + + public function test_expected_error_options_no_array(): void + { + $form = new FormHandler(); + + $form->selectField("Selectfield", "selectfield", "noarray"); + + $this->assertTriggertError("You have to give an array as value with the selectfield 'selectfield'", E_USER_WARNING); + } }; diff --git a/tests/uts/formhandler_TimeFieldTest.php b/tests/uts/formhandler_TimeFieldTest.php index eca09f1..a10fdc3 100644 --- a/tests/uts/formhandler_TimeFieldTest.php +++ b/tests/uts/formhandler_TimeFieldTest.php @@ -219,6 +219,18 @@ public function test_new_format(): void $this->assertFormFlushContains($form, $aExpected); } + public function test_new_format_expected_formaterror(): void + { + define('FH_TIMEFIELD_SET_CUR_TIME', false); + $form = new FormHandler(); + + $this->assertFalse($form->isPosted()); + + $form->timeField("Timefield", "timefield", null, false, 13); + + $this->assertTriggertError("Invalid value as hour format! Only 12 or 24 are allowed!", E_USER_WARNING); + } + /** * @dataProvider timeintervall */ diff --git a/tests/uts/formhandler_UploadFieldTest.php b/tests/uts/formhandler_UploadFieldTest.php index 2914e87..c88e1e9 100644 --- a/tests/uts/formhandler_UploadFieldTest.php +++ b/tests/uts/formhandler_UploadFieldTest.php @@ -572,4 +572,42 @@ public function test_mergeImage(): void $this->assertTrue(unlink("{$this->_tempPath}/uploadedandmerged.jpg")); } + + public function testIsUploaded_expectedError(): void + { + $form = new FormHandler(); + + $this->assertFalse($form->isUploaded('upload')); + + $this->assertTriggertError('Error, the uploadfield "upload" does not exists!', E_USER_NOTICE); + } + + public function testIsUploaded_expectedError2(): void + { + $form = new FormHandler(); + + $form->textField("Upload", "upload"); + + $this->assertFalse($form->isUploaded('upload')); + + $this->assertTriggertError('Error, the field "upload" is not an uploadfield!', E_USER_NOTICE); + } + + public function testGetFileInfo_expectedError(): void + { + $form = new FormHandler(); + + $this->assertCount(0, $form->getFileInfo('upload')); + $this->assertTriggertError('Error, the uploadfield "upload" does not exists!', E_USER_NOTICE); + } + + public function testGetFileInfo_expectedError2(): void + { + $form = new FormHandler(); + + $form->textField("Upload", "upload"); + + $this->assertCount(0, $form->getFileInfo('upload')); + $this->assertTriggertError('Error, the field "upload" is not an uploadfield!', E_USER_NOTICE); + } };