From 0b26d450145353ba16bf6cec2af54bc067484d7c Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Wed, 25 Oct 2023 17:50:41 +0200 Subject: [PATCH 1/2] :white_check_mark: Prerequisites for boosting tests --- .../src/BaseTestCase/ItopTestCase.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php index cd5bcb0a5..583ed8e65 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php @@ -26,6 +26,8 @@ abstract class ItopTestCase extends TestCase { const TEST_LOG_DIR = 'test'; + protected static $aBackupStaticProperties = []; + /** @noinspection UsingInclusionOnceReturnValueInspection avoid errors for approot includes */ protected function setUp(): void { @@ -254,6 +256,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 @@ -300,4 +338,76 @@ abstract class ItopTestCase extends TestCase $oProperty->setValue($value); } + 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)) { + static::RecurseRmdir($dir.DIRECTORY_SEPARATOR.$object); + } else { + unlink($dir.DIRECTORY_SEPARATOR.$object); + } + } + } + rmdir($dir); + } + } + + public static function CreateTmpdir() { + $sTmpDir=tempnam(sys_get_temp_dir(),''); + if (file_exists($sTmpDir)) + { + unlink($sTmpDir); + } + mkdir($sTmpDir); + if (is_dir($sTmpDir)) + { + return $sTmpDir; + } + + return sys_get_temp_dir(); + } + + public function RecurseMkdir($sDir){ + if (strpos($sDir, DIRECTORY_SEPARATOR) === 0){ + $sPath = DIRECTORY_SEPARATOR; + } else { + $sPath = ""; + } + + foreach (explode(DIRECTORY_SEPARATOR, $sDir) as $sSubDir){ + if (($sSubDir === '..')) { + break; + } + + if (( trim($sSubDir) === '' ) || ( $sSubDir === '.' )) { + continue; + } + + $sPath .= $sSubDir . DIRECTORY_SEPARATOR; + if (!is_dir($sPath)) { + var_dump($sPath); + @mkdir($sPath); + } + } + + } + + public static function RecurseCopy($src,$dst) { + $dir = opendir($src); + @mkdir($dst); + while(false !== ( $file = readdir($dir)) ) { + if (( $file != '.' ) && ( $file != '..' )) { + if ( is_dir($src . '/' . $file) ) { + static::RecurseCopy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file); + } + else { + copy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file); + } + } + } + closedir($dir); + } } From 037dfe1df6b3af8a9df6b5e5926f38b638c51846 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Wed, 25 Oct 2023 17:51:12 +0200 Subject: [PATCH 2/2] :white_check_mark: Optimize tests execution time --- .../ApplicationExtensionTest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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 997a52258..e921b52f0 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