New methods for creating/import test data from XML files.

This commit is contained in:
denis.flaven@combodo.com
2021-10-25 11:49:23 +02:00
parent 63448276cc
commit ee848129b7
2 changed files with 159 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ namespace Combodo\iTop\Test\UnitTest;
use ArchivedObjectException;
use CMDBSource;
use CMDBObject;
use Contact;
use DBObject;
use DBObjectSet;
@@ -47,6 +48,7 @@ use Ticket;
use URP_UserProfile;
use VirtualHost;
use VirtualMachine;
use XMLDataLoader;
/** @see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::CreateObjectWithTagSet() */
@@ -806,4 +808,43 @@ class ItopDataTestCase extends ItopTestCase
static::assertTrue(true);
}
}
/**
* Import a set of XML files describing a consistent set of iTop objects
* @param string[] $aFiles
* @param boolean $bSearch If true, a search will be performed on each object (based on its reconciliation keys)
* before trying to import it (existing objects will be updated)
* @return int Number of objects created
*/
protected function CreateFromXMLFiles($aFiles, $bSearch = false)
{
$oLoader = new XMLDataLoader();
$oLoader->StartSession(CMDBObject::GetCurrentChange());
foreach($aFiles as $sFilePath)
{
$oLoader->LoadFile($sFilePath, false, $bSearch);
}
$oLoader->EndSession();
return $oLoader->GetCountCreated();
}
/**
* Import a consistent set of iTop objects from the specified XML text string
* @param string $sXmlDataset
* @param boolean $bSearch If true, a search will be performed on each object (based on its reconciliation keys)
* before trying to import it (existing objects will be updated)
* @return int The number of objects created
*/
protected function CreateFromXMLString($sXmlDataset, $bSearch = false)
{
$sTmpFileName = tempnam(sys_get_temp_dir(), 'xml');
file_put_contents($sTmpFileName, $sXmlDataset);
$ret = $this->CreateFromXMLFiles([$sTmpFileName], $bSearch);
unlink($sTmpFileName);
return $ret;
}
}