N°7847 - Extensions via interface not recognized

N°7803 - MTP from itophub/designer failing in itop 3.2.0
This commit is contained in:
Eric Espie
2024-10-10 17:59:50 +02:00
parent 3a4d9e64f8
commit 75520bfaf9
6 changed files with 243 additions and 68 deletions

View File

@@ -507,20 +507,38 @@ abstract class ItopTestCase extends TestCase
*
* @since 3.2.0
*/
protected function AssertArraysHaveSameItems(array $aExpectedClasses, array $aClasses, string $sMessage = ''): void
protected function AssertArraysHaveSameItems(array $aExpected, array $aActual, string $sMessage = ''): void
{
sort($aClasses);
sort($aExpectedClasses);
sort($aActual);
sort($aExpected);
$sExpected = implode("\n", $aExpectedClasses);
$sActual = implode("\n", $aClasses);
$sExpected = implode("\n", $aExpected);
$sActual = implode("\n", $aActual);
if ($sExpected === $sActual) {
$this->assertTrue(true);
return;
}
$sMessage .= "\nExpected:\n$sExpected\nActual:\n$sActual";
var_export($aClasses);
var_export($aActual);
$this->fail($sMessage);
}
/**
* The order of the files is not important
*
* @since 3.2.1
*/
public function AssertDirectoryListingEquals(array $aExpected, string $sDir, string $sMessage = ''): void
{
$aFiles = [];
foreach (scandir($sDir) as $sFile) {
if ($sFile !== '.' && $sFile !== '..') {
$aFiles[] = basename($sFile);
}
}
$this->AssertArraysHaveSameItems($aExpected, $aFiles, $sMessage);
}
}

View File

@@ -4,7 +4,6 @@ namespace Combodo\iTop\Test\UnitTest\Service\Cache;
use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Exception;
class DataModelDependantCacheTest extends ItopTestCase
{
@@ -19,12 +18,12 @@ class DataModelDependantCacheTest extends ItopTestCase
$this->sCacheRootDir = self::CreateTmpdir();
$this->oCacheService = DataModelDependantCache::GetInstance();
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', $this->sCacheRootDir);
$this->oCacheService->SetStorageRootDir($this->sCacheRootDir);
}
protected function tearDown(): void
{
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', null);
$this->oCacheService->SetStorageRootDir(null);
self::RecurseRmdir($this->sCacheRootDir);
parent::tearDown();
@@ -50,7 +49,7 @@ class DataModelDependantCacheTest extends ItopTestCase
public function testShouldStoreInADirectoryRebuiltOnCompilation(): void
{
// Given the storage is reset to the default
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', null);
$this->oCacheService->SetStorageRootDir(null);
// Then
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', 'key']);

View File

@@ -3,22 +3,38 @@
namespace Combodo\iTop\Test\UnitTest\Service\InterfaceDiscovery;
use Combodo\iTop\Application\UI\Base\iUIBlockFactory;
use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use MetaModel;
class InterfaceDiscoveryTest extends ItopDataTestCase
{
private InterfaceDiscovery $oInterfaceDiscovery;
private string $sCacheRootDir;
private DataModelDependantCache $oCacheService;
protected function setUp(): void
{
parent::setUp();
$this->oInterfaceDiscovery = InterfaceDiscovery::GetInstance();
$this->sCacheRootDir = self::CreateTmpdir();
$this->oCacheService = DataModelDependantCache::GetInstance();
$this->oCacheService->SetStorageRootDir($this->sCacheRootDir);
$this->oInterfaceDiscovery->SetCacheService($this->oCacheService);
}
protected function tearDown(): void
{
$this->SetNonPublicProperty(InterfaceDiscovery::GetInstance(), 'aForcedClassMap', null);
$this->oCacheService->SetStorageRootDir(null);
self::RecurseRmdir($this->sCacheRootDir);
parent::tearDown();
}
public function testShouldSelectTheRequestedItopClasses()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
$this->GivenClassMap([
'Combodo\iTop\Application\UI\Base\Component\Alert\Alert' => APPROOT . '/sources/Application/UI/Base/Component/Alert/Alert.php',
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/ButtonGroup/ButtonGroupUIBlockFactory.php',
@@ -29,29 +45,26 @@ class InterfaceDiscoveryTest extends ItopDataTestCase
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory',
],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class)
$this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)
);
}
public function testShouldExcludeSpecifiedDirectories()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
$this->GivenClassMap([
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/ButtonGroup/ButtonGroupUIBlockFactory.php',
]);
$this->AssertArraysHaveSameItems(
[],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class, ['Component/ButtonGroup', '/Alert/'])
$this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class, ['Component/ButtonGroup', '/Alert/'])
);
}
public function testShouldExcludeAliases()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
$this->GivenClassMap([
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'AlbertIsBlockingTheFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
]);
@@ -62,12 +75,48 @@ class InterfaceDiscoveryTest extends ItopDataTestCase
[
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class)
$this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)
);
}
private function GivenClassMap(InterfaceDiscovery $oInterfaceDiscoveryService, array $aClassMap): void
public function testShouldNotProduceCacheForDevelopers()
{
$this->SetNonPublicProperty($oInterfaceDiscoveryService, 'aForcedClassMap', $aClassMap);
DataModelDependantCache::GetInstance()->Clear('InterfaceDiscovery');
MetaModel::GetConfig()->Set('developer_mode.enabled', true);
MetaModel::GetConfig()->Set('developer_mode.interface_cache.enabled', false);
$this->assertGreaterThan(0, count($this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)));
$this->assertFileDoesNotExist($this->sCacheRootDir.'/InterfaceDiscovery');
}
public function testShouldProduceDynamicCacheForDevelopersWillingTo()
{
DataModelDependantCache::GetInstance()->Clear('InterfaceDiscovery');
MetaModel::GetConfig()->Set('developer_mode.enabled', true);
MetaModel::GetConfig()->Set('developer_mode.interface_cache.enabled', true);
$this->assertGreaterThan(0, count($this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)));
$this->AssertDirectoryListingEquals([
'autoload_classmaps.php',
'310db363d8e32bfcf57cbb3800912ea2_iUIBlockFactory.php'
],
$this->sCacheRootDir.'/InterfaceDiscovery');
}
public function testShouldProduceStaticCacheForProduction()
{
DataModelDependantCache::GetInstance()->Clear('InterfaceDiscovery');
MetaModel::GetConfig()->Set('developer_mode.enabled', false);
$this->assertGreaterThan(0, count($this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)));
$this->AssertDirectoryListingEquals(['310db363d8e32bfcf57cbb3800912ea2_iUIBlockFactory.php'], $this->sCacheRootDir.'/InterfaceDiscovery');
}
private function GivenClassMap(array $aClassMap): void
{
$this->SetNonPublicProperty($this->oInterfaceDiscovery, 'aForcedClassMap', $aClassMap);
}
}