N°6275 - test ormCaseLogService with fake iOrmCaseLogExtension implementation

This commit is contained in:
odain
2023-05-09 15:44:58 +02:00
parent 2c3b8c0bcd
commit f5967f200a
3 changed files with 112 additions and 40 deletions

View File

@@ -17,20 +17,21 @@ class ormCaseLogService
*/
protected $aOrmCaseLogExtension = null;
public function __construct()
public function __construct(array $aOrmCaseLogExtensions=null)
{
$this->aOrmCaseLogExtension = $aOrmCaseLogExtensions;
}
protected function LoadCaseLogExtensions()
{
if ($this->aOrmCaseLogExtension !== null) return;
$aOrmCaseLogExtension = [];
$aProviderClasses = \utils::GetClassesForInterface(iOrmCaseLogExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
foreach($aProviderClasses as $sProviderClass) {
$aOrmCaseLogExtension[] = new $sProviderClass();
$aOrmCaseLogExtensions = [];
$aOrmCaseLogExtensionClasses = \utils::GetClassesForInterface(iOrmCaseLogExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
foreach($aOrmCaseLogExtensionClasses as $sOrmCaseLogExtensionClass) {
$aOrmCaseLogExtensions[] = new $sOrmCaseLogExtensionClass();
}
$this->aOrmCaseLogExtension = $aOrmCaseLogExtension;
$this->aOrmCaseLogExtension = $aOrmCaseLogExtensions;
}
/**

View File

@@ -0,0 +1,24 @@
<?php
class OrmCaseLogExtensionForTest implements \iOrmCaseLogExtension
{
private $sReturnedLog;
private $aReturnedIndex;
private $bTouched;
public function __construct(){
}
public function Init($bTouched, $sReturnedLog, $aReturnedIndex){
$this->bTouched = $bTouched;
$this->sReturnedLog = $sReturnedLog;
$this->aReturnedIndex = $aReturnedIndex;
}
public function Rebuild(&$sLog, &$aIndex) : bool{
$sLog = $this->sReturnedLog;
$aIndex = $this->aReturnedIndex;
return $this->bTouched;
}
}

View File

@@ -10,7 +10,6 @@ namespace Combodo\iTop\Test\UnitTest\Core;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use ormCaseLog;
/**
* Tests of the ormCaseLog class
*
@@ -28,7 +27,8 @@ class ormCaseLogTest extends ItopDataTestCase
public function setUp() :void{
parent::setUp(); // TODO: Change the autogenerated stub
//require_once APPROOT . "core/CaseLogService.php";
require_once __DIR__ . "/OrmCaseLogExtensionForTest.php";
require_once APPROOT . "core/ormcaselogservice.inc.php";
$oAdminProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'Administrator'), true);
@@ -117,13 +117,20 @@ class ormCaseLogTest extends ItopDataTestCase
$this->assertTrue(\UserRights::Login($this->sLogin));
$sLog = "aaaaa";
$sDate = date(\AttributeDateTime::GetInternalFormat());
$sJson = json_encode(
[
'user_login' => 'gabuzmeu',
'message' => $sLog,
]
);
$oJson = json_decode($sJson);
$iUserId = \UserRights::GetUserId();
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
$sDate = date(\AttributeDateTime::GetInternalFormat());
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$sExpectedLog = $sSeparator."<p>$sLog</p>";
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$aExpectedIndex = [
[
'user_name' => $sOnBehalfOf,
@@ -135,22 +142,20 @@ class ormCaseLogTest extends ItopDataTestCase
]
];
$oOrmCaseLogService->expects($this->exactly(2))
->method('Rebuild')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(null, null);
//create a real service with no extension to speed up treatment and avoid +1s delta in history diff
$oOrmCaseLogService = new \ormCaseLogService([]);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$oOrmCaseLogService->expects($this->exactly(1))
->method('Rebuild')
->withConsecutive([$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(null);
$this->SetNonPublicProperty($oLog, 'oOrmCaseLogService', $oOrmCaseLogService);
if ($bTestAddLogEntry){
$oLog->AddLogEntry($sLog);
} else {
$sJson = json_encode(
[
'user_login' => 'gabuzmeu',
'message' => $sLog,
]
);
$oJson = json_decode($sJson);
$oLog->AddLogEntryFromJSON($oJson, false);
}
@@ -161,22 +166,26 @@ class ormCaseLogTest extends ItopDataTestCase
/**
* @dataProvider AddLogEntryProvider
*/
public function testAddLogEntry($bTestAddLogEntry=true){
public function testAddLogEntryWithRebuild($bTestAddLogEntry=true){
$_SESSION = array();
$this->assertTrue(\UserRights::Login($this->sLogin));
$sLog = "aaaaa";
$sDate = date(\AttributeDateTime::GetInternalFormat());
$iUserId = \UserRights::GetUserId();
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$sExpectedLog = $sSeparator."<p>$sLog</p>";
$aRebuiltIndex = ['c' => 'd'];
$sRebuiltLog = "bbbb";
$sLog = "aaaaa";
$sJson = json_encode(
[
'user_login' => 'gabuzmeu',
'message' => $sLog,
]
);
$oJson = json_decode($sJson);
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$iUserId = \UserRights::GetUserId();
$sOnBehalfOf = \UserRights::GetUserFriendlyName();
$sDate = date(\AttributeDateTime::GetInternalFormat());
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$sExpectedLog = $sSeparator."<p>$sLog</p>";
$aExpectedIndex = [
[
'user_name' => $sOnBehalfOf,
@@ -187,18 +196,56 @@ class ormCaseLogTest extends ItopDataTestCase
'format' => 'html',
]
];
$oOrmCaseLogService->expects($this->exactly(2))
->method('Rebuild')
->withConsecutive(['', []], [$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(
null,
new ormCaseLog($sRebuiltLog, $aRebuiltIndex)
);
//create a real service with no extension to speed up treatment and avoid +1s delta in history diff
$oOrmCaseLogService = new \ormCaseLogService([]);
$oLog = new ormCaseLog('', [], $oOrmCaseLogService);
$oLog->AddLogEntry($sLog);
$oOrmCaseLogService = $this->createMock(\ormCaseLogService::class);
$oOrmCaseLogService->expects($this->exactly(1))
->method('Rebuild')
->withConsecutive([$sExpectedLog, $aExpectedIndex])
->willReturnOnConsecutiveCalls(new ormCaseLog($sRebuiltLog, $aRebuiltIndex));
$this->SetNonPublicProperty($oLog, 'oOrmCaseLogService', $oOrmCaseLogService);
if ($bTestAddLogEntry){
$oLog->AddLogEntry($sLog);
} else {
$oLog->AddLogEntryFromJSON($oJson, false);
}
$this->assertEquals($sRebuiltLog, $oLog->GetText());
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
}
public function RebuildThroughApplicationExtensionImplementation(){
return [
'caselog is declared as modified by iOrmCaseLogExtension' => [ true ],
'caselog is declared as untouched by iOrmCaseLogExtension' => [ false ],
];
}
/**
* @dataProvider RebuildThroughApplicationExtensionImplementation
*/
public function testRebuildThroughApplicationExtensionImplementation(bool $bTouched){
$sLog = "aaaaa";
$sRebuiltLog = "bbbb";
$aInitialIndex = ['a' => 'b'];
$aRebuiltIndex = ['c' => 'd'];
$aOrmCaseLogExtensionForTest = new \OrmCaseLogExtensionForTest();
$aOrmCaseLogExtensionForTest->Init($bTouched, $sRebuiltLog, $aRebuiltIndex);
$aOrmCaseLogExtension=[$aOrmCaseLogExtensionForTest];
$oOrmCaseLogService = new \ormCaseLogService($aOrmCaseLogExtension);
$oLog = new ormCaseLog($sLog, $aInitialIndex, $oOrmCaseLogService);
if ($bTouched){
$this->assertEquals($aRebuiltIndex, $oLog->GetIndex());
$this->assertEquals($sRebuiltLog, $oLog->GetText());
} else {
$this->assertEquals($aInitialIndex, $oLog->GetIndex());
$this->assertEquals($sLog, $oLog->GetText());
}
}
}