N°4756 - Fix Unit tests

This commit is contained in:
Eric Espie
2022-11-18 10:49:59 +01:00
parent 6585f717c5
commit 52c984d5e7
4 changed files with 32 additions and 14 deletions

View File

@@ -5827,13 +5827,15 @@ abstract class DBObject implements iDisplay
*/
public function FireEvent($sEvent, $aEventData = array())
{
$aEventData['debug_info'] = 'from: '.get_class($this).':'.$this->GetKey();
$aEventData['object'] = $this;
$aEventSources = [$this->m_sObjectUniqId];
foreach (MetaModel::EnumParentClasses(get_class($this), ENUM_PARENT_CLASSES_ALL, false) as $sClass) {
$aEventSources[] = $sClass;
if (EventService::IsEventRegistered($sEvent)) {
$aEventData['debug_info'] = 'from: '.get_class($this).':'.$this->GetKey();
$aEventData['object'] = $this;
$aEventSources = [$this->m_sObjectUniqId];
foreach (MetaModel::EnumParentClasses(get_class($this), ENUM_PARENT_CLASSES_ALL, false) as $sClass) {
$aEventSources[] = $sClass;
}
EventService::FireEvent(new EventData($sEvent, $aEventSources, $aEventData));
}
EventService::FireEvent(new EventData($sEvent, $aEventSources, $aEventData));
}
protected function EventInsertRequested()

View File

@@ -1100,8 +1100,8 @@ EOF
$sDescription = var_export($aEventDescription, true);
$sConstant = $sName;
$sOutput = "define('$sConstant', '$sName');\n";
$sOutput .= "Combodo\iTop\Service\EventService::RegisterEvent('$sName', $sDescription, '$sModuleName');\n";
$sOutput = "const $sConstant = '$sName';\n";
$sOutput .= "Combodo\iTop\Service\EventService::RegisterEvent($sName, $sDescription, '$sModuleName');\n";
return $sOutput;
}

View File

@@ -70,6 +70,11 @@ class EventHelper
IssueLog::Error($sMessage, LogChannels::EVENT_SERVICE);
}
public static function Warning($sMessage)
{
IssueLog::Warning($sMessage, LogChannels::EVENT_SERVICE);
}
public static function MatchEventSource($srcRegistered, $srcEvent): bool
{
if (empty($srcRegistered)) {

View File

@@ -16,9 +16,9 @@ use utils;
class EventService
{
public static $aEventListeners;
private static $iEventIdCounter;
private static $aEventDescription;
public static $aEventListeners = [];
private static $iEventIdCounter = 0;
private static $aEventDescription = [];
public static function InitService()
{
@@ -103,7 +103,7 @@ class EventService
public static function FireEvent(EventData $oEventData)
{
$sEvent = $oEventData->GetEvent();
if (!array_key_exists($sEvent, self::$aEventDescription)) {
if (!self::IsEventRegistered($sEvent)) {
$sError = "Fire event error: Event $sEvent is not registered";
EventHelper::Error($sError);
throw new CoreException($sError);
@@ -267,9 +267,10 @@ class EventService
*/
public static function RegisterEvent(string $sEvent, array $aDescription, string $sModule)
{
if (isset(self::$aEventDescription[$sEvent])) {
if (self::IsEventRegistered($sEvent)) {
$sPrevious = self::$aEventDescription[$sEvent]['module'];
EventHelper::Error("The Event $sEvent defined by $sModule has already been defined in $sPrevious, check your delta");
EventHelper::Warning("The Event $sEvent defined by $sModule has already been defined in $sPrevious, check your delta");
return;
}
self::$aEventDescription[$sEvent] = [
@@ -309,6 +310,16 @@ class EventService
return implode('_', $aRet);
}
/**
* @param string $sEvent
*
* @return bool
*/
public static function IsEventRegistered(string $sEvent): bool
{
return array_key_exists($sEvent, self::$aEventDescription);
}
public static function GetDefinedEventsAsJSON()
{
return json_encode(self::$aEventDescription, JSON_PRETTY_PRINT);