From 1ad28312ecabc30447b9606d84dde9afaa2565fc Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 20 Jul 2023 17:37:11 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B06097=20-=20Tests:=20Introduce=20autoload?= =?UTF-8?q?er=20for=20"utility"=20classes=20and=20move=20them=20to=20a=20s?= =?UTF-8?q?ub-folder=20for=20better=20organization=20as=20folder=20was=20s?= =?UTF-8?q?till=20messy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note that unittestautoload.php is now useless. We just keep for now until everything is migrated (projects / branches / modules) --- .../ItopCustomDatamodelTestCase.php | 152 ------------------ tests/php-unit-tests/composer.json | 6 + .../ItopCustomDatamodelTestCase.php | 116 ++++--------- .../BaseTestCase}/ItopDataTestCase.php | 21 +-- .../{ => src/BaseTestCase}/ItopTestCase.php | 19 +-- .../Service}/UnitTestRunTimeEnvironment.php | 6 + tests/php-unit-tests/unittestautoload.php | 9 +- 7 files changed, 57 insertions(+), 272 deletions(-) delete mode 100644 tests/php-unit-tests/ItopCustomDatamodelTestCase.php rename tests/php-unit-tests/{ => src/BaseTestCase}/ItopDataTestCase.php (96%) rename tests/php-unit-tests/{ => src/BaseTestCase}/ItopTestCase.php (90%) rename tests/php-unit-tests/{ => src/Service}/UnitTestRunTimeEnvironment.php (95%) diff --git a/tests/php-unit-tests/ItopCustomDatamodelTestCase.php b/tests/php-unit-tests/ItopCustomDatamodelTestCase.php deleted file mode 100644 index 6eff53a52..000000000 --- a/tests/php-unit-tests/ItopCustomDatamodelTestCase.php +++ /dev/null @@ -1,152 +0,0 @@ -GetTestEnvironment().'.log'; - IssueLog::Enable($sLogFileAbsPath); - } - - - /** - * @return string Abs path to the XML delta to use for the tests of that class - */ - abstract public function GetDatamodelDeltaAbsPath(): string; - - /** - * @inheritDoc - */ - protected function LoadRequiredFiles(): void - { - $this->RequireOnceItopFile('setup/setuputils.class.inc.php'); - $this->RequireOnceItopFile('setup/runtimeenv.class.inc.php'); - $this->RequireOnceUnitTestFile('UnitTestRunTimeEnvironment.php'); - } - - /** - * @return string Environment used as a base (conf. file, modules, DB, ...) to prepare the test environment - */ - protected function GetSourceEnvironment(): string - { - return 'production'; - } - - /** - * @inheritDoc - * - * This is final for now as we don't support yet to have several environments in // to test incompatible DMs / deltas. - * When / if we do this, keep in mind that should ONLY be overloaded if your test case XML deltas are NOT compatible with the others, as it will create / compile another environment, increasing the global test time. - */ - final public function GetTestEnvironment(): string - { - return 'php-unit-tests'; - } - - /** - * @inheritDoc - */ - protected function PrepareEnvironment(): void - { - $sSourceEnv = $this->GetSourceEnvironment(); - $sTestEnv = $this->GetTestEnvironment(); - - // Check if test env. if already set and only prepare it if it doesn't already exist - // - // Note: To improve performances, we compile all XML deltas from test cases derived from this class and make a single environment where everything will be ran at once. - // This requires XML deltas to be compatible, but it is a known and accepted trade-off. See PR #457 - if (false === static::$bIsCustomEnvironmentReady) { - //---------------------------------------------------- - // Clear any previous "$sTestEnv" environment - //---------------------------------------------------- - - // - Configuration file - $sConfFile = utils::GetConfigFilePath($sTestEnv); - $sConfFolder = dirname($sConfFile); - if (is_file($sConfFile)) { - chmod($sConfFile, 0777); - SetupUtils::tidydir($sConfFolder); - } - - // - Datamodel delta files - // - Cache folder - // - Compiled folder - // We don't need to clean them as they are already by the compilation - - // - Drop database - // We don't do that now, it will be done before re-creating the DB, once the metamodel is started - - //---------------------------------------------------- - // Prepare "$sTestEnv" environment - //---------------------------------------------------- - - // All the following is greatly inspired by the toolkit's sandbox script - // - Prepare config file - $oSourceConf = new Config(utils::GetConfigFilePath($sSourceEnv)); - if ($oSourceConf->Get('source_dir') === '') { - throw new Exception('Missing entry source_dir from the config file'); - } - - $oTestConfig = clone($oSourceConf); - $oTestConfig->ChangeModulesPath($sSourceEnv, $sTestEnv); - // - Switch DB name to a dedicated one so we don't mess with the original one - $sTestEnvSanitizedForDBName = preg_replace('/[^\d\w]/', '', $sTestEnv); - $oTestConfig->Set('db_name', $oTestConfig->Get('db_name').'_'.$sTestEnvSanitizedForDBName); - - // - Compile env. based on the existing 'production' env. - $oEnvironment = new UnitTestRunTimeEnvironment($sTestEnv); - $oEnvironment->WriteConfigFileSafe($oTestConfig); - $oEnvironment->CompileFrom($sSourceEnv, false); - - // - Force re-creating of the DB -// // TODO: Create tmp DB - // But how to use it now when the metamodel is not started yet ?? -// MetaModel::LoadConfig($oTestConfig); -// if (MetaModel::DBExists()) { -// MetaModel::DBDrop(); -// } -// MetaModel::DBCreate(); - - static::$bIsCustomEnvironmentReady = true; - } - - parent::PrepareEnvironment(); - } -} diff --git a/tests/php-unit-tests/composer.json b/tests/php-unit-tests/composer.json index 61b516347..1386f61b9 100644 --- a/tests/php-unit-tests/composer.json +++ b/tests/php-unit-tests/composer.json @@ -2,5 +2,11 @@ "require-dev": { "phpunit/phpunit": "^8.5.23", "sempro/phpunit-pretty-print": "^1.4" + }, + "autoload": { + "psr-4": { + "Combodo\\iTop\\Test\\UnitTest\\": "src/BaseTestCase/", + "Combodo\\iTop\\Test\\UnitTest\\Service\\": "src/Service/" + } } } diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php index 093aab38d..d10e609a6 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php @@ -6,13 +6,10 @@ namespace Combodo\iTop\Test\UnitTest; -use CMDBSource; -use Combodo\iTop\Test\UnitTest\Hook\TestsRunStartHook; use Combodo\iTop\Test\UnitTest\Service\UnitTestRunTimeEnvironment; use Config; use Exception; use IssueLog; -use MetaModel; use SetupUtils; use utils; @@ -20,37 +17,38 @@ use utils; /** * Class ItopCustomDatamodelTestCase * - * Helper class to extend for tests needing a custom DataModel (eg. classes, attributes, etc conditions not available in the standard DM) - * Usage: - * - Create a test case class extending this one - * - Override the {@see ItopCustomDatamodelTestCase::GetDatamodelDeltaAbsPath()} method to define where you XML delta is - * - Implement your test case methods as usual + * Helper class to extend for tests needing a custom DataModel access to iTop's metamodel * - * @since N°6097 2.7.9 3.0.4 3.1.0 + * **⚠ Warning** Each class extending this one needs to NOT have @runTestsInSeparateProcesses annotation; otherwise the test env. will be re-compiled each time. + * + * @runTestsInSeparateProcesseszzz + * @preserveGlobalState disabled + * @backupGlobals disabled + * + * @since 2.7.9 3.0.4 3.1.0 */ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase { /** - * @var bool[] + * @var bool + * @since N°6097 2.7.10 3.0.4 3.1.1 3.2.0 + * + * @note If we change this to an array (with {@see \Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCase::GetTestEnvironment()} as the key), we could eventually have several environments in // to test incompatible DMs / deltas. */ - protected static $aReadyCustomEnvironments = []; + protected static $bIsCustomEnvironmentReady = false; /** * @inheritDoc - * @since N°6097 Workaround to make the "runClassInSeparateProcess" directive work */ - public function __construct($name = null, array $data = [], $dataName = '') + protected function setUp(): void { - parent::__construct($name, $data, $dataName); + parent::setUp(); - // Ensure that a test class derived from this one runs in a dedicated process as it changes the MetaModel / environment on the fly and - // for now we have no way of switching environments properly in memory and it will result in other (regular) test classes to fail as they won't be on the expected environment. - // - // If we don't do this, we would have to add the `@runTestsInSeparateProcesses` on *each* test classes which we want to avoid for obvious possible mistakes. - // Note that the `@runClassInSeparateProcess` don't work in PHPUnit yet. - $this->setRunClassInSeparateProcess(true); + $sLogFileAbsPath = APPROOT.'log/php_unit_tests_-_custom_datamodel_for_env_-_'.$this->GetTestEnvironment().'.log'; + IssueLog::Enable($sLogFileAbsPath); } + /** * @return string Abs path to the XML delta to use for the tests of that class */ @@ -59,10 +57,8 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase /** * @inheritDoc */ - protected function LoadRequiredItopFiles(): void + protected function LoadRequiredFiles(): void { - parent::LoadRequiredItopFiles(); - $this->RequireOnceItopFile('setup/setuputils.class.inc.php'); $this->RequireOnceItopFile('setup/runtimeenv.class.inc.php'); } @@ -72,62 +68,20 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase */ protected function GetSourceEnvironment(): string { - return static::DEFAULT_TEST_ENVIRONMENT; + return 'production'; } /** * @inheritDoc - * @warning This should ONLY be overloaded if your test case XML deltas are NOT compatible with the others, as it will create / compile another environment, increasing the global testing time. + * + * This is final for now as we don't support yet to have several environments in // to test incompatible DMs / deltas. + * When / if we do this, keep in mind that should ONLY be overloaded if your test case XML deltas are NOT compatible with the others, as it will create / compile another environment, increasing the global test time. */ - public function GetTestEnvironment(): string + final public function GetTestEnvironment(): string { return 'php-unit-tests'; } - /** - * @return string Absolute path to the {@see \Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCase::GetTestEnvironment()} folder - */ - final private function GetTestEnvironmentFolderAbsPath(): string - { - return APPROOT.'env-'.$this->GetTestEnvironment().'/'; - } - - /** - * Mark {@see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::GetTestEnvironment()} as ready (compiled) - * - * @return void - */ - final private function MarkEnvironmentReady(): void - { - if (false === $this->IsEnvironmentReady()) { - touch(static::GetTestEnvironmentFolderAbsPath()); - } - } - - /** - * @return bool True if the {@see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::GetTestEnvironment()} is ready (compiled, but not started) - * - * @details Having the environment ready means that it has been compiled for this global tests run, not that it is a relic from a previous global tests run - */ - final private function IsEnvironmentReady(): bool - { - // As these test cases run in separate processes, the best way we found to let know a process if its environment was already prepared for **this run** was to compare the modification times of: - // - its own env- folder - // - a file generated at the beginning of the global test run {@see \Combodo\iTop\Test\UnitTest\Hook\TestsRunStartHook} - $sRunStartedFilePath = TestsRunStartHook::GetRunStartedFileAbsPath(); - $sEnvFolderPath = static::GetTestEnvironmentFolderAbsPath(); - - clearstatcache(); - if (false === file_exists($sRunStartedFilePath) || false === file_exists($sEnvFolderPath)) { - return false; - } - - $iRunStartedFileModificationTime = filemtime($sRunStartedFilePath); - $iEnvFolderModificationTime = filemtime($sEnvFolderPath); - - return $iEnvFolderModificationTime >= $iRunStartedFileModificationTime; - } - /** * @inheritDoc */ @@ -136,11 +90,11 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase $sSourceEnv = $this->GetSourceEnvironment(); $sTestEnv = $this->GetTestEnvironment(); - // Check if test env. is already set and only prepare it if it's not up-to-date + // Check if test env. if already set and only prepare it if it doesn't already exist // // Note: To improve performances, we compile all XML deltas from test cases derived from this class and make a single environment where everything will be ran at once. // This requires XML deltas to be compatible, but it is a known and accepted trade-off. See PR #457 - if (false === $this->IsEnvironmentReady()) { + if (false === static::$bIsCustomEnvironmentReady) { //---------------------------------------------------- // Clear any previous "$sTestEnv" environment //---------------------------------------------------- @@ -183,16 +137,16 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase $oEnvironment->WriteConfigFileSafe($oTestConfig); $oEnvironment->CompileFrom($sSourceEnv, false); - // - Force re-creating a fresh DB - CMDBSource::InitFromConfig($oTestConfig); - if (CMDBSource::IsDB($oTestConfig->Get('db_name'))) { - CMDBSource::DropDB(); - } - CMDBSource::CreateDB($oTestConfig->Get('db_name')); - MetaModel::Startup($sConfFile, false /* $bModelOnly */, true /* $bAllowCache */, false /* $bTraceSourceFiles */, $sTestEnv); + // - Force re-creating of the DB +// // TODO: Create tmp DB + // But how to use it now when the metamodel is not started yet ?? +// MetaModel::LoadConfig($oTestConfig); +// if (MetaModel::DBExists()) { +// MetaModel::DBDrop(); +// } +// MetaModel::DBCreate(); - $this->MarkEnvironmentReady(); - $this->debug('Preparation of custom environment "'.$sTestEnv.'" done.'); + static::$bIsCustomEnvironmentReady = true; } parent::PrepareEnvironment(); diff --git a/tests/php-unit-tests/ItopDataTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php similarity index 96% rename from tests/php-unit-tests/ItopDataTestCase.php rename to tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php index c68e116db..77dabbef9 100644 --- a/tests/php-unit-tests/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -1,21 +1,8 @@ -// +/* + * @copyright Copyright (C) 2010-2023 Combodo SARL + * @license http://opensource.org/licenses/AGPL-3.0 + */ namespace Combodo\iTop\Test\UnitTest; diff --git a/tests/php-unit-tests/ItopTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php similarity index 90% rename from tests/php-unit-tests/ItopTestCase.php rename to tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php index 12eeeb434..3eaddccd9 100644 --- a/tests/php-unit-tests/ItopTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php @@ -1,20 +1,7 @@