Update hash class unit tests, work on DB IO unit tests

This commit is contained in:
Clemens Schwaighofer
2022-02-25 18:08:10 +09:00
parent 780fdedcfd
commit 7a902b5681
4 changed files with 306 additions and 87 deletions

View File

@@ -80,7 +80,7 @@ final class CoreLibsCreateHashTest extends TestCase
foreach ([null, 'crc32b', 'adler32', 'fnv132', 'fnv1a32', 'joaat'] as $_hash_type) {
// default value test
if ($_hash_type === null) {
$hash_type = 'crc32b';
$hash_type = \CoreLibs\Create\Hash::STANDARD_HASH_SHORT;
} else {
$hash_type = $_hash_type;
}
@@ -94,6 +94,31 @@ final class CoreLibsCreateHashTest extends TestCase
return $list;
}
/**
* Undocumented function
*
* @return array
*/
public function hashLongProvider(): array
{
$hash_source = 'Some String Text';
return [
'Long Hash check: ' . \CoreLibs\Create\Hash::STANDARD_HASH_LONG => [
$hash_source,
hash(\CoreLibs\Create\Hash::STANDARD_HASH_LONG, $hash_source)
],
];
}
public function uniqIdLongProvider(): array
{
return [
'uniq id long: ' . \CoreLibs\Create\Hash::STANDARD_HASH_LONG => [
strlen(hash(\CoreLibs\Create\Hash::STANDARD_HASH_LONG, 'A'))
],
];
}
/**
* Undocumented function
*
@@ -172,20 +197,57 @@ final class CoreLibsCreateHashTest extends TestCase
/**
* Undocumented function
*
* @covers ::__uniqueID
* @covers ::__uniqueId
* @testWith [8]
* @testdox __uniqId will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqID(int $expected): void
public function testUniqId(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Hash::__uniqId())
);
}
/**
* Undocumented function
*
* @covers ::__hashLong
* @dataProvider hashLongProvider
* @testdox __hashLong $input will be $expected [$_dataName]
*
* @param string $input
* @param string $expected
* @return void
*/
public function testHashLong(string $input, string $expected): void
{
$this->assertEquals(
$expected,
\CoreLibs\Create\Hash::__hashLong($input)
);
}
/**
* Undocumented function
*
* @covers ::__uniqueIdLong
* @dataProvider uniqIdLongProvider
* @testdox __uniqIdLong will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqIdLong(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Hash::__uniqIdLong())
);
}
}
// __END__

View File

@@ -15,39 +15,38 @@ use PHPUnit\Framework\TestCase;
*/
final class CoreLibsDBIOTest extends TestCase
{
private static $db_config = [];
private static $db_config = [
// self localhost/ip connection
'valid' => [
'db_name' => 'corelibs_db_io_test',
'db_user' => 'corelibs_db_io_test',
'db_pass' => 'corelibs_db_io_test',
'db_host' => 'localhost',
'db_port' => 5432,
'db_schema' => 'public',
'db_type' => 'pgsql',
'db_encoding' => '',
'db_ssl' => 'allow', // allow, disable, require, prefer
'db_debug' => true,
],
'invalid' => [
'db_name' => '',
'db_user' => '',
'db_pass' => '',
'db_host' => '',
'db_port' => 5432,
'db_schema' => 'public',
'db_type' => 'pgsql',
'db_encoding' => '',
'db_ssl' => 'allow', // allow, disable, require, prefer
'db_debug' => true,
],
];
private static $log;
public static function setUpBeforeClass(): void
{
// define basic connection set valid and one invalid
self::$db_config = [
// self localhost/ip connection
'valid' => [
'db_name' => 'corelibs_db_io_test',
'db_user' => 'corelibs_db_io_test',
'db_pass' => 'corelibs_db_io_test',
'db_host' => 'localhost',
'db_port' => 5432,
'db_schema' => 'public',
'db_type' => 'pgsql',
'db_encoding' => '',
'db_ssl' => 'allow', // allow, disable, require, prefer
'db_debug' => true,
],
'invalid' => [
'db_name' => '',
'db_user' => '',
'db_pass' => '',
'db_host' => '',
'db_port' => 5432,
'db_schema' => 'public',
'db_type' => 'pgsql',
'db_encoding' => '',
'db_ssl' => 'allow', // allow, disable, require, prefer
'db_debug' => true,
],
];
self::$log = new \CoreLibs\Debug\Logging([
// 'log_folder' => __DIR__ . DIRECTORY_SEPARATOR . 'log',
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
@@ -73,19 +72,95 @@ final class CoreLibsDBIOTest extends TestCase
// print_r(self::$db_config);
}
// - connect to DB test (getConnectionStatus)
// - connect to DB test (dbGetConnectionStatus)
// - connected get dbInfo data check (show true, false)
// - disconnect: dbClose
/**
* connection DB strings
*
* @return array
*/
public function connectionProvider(): array
{
// 0: connection array
// 1: status after connection
// 2: info string
return [
'invalid connection' => [
self::$db_config['invalid'],
false,
"-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 **** ",
null,
],
'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' **** "
. "-DB-info-> DB IO Class debug output: Yes **** ",
null,
],
];
}
/**
* Connection tests
*
* @covers ::__connectToDB
* @dataProvider connectionProvider
* @testdox Connection will be $expected [$_dataName]
*
* @return void
*/
public function testConnection(
array $connection,
bool $expected_status,
string $expected_string
): void {
$db = new \CoreLibs\DB\IO(
$connection,
self::$log
);
$this->assertEquals(
$db->dbGetConnectionStatus(),
$expected_status
);
$this->assertEquals(
$db->dbInfo(false, true),
$expected_string
);
// print "DB: " . $db->dbInfo(false, true) . "\n";
if ($db->dbGetConnectionStatus()) {
// db close check
$db->dbClose();
$this->assertEquals(
$db->dbGetConnectionStatus(),
false
);
} else {
// TODO: error checks
// print "LAST ERROR: " . $db->dbGetLastError(true) . "\n";
// print "ERRORS: " . print_r($db->dbGetErrorHistory(true), true) . "\n";
}
}
// - connected get all default settings via get
// dbGetDebug, dbGetMaxQueryCall, dbGetSchema, dbGetEncoding,
// dbVerions, dbCompareVersion
// dbGetDebug, dbGetSchema, dbGetEncoding, dbGetMaxQueryCall
// dbGetSetting (name, user, ecnoding, schema, host, port, ssl, debug, password)
// - connected set
// dbSetMaxQueryCall, dbSetDebug, dbToggleDebug, dbSetSchema, dbSetEncoding
// dbSetMaxQueryCall, ,
// dbSetDebug, dbToggleDebug, dbSetSchema, dbSetEncoding
// - db execution tests
// dbReturn, dbDumpData, dbCacheReset, dbExec, dbExecAsync, dbCheckAsync
// dbFetchArray, dbReturnRow, dbReturnArray, dbCursorPos, dbCursorNumRows,
// dbShowTableMetaData, dbPrepare, dbExecute
// - connected stand alone tests
// dbEscapeString, dbEscapeLiteral, dbEscapeBytea, dbSqlEscape, dbArrayParse
// - complex write sets
// dbWriteData, dbWriteDataExt
@@ -100,21 +175,71 @@ final class CoreLibsDBIOTest extends TestCase
// getInsertReturn, getReturning, getInsertPK, getReturningExt,
// getCursorExt, getNumRows
public function testConnection()
// public function testDbSettings(): void
// {
// //
// }
// - connected version test
// dbVerions, dbCompareVersion
/**
* Undocumented function
*
* @return array
*/
public function versionProvider(): array
{
//
$db = new \CoreLibs\DB\IO(
self::$db_config['invalid'],
self::$log
);
print "INIT ERROR INVALID: " . $db->getConnectionStatus() . "\n";
print "LAST ERROR: " . $db->dbGetLastError(true) . "\n";
print "ERRORS: " . print_r($db->dbGetErrorHistory(true), true) . "\n";
return [
'compare = ok' => [ '=13.5.0', true ],
'compare = bad' => [ '=9.2.0', false ],
'compare < ok' => [ '<20.0.0', true ],
'compare < bad' => [ '<9.2.0', false ],
'compare <= ok a' => [ '<=20.0.0', true ],
'compare <= ok b' => [ '<=13.5.0', true ],
'compare <= false' => [ '<=9.2.0', false ],
'compare > ok' => [ '>9.2.0', true ],
'compare > bad' => [ '>20.2.0', false ],
'compare >= ok a' => [ '>=13.5.0', true ],
'compare >= ok b' => [ '>=9.2.0', true ],
'compare >= bad' => [ '>=20.0.0', false ],
];
}
/**
* NOTE
* Version tests will fail if versions change
* Current base as Version 13.5 for equal check
* I can't mock a function on the same class when it is called in a method
* NOTE
*
* @covers ::dbCompareVersion
* @dataProvider versionProvider
* @testdox Version $input compares as $expected [$_dataName]
*
* @return void
*/
public function testDbVerson(string $input, bool $expected): void
{
// connect to valid DB
$db = new \CoreLibs\DB\IO(
self::$db_config['valid'],
self::$log
);
print "INIT ERROR VALID: " . $db->getConnectionStatus() . "\n";
// print "DB VERSION: " . $db->dbVersion() . "\n";
// Create a stub for the SomeClass class.
// $stub = $this->createMock(\CoreLibs\DB\IO::class);
// $stub->method('dbVersion')
// ->willReturn('13.1.0');
$this->assertEquals(
$db->dbCompareVersion($input),
$expected
);
// print "IT HAS TO BE 13.1.0: " . $stub->dbVersion() . "\n";
}
/**