Skip to content

Commit

Permalink
new: triggered Errors by assertTriggertError
Browse files Browse the repository at this point in the history
  • Loading branch information
tombrain committed Nov 5, 2023
1 parent fe1d617 commit 1df36ec
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 44 deletions.
3 changes: 1 addition & 2 deletions tests/uts/dbFormhandler_TextFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/uts/dbFormhandler_dbListFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/uts/formhandler_AutocompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
};
3 changes: 2 additions & 1 deletion tests/uts/formhandler_ConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
26 changes: 15 additions & 11 deletions tests/uts/formhandler_DateTextFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down Expand Up @@ -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);
}
}
};
4 changes: 2 additions & 2 deletions tests/uts/formhandler_LookAndFeel_setMaxLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
};
6 changes: 3 additions & 3 deletions tests/uts/formhandler_LookAndFeel_setTabIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
5 changes: 2 additions & 3 deletions tests/uts/formhandler_SetFocusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/uts/formhandler_ViewModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
};
4 changes: 2 additions & 2 deletions tests/uts/formhandler_jsDateTextFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
28 changes: 22 additions & 6 deletions tests/uts/helper/FormhandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 1df36ec

Please sign in to comment.