From 2da7463edf2a90a4d2f172abd7b2ac11bbd68b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 18 Jun 2023 17:25:55 +0200 Subject: [PATCH] Use a enum for level in LogEntries --- src/Entity/LogSystem/AbstractLogEntry.php | 85 ++----------- .../LogSystem/CollectionElementDeleted.php | 4 +- .../LogSystem/ElementCreatedLogEntry.php | 4 +- .../LogSystem/ElementDeletedLogEntry.php | 4 +- .../LogSystem/ElementEditedLogEntry.php | 2 +- src/Entity/LogSystem/LogLevel.php | 117 ++++++++++++++++++ .../LogSystem/PartStockChangedLogEntry.php | 2 +- .../LogSystem/SecurityEventLogEntry.php | 3 +- src/Entity/LogSystem/UserLoginLogEntry.php | 2 +- src/Entity/LogSystem/UserLogoutLogEntry.php | 2 +- .../LogSystem/UserNotAllowedLogEntry.php | 2 +- .../Constraints/EnumConstraintType.php | 73 +++++++++++ src/Form/Filters/LogFilterType.php | 18 +-- src/Services/LogSystem/EventLogger.php | 10 +- templates/form/filter_types_layout.html.twig | 4 + .../Entity/LogSystem/AbstractLogEntryTest.php | 38 ------ tests/Entity/LogSystem/LogLevelTest.php | 84 +++++++++++++ tests/Services/LogSystem/EventLoggerTest.php | 17 +-- 18 files changed, 322 insertions(+), 149 deletions(-) create mode 100644 src/Entity/LogSystem/LogLevel.php create mode 100644 src/Form/Filters/Constraints/EnumConstraintType.php create mode 100644 tests/Entity/LogSystem/LogLevelTest.php diff --git a/src/Entity/LogSystem/AbstractLogEntry.php b/src/Entity/LogSystem/AbstractLogEntry.php index 20626385..eb16e3ca 100644 --- a/src/Entity/LogSystem/AbstractLogEntry.php +++ b/src/Entity/LogSystem/AbstractLogEntry.php @@ -46,7 +46,7 @@ use App\Entity\UserSystem\User; use DateTime; use Doctrine\ORM\Mapping as ORM; use InvalidArgumentException; -use Psr\Log\LogLevel; +use Psr\Log\LogLevel as PsrLogLevel; use App\Repository\LogEntryRepository; /** @@ -63,15 +63,6 @@ use App\Repository\LogEntryRepository; #[ORM\Index(columns: ['datetime'], name: 'log_idx_datetime')] abstract class AbstractLogEntry extends AbstractDBElement { - final public const LEVEL_EMERGENCY = 0; - final public const LEVEL_ALERT = 1; - final public const LEVEL_CRITICAL = 2; - final public const LEVEL_ERROR = 3; - final public const LEVEL_WARNING = 4; - final public const LEVEL_NOTICE = 5; - final public const LEVEL_INFO = 6; - final public const LEVEL_DEBUG = 7; - protected const TARGET_TYPE_NONE = 0; protected const TARGET_TYPE_USER = 1; protected const TARGET_TYPE_ATTACHEMENT = 2; @@ -93,19 +84,6 @@ abstract class AbstractLogEntry extends AbstractDBElement protected const TARGET_TYPE_PARAMETER = 18; protected const TARGET_TYPE_LABEL_PROFILE = 19; - /** - * @var array This const is used to convert the numeric level to a PSR-3 compatible log level - */ - protected const LEVEL_ID_TO_STRING = [ - self::LEVEL_EMERGENCY => LogLevel::EMERGENCY, - self::LEVEL_ALERT => LogLevel::ALERT, - self::LEVEL_CRITICAL => LogLevel::CRITICAL, - self::LEVEL_ERROR => LogLevel::ERROR, - self::LEVEL_WARNING => LogLevel::WARNING, - self::LEVEL_NOTICE => LogLevel::NOTICE, - self::LEVEL_INFO => LogLevel::INFO, - self::LEVEL_DEBUG => LogLevel::DEBUG, - ]; protected const TARGET_CLASS_MAPPING = [ self::TARGET_TYPE_USER => User::class, @@ -146,10 +124,11 @@ abstract class AbstractLogEntry extends AbstractDBElement #[ORM\Column(name: 'datetime', type: Types::DATETIME_MUTABLE)] protected \DateTimeInterface $timestamp; - /** @var int The priority level of the associated level. 0 is highest, 7 lowest + /** + * @var LogLevel The priority level of the associated level. 0 is highest, 7 lowest */ - #[ORM\Column(type: 'tinyint', name: 'level')] - protected int $level = self::LEVEL_WARNING; + #[ORM\Column(name: 'level', type: 'tinyint', enumType: LogLevel::class)] + protected LogLevel $level = LogLevel::WARNING; /** @var int The ID of the element targeted by this event */ @@ -267,16 +246,10 @@ abstract class AbstractLogEntry extends AbstractDBElement } /** - * Get the priority level of this log entry. 0 is highest and 7 lowest level. - * See LEVEL_* consts in this class for more info. + * Get the priority level of this log entry. */ - public function getLevel(): int + public function getLevel(): LogLevel { - //It is always alerting when a wrong int is saved in DB... - if ($this->level < 0 || $this->level > 7) { - return self::LEVEL_ALERT; - } - return $this->level; } @@ -285,13 +258,9 @@ abstract class AbstractLogEntry extends AbstractDBElement * * @return $this */ - public function setLevel(int $level): self + public function setLevel(LogLevel $level): self { - if ($level < 0 || $this->level > 7) { - throw new InvalidArgumentException(sprintf('$level must be between 0 and 7! %d given!', $level)); - } $this->level = $level; - return $this; } @@ -300,7 +269,7 @@ abstract class AbstractLogEntry extends AbstractDBElement */ public function getLevelString(): string { - return self::levelIntToString($this->getLevel()); + return $this->level->toPSR3LevelString(); } /** @@ -310,8 +279,7 @@ abstract class AbstractLogEntry extends AbstractDBElement */ public function setLevelString(string $level): self { - $this->setLevel(self::levelStringToInt($level)); - + LogLevel::fromPSR3LevelString($level); return $this; } @@ -402,39 +370,6 @@ abstract class AbstractLogEntry extends AbstractDBElement return $this->extra; } - /** - * This function converts the internal numeric log level into an PSR3 compatible level string. - * - * @param int $level The numerical log level - * - * @return string The PSR3 compatible level string - */ - final public static function levelIntToString(int $level): string - { - if (!isset(self::LEVEL_ID_TO_STRING[$level])) { - throw new InvalidArgumentException('No level with this int is existing!'); - } - - return self::LEVEL_ID_TO_STRING[$level]; - } - - /** - * This function converts a PSR3 compatible string to the internal numeric level string. - * - * @param string $level the PSR3 compatible string that should be converted - * - * @return int the internal int representation - */ - final public static function levelStringToInt(string $level): int - { - $tmp = array_flip(self::LEVEL_ID_TO_STRING); - if (!isset($tmp[$level])) { - throw new InvalidArgumentException('No level with this string is existing!'); - } - - return $tmp[$level]; - } - /** * Converts a target type id to a full qualified class name. * diff --git a/src/Entity/LogSystem/CollectionElementDeleted.php b/src/Entity/LogSystem/CollectionElementDeleted.php index 8e85dd10..bc4a5bd9 100644 --- a/src/Entity/LogSystem/CollectionElementDeleted.php +++ b/src/Entity/LogSystem/CollectionElementDeleted.php @@ -88,11 +88,13 @@ use InvalidArgumentException; class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventUndoInterface { protected string $typeString = 'collection_element_deleted'; - protected int $level = self::LEVEL_INFO; public function __construct(AbstractDBElement $changed_element, string $collection_name, AbstractDBElement $deletedElement) { parent::__construct(); + + $this->level = LogLevel::INFO; + $this->setTargetElement($changed_element); $this->extra['n'] = $collection_name; $this->extra['c'] = self::targetTypeClassToID($deletedElement::class); diff --git a/src/Entity/LogSystem/ElementCreatedLogEntry.php b/src/Entity/LogSystem/ElementCreatedLogEntry.php index 9cac58b3..90f72d48 100644 --- a/src/Entity/LogSystem/ElementCreatedLogEntry.php +++ b/src/Entity/LogSystem/ElementCreatedLogEntry.php @@ -38,12 +38,12 @@ class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentI public function __construct(AbstractDBElement $new_element) { parent::__construct(); - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setTargetElement($new_element); //Creation of new users is maybe more interesting... if ($new_element instanceof User || $new_element instanceof Group) { - $this->level = self::LEVEL_NOTICE; + $this->level = LogLevel::NOTICE; } } diff --git a/src/Entity/LogSystem/ElementDeletedLogEntry.php b/src/Entity/LogSystem/ElementDeletedLogEntry.php index 29ab6a91..6b0e0f0a 100644 --- a/src/Entity/LogSystem/ElementDeletedLogEntry.php +++ b/src/Entity/LogSystem/ElementDeletedLogEntry.php @@ -40,12 +40,12 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter public function __construct(AbstractDBElement $deleted_element) { parent::__construct(); - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setTargetElement($deleted_element); //Deletion of a user is maybe more interesting... if ($deleted_element instanceof User || $deleted_element instanceof Group) { - $this->level = self::LEVEL_NOTICE; + $this->level = LogLevel::NOTICE; } } diff --git a/src/Entity/LogSystem/ElementEditedLogEntry.php b/src/Entity/LogSystem/ElementEditedLogEntry.php index dab87652..d524f3c1 100644 --- a/src/Entity/LogSystem/ElementEditedLogEntry.php +++ b/src/Entity/LogSystem/ElementEditedLogEntry.php @@ -38,7 +38,7 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf public function __construct(AbstractDBElement $changed_element) { parent::__construct(); - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setTargetElement($changed_element); } diff --git a/src/Entity/LogSystem/LogLevel.php b/src/Entity/LogSystem/LogLevel.php new file mode 100644 index 00000000..98fb3649 --- /dev/null +++ b/src/Entity/LogSystem/LogLevel.php @@ -0,0 +1,117 @@ +. + */ + +namespace App\Entity\LogSystem; + +use \Psr\Log\LogLevel as PSRLogLevel; + +enum LogLevel: int +{ + case EMERGENCY = 0; + case ALERT = 1; + case CRITICAL = 2; + case ERROR = 3; + case WARNING = 4; + case NOTICE = 5; + case INFO = 6; + case DEBUG = 7; + + /** + * Converts the current log level to a PSR-3 log level string. + * @return string + */ + public function toPSR3LevelString(): string + { + return match ($this) { + self::EMERGENCY => PSRLogLevel::EMERGENCY, + self::ALERT => PSRLogLevel::ALERT, + self::CRITICAL => PSRLogLevel::CRITICAL, + self::ERROR => PSRLogLevel::ERROR, + self::WARNING => PSRLogLevel::WARNING, + self::NOTICE => PSRLogLevel::NOTICE, + self::INFO => PSRLogLevel::INFO, + self::DEBUG => PSRLogLevel::DEBUG, + }; + } + + /** + * Creates a log level (enum) from a PSR-3 log level string. + * @param string $level + * @return self + */ + public static function fromPSR3LevelString(string $level): self + { + return match ($level) { + PSRLogLevel::EMERGENCY => self::EMERGENCY, + PSRLogLevel::ALERT => self::ALERT, + PSRLogLevel::CRITICAL => self::CRITICAL, + PSRLogLevel::ERROR => self::ERROR, + PSRLogLevel::WARNING => self::WARNING, + PSRLogLevel::NOTICE => self::NOTICE, + PSRLogLevel::INFO => self::INFO, + PSRLogLevel::DEBUG => self::DEBUG, + default => throw new \InvalidArgumentException("Invalid log level: $level"), + }; + } + + /** + * Checks if the current log level is more important than the given one. + * @param LogLevel $other + * @return bool + */ + public function moreImportThan(self $other): bool + { + //Smaller values are more important + return $this->value < $other->value; + } + + /** + * Checks if the current log level is more important or equal than the given one. + * @param LogLevel $other + * @return bool + */ + public function moreImportOrEqualThan(self $other): bool + { + //Smaller values are more important + return $this->value <= $other->value; + } + + /** + * Checks if the current log level is less important than the given one. + * @param LogLevel $other + * @return bool + */ + public function lessImportThan(self $other): bool + { + //Bigger values are less important + return $this->value > $other->value; + } + + /** + * Checks if the current log level is less important or equal than the given one. + * @param LogLevel $other + * @return bool + */ + public function lessImportOrEqualThan(self $other): bool + { + //Bigger values are less important + return $this->value >= $other->value; + } +} diff --git a/src/Entity/LogSystem/PartStockChangedLogEntry.php b/src/Entity/LogSystem/PartStockChangedLogEntry.php index e77ea482..34ae15fb 100644 --- a/src/Entity/LogSystem/PartStockChangedLogEntry.php +++ b/src/Entity/LogSystem/PartStockChangedLogEntry.php @@ -55,7 +55,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry } //Same as every other element change log entry - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setTargetElement($lot); diff --git a/src/Entity/LogSystem/SecurityEventLogEntry.php b/src/Entity/LogSystem/SecurityEventLogEntry.php index 0dafce92..b1b6227a 100644 --- a/src/Entity/LogSystem/SecurityEventLogEntry.php +++ b/src/Entity/LogSystem/SecurityEventLogEntry.php @@ -69,10 +69,9 @@ class SecurityEventLogEntry extends AbstractLogEntry public function __construct(string $type, string $ip_address, bool $anonymize = true) { parent::__construct(); - $this->level = self::LEVEL_INFO; $this->setIPAddress($ip_address, $anonymize); $this->setEventType($type); - $this->level = self::LEVEL_NOTICE; + $this->level = LogLevel::NOTICE; } public function setTargetElement(?AbstractDBElement $element): AbstractLogEntry diff --git a/src/Entity/LogSystem/UserLoginLogEntry.php b/src/Entity/LogSystem/UserLoginLogEntry.php index 166533c4..c9e6bc21 100644 --- a/src/Entity/LogSystem/UserLoginLogEntry.php +++ b/src/Entity/LogSystem/UserLoginLogEntry.php @@ -36,7 +36,7 @@ class UserLoginLogEntry extends AbstractLogEntry public function __construct(string $ip_address, bool $anonymize = true) { parent::__construct(); - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setIPAddress($ip_address, $anonymize); } diff --git a/src/Entity/LogSystem/UserLogoutLogEntry.php b/src/Entity/LogSystem/UserLogoutLogEntry.php index 9fce3069..ba52de87 100644 --- a/src/Entity/LogSystem/UserLogoutLogEntry.php +++ b/src/Entity/LogSystem/UserLogoutLogEntry.php @@ -33,7 +33,7 @@ class UserLogoutLogEntry extends AbstractLogEntry public function __construct(string $ip_address, bool $anonymize = true) { parent::__construct(); - $this->level = self::LEVEL_INFO; + $this->level = LogLevel::INFO; $this->setIPAddress($ip_address, $anonymize); } diff --git a/src/Entity/LogSystem/UserNotAllowedLogEntry.php b/src/Entity/LogSystem/UserNotAllowedLogEntry.php index 99fb2492..c570a012 100644 --- a/src/Entity/LogSystem/UserNotAllowedLogEntry.php +++ b/src/Entity/LogSystem/UserNotAllowedLogEntry.php @@ -32,7 +32,7 @@ class UserNotAllowedLogEntry extends AbstractLogEntry public function __construct(string $path) { parent::__construct(); - $this->level = static::LEVEL_WARNING; + $this->level = LogLevel::WARNING; $this->extra['a'] = $path; } diff --git a/src/Form/Filters/Constraints/EnumConstraintType.php b/src/Form/Filters/Constraints/EnumConstraintType.php new file mode 100644 index 00000000..59259169 --- /dev/null +++ b/src/Form/Filters/Constraints/EnumConstraintType.php @@ -0,0 +1,73 @@ +. + */ +namespace App\Form\Filters\Constraints; + +use App\DataTables\Filters\Constraints\ChoiceConstraint; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\EnumType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class EnumConstraintType extends AbstractType +{ + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setRequired('enum_class'); + $resolver->setAllowedTypes('enum_class', 'string'); + + $resolver->setRequired('choice_label'); + $resolver->setAllowedTypes('choice_label', ['string', 'callable']); + + $resolver->setDefaults([ + 'compound' => true, + 'data_class' => ChoiceConstraint::class, + ]); + + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $choices = [ + '' => '', + 'filter.choice_constraint.operator.ANY' => 'ANY', + 'filter.choice_constraint.operator.NONE' => 'NONE', + ]; + + $builder->add('operator', ChoiceType::class, [ + 'choices' => $choices, + 'required' => false, + ]); + + $builder->add('value', EnumType::class, [ + 'class' => $options['enum_class'], + 'choice_label' => $options['choice_label'], + 'required' => false, + 'multiple' => true, + 'attr' => [ + 'data-controller' => 'elements--select-multiple', + ] + ]); + } + +} diff --git a/src/Form/Filters/LogFilterType.php b/src/Form/Filters/LogFilterType.php index 16d2c7aa..2db1aa14 100644 --- a/src/Form/Filters/LogFilterType.php +++ b/src/Form/Filters/LogFilterType.php @@ -25,6 +25,7 @@ namespace App\Form\Filters; use App\DataTables\Filters\LogFilter; use App\Entity\Attachments\Attachment; use App\Entity\Attachments\AttachmentType; +use App\Entity\LogSystem\LogLevel; use App\Entity\LogSystem\PartStockChangedLogEntry; use App\Entity\ProjectSystem\Project; use App\Entity\ProjectSystem\ProjectBOMEntry; @@ -56,6 +57,7 @@ use App\Entity\UserSystem\Group; use App\Entity\UserSystem\User; use App\Form\Filters\Constraints\ChoiceConstraintType; use App\Form\Filters\Constraints\DateTimeConstraintType; +use App\Form\Filters\Constraints\EnumConstraintType; use App\Form\Filters\Constraints\InstanceOfConstraintType; use App\Form\Filters\Constraints\NumberConstraintType; use App\Form\Filters\Constraints\UserEntityConstraintType; @@ -67,17 +69,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver; class LogFilterType extends AbstractType { - protected const LEVEL_CHOICES = [ - 'log.level.debug' => AbstractLogEntry::LEVEL_DEBUG, - 'log.level.info' => AbstractLogEntry::LEVEL_INFO, - 'log.level.notice' => AbstractLogEntry::LEVEL_NOTICE, - 'log.level.warning' => AbstractLogEntry::LEVEL_WARNING, - 'log.level.error' => AbstractLogEntry::LEVEL_ERROR, - 'log.level.critical' => AbstractLogEntry::LEVEL_CRITICAL, - 'log.level.alert' => AbstractLogEntry::LEVEL_ALERT, - 'log.level.emergency' => AbstractLogEntry::LEVEL_EMERGENCY, - ]; - protected const TARGET_TYPE_CHOICES = [ 'log.type.collection_element_deleted' => CollectionElementDeleted::class, 'log.type.database_updated' => DatabaseUpdatedLogEntry::class, @@ -117,9 +108,10 @@ class LogFilterType extends AbstractType - $builder->add('level', ChoiceConstraintType::class, [ + $builder->add('level', EnumConstraintType::class, [ 'label' => 'log.level', - 'choices' => self::LEVEL_CHOICES, + 'enum_class' => LogLevel::class, + 'choice_label' => fn(LogLevel $level): string => 'log.level.' . $level->toPSR3LevelString(), ]); $builder->add('eventType', InstanceOfConstraintType::class, [ diff --git a/src/Services/LogSystem/EventLogger.php b/src/Services/LogSystem/EventLogger.php index 909c5ab2..11147de6 100644 --- a/src/Services/LogSystem/EventLogger.php +++ b/src/Services/LogSystem/EventLogger.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace App\Services\LogSystem; +use App\Entity\LogSystem\LogLevel; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Security\Core\User\UserInterface; use App\Entity\LogSystem\AbstractLogEntry; @@ -34,8 +35,11 @@ use Doctrine\ORM\EntityManagerInterface; */ class EventLogger { - public function __construct(protected int $minimum_log_level, protected array $blacklist, protected array $whitelist, protected EntityManagerInterface $em, protected Security $security, protected ConsoleInfoHelper $console_info_helper) + protected LogLevel $minimum_log_level; + + public function __construct(int $minimum_log_level, protected array $blacklist, protected array $whitelist, protected EntityManagerInterface $em, protected Security $security, protected ConsoleInfoHelper $console_info_helper) { + $this->minimum_log_level = LogLevel::tryFrom($minimum_log_level); } /** @@ -108,7 +112,7 @@ class EventLogger public function shouldBeAdded( AbstractLogEntry $logEntry, - ?int $minimum_log_level = null, + ?LogLevel $minimum_log_level = null, ?array $blacklist = null, ?array $whitelist = null ): bool { @@ -118,7 +122,7 @@ class EventLogger $whitelist ??= $this->whitelist; //Don't add the entry if it does not reach the minimum level - if ($logEntry->getLevel() > $minimum_log_level) { + if ($logEntry->getLevel()->lessImportThan($minimum_log_level)) { return false; } diff --git a/templates/form/filter_types_layout.html.twig b/templates/form/filter_types_layout.html.twig index 70927d57..e99e6b62 100644 --- a/templates/form/filter_types_layout.html.twig +++ b/templates/form/filter_types_layout.html.twig @@ -44,6 +44,10 @@ {{ block('text_constraint_widget') }} {% endblock %} +{% block enum_constraint_widget %} + {{ block('text_constraint_widget') }} +{% endblock %} + {% block parameter_constraint_widget %} {% import 'components/collection_type.macro.html.twig' as collection %} diff --git a/tests/Entity/LogSystem/AbstractLogEntryTest.php b/tests/Entity/LogSystem/AbstractLogEntryTest.php index cc1dc74b..9d9d823d 100644 --- a/tests/Entity/LogSystem/AbstractLogEntryTest.php +++ b/tests/Entity/LogSystem/AbstractLogEntryTest.php @@ -60,22 +60,6 @@ use PHPUnit\Framework\TestCase; class AbstractLogEntryTest extends TestCase { - public function levelDataProvider(): array - { - return [ - [0, 'emergency'], - [1, 'alert'], - [2, 'critical'], - [3, 'error'], - [4, 'warning'], - [5, 'notice'], - [6, 'info'], - [7, 'debug'], - [8, 'blabla', true], - [-1, 'test', true], - ]; - } - public function targetTypeDataProvider(): array { return [ @@ -95,28 +79,6 @@ class AbstractLogEntryTest extends TestCase ]; } - /** - * @dataProvider levelDataProvider - */ - public function testLevelIntToString(int $int, string $expected_string, bool $expect_exception = false): void - { - if ($expect_exception) { - $this->expectException(\InvalidArgumentException::class); - } - $this->assertSame($expected_string, AbstractLogEntry::levelIntToString($int)); - } - - /** - * @dataProvider levelDataProvider - */ - public function testLevelStringToInt(int $expected_int, string $string, bool $expect_exception = false): void - { - if ($expect_exception) { - $this->expectException(\InvalidArgumentException::class); - } - $this->assertSame($expected_int, AbstractLogEntry::levelStringToInt($string)); - } - /** * @dataProvider targetTypeDataProvider */ diff --git a/tests/Entity/LogSystem/LogLevelTest.php b/tests/Entity/LogSystem/LogLevelTest.php new file mode 100644 index 00000000..634d5dd5 --- /dev/null +++ b/tests/Entity/LogSystem/LogLevelTest.php @@ -0,0 +1,84 @@ +. + */ + +namespace App\Tests\Entity\LogSystem; + +use App\Entity\LogSystem\LogLevel; +use PHPUnit\Framework\TestCase; + +class LogLevelTest extends TestCase +{ + + public function testToPSR3LevelString(): void + { + $this->assertSame('debug', LogLevel::DEBUG->toPSR3LevelString()); + $this->assertSame('info', LogLevel::INFO->toPSR3LevelString()); + $this->assertSame('notice', LogLevel::NOTICE->toPSR3LevelString()); + $this->assertSame('warning', LogLevel::WARNING->toPSR3LevelString()); + $this->assertSame('error', LogLevel::ERROR->toPSR3LevelString()); + $this->assertSame('critical', LogLevel::CRITICAL->toPSR3LevelString()); + $this->assertSame('alert', LogLevel::ALERT->toPSR3LevelString()); + $this->assertSame('emergency', LogLevel::EMERGENCY->toPSR3LevelString()); + } + + public function testFromPSR3LevelString(): void + { + $this->assertSame(LogLevel::DEBUG, LogLevel::fromPSR3LevelString('debug')); + $this->assertSame(LogLevel::INFO, LogLevel::fromPSR3LevelString('info')); + $this->assertSame(LogLevel::NOTICE, LogLevel::fromPSR3LevelString('notice')); + $this->assertSame(LogLevel::WARNING, LogLevel::fromPSR3LevelString('warning')); + $this->assertSame(LogLevel::ERROR, LogLevel::fromPSR3LevelString('error')); + $this->assertSame(LogLevel::CRITICAL, LogLevel::fromPSR3LevelString('critical')); + $this->assertSame(LogLevel::ALERT, LogLevel::fromPSR3LevelString('alert')); + $this->assertSame(LogLevel::EMERGENCY, LogLevel::fromPSR3LevelString('emergency')); + } + + public function testMoreImportOrEqualThan(): void + { + $this->assertTrue(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::DEBUG)); + $this->assertFalse(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::INFO)); + $this->assertFalse(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::NOTICE)); + $this->assertTrue(LogLevel::EMERGENCY->moreImportOrEqualThan(LogLevel::DEBUG)); + } + + public function testMoreImportThan(): void + { + $this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::DEBUG)); + $this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::INFO)); + $this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::NOTICE)); + $this->assertTrue(LogLevel::EMERGENCY->moreImportThan(LogLevel::DEBUG)); + } + + public function testLessImportThan(): void + { + $this->assertFalse(LogLevel::DEBUG->lessImportThan(LogLevel::DEBUG)); + $this->assertTrue(LogLevel::DEBUG->lessImportThan(LogLevel::INFO)); + $this->assertTrue(LogLevel::DEBUG->lessImportThan(LogLevel::NOTICE)); + $this->assertFalse(LogLevel::EMERGENCY->lessImportThan(LogLevel::DEBUG)); + } + + public function testLessImportOrEqualThan(): void + { + $this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::DEBUG)); + $this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::INFO)); + $this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::NOTICE)); + $this->assertFalse(LogLevel::EMERGENCY->lessImportOrEqualThan(LogLevel::DEBUG)); + } +} diff --git a/tests/Services/LogSystem/EventLoggerTest.php b/tests/Services/LogSystem/EventLoggerTest.php index cb74adb4..0dbb85a3 100644 --- a/tests/Services/LogSystem/EventLoggerTest.php +++ b/tests/Services/LogSystem/EventLoggerTest.php @@ -42,6 +42,7 @@ declare(strict_types=1); namespace App\Tests\Services\LogSystem; use App\Entity\LogSystem\AbstractLogEntry; +use App\Entity\LogSystem\LogLevel; use App\Entity\LogSystem\UserLoginLogEntry; use App\Entity\LogSystem\UserLogoutLogEntry; use App\Services\LogSystem\EventLogger; @@ -67,21 +68,21 @@ class EventLoggerTest extends WebTestCase { $event1 = new UserLoginLogEntry('127.0.0.1'); $event2 = new UserLogoutLogEntry('127.0.0.1'); - $event2->setLevel(AbstractLogEntry::LEVEL_CRITICAL); + $event2->setLevel(LogLevel::CRITICAL); //Test without restrictions - $this->assertTrue($this->service->shouldBeAdded($event1, 7, [], [])); + $this->assertTrue($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [], [])); //Test minimum log level - $this->assertFalse($this->service->shouldBeAdded($event1, 2, [], [])); - $this->assertTrue($this->service->shouldBeAdded($event2, 2, [], [])); + $this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::CRITICAL, [], [])); + $this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::CRITICAL, [], [])); //Test blacklist - $this->assertFalse($this->service->shouldBeAdded($event1, 7, [UserLoginLogEntry::class], [])); - $this->assertTrue($this->service->shouldBeAdded($event2, 7, [UserLoginLogEntry::class], [])); + $this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [UserLoginLogEntry::class], [])); + $this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::DEBUG, [UserLoginLogEntry::class], [])); //Test whitelist - $this->assertFalse($this->service->shouldBeAdded($event1, 7, [], [UserLogoutLogEntry::class])); - $this->assertTrue($this->service->shouldBeAdded($event2, 7, [], [UserLogoutLogEntry::class])); + $this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [], [UserLogoutLogEntry::class])); + $this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::DEBUG, [], [UserLogoutLogEntry::class])); } }