From 1df36ec243697db189a005826c3dfc4316c03a7a Mon Sep 17 00:00:00 2001 From: tombrain Date: Sun, 5 Nov 2023 21:08:47 +0100 Subject: [PATCH] new: triggered Errors by assertTriggertError --- tests/uts/dbFormhandler_TextFieldTest.php | 3 +- tests/uts/dbFormhandler_dbListFieldTest.php | 3 +- tests/uts/formhandler_AutocompleteTest.php | 16 +++++------ tests/uts/formhandler_ConstructorTest.php | 3 +- tests/uts/formhandler_DateTextFieldTest.php | 26 +++++++++-------- ...rmhandler_LookAndFeel_setMaxLengthTest.php | 4 +-- ...ormhandler_LookAndFeel_setTabIndexTest.php | 6 ++-- tests/uts/formhandler_SetFocusTest.php | 5 ++-- tests/uts/formhandler_ViewModeTest.php | 8 +++--- tests/uts/formhandler_jsDateTextFieldTest.php | 4 +-- tests/uts/helper/FormhandlerTestCase.php | 28 +++++++++++++++---- 11 files changed, 62 insertions(+), 44 deletions(-) diff --git a/tests/uts/dbFormhandler_TextFieldTest.php b/tests/uts/dbFormhandler_TextFieldTest.php index e1553cc..cc4dc0e 100644 --- a/tests/uts/dbFormhandler_TextFieldTest.php +++ b/tests/uts/dbFormhandler_TextFieldTest.php @@ -45,9 +45,8 @@ public function test_edit_noDataset(): void ->query($this->matches("SELECT * FROM test WHERE id = '123'")) ->willReturnResultSet([]); - $this->expectExceptionMessage("Try to edit a none existing record!"); - $this->setConnectedTable($form, "test"); + $this->assertTriggertError("Try to edit a none existing record!", E_USER_ERROR); } public function test_edit(): void diff --git a/tests/uts/dbFormhandler_dbListFieldTest.php b/tests/uts/dbFormhandler_dbListFieldTest.php index 13dbc0c..95c93a7 100644 --- a/tests/uts/dbFormhandler_dbListFieldTest.php +++ b/tests/uts/dbFormhandler_dbListFieldTest.php @@ -45,9 +45,8 @@ public function test_edit_noDataset(): void ->query($this->matches("SELECT * FROM test WHERE id = '123'")) ->willReturnResultSet([]); - $this->expectExceptionMessage("Try to edit a none existing record!"); - $this->setConnectedTable($form, "test"); + $this->assertTriggertError("Try to edit a none existing record!", E_USER_ERROR); } public function test_edit(): void diff --git a/tests/uts/formhandler_AutocompleteTest.php b/tests/uts/formhandler_AutocompleteTest.php index a2450fd..461dca5 100644 --- a/tests/uts/formhandler_AutocompleteTest.php +++ b/tests/uts/formhandler_AutocompleteTest.php @@ -27,9 +27,9 @@ public function test_no_textfield(): void $aOptions = ["first", "second", "third"]; - $this->expectExceptionMessage('You have to declare the textfield first! The field "textfield" does not exists in the form!'); - $form->setAutoComplete("textfield", $aOptions); + + $this->assertTriggertError('You have to declare the textfield first! The field "textfield" does not exists in the form!', E_USER_WARNING); } public function test_no_optionarray(): void @@ -38,9 +38,9 @@ public function test_no_optionarray(): void $form->textField("Textfield", "textfield"); - $this->expectExceptionMessage('You have to give an array as options!'); - $form->setAutoComplete("textfield", "nooptions"); + + $this->assertTriggertError('You have to give an array as options!', E_USER_WARNING); } public function test_after_show(): void @@ -66,9 +66,9 @@ public function test_after_no_textfield(): void $aOptions = ["first", "second", "third"]; - $this->expectExceptionMessage('You have to declare the textfield first! The field "textfield" does not exists in the form!'); - $form->setAutoCompleteAfter("textfield", "@", $aOptions); + + $this->assertTriggertError('You have to declare the textfield first! The field "textfield" does not exists in the form!', E_USER_WARNING); } public function test_after_no_optionarray(): void @@ -77,8 +77,8 @@ public function test_after_no_optionarray(): void $form->textField("Textfield", "textfield"); - $this->expectExceptionMessage('You have to give an array as options!'); - $form->setAutoCompleteAfter("textfield", "@", "nooptions"); + + $this->assertTriggertError('You have to give an array as options!', E_USER_WARNING); } }; diff --git a/tests/uts/formhandler_ConstructorTest.php b/tests/uts/formhandler_ConstructorTest.php index 8eb9b5e..e27722c 100644 --- a/tests/uts/formhandler_ConstructorTest.php +++ b/tests/uts/formhandler_ConstructorTest.php @@ -112,8 +112,9 @@ public function testNoField(): void { $form = new FormHandler(); - $this->expectExceptionMessage('Try to get the value of an unknown field "notexist"!'); + $this->assertNull($form->getValue("notexist")); + $this->assertTriggertError('Try to get the value of an unknown field "notexist"!', E_USER_WARNING); $form->getValue("notexist"); } } diff --git a/tests/uts/formhandler_DateTextFieldTest.php b/tests/uts/formhandler_DateTextFieldTest.php index a4d1c75..3a96d55 100644 --- a/tests/uts/formhandler_DateTextFieldTest.php +++ b/tests/uts/formhandler_DateTextFieldTest.php @@ -40,8 +40,8 @@ public function test_posted(): void $this->assertEquals("14-04-2020", $form->getValue("datetextfield4")); // already parsed into correct presentation! $this->assertEquals([2020, 4, 14], $form->getAsArray("datetextfield")); - $this->expectExceptionMessage("Value is not a valid date [14.04.2020]"); - $form->getAsArray("datetextfield2"); + $this->assertNull($form->getAsArray("datetextfield2")); + $this->assertTriggertError("Value is not a valid date [14.04.2020]", E_USER_ERROR); $this->assertEquals([2020, 4, 14], $form->getAsArray("datetextfield3")); $this->assertEquals([2020, 4, 14], $form->getAsArray("datetextfield4")); @@ -173,18 +173,22 @@ public function testParseOtherRepresentaions($dataTestGetAsArray): void if ($dataTestGetAsArray['mask'] != FH_DATETEXTFIELD_DEFAULT_DISPLAY) { - $this->expectExceptionMessage("Value is not a valid date [" . $dataTestGetAsArray['value'] . "]"); + $this->assertNull($form->getAsArray('datetextfield')); + $this->assertTriggertError("Value is not a valid date [" . $dataTestGetAsArray['value'] . "]", E_USER_ERROR); } - list($year, $month, $day) = $form->getAsArray('datetextfield'); + else + { + list($year, $month, $day) = $form->getAsArray('datetextfield'); - $this->assertEquals($dataTestGetAsArray['result']['year'], $year); - $this->assertEquals($dataTestGetAsArray['result']['month'], $month); - $this->assertEquals($dataTestGetAsArray['result']['day'], $day); + $this->assertEquals($dataTestGetAsArray['result']['year'], $year); + $this->assertEquals($dataTestGetAsArray['result']['month'], $month); + $this->assertEquals($dataTestGetAsArray['result']['day'], $day); - list($year2, $month2, $day2) = $form->getAsArray('datetextfield2'); + list($year2, $month2, $day2) = $form->getAsArray('datetextfield2'); - $this->assertEquals($dataTestGetAsArray['result']['year'], $year2); - $this->assertEquals($dataTestGetAsArray['result']['month'], $month2); - $this->assertEquals($dataTestGetAsArray['result']['day'], $day2); + $this->assertEquals($dataTestGetAsArray['result']['year'], $year2); + $this->assertEquals($dataTestGetAsArray['result']['month'], $month2); + $this->assertEquals($dataTestGetAsArray['result']['day'], $day2); + } } }; diff --git a/tests/uts/formhandler_LookAndFeel_setMaxLengthTest.php b/tests/uts/formhandler_LookAndFeel_setMaxLengthTest.php index d0e4bfe..2b17d5d 100644 --- a/tests/uts/formhandler_LookAndFeel_setMaxLengthTest.php +++ b/tests/uts/formhandler_LookAndFeel_setMaxLengthTest.php @@ -36,8 +36,8 @@ public function test_noField(): void { $form = new FormHandler(); - $this->expectExceptionMessage('You have to declare the textarea first! The field "textarea" does not exists in the form!'); $form->setMaxLength("textarea", 123); + $this->assertTriggertError('You have to declare the textarea first! The field "textarea" does not exists in the form!', E_USER_WARNING); } public function test_wrongField(): void @@ -46,7 +46,7 @@ public function test_wrongField(): void $form->textField("Textfield", "textfield"); - $this->expectExceptionMessage('You have to declare the textarea first! The field "textarea" does not exists in the form!'); $form->setMaxLength("textarea", 123); + $this->assertTriggertError('You have to declare the textarea first! The field "textarea" does not exists in the form!', E_USER_WARNING); } }; diff --git a/tests/uts/formhandler_LookAndFeel_setTabIndexTest.php b/tests/uts/formhandler_LookAndFeel_setTabIndexTest.php index 1c900de..600f8ce 100644 --- a/tests/uts/formhandler_LookAndFeel_setTabIndexTest.php +++ b/tests/uts/formhandler_LookAndFeel_setTabIndexTest.php @@ -104,8 +104,8 @@ public function test_error(): void $form->setTabIndex($tabs); - $this->expectExceptionMessage('Error, try to set the tabindex of an unknown field "fld4"!'); - - $form->flush(); + $form->flush(true); + $this->assertTriggertError('Error, try to set the tabindex of an unknown field "fld4"!', E_USER_NOTICE); + $this->assertTriggertError('Could net set focus to unknown field "fld4"', E_USER_NOTICE); } }; diff --git a/tests/uts/formhandler_SetFocusTest.php b/tests/uts/formhandler_SetFocusTest.php index dcb930b..350cc0a 100644 --- a/tests/uts/formhandler_SetFocusTest.php +++ b/tests/uts/formhandler_SetFocusTest.php @@ -60,9 +60,8 @@ public function test_nofield(): void { $form = new FormHandler(); - $this->expectExceptionMessage('Could net set focus to unknown field "textfield"'); - - $form->setFocus("textfield"); + static::assertFalse($form->setFocus("textfield")); + $this->assertTriggertError('Could net set focus to unknown field "textfield"', E_USER_NOTICE); } public function test_set_specialfield_datefield(): void diff --git a/tests/uts/formhandler_ViewModeTest.php b/tests/uts/formhandler_ViewModeTest.php index 3c3bbf7..ac6a7b8 100644 --- a/tests/uts/formhandler_ViewModeTest.php +++ b/tests/uts/formhandler_ViewModeTest.php @@ -65,9 +65,9 @@ public function test_fieldViewMode_error_fieldNotExist(): void $form->textField("Textfield", "textfield"); - $this->expectExceptionMessage('Error, could not find field "textfield2"! Please define the field first!'); - $form->setFieldViewMode("textfield2"); + + $this->assertTriggertError('Error, could not find field "textfield2"! Please define the field first!', E_USER_NOTICE); } public function test_fieldViewMode_error_NotAField(): void @@ -81,8 +81,8 @@ public function test_fieldViewMode_error_NotAField(): void $form->textField("Textfield", "textfield"); - $this->expectExceptionMessage('Error, could not find field "0"! Please define the field first!'); - $form->isFieldViewMode(0); + + $this->assertTriggertError('Error, could not find field "0"! Please define the field first!', E_USER_NOTICE); } }; diff --git a/tests/uts/formhandler_jsDateTextFieldTest.php b/tests/uts/formhandler_jsDateTextFieldTest.php index 39e7676..51e035a 100644 --- a/tests/uts/formhandler_jsDateTextFieldTest.php +++ b/tests/uts/formhandler_jsDateTextFieldTest.php @@ -55,8 +55,8 @@ public function test_posted(): void $this->assertEquals("14-04-2020", $form->getValue("jsdatetextfield4")); // already parsed into correct presentation! $this->assertEquals([2020, 4, 14], $form->getAsArray("jsdatetextfield")); - $this->expectExceptionMessage("Value is not a valid date [14.04.2020]"); - $form->getAsArray("jsdatetextfield2"); + $this->assertNull($form->getAsArray("jsdatetextfield2")); + $this->assertTriggertError("Value is not a valid date [14.04.2020]", E_USER_ERROR); $this->assertEquals([2020, 4, 14], $form->getAsArray("jsdatetextfield3")); $this->assertEquals([2020, 4, 14], $form->getAsArray("jsdatetextfield4")); diff --git a/tests/uts/helper/FormhandlerTestCase.php b/tests/uts/helper/FormhandlerTestCase.php index 86f012a..ae85f3c 100644 --- a/tests/uts/helper/FormhandlerTestCase.php +++ b/tests/uts/helper/FormhandlerTestCase.php @@ -17,18 +17,34 @@ abstract class FormhandlerTestCase extends TestCase { private ?ReflectionClass $_Reflector = null; + private array $_triggeredErrors = array(); + + protected function assertTriggertError(string $message, int $errorlevel): void + { + static::assertTrue(count($this->_triggeredErrors) > 0, "no errors triggered"); + + $triggeredError = array_shift($this->_triggeredErrors); + static::assertEquals($triggeredError["level"], $errorlevel, "wrong expected errorlevel"); + static::assertEquals($triggeredError["message"], $message, "wrong expected errormessage"); + } + + public function errorHandler(int $errorlevel, string $message) + { + array_push($this->_triggeredErrors, array("level" => $errorlevel, "message" => $message)); + } + protected function setUp(): void { - set_error_handler( - static function ( $errno, $errstr ) { - throw new \Exception( $errstr, $errno ); - }, - E_ALL - ); + set_error_handler(array($this, 'errorhandler')); } protected function tearDown(): void { + if (count($this->_triggeredErrors) > 0) + { + $triggeredErrormessage = $this->_triggeredErrors[0]["message"]; + static::fail("Unexpected triggered error: {$triggeredErrormessage}"); + } restore_error_handler(); }