diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php index 7b77ab91a..821c4839a 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php @@ -15,6 +15,8 @@ use const DEBUG_BACKTRACE_IGNORE_ARGS; define('DEBUG_UNIT_TEST', true); /** + * Class ItopTestCase + * * Helper class to extend for tests that DO NOT need to access the DataModel or the Database * * @since 3.0.4 3.1.1 3.2.0 N°6658 move some setUp/tearDown code to the corresponding methods *BeforeClass to speed up tests process time. @@ -23,6 +25,7 @@ abstract class ItopTestCase extends TestCase { public const TEST_LOG_DIR = 'test'; public static $DEBUG_UNIT_TEST = false; + protected static $aBackupStaticProperties = []; /** * @link https://docs.phpunit.de/en/9.6/annotations.html#preserveglobalstate PHPUnit `preserveGlobalState` annotation documentation @@ -309,6 +312,42 @@ abstract class ItopTestCase extends TestCase return $oProperty->getValue($oObject); } + /** + * Backup every static property of the class (even protected ones) + * @param string $sClass + * + * @return void + * + * @since 3.2.0 + */ + public static function BackupStaticProperties($sClass) + { + $class = new \ReflectionClass($sClass); + foreach ($class->getProperties() as $property) { + if (!$property->isStatic()) continue; + $property->setAccessible(true); + static::$aBackupStaticProperties[$sClass][$property->getName()] = $property->getValue(); + } + } + + /** + * Restore every static property of the class (even protected ones) + * @param string $sClass + * + * @return void + * + * @since 3.2.0 + */ + public static function RestoreStaticProperties($sClass) + { + $class = new \ReflectionClass($sClass); + foreach ($class->getProperties() as $property) { + if (!$property->isStatic()) continue; + $property->setAccessible(true); + $property->setValue(static::$aBackupStaticProperties[$sClass][$property->getName()]); + } + } + /** * @param string $sClass * @param string $sProperty @@ -355,22 +394,24 @@ abstract class ItopTestCase extends TestCase $oProperty->setValue($value); } - public function RecurseRmdir($dir) { + public static function RecurseRmdir($dir) + { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { - if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) - $this->RecurseRmdir($dir.DIRECTORY_SEPARATOR.$object); - else + if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { + static::RecurseRmdir($dir.DIRECTORY_SEPARATOR.$object); + } else { unlink($dir.DIRECTORY_SEPARATOR.$object); + } } } rmdir($dir); } } - public function CreateTmpdir() { + public static function CreateTmpdir() { $sTmpDir=tempnam(sys_get_temp_dir(),''); if (file_exists($sTmpDir)) { @@ -410,13 +451,13 @@ abstract class ItopTestCase extends TestCase } - public function RecurseCopy($src,$dst) { + public static function RecurseCopy($src,$dst) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($src . '/' . $file) ) { - $this->RecurseCopy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file); + static::RecurseCopy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file); } else { copy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file); diff --git a/tests/php-unit-tests/unitary-tests/application/applicationextension/ApplicationExtensionTest.php b/tests/php-unit-tests/unitary-tests/application/applicationextension/ApplicationExtensionTest.php index 58dc826df..6a6fd3545 100644 --- a/tests/php-unit-tests/unitary-tests/application/applicationextension/ApplicationExtensionTest.php +++ b/tests/php-unit-tests/unitary-tests/application/applicationextension/ApplicationExtensionTest.php @@ -36,20 +36,18 @@ class ApplicationExtensionTest extends ItopCustomDatamodelTestCase * - Add the API to the provider * - Add a class extending / implementing the API in ./Delta/application-extension-usages-in-snippets.xml * - * @param string $sAPIFQCN - * @param string $sCallMethod - * * @return void - * @dataProvider ExtensionAPIRegisteredAndCalledProvider */ - public function testExtensionAPIRegisteredAndCalled(string $sAPIFQCN, string $sCallMethod) + public function testExtensionAPIRegisteredAndCalled() { - if ($sCallMethod === static::ENUM_API_CALL_METHOD_ENUMPLUGINS) { - $iExtendingClassesCount = count(MetaModel::EnumPlugins($sAPIFQCN)); - } else { - $iExtendingClassesCount = count(utils::GetClassesForInterface($sAPIFQCN, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'])); + foreach ($this->ExtensionAPIRegisteredAndCalledProvider() as list($sAPIFQCN, $sCallMethod)) { + if ($sCallMethod === static::ENUM_API_CALL_METHOD_ENUMPLUGINS) { + $iExtendingClassesCount = count(MetaModel::EnumPlugins($sAPIFQCN)); + } else { + $iExtendingClassesCount = count(utils::GetClassesForInterface($sAPIFQCN, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'])); + } + $this->assertGreaterThan(0, $iExtendingClassesCount, "Found no class extending the $sAPIFQCN API"); } - $this->assertGreaterThan(0, $iExtendingClassesCount, "Found no class extending the $sAPIFQCN API"); } public function ExtensionAPIRegisteredAndCalledProvider(): array