Update to Exceptions: add codes, update phpunit tests
DB Class throws Exception if on init it fails to connect to the DB, will not throw Exception if failed connection during execution but will do the normal retry and soft failure run DB\ArrayIO will throw Exception on missing table array and table name All Exceptions have a code set
This commit is contained in:
@@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
final class CoreLibsCheckColorsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function validateColorProvider(): array
|
||||
{
|
||||
/*
|
||||
|
||||
@@ -310,52 +310,72 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'2021-12-12',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal' => [
|
||||
'2020-12-12',
|
||||
'2020-12-12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'second date smaller' => [
|
||||
'2021-12-12',
|
||||
'2020-12-12',
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal with different time' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 13:13:13',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'invalid dates --' => [
|
||||
'--',
|
||||
'--',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
1,
|
||||
],
|
||||
'empty dates' => [
|
||||
'',
|
||||
'',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'invalid dates' => [
|
||||
'not a date',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'invalid end date' => [
|
||||
'1990-01-01',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
'out of bound dates' => [
|
||||
'1900-1-1',
|
||||
'9999-12-31',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function dateTimeCompareProvider(): array
|
||||
{
|
||||
return [
|
||||
@@ -364,60 +384,84 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'2021-12-12',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal no timestamp' => [
|
||||
'2020-12-12',
|
||||
'2020-12-12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'second date smaller no timestamp' => [
|
||||
'2021-12-12',
|
||||
'2020-12-12',
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal first time smaller' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 13:13:13',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal time equal' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 12:12:12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal second time smaller' => [
|
||||
'2020-12-12 13:13:13',
|
||||
'2020-12-12 12:12:12',
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'valid date invalid time' => [
|
||||
'2020-12-12 13:99:13',
|
||||
'2020-12-12 12:12:99',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'valid date invalid end time' => [
|
||||
'2020-12-12 13:12:13',
|
||||
'2020-12-12 12:12:99',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
'invalid datetimes --' => [
|
||||
'--',
|
||||
'--',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'empty datetimess' => [
|
||||
'',
|
||||
'',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'invalid datetimes' => [
|
||||
'invalid date times' => [
|
||||
'not a date',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException'
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'invalid end date time' => [
|
||||
'1990-01-01 12:12:12',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
];
|
||||
}
|
||||
@@ -632,12 +676,20 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @param string|null $exception
|
||||
* @param int|null $exception_code
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDate(string $input_a, string $input_b, int|bool $expected, ?string $exception): void
|
||||
{
|
||||
public function testCompareDate(
|
||||
string $input_a,
|
||||
string $input_b,
|
||||
int|bool $expected,
|
||||
?string $exception,
|
||||
?int $exception_code
|
||||
): void {
|
||||
if ($expected === false) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
@@ -655,12 +707,20 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @param string|null $exception
|
||||
* @param int|null $exception_code
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDateTime(string $input_a, string $input_b, int|bool $expected, ?string $exception): void
|
||||
{
|
||||
public function testCompareDateTime(
|
||||
string $input_a,
|
||||
string $input_b,
|
||||
int|bool $expected,
|
||||
?string $exception,
|
||||
?int $exception_code
|
||||
): void {
|
||||
if ($expected === false) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
|
||||
@@ -32,7 +32,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
// getSessionId: string or false
|
||||
// 3: exepcted name (session)]
|
||||
// 4: Exception thrown on error
|
||||
// 5: expected error string
|
||||
// 5: exception code, null for none
|
||||
// 6: expected error string
|
||||
return [
|
||||
'session parameter' => [
|
||||
'sessionNameParameter',
|
||||
@@ -46,6 +47,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'sessionNameParameter',
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
'session globals' => [
|
||||
@@ -60,6 +62,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'sessionNameGlobals',
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
'session name default' => [
|
||||
@@ -74,6 +77,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
// error checks
|
||||
@@ -90,6 +94,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
1,
|
||||
'[SESSION] No sessions in php cli'
|
||||
],
|
||||
// 2: session disabled
|
||||
@@ -105,6 +110,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
2,
|
||||
'[SESSION] Sessions are disabled'
|
||||
],
|
||||
// 3: invalid session name: string
|
||||
@@ -120,6 +126,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
3,
|
||||
'[SESSION] Invalid session name: 1invalid$session#;'
|
||||
],
|
||||
// 3: invalid session name: only numbers
|
||||
@@ -135,6 +142,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
3,
|
||||
'[SESSION] Invalid session name: 123'
|
||||
],
|
||||
// 3: invalid session name: invalid name short
|
||||
@@ -152,6 +160,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
4,
|
||||
'[SESSION] Failed to activate session'
|
||||
],
|
||||
// 5: get session id return false
|
||||
@@ -167,6 +176,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
5,
|
||||
'[SESSION] getSessionId did not return a session id'
|
||||
],
|
||||
];
|
||||
@@ -193,6 +203,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
array $mock_data,
|
||||
string $expected,
|
||||
?string $exception,
|
||||
?int $exception_code,
|
||||
string $expected_error
|
||||
): void {
|
||||
// override expected
|
||||
@@ -238,6 +249,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
|
||||
if ($exception !== null) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
|
||||
unset($GLOBALS['SET_SESSION_NAME']);
|
||||
|
||||
@@ -445,12 +445,14 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
{
|
||||
// 0: connection array
|
||||
// 1: status after connection
|
||||
// 2: exception name
|
||||
// 2: info string
|
||||
// 3: ???
|
||||
return [
|
||||
'invalid connection' => [
|
||||
self::$db_config['invalid'],
|
||||
false,
|
||||
'RuntimeException',
|
||||
"-DB-info-> Connected to db '' with schema 'public' as user "
|
||||
. "'' at host '' on port '5432' with ssl mode 'allow' **** "
|
||||
. "-DB-info-> DB IO Class debug output: Yes **** ",
|
||||
@@ -459,6 +461,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'valid connection' => [
|
||||
self::$db_config['valid'],
|
||||
true,
|
||||
'',
|
||||
"-DB-info-> Connected to db 'corelibs_db_io_test' with "
|
||||
. "schema 'public' as user 'corelibs_db_io_test' at host "
|
||||
. "'localhost' on port '5432' with ssl mode 'allow' **** "
|
||||
@@ -475,13 +478,21 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
* @dataProvider connectionProvider
|
||||
* @testdox Connection will be $expected [$_dataName]
|
||||
*
|
||||
* @param array $connection
|
||||
* @param bool $expected_status
|
||||
* @param string $exception
|
||||
* @param string $expected_string
|
||||
* @return void
|
||||
*/
|
||||
public function testConnection(
|
||||
array $connection,
|
||||
bool $expected_status,
|
||||
string $exception,
|
||||
string $expected_string
|
||||
): void {
|
||||
if ($expected_status === false) {
|
||||
$this->expectException($exception);
|
||||
}
|
||||
$db = new \CoreLibs\DB\IO(
|
||||
$connection,
|
||||
self::$log
|
||||
|
||||
Reference in New Issue
Block a user