Events to cmdbAbstract

This commit is contained in:
Eric Espie
2022-04-07 17:02:19 +02:00
parent 5ac9b05b2d
commit b0a55e057b
9 changed files with 554 additions and 280 deletions

View File

@@ -27,8 +27,10 @@ namespace Combodo\iTop\Test\UnitTest;
*/
use ArchivedObjectException;
use CMDBSource;
use CMDBObject;
use CMDBSource;
use Combodo\iTop\Service\EventData;
use Combodo\iTop\Service\EventService;
use Contact;
use DBObject;
use DBObjectSet;
@@ -73,6 +75,9 @@ class ItopDataTestCase extends ItopTestCase
// For cleanup
private $aCreatedObjects = array();
// Counts
public $aReloadCount = [];
const USE_TRANSACTION = true;
const CREATE_TEST_ORG = false;
@@ -96,6 +101,8 @@ class ItopDataTestCase extends ItopTestCase
{
$this->CreateTestOrganization();
}
EventService::RegisterListener(EVENT_SERVICE_DB_OBJECT_RELOAD, [$this, 'CountObjectReload']);
}
/**
@@ -790,6 +797,44 @@ class ItopDataTestCase extends ItopTestCase
$this->iTestOrgId = $oOrg->GetKey();
}
public function ResetReloadCount()
{
$this->aReloadCount = [];
}
public function DebugReloadCount($sMsg, $bResetCount = true)
{
$iTotalCount = 0;
$aTotalPerClass = [];
foreach ($this->aReloadCount as $sClass => $aCountByKeys) {
$iClassCount = 0;
foreach ($aCountByKeys as $iCount) {
$iClassCount += $iCount;
}
$iTotalCount += $iClassCount;
$aTotalPerClass[$sClass] = $iClassCount;
}
$this->debug("$sMsg - $iTotalCount reload(s)");
foreach ($this->aReloadCount as $sClass => $aCountByKeys) {
$this->debug(" $sClass => $aTotalPerClass[$sClass] reload(s)");
foreach ($aCountByKeys as $sKey => $iCount) {
$this->debug(" $sClass::$sKey => $iCount");
}
}
if ($bResetCount) {
$this->ResetReloadCount();
}
}
public function CountObjectReload(EventData $oData)
{
$oObject = $oData->Get('object');
$sClass = get_class($oObject);
$sKey = $oObject->GetKey();
$iCount = $this->aReloadCount[$sClass][$sKey] ?? 0;
$this->aReloadCount[$sClass][$sKey] = 1 + $iCount;
}
/**
* Assert that a series of operations will trigger a given number of MySL queries
*
@@ -811,7 +856,7 @@ class ItopDataTestCase extends ItopTestCase
}
else
{
// Otherwise PHP Unit will consider that no assertion has been made
// Otherwise, PHP Unit will consider that no assertion has been made
static::assertTrue(true);
}
}

View File

@@ -243,6 +243,51 @@ class DBObjectTest extends ItopDataTestCase
});
}
/**
* @covers DBObject::NewObject
* @covers DBObject::Get
* @covers DBObject::Set
*/
public function testInsertNoReloadAttributeRefresh_ExternalKeysAndFields()
{
$this->ResetReloadCount();
static::assertDBQueryCount(0, function() use (&$oObject){
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));
});
static::assertDBQueryCount(49, function() use (&$oObject) {
$oObject->DBInsertNoReload();
});
$this->DebugReloadCount("Person::DBInsertNoReload()");
static::assertDBQueryCount(3, function() use (&$oObject){
static::assertEquals('Demo', $oObject->Get('org_id_friendlyname'));
static::assertEquals('Grenoble', $oObject->Get('location_id_friendlyname'));
});
$this->DebugReloadCount("Get('org_id_friendlyname') and Get('location_id_friendlyname')");
// External key given as an id
static::assertDBQueryCount(1, function() use (&$oObject){
$oObject->Set('org_id', 2);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
});
$this->DebugReloadCount("Set('org_id', 2) andGet('org_id_friendlyname')");
// External key given as an object
static::assertDBQueryCount(1, function() use (&$oBordeaux){
$oBordeaux = \MetaModel::GetObject('Location', 1);
});
$this->DebugReloadCount("GetObject('Location', 1)");
static::assertDBQueryCount(5, function() use (&$oBordeaux, &$oObject){
$oObject->Set('location_id', $oBordeaux);
static::assertEquals('IT Department', $oObject->Get('org_id_friendlyname'));
static::assertEquals('IT Department', $oObject->Get('org_name'));
static::assertEquals('Bordeaux', $oObject->Get('location_id_friendlyname'));
});
$this->DebugReloadCount("Set('location_id',...) Get('org_id_friendlyname') Get('org_name') Get('location_id_friendlyname')");
}
public function testSetExtKeyUnsetDependentAttribute()
{
$oObject = \MetaModel::NewObject('Person', array('name' => 'Foo', 'first_name' => 'John', 'org_id' => 3, 'location_id' => 2));