Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
872409ef54 | ||
|
|
c4d5cad9e8 | ||
|
|
90550746ab | ||
|
|
724031b944 | ||
|
|
027c35f9f0 | ||
|
|
6ad844b519 | ||
|
|
e10987ce8b | ||
|
|
b3617954eb | ||
|
|
67fd7b172a | ||
|
|
fe729453ac | ||
|
|
b939edac3f | ||
|
|
00528cb7d7 | ||
|
|
3b8583de61 | ||
|
|
f6821b7c21 | ||
|
|
7b49394c5a | ||
|
|
f624776397 | ||
|
|
5e31559c3e | ||
|
|
52a7f1197b | ||
|
|
e1466432a8 |
@@ -1 +1 @@
|
||||
9.5.1
|
||||
9.7.6
|
||||
|
||||
@@ -35,6 +35,8 @@ class EditBase
|
||||
private \CoreLibs\Output\Form\Generate $form;
|
||||
/** @var \CoreLibs\Logging\Logging */
|
||||
public \CoreLibs\Logging\Logging $log;
|
||||
/** @var \CoreLibs\Language\L10n */
|
||||
public \CoreLibs\Language\L10n $l;
|
||||
/** @var \CoreLibs\ACL\Login */
|
||||
public \CoreLibs\ACL\Login $login;
|
||||
|
||||
@@ -57,6 +59,7 @@ class EditBase
|
||||
) {
|
||||
$this->log = $log;
|
||||
$this->login = $login;
|
||||
$this->l = $l10n;
|
||||
// smarty template engine (extended Translation version)
|
||||
$this->smarty = new \CoreLibs\Template\SmartyExtend(
|
||||
$l10n,
|
||||
@@ -77,7 +80,7 @@ class EditBase
|
||||
echo "I am sorry, but this page cannot be viewed by a mobile phone";
|
||||
exit;
|
||||
}
|
||||
// $this->form->log->debug('POST', $this->form->log->prAr($_POST));
|
||||
// $this->log->debug('POST', $this->log->prAr($_POST));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,7 +182,7 @@ class EditBase
|
||||
} // while read data ...
|
||||
|
||||
// html title
|
||||
$this->HEADER['HTML_TITLE'] = $this->form->l->__('Edit Order');
|
||||
$this->HEADER['HTML_TITLE'] = $this->l->__('Edit Order');
|
||||
|
||||
$messages = [];
|
||||
$error = $_POST['error'] ?? 0;
|
||||
@@ -632,7 +635,7 @@ class EditBase
|
||||
'editAdmin_' . $this->smarty->lang
|
||||
);
|
||||
|
||||
$this->form->log->debug('DEBUGEND', '==================================== [Form END]');
|
||||
$this->log->debug('DEBUGEND', '==================================== [Form END]');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,8 +26,12 @@ class SetVarTypeMain
|
||||
?string $default = null,
|
||||
bool $to_null = false
|
||||
): ?string {
|
||||
if (is_string($val)) {
|
||||
return $val;
|
||||
if (
|
||||
$val === null ||
|
||||
is_scalar($val) ||
|
||||
$val instanceof \Stringable
|
||||
) {
|
||||
return (string)$val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (string)$default;
|
||||
@@ -39,6 +43,7 @@ class SetVarTypeMain
|
||||
* Will convert input data to string if possible.
|
||||
* Runs for string/int/float/bool/null
|
||||
* Will skip array/object/resource/callable/etc and use default for that
|
||||
* Note: this is pretty much the same as setStrMain because string is easy
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param string|null $default Default value
|
||||
@@ -71,6 +76,7 @@ class SetVarTypeMain
|
||||
/**
|
||||
* If input variable is int, return it, else return default value. If to_null
|
||||
* is true then null as return is allowed, else only int is returned
|
||||
* Note, if float is sent in, int is returned
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param int|null $default Default value
|
||||
@@ -82,8 +88,8 @@ class SetVarTypeMain
|
||||
?int $default = null,
|
||||
bool $to_null = false
|
||||
): ?int {
|
||||
if (is_int($val)) {
|
||||
return $val;
|
||||
if (is_numeric($val)) {
|
||||
return (int)$val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (int)$default;
|
||||
@@ -129,6 +135,7 @@ class SetVarTypeMain
|
||||
/**
|
||||
* If input is float return it, else set to default value. If to_null is set
|
||||
* to true, allow null return
|
||||
* Note if an int is sent in, float is returned
|
||||
*
|
||||
* @param mixed $val Input variable
|
||||
* @param float|null $default Default value
|
||||
@@ -140,8 +147,8 @@ class SetVarTypeMain
|
||||
?float $default = null,
|
||||
bool $to_null = false
|
||||
): ?float {
|
||||
if (is_float($val)) {
|
||||
return $val;
|
||||
if (is_numeric($val)) {
|
||||
return (float)$val;
|
||||
}
|
||||
if ($to_null === false) {
|
||||
return (float)$default;
|
||||
|
||||
@@ -198,7 +198,7 @@ class ArrayIO extends \CoreLibs\DB\IO
|
||||
public function dbCheckPkSet(): bool
|
||||
{
|
||||
// if pk_id is set, overrule ...
|
||||
if ($this->pk_id) {
|
||||
if (!empty($this->pk_id)) {
|
||||
$this->table_array[$this->pk_name]['value'] = $this->pk_id;
|
||||
}
|
||||
// if not set ... produce error
|
||||
|
||||
@@ -3232,7 +3232,7 @@ class IO
|
||||
*
|
||||
* @param array<mixed> $write_array list of elements to write
|
||||
* @param array<mixed> $not_write_array list of elements not to write
|
||||
* @param int $primary_key id key to decide if we write insert or update
|
||||
* @param int|null $primary_key id key to decide if we write insert or update
|
||||
* @param string $table name for the target table
|
||||
* @param array<mixed> $data data array to override _POST data
|
||||
* @return int|false primary key
|
||||
@@ -3240,7 +3240,7 @@ class IO
|
||||
public function dbWriteData(
|
||||
array $write_array,
|
||||
array $not_write_array,
|
||||
int $primary_key,
|
||||
?int $primary_key,
|
||||
string $table,
|
||||
array $data = []
|
||||
): int|false {
|
||||
@@ -3260,19 +3260,19 @@ class IO
|
||||
* PARAM INFO: $primary key
|
||||
* this can be a plain string/int and will be internal transformed into the array form
|
||||
* or it takes the array form of array [row => column, value => pk value]
|
||||
* @param array<mixed> $write_array list of elements to write
|
||||
* @param int|string|array<mixed> $primary_key primary key string or array set
|
||||
* @param string $table name for the target table
|
||||
* @param array<mixed> $not_write_array list of elements not to write (optional)
|
||||
* @param array<mixed> $not_write_update_array list of elements not
|
||||
* to write during update (optional)
|
||||
* @param array<mixed> $data optional array with data
|
||||
* if not _POST vars are used
|
||||
* @return int|false primary key
|
||||
* @param array<mixed> $write_array list of elements to write
|
||||
* @param null|int|string|array<mixed> $primary_key primary key string or array set
|
||||
* @param string $table name for the target table
|
||||
* @param array<mixed> $not_write_array list of elements not to write (optional)
|
||||
* @param array<mixed> $not_write_update_array list of elements not
|
||||
* to write during update (optional)
|
||||
* @param array<mixed> $data optional array with data
|
||||
* if not _POST vars are used
|
||||
* @return int|false primary key
|
||||
*/
|
||||
public function dbWriteDataExt(
|
||||
array $write_array,
|
||||
int|string|array $primary_key,
|
||||
null|int|string|array $primary_key,
|
||||
string $table,
|
||||
array $not_write_array = [],
|
||||
array $not_write_update_array = [],
|
||||
|
||||
239
src/Logging/ErrorMessage.php
Normal file
239
src/Logging/ErrorMessage.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTOR: Clemens Schwaighofer
|
||||
* CREATED: 2023/9/7
|
||||
* DESCRIPTION:
|
||||
* General error collection class for output to frontend or to log
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Logging;
|
||||
|
||||
use CoreLibs\Logging\Logger\MessageLevel;
|
||||
|
||||
class ErrorMessage
|
||||
{
|
||||
/** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */
|
||||
private array $error_str = [];
|
||||
/** @var \CoreLibs\Logging\Logging $log */
|
||||
public \CoreLibs\Logging\Logging $log;
|
||||
|
||||
/** @var bool $log_error global flag to log error level message */
|
||||
private bool $log_error = false;
|
||||
|
||||
/**
|
||||
* init ErrorMessage
|
||||
*
|
||||
* @param \CoreLibs\Logging\Logging $log
|
||||
* @param bool $log_error [=false]
|
||||
*/
|
||||
public function __construct(
|
||||
\CoreLibs\Logging\Logging $log,
|
||||
bool $log_error = false
|
||||
) {
|
||||
$this->log = $log;
|
||||
$this->log_error = $log_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* pushes new error message into the error_str array
|
||||
* error_id: internal Error ID (should be unique)
|
||||
* level: error level, can only be ok, info, warn, error, abort, crash
|
||||
* ok and info are positive response: success
|
||||
* warn: success, but there might be some things that are not 100% ok
|
||||
* error: input error or error in executing request
|
||||
* abort: an internal error happened as mandatory information that normally is
|
||||
* there is missing, or the ACL level that should normally match does not
|
||||
* will be logged to "critical"
|
||||
* crash: system failure or critical system problems (db connection failure)
|
||||
* will be logged as "alert"
|
||||
* not set: unkown, will be logged as "emergency"
|
||||
* target/highlight: id target name for frontend where to attach this message
|
||||
* highlight is a list of other target points to highlight
|
||||
* for highlight targets css names are $level without a prefix and should be
|
||||
* nested in the target element "input .error { ... }"
|
||||
* target_style: if not set uses 'error-' $level as css style. applies to targets or main only
|
||||
*
|
||||
* @param string $error_id Any internal error ID for this error
|
||||
* @param string $level Error level in ok/info/warn/error
|
||||
* @param string $str Error message (out)
|
||||
* @param string $target alternate attachment point for this error message
|
||||
* @param string $target_style Alternate color style for the error message
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @param string|null $message If abort/crash, non localized $str
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
|
||||
* else set for this call only
|
||||
*/
|
||||
public function setErrorMsg(
|
||||
string $error_id,
|
||||
string $level,
|
||||
string $str,
|
||||
string $target = '',
|
||||
string $target_style = '',
|
||||
array $highlight = [],
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
): void {
|
||||
if ($log_error === null) {
|
||||
$log_error = $this->log_error;
|
||||
}
|
||||
$original_level = $level;
|
||||
$level = MessageLevel::fromName($level)->name;
|
||||
// if not string set, write message string if set, else level/error id
|
||||
if (empty($str)) {
|
||||
$str = $message ?? 'L:' . $level . '|E:' . $error_id;
|
||||
}
|
||||
$this->error_str[] = [
|
||||
'id' => $error_id,
|
||||
'level' => $level,
|
||||
'str' => $str,
|
||||
'target' => $target,
|
||||
'target_style' => $target_style,
|
||||
'highlight' => $highlight,
|
||||
];
|
||||
// write to log for abort/crash
|
||||
switch ($level) {
|
||||
case 'error':
|
||||
if ($log_error) {
|
||||
$this->log->error($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
}
|
||||
break;
|
||||
case 'abort':
|
||||
$this->log->critical($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
break;
|
||||
case 'crash':
|
||||
$this->log->alert($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
break;
|
||||
case 'unknown':
|
||||
$this->log->emergency($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* pushes new error message into the error_str array
|
||||
* Note, the parameter order is different and does not need an error id
|
||||
* This is for backend alerts
|
||||
*
|
||||
* @param string $level error level (ok/warn/info/error)
|
||||
* @param string $str error string
|
||||
* @param string|null $error_id optional error id for precise error lookup
|
||||
* @param string $target Alternate id name for output target on frontend
|
||||
* @param string $target_style Alternate color style for the error message
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @param string|null $message If abort/crash, non localized $str
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
|
||||
* else set for this call only
|
||||
*/
|
||||
public function setMessage(
|
||||
string $level,
|
||||
string $str,
|
||||
?string $error_id = null,
|
||||
string $target = '',
|
||||
string $target_style = '',
|
||||
array $highlight = [],
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
): void {
|
||||
$this->setErrorMsg(
|
||||
$error_id ?? '',
|
||||
$level,
|
||||
$str,
|
||||
$target,
|
||||
$target_style,
|
||||
$highlight,
|
||||
$message,
|
||||
$context,
|
||||
$log_error
|
||||
);
|
||||
}
|
||||
|
||||
// *********************************************************************
|
||||
// GETTERS
|
||||
// *********************************************************************
|
||||
|
||||
/**
|
||||
* Returns the current set error content from setErrorMsg method
|
||||
*
|
||||
* @return array<int,array{id:string,level:string,str:string,target:string,highlight:string[]}> Error messages array
|
||||
*/
|
||||
public function getErrorMsg(): array
|
||||
{
|
||||
return $this->error_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current set error ids
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getErrorIds(): array
|
||||
{
|
||||
return array_column($this->error_str, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the LAST entry in the array list.
|
||||
* If nothing found returns empty array set
|
||||
*
|
||||
* @return array{id:string,level:string,str:string,target:string,target:string,highlight:string[]} Error block
|
||||
*/
|
||||
public function getLastErrorMsg(): array
|
||||
{
|
||||
return $this->error_str[array_key_last($this->error_str)] ?? [
|
||||
'level' => '',
|
||||
'str' => '',
|
||||
'id' => '',
|
||||
'target' => '',
|
||||
'target_string' => '',
|
||||
'highlight' => [],
|
||||
];
|
||||
}
|
||||
|
||||
// *********************************************************************
|
||||
// FLAG SETTERS
|
||||
// *********************************************************************
|
||||
|
||||
/**
|
||||
* Set the log error flag
|
||||
*
|
||||
* @param bool $flag True to log level error too, False for do not (Default)
|
||||
* @return void
|
||||
*/
|
||||
public function setFlagLogError(bool $flag): void
|
||||
{
|
||||
$this->log_error = $flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current log error flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getFlagLogError(): bool
|
||||
{
|
||||
return $this->log_error;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -113,17 +113,32 @@ enum Level: int
|
||||
|
||||
/**
|
||||
* Returns true if the passed $level is higher or equal to $this
|
||||
*
|
||||
* @param Level $level
|
||||
* @return bool
|
||||
*/
|
||||
public function includes(Level $level): bool
|
||||
{
|
||||
return $this->value <= $level->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* If level is higher than set one
|
||||
*
|
||||
* @param Level $level
|
||||
* @return bool
|
||||
*/
|
||||
public function isHigherThan(Level $level): bool
|
||||
{
|
||||
return $this->value > $level->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* if level is lower than set one
|
||||
*
|
||||
* @param Level $level
|
||||
* @return bool
|
||||
*/
|
||||
public function isLowerThan(Level $level): bool
|
||||
{
|
||||
return $this->value < $level->value;
|
||||
|
||||
84
src/Logging/Logger/MessageLevel.php
Normal file
84
src/Logging/Logger/MessageLevel.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php // phpcs:disable Generic.Files.LineLength
|
||||
|
||||
/**
|
||||
* AUTOR: Clemens Schwaighofer
|
||||
* CREATED: 2023-09-08
|
||||
* DESCRIPTION:
|
||||
* Error message return levels
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Logging\Logger;
|
||||
|
||||
enum MessageLevel: int
|
||||
{
|
||||
case ok = 100;
|
||||
case info = 200;
|
||||
case warn = 300;
|
||||
case error = 400;
|
||||
case abort = 500;
|
||||
case crash = 550;
|
||||
case unknown = 600;
|
||||
|
||||
/**
|
||||
* @param string $name any string name, if not matching use unkown
|
||||
* @return static
|
||||
*/
|
||||
public static function fromName(string $name): self
|
||||
{
|
||||
return match (strtolower($name)) {
|
||||
'ok' => self::ok,
|
||||
'info' => self::info,
|
||||
'warn', 'warning' => self::warn,
|
||||
'error' => self::error,
|
||||
'abort' => self::abort,
|
||||
'crash' => self::crash,
|
||||
default => self::unknown,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
* @return static
|
||||
*/
|
||||
public static function fromValue(int $value): self
|
||||
{
|
||||
return self::tryFrom($value) ?? self::unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the passed $level is higher or equal to $this
|
||||
*
|
||||
* @param MessageLevel $level
|
||||
* @return bool
|
||||
*/
|
||||
public function includes(MessageLevel $level): bool
|
||||
{
|
||||
return $this->value <= $level->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* If level is higher than set one
|
||||
*
|
||||
* @param MessageLevel $level
|
||||
* @return bool
|
||||
*/
|
||||
public function isHigherThan(MessageLevel $level): bool
|
||||
{
|
||||
return $this->value > $level->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* if level is lower than set one
|
||||
*
|
||||
* @param MessageLevel $level
|
||||
* @return bool
|
||||
*/
|
||||
public function isLowerThan(MessageLevel $level): bool
|
||||
{
|
||||
return $this->value < $level->value;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -300,7 +300,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
// log
|
||||
/** @var \CoreLibs\Logging\Logging */
|
||||
public \CoreLibs\Logging\Logging $log;
|
||||
|
||||
/** @var \CoreLibs\DB\Extended\ArrayIO */
|
||||
public \CoreLibs\DB\Extended\ArrayIO $dba;
|
||||
// now some default error msgs (english)
|
||||
/** @var array<mixed> */
|
||||
public array $language_array = [];
|
||||
@@ -382,6 +383,15 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
$this->base_acl_level,
|
||||
$this->acl_admin
|
||||
);
|
||||
$this->dba = new \CoreLibs\DB\Extended\ArrayIO(
|
||||
$db_config,
|
||||
$config_array['table_array'],
|
||||
$config_array['table_name'],
|
||||
$this->log,
|
||||
// set the ACL
|
||||
$this->base_acl_level,
|
||||
$this->acl_admin
|
||||
);
|
||||
// here should be a check if the config_array is correct ...
|
||||
if (isset($config_array['show_fields']) && is_array($config_array['show_fields'])) {
|
||||
$this->field_array = $config_array['show_fields'];
|
||||
|
||||
40
src/Output/Form/TableArrays/EditOrder.php
Normal file
40
src/Output/Form/TableArrays/EditOrder.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Output\Form\TableArrays;
|
||||
|
||||
class EditOrder implements Interface\TableArraysInterface
|
||||
{
|
||||
/** @var \CoreLibs\Output\Form\Generate */
|
||||
private \CoreLibs\Output\Form\Generate $form;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param \CoreLibs\Output\Form\Generate $form base form class
|
||||
*/
|
||||
public function __construct(\CoreLibs\Output\Form\Generate $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: this is a dummy array to just init the Form\Generate class and is not used for anything else
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function setTableArray(): array
|
||||
{
|
||||
return [
|
||||
'table_array' => [
|
||||
'-'
|
||||
],
|
||||
'table_name' => '-',
|
||||
'load_query' => '',
|
||||
'show_fields' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -43,12 +43,17 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'int, no override' => [
|
||||
1,
|
||||
null,
|
||||
null
|
||||
'1'
|
||||
],
|
||||
'int, override set' => [
|
||||
1,
|
||||
'not int',
|
||||
'not int'
|
||||
'1'
|
||||
],
|
||||
'array, override set' => [
|
||||
[1, 2],
|
||||
null,
|
||||
null
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -201,7 +206,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'float' => [
|
||||
1.5,
|
||||
null,
|
||||
null
|
||||
1
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -349,7 +354,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'int' => [
|
||||
1,
|
||||
null,
|
||||
null
|
||||
1.0
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
@@ -43,11 +43,16 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'int, no override' => [
|
||||
1,
|
||||
null,
|
||||
''
|
||||
'1'
|
||||
],
|
||||
'int, override set' => [
|
||||
1,
|
||||
'not int',
|
||||
'1'
|
||||
],
|
||||
'array, override set' => [
|
||||
[1, 2],
|
||||
'not int',
|
||||
'not int'
|
||||
]
|
||||
];
|
||||
@@ -189,7 +194,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'float' => [
|
||||
1.5,
|
||||
null,
|
||||
0
|
||||
1
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -330,7 +335,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'int' => [
|
||||
1,
|
||||
null,
|
||||
0.0
|
||||
1.0
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -341,7 +346,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
* @dataProvider varSetTypeFloatProvider
|
||||
* @testdox setFloat $input with override $default will be $expected [$_dataName]
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param mixed $input
|
||||
* @param float|null $default
|
||||
* @param float $expected
|
||||
* @return void
|
||||
|
||||
349
test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php
Normal file
349
test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php
Normal file
@@ -0,0 +1,349 @@
|
||||
<?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',
|
||||
],
|
||||
'info' => [
|
||||
'level' => 'info',
|
||||
'str' => 'INFO',
|
||||
'expected' => 'info',
|
||||
],
|
||||
'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' => 'testErrorMessages',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Debug,
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setMessage(
|
||||
$level,
|
||||
$str
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'level' => $expected,
|
||||
'str' => $str,
|
||||
'id' => '',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'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' => 'testErrorMessages',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Debug
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setErrorMsg(
|
||||
'100',
|
||||
'info',
|
||||
'INFO MESSAGE'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
$em->getLastErrorMsg()
|
||||
);
|
||||
$this->assertEquals(
|
||||
['100'],
|
||||
$em->getErrorIds()
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
]
|
||||
],
|
||||
$em->getErrorMsg()
|
||||
);
|
||||
|
||||
$em->setErrorMsg(
|
||||
'200',
|
||||
'error',
|
||||
'ERROR MESSAGE'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
$em->getLastErrorMsg()
|
||||
);
|
||||
$this->assertEquals(
|
||||
['100', '200'],
|
||||
$em->getErrorIds()
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
[
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
]
|
||||
],
|
||||
$em->getErrorMsg()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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',
|
||||
],
|
||||
'crash' => [
|
||||
'id' => '300',
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> CRASH MESSAGE',
|
||||
],
|
||||
'crash, message' => [
|
||||
'id' => '300',
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => 'OTHER CRASH MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> OTHER CRASH MESSAGE',
|
||||
],
|
||||
'abort' => [
|
||||
'id' => '200',
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> ABORT MESSAGE',
|
||||
],
|
||||
'abort, message' => [
|
||||
'id' => '200',
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => 'OTHER ABORT MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
|
||||
],
|
||||
'unknown' => [
|
||||
'id' => '400',
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
|
||||
],
|
||||
'unknown, message' => [
|
||||
'id' => '400',
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => 'OTHER WRONG LEVEL MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider providerErrorMessageLog
|
||||
* @testdox Test Log writing [$_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 testErrorMessageLog(
|
||||
string $id,
|
||||
string $level,
|
||||
string $str,
|
||||
?string $message,
|
||||
?bool $log_error,
|
||||
string $expected
|
||||
): void {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessages',
|
||||
'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 n
|
||||
if ($level == 'error' && ($log_error === null || $log_error === false)) {
|
||||
$this->assertStringNotContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} else {
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
Reference in New Issue
Block a user