From 32140b360f4803f3b918780f6ac6ce71c6a91d80 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Mon, 29 Apr 2024 11:45:09 +0200 Subject: [PATCH] :white_check_mark: Cherry pick fixes from 59a955f4 --- tests/php-unit-tests/composer.json | 3 + .../Service/UnitTestRunTimeEnvironment.php | 63 +++++++++++++++++-- ...plication-extension-usages-in-snippets.xml | 2 +- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/tests/php-unit-tests/composer.json b/tests/php-unit-tests/composer.json index fef0397e9..36e0c7e44 100644 --- a/tests/php-unit-tests/composer.json +++ b/tests/php-unit-tests/composer.json @@ -4,6 +4,9 @@ "sempro/phpunit-pretty-print": "^1.4" }, "autoload": { + "classmap": [ + "unitary-tests/" + ], "psr-4": { "Combodo\\iTop\\Test\\UnitTest\\": "src/BaseTestCase/", "Combodo\\iTop\\Test\\UnitTest\\Hook\\": "src/Hook/", diff --git a/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php b/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php index 63ded52c7..0c76e727f 100644 --- a/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php +++ b/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php @@ -9,9 +9,11 @@ namespace Combodo\iTop\Test\UnitTest\Service; use Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCase; use IssueLog; +use LogChannels; use MFCoreModule; use ReflectionClass; use RunTimeEnvironment; +use utils; /** @@ -33,12 +35,15 @@ class UnitTestRunTimeEnvironment extends RunTimeEnvironment /** @var string[] $aDeltaFiles Referential of loaded deltas. Mostly to avoid duplicates. */ $aDeltaFiles = []; - foreach (get_declared_classes() as $sClass) { - // Filter on classes derived from this \Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCaseItopCustomDatamodelTestCase - if (false === is_a($sClass, ItopCustomDatamodelTestCase::class, true)) { - continue; - } - + $aRelatedClasses = $this->GetClassesExtending( + ItopCustomDatamodelTestCase::class, + array( + '[\\\\/]tests[\\\\/]php-unit-tests[\\\\/]vendor[\\\\/]', + '[\\\\/]tests[\\\\/]php-unit-tests[\\\\/]unitary-tests[\\\\/]datamodels[\\\\/]2.x[\\\\/]authent-local', + )); + //Combodo\iTop\Test\UnitTest\Application\ApplicationExtensionTest + //Combodo\iTop\Test\UnitTest\Application\ApplicationExtensionTest + foreach ($aRelatedClasses as $sClass) { $oReflectionClass = new ReflectionClass($sClass); $oReflectionMethod = $oReflectionClass->getMethod('GetDatamodelDeltaAbsPath'); @@ -83,4 +88,50 @@ class UnitTestRunTimeEnvironment extends RunTimeEnvironment return $aRet; } + protected function GetClassesExtending (string $sExtendedClass, array $aExcludedPath = []) : array { + $aMatchingClasses = []; + + $aAutoloadClassMaps =[__DIR__."/../../vendor/composer/autoload_classmap.php"]; + + $aClassMap = []; + $aAutoloaderErrors = []; + foreach ($aAutoloadClassMaps as $sAutoloadFile) { + $aTmpClassMap = include $sAutoloadFile; + /** @noinspection SlowArrayOperationsInLoopInspection we are getting an associative array so the documented workarounds cannot be used */ + $aClassMap = array_merge($aClassMap, $aTmpClassMap); + } + foreach ($aClassMap as $sPHPClass => $sPHPFile) { + $bSkipped = false; + if (utils::IsNotNullOrEmptyString($sPHPFile)) { + $sPHPFile = utils::LocalPath($sPHPFile); + if ($sPHPFile !== false) { + $sPHPFile = '/'.$sPHPFile; // for regex + foreach ($aExcludedPath as $sExcludedPath) { + // Note: We use '#' as delimiters as usual '/' is often used in paths. + if ($sExcludedPath !== '' && preg_match('#'.$sExcludedPath.'#', $sPHPFile) === 1) { + $bSkipped = true; + break; + } + } + } else { + $bSkipped = true; // file not found + } + } + + if (!$bSkipped) { + try { + $oRefClass = new ReflectionClass($sPHPClass); + if ($oRefClass->isSubclassOf($sExtendedClass) && + !$oRefClass->isInterface() && !$oRefClass->isAbstract() && !$oRefClass->isTrait()) { + $aMatchingClasses[] = $sPHPClass; + } + } + catch (Exception $e) { + } + } + } + return $aMatchingClasses; + } + + } \ No newline at end of file diff --git a/tests/php-unit-tests/unitary-tests/application/applicationextension/Delta/application-extension-usages-in-snippets.xml b/tests/php-unit-tests/unitary-tests/application/applicationextension/Delta/application-extension-usages-in-snippets.xml index e404ac49d..d5af912f8 100644 --- a/tests/php-unit-tests/unitary-tests/application/applicationextension/Delta/application-extension-usages-in-snippets.xml +++ b/tests/php-unit-tests/unitary-tests/application/applicationextension/Delta/application-extension-usages-in-snippets.xml @@ -140,7 +140,7 @@ class ExampleFor_iQueryModifier implements \iQueryModifier public function GetFieldExpression(QueryBuilderContext &$oBuild, $sClass, $sAttCode, $sColId, Expression $oFieldSQLExp, SQLQuery &$oSelect) { - // Do nothing, we just need the class to exists for the unit test + return $oFieldSQLExp; } } ]]>