Files
CoreLibs-Composer-All/test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php

510 lines
11 KiB
PHP
Raw Normal View History

2023-09-08 18:38:19 +09:00
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
use CoreLibs\Logging\Logger\Level;
/**
* Test class for Logging
* @coversDefaultClass \CoreLibs\Logging\ErrorMessages
* @testdox \CoreLibs\Logging\ErrorMEssages method tests
*/
final class CoreLibsLoggingErrorMessagesTest extends TestCase
{
private const LOG_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
/**
* tear down and remove log data
*
* @return void
*/
public static function tearDownAfterClass(): void
{
array_map('unlink', glob(self::LOG_FOLDER . '*.log'));
}
/**
* for checking level only
*
* @return array
*/
public function providerErrorMessageLevel(): array
{
return [
'ok' => [
'level' => 'ok',
'str' => 'OK',
'expected' => 'ok',
],
Various updates as follows * commit 5cec54d508fd4a34de509b3ac28d6277b6abc090 (HEAD -> master, all/master, all/development, all/NewFeatures) | log size 644 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-09-20 13:33:19 +0900 | | Add "Success" to message logging levels, fixes for PHP 8.4, other preg_match fixes | | The Logger/MessageLevel gets "success" as level 110 to something a bit | heigher than "ok" which is the general "OK" for anything ending without | an error. The "success" is currently only used in file uploads with the | java script ajax file uploader | | Fix any "type $var = null" with correctly "?type $var = null" for PHP 8.4 (phphan) | | Fix preg match no return catches for DB IO compare version and for language | look up. | | 4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php | 5 +++++ | www/admin/class_test.html.php | 1 + | www/admin/class_test.php | 2 +- | www/admin/layout/javascript/edit.jq.js | 2 +- | www/lib/CoreLibs/Admin/Backend.php | 6 +++--- | www/lib/CoreLibs/Basic.php | 2 +- | www/lib/CoreLibs/Convert/Extends/SetVarTypeMain.php | 6 +++--- | www/lib/CoreLibs/Convert/SetVarTypeNull.php | 6 +++--- | www/lib/CoreLibs/DB/IO.php | 24 ++++++++++++++++++------ | www/lib/CoreLibs/Language/GetLocale.php | 4 ++-- | www/lib/CoreLibs/Logging/Logger/MessageLevel.php | 2 ++ | www/lib/CoreLibs/Output/Form/Elements.php | 12 ++++++------ | www/lib/CoreLibs/Template/HtmlBuilder/Element.php | 4 ++-- | 13 files changed, 48 insertions(+), 28 deletions(-) | * commit 8e60c992f109993b40d9e5ec9354ffb7d7db9f79 | log size 123 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-09-03 12:06:01 +0900 | | Fixes phan/phpstan | | phpstan.neon | 6 +++--- | www/lib/CoreLibs/Get/System.php | 2 +- | www/lib/CoreLibs/Template/HtmlBuilder/Block.php | 4 ---- | 3 files changed, 4 insertions(+), 8 deletions(-) | * commit 1b5437b675f6ceda4318f19522d47a08f28f95a8 | log size 147 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-09-03 11:58:36 +0900 | | Add testing for new added bom utf8 replace | | 4dev/tests/Convert/CoreLibsConvertStringsTest.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ | 4dev/tests/Convert/data/UTF8.csv | 1 + | 4dev/tests/Convert/data/UTF8BOM.csv | 1 + | 4dev/tests/Get/CoreLibsGetSystemTest.php | 23 +++++++++++++++++++++++ | www/admin/class_test.system.php | 4 +++- | 5 files changed, 76 insertions(+), 1 deletion(-) | * commit ef80cba5610b8acae8fc11d3f0a441c1f5a03735 | log size 659 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-09-03 09:49:01 +0900 | | Add new functions: get IPs, strip UTF8 BOM from text, text updates | | Add the following new static methods | | Convert\Strings::stripUTF8BomBytes: removes the UTF8 BOM bytes from the beginning of a line | Used for CSV files created in Excel for the first header entry (line 0/row 0) | | Get\Systen::getIpAddresses: gets all IP addresses for the the current access user | and returns an array | | Moved the frontend folder detection from the first load config to the config.path.php | | Cleaned up the translations JS scripts | | 4dev/bin/create_mo.sh | 9 +++++---- | 4dev/bin/mo_to_js.sh | 63 +++++++++++++++++++++++++++++++++++---------------------------- | www/configs/config.path.php | 16 +++++++++++++++- | www/configs/config.php | 15 +-------------- | www/lib/CoreLibs/Convert/Strings.php | 12 ++++++++++++ | www/lib/CoreLibs/DB/IO.php | 2 +- | www/lib/CoreLibs/Get/System.php | 23 +++++++++++++++++++++++ | www/lib/CoreLibs/Template/HtmlBuilder/Block.php | 37 ++++++++++++++++++++++++++----------- | 8 files changed, 118 insertions(+), 59 deletions(-) | * commit 2d71e760e8fb7b4b0a5552a2f62bedffad928f4f | log size 120 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-08-21 11:45:17 +0900 | | Composer update | | www/composer.lock | 12 ++++++------ | www/lib/CoreLibs/Template/SmartyExtend.php | 3 ++- | www/vendor/composer/installed.json | 14 +++++++------- | www/vendor/composer/installed.php | 6 +++--- | www/vendor/gullevek/dotenv/Readme.md | 23 +++++++++++++++++++++++ | www/vendor/gullevek/dotenv/src/DotEnv.php | 13 ++++++++++--- | 6 files changed, 51 insertions(+), 20 deletions(-) | * commit a8d07634ffaf961a37557437aac1f202ae18fa6e | log size 182 | Author: Clemens Schwaighofer <clemens.schwaighofer@egplusww.com> | Date: 2024-08-07 13:41:09 +0900 | | Add soba.egplusww.jp as local development host, jshint esversion update to 11 | | www/admin/layout/javascript/edit.jq.js | 2 +- | www/configs/config.host.php | 28 +++++++++++----------------- | 2 files changed, 12 insertions(+), 18 deletions(-) |
2024-09-20 13:40:20 +09:00
'success' => [
'level' => 'success',
'str' => 'SUCCESS',
'expected' => 'success',
],
2023-09-08 18:38:19 +09:00
'info' => [
'level' => 'info',
'str' => 'INFO',
'expected' => 'info',
],
2023-09-27 11:43:12 +09:00
'notice' => [
'level' => 'notice',
'str' => 'NOTICE',
'expected' => 'notice',
],
2023-09-08 18:38:19 +09:00
'warn' => [
'level' => 'warn',
'str' => 'WARN',
'expected' => 'warn'
],
'warning' => [
'level' => 'warning',
'str' => 'WARN',
'expected' => 'warn'
],
'error' => [
'level' => 'error',
'str' => 'ERROR',
'expected' => 'error'
],
'abort' => [
'level' => 'abort',
'str' => 'ABORT',
'expected' => 'abort'
],
'crash' => [
'level' => 'crash',
'str' => 'CRASH',
'expected' => 'crash'
],
'wrong level' => [
'level' => 'wrong',
'str' => 'WRONG',
'expected' => 'unknown'
]
];
}
/**
* Undocumented function
*
* @dataProvider providerErrorMessageLevel
* @testdox error message level: $level will be $expected [$_dataName]
*
* @param string $level
* @param string $str
* @param string $expected
* @return void
*/
public function testErrorMessageLevelOk(string $level, string $str, string $expected): void
{
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLevelOk',
2023-09-08 18:38:19 +09:00
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Error,
2023-09-08 18:38:19 +09:00
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setMessage(
2023-09-08 18:38:19 +09:00
$level,
$str
);
$this->assertEquals(
[
'level' => $expected,
'str' => $str,
'id' => '',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
],
$em->getLastErrorMsg()
);
}
/**
* Undocumented function
*
* @testdox Test of all methods for n messages [$_dataName]
*
* @return void
*/
public function testErrorMessageOk(): void
{
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesOk',
2023-09-08 18:38:19 +09:00
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Error
2023-09-08 18:38:19 +09:00
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setErrorMsg(
'100',
'info',
'INFO MESSAGE'
);
$this->assertEquals(
[
'id' => '100',
'level' => 'info',
'str' => 'INFO MESSAGE',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
],
$em->getLastErrorMsg()
);
$this->assertEquals(
['100'],
$em->getErrorIds()
);
$this->assertEquals(
[
[
'id' => '100',
'level' => 'info',
'str' => 'INFO MESSAGE',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
]
],
$em->getErrorMsg()
);
$em->setErrorMsg(
'200',
'error',
'ERROR MESSAGE'
);
$this->assertEquals(
[
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
],
$em->getLastErrorMsg()
);
$this->assertEquals(
['100', '200'],
$em->getErrorIds()
);
$this->assertEquals(
[
[
'id' => '100',
'level' => 'info',
'str' => 'INFO MESSAGE',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
],
[
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'target' => '',
2023-09-15 18:32:55 +09:00
'target_style' => '',
2023-09-08 18:38:19 +09:00
'highlight' => [],
]
],
$em->getErrorMsg()
);
}
/**
* Undocumented function
*
* @return array
*/
2023-09-08 18:38:19 +09:00
public function providerErrorMessageLog(): array
{
return [
'error, not logged' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => null,
'log_error' => null,
'expected' => '<ERROR> ERROR MESSAGE',
],
'error, logged' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => null,
'log_error' => true,
'expected' => '<ERROR> ERROR MESSAGE',
],
'error, logged, message' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => 'OTHER ERROR MESSAGE',
'log_error' => true,
'expected' => '<ERROR> OTHER ERROR MESSAGE',
],
2023-09-27 11:43:12 +09:00
'notice' => [
'id' => '100',
'level' => 'notice',
'str' => 'NOTICE MESSAGE',
'message' => null,
'log_error' => null,
'expected' => '<NOTICE> NOTICE MESSAGE',
],
'notice, message' => [
'id' => '100',
'level' => 'notice',
'str' => 'NOTICE MESSAGE',
'message' => 'OTHER NOTICE MESSAGE',
'log_error' => null,
'expected' => '<NOTICE> OTHER NOTICE MESSAGE',
],
2023-09-08 18:38:19 +09:00
'crash' => [
'id' => '300',
'level' => 'crash',
'str' => 'CRASH MESSAGE',
'message' => null,
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<ALERT> CRASH MESSAGE',
],
'crash, message' => [
'id' => '300',
'level' => 'crash',
'str' => 'CRASH MESSAGE',
'message' => 'OTHER CRASH MESSAGE',
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<ALERT> OTHER CRASH MESSAGE',
],
'abort' => [
'id' => '200',
'level' => 'abort',
'str' => 'ABORT MESSAGE',
'message' => null,
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<CRITICAL> ABORT MESSAGE',
],
'abort, message' => [
'id' => '200',
'level' => 'abort',
'str' => 'ABORT MESSAGE',
'message' => 'OTHER ABORT MESSAGE',
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
],
'unknown' => [
'id' => '400',
'level' => 'wrong level',
'str' => 'WRONG LEVEL MESSAGE',
'message' => null,
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
],
'unknown, message' => [
'id' => '400',
'level' => 'wrong level',
'str' => 'WRONG LEVEL MESSAGE',
'message' => 'OTHER WRONG LEVEL MESSAGE',
'log_error' => null,
2023-09-08 18:38:19 +09:00
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
],
];
}
/**
* Undocumented function
*
* @dataProvider providerErrorMessageLog
* @testdox Test Log writing with log level Error [$_dataName]
2023-09-08 18:38:19 +09:00
*
* @param string $id
* @param string $level
* @param string $str
* @param string|null $message
* @param bool|null $log_error
* @param string $expected
2023-09-08 18:38:19 +09:00
* @return void
*/
public function testErrorMessageLogErrorLevel(
string $id,
string $level,
string $str,
?string $message,
?bool $log_error,
string $expected
): void {
2023-09-08 18:38:19 +09:00
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLogError',
2023-09-08 18:38:19 +09:00
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Notice,
2023-09-08 18:38:19 +09:00
'log_per_run' => true
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setErrorMsg(
$id,
$level,
$str,
message: $message,
log_error: $log_error
2023-09-08 18:38:19 +09:00
);
$file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) {
$file_content = file_get_contents(
$log->getLogFolder() . $log->getLogFile()
) ?: '';
}
// if error, if null or false, it will not be logged
if ($level == 'error' && ($log_error === null || $log_error === false)) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else {
$this->assertStringContainsString(
$expected,
$file_content
);
}
2023-09-08 18:38:19 +09:00
}
/**
* Undocumented function
*
* @dataProvider providerErrorMessageLog
* @testdox Test Log writing with log Level Debug [$_dataName]
*
* @param string $id
* @param string $level
* @param string $str
* @param string|null $message
* @param bool|null $log_error
* @param string $expected
* @return void
*/
public function testErrorMessageLogErrorDebug(
string $id,
string $level,
string $str,
?string $message,
?bool $log_error,
string $expected
): void {
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLogDebug',
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug,
'log_per_run' => true
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setErrorMsg(
$id,
$level,
$str,
message: $message,
log_error: $log_error
);
$file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) {
$file_content = file_get_contents(
$log->getLogFolder() . $log->getLogFile()
) ?: '';
}
// if error, and log is debug level, only explicit false are not logged
if ($level == 'error' && $log_error === false) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else {
$this->assertStringContainsString(
$expected,
$file_content
);
}
}
2023-10-02 14:04:59 +09:00
/**
* Undocumented function
*
* @testdox Test jump target set and reporting
*
* @return void
*/
public function testJumpTarget(): void
{
$log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessagesLogDebug',
'log_folder' => self::LOG_FOLDER,
'log_level' => Level::Debug,
'log_per_run' => true
]);
$em = new \CoreLibs\Logging\ErrorMessage($log);
$em->setJumpTarget(
'target-f',
'Target text'
);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
2023-10-02 14:04:59 +09:00
],
$em->getJumpTarget()
);
// set same target, keep as before
$em->setJumpTarget(
'target-f',
'Other text'
);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
2023-10-02 14:04:59 +09:00
],
$em->getJumpTarget()
);
// add new now two messages
$em->setJumpTarget(
'target-s',
'More text'
);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
2023-10-02 14:04:59 +09:00
],
$em->getJumpTarget()
);
// add empty info
$em->setJumpTarget(
'target-e',
''
);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
],
$em->getJumpTarget()
);
// add through message
$em->setErrorMsg('E-101', 'abort', 'Abort message', jump_target:[
'target' => 'abort-target',
'info' => 'Abort error'
]);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
['target' => 'abort-target', 'info' => 'Abort error', 'level' => 'abort'],
2023-10-02 14:04:59 +09:00
],
$em->getJumpTarget()
);
}
2023-09-08 18:38:19 +09:00
}
// __END__