From ed6df77cbb55281c61969dea6c1100f2276f603a Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 20 Jul 2023 19:47:25 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B06097=20-=20Tests:=20Optimize=20performan?= =?UTF-8?q?ces=20by=20creating=20custom=20env.=20only=20once=20and=20re-us?= =?UTF-8?q?ing=20it=20across=20test=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/php-unit-tests/composer.json | 1 + .../ItopCustomDatamodelTestCase.php | 114 ++++++++++++------ .../src/BaseTestCase/ItopDataTestCase.php | 9 +- .../src/Hook/TestsRunStartHook.php | 63 ++++++++++ .../Service/UnitTestRunTimeEnvironment.php | 7 +- .../core/TestGLA/TestGLA2Test.delta.xml | 14 --- .../core/TestGLA/TestGLA2Test.php | 34 ------ .../core/TestGLA/TestGLATest.delta.xml | 14 --- .../core/TestGLA/TestGLATest.php | 34 ------ 9 files changed, 155 insertions(+), 135 deletions(-) create mode 100644 tests/php-unit-tests/src/Hook/TestsRunStartHook.php delete mode 100644 tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.delta.xml delete mode 100644 tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.php delete mode 100644 tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLATest.delta.xml delete mode 100644 tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLATest.php diff --git a/tests/php-unit-tests/composer.json b/tests/php-unit-tests/composer.json index 1386f61b9..fef0397e9 100644 --- a/tests/php-unit-tests/composer.json +++ b/tests/php-unit-tests/composer.json @@ -6,6 +6,7 @@ "autoload": { "psr-4": { "Combodo\\iTop\\Test\\UnitTest\\": "src/BaseTestCase/", + "Combodo\\iTop\\Test\\UnitTest\\Hook\\": "src/Hook/", "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 d10e609a6..91c335736 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopCustomDatamodelTestCase.php @@ -6,10 +6,13 @@ 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; @@ -17,38 +20,37 @@ use utils; /** * Class ItopCustomDatamodelTestCase * - * Helper class to extend for tests needing a custom DataModel access to iTop's metamodel + * 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 * - * **⚠ 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 + * @since N°6097 2.7.9 3.0.4 3.1.0 */ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase { /** - * @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. + * @var bool[] */ - protected static $bIsCustomEnvironmentReady = false; + protected static $aReadyCustomEnvironments = []; /** * @inheritDoc + * @since N°6097 Workaround to make the "runClassInSeparateProcess" directive work */ - protected function setUp(): void + public function __construct($name = null, array $data = [], $dataName = '') { - parent::setUp(); + parent::__construct($name, $data, $dataName); - $sLogFileAbsPath = APPROOT.'log/php_unit_tests_-_custom_datamodel_for_env_-_'.$this->GetTestEnvironment().'.log'; - IssueLog::Enable($sLogFileAbsPath); + // 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); } - /** * @return string Abs path to the XML delta to use for the tests of that class */ @@ -57,8 +59,10 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase /** * @inheritDoc */ - protected function LoadRequiredFiles(): void + protected function LoadRequiredItopFiles(): void { + parent::LoadRequiredItopFiles(); + $this->RequireOnceItopFile('setup/setuputils.class.inc.php'); $this->RequireOnceItopFile('setup/runtimeenv.class.inc.php'); } @@ -73,15 +77,57 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase /** * @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. + * @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. */ - final public function GetTestEnvironment(): string + 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 */ @@ -90,11 +136,11 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase $sSourceEnv = $this->GetSourceEnvironment(); $sTestEnv = $this->GetTestEnvironment(); - // Check if test env. if already set and only prepare it if it doesn't already exist + // Check if test env. is already set and only prepare it if it's not up-to-date // // 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) { + if (false === $this->IsEnvironmentReady()) { //---------------------------------------------------- // Clear any previous "$sTestEnv" environment //---------------------------------------------------- @@ -137,16 +183,16 @@ abstract class ItopCustomDatamodelTestCase extends ItopDataTestCase $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(); + // - 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); - static::$bIsCustomEnvironmentReady = true; + $this->MarkEnvironmentReady(); + $this->debug('Preparation of custom environment "'.$sTestEnv.'" done.'); } parent::PrepareEnvironment(); diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php index 77dabbef9..6b6aa550c 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -15,6 +15,7 @@ namespace Combodo\iTop\Test\UnitTest; use ArchivedObjectException; use CMDBSource; +use Config; use Contact; use DBObject; use DBObjectSet; @@ -63,6 +64,10 @@ abstract class ItopDataTestCase extends ItopTestCase // For cleanup private $aCreatedObjects = array(); + /** + * @var string Default environment to use for test cases + */ + const DEFAULT_TEST_ENVIRONMENT = 'production'; const USE_TRANSACTION = true; const CREATE_TEST_ORG = false; @@ -126,12 +131,12 @@ abstract class ItopDataTestCase extends ItopTestCase } /** - * @return string Environment in the test will run + * @return string Environment the test will run in * @since 2.7.9 3.0.4 3.1.0 */ protected function GetTestEnvironment(): string { - return 'production'; + return self::DEFAULT_TEST_ENVIRONMENT; } /** diff --git a/tests/php-unit-tests/src/Hook/TestsRunStartHook.php b/tests/php-unit-tests/src/Hook/TestsRunStartHook.php new file mode 100644 index 000000000..7d2f6c212 --- /dev/null +++ b/tests/php-unit-tests/src/Hook/TestsRunStartHook.php @@ -0,0 +1,63 @@ + + * @package Combodo\iTop\Test\UnitTest\Hook + * @since N°6097 2.7.10 3.0.4 3.1.1 + */ +class TestsRunStartHook implements BeforeFirstTestHook, AfterLastTestHook +{ + /** + * Use the modification time on this file to check whereas it is newer than the requirements in a test case + * + * @return string Abs. path to a file generated when the global tests run starts. + */ + public static function GetRunStartedFileAbsPath(): string + { + // Note: This can't be put in the cache- folder as we have multiple running across the test cases + // We also don't want to put it in the unit tests folder as it is not supposed to be writable + return APPROOT.'data/.php-unit-tests-run-started'; + } + + /** + * @inheritDoc + */ + public function executeBeforeFirstTest(): void + { + // Create / change modification timestamp of file marking the beginning of the tests run + touch(static::GetRunStartedFileAbsPath()); + } + + /** + * @inheritDoc + */ + public function executeAfterLastTest(): void + { + // Cleanup of file marking the beginning of the tests run + if (file_exists(static::GetRunStartedFileAbsPath())) { + unlink(static::GetRunStartedFileAbsPath()); + } + } + + +} \ No newline at end of file diff --git a/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php b/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php index d718893ce..63ded52c7 100644 --- a/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php +++ b/tests/php-unit-tests/src/Service/UnitTestRunTimeEnvironment.php @@ -9,7 +9,7 @@ namespace Combodo\iTop\Test\UnitTest\Service; use Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCase; use IssueLog; -use MFDeltaModule; +use MFCoreModule; use ReflectionClass; use RunTimeEnvironment; @@ -17,7 +17,7 @@ use RunTimeEnvironment; /** * Class UnitTestRunTimeEnvironment * - * Runtime env. dedicated to creating an temp. environment for a group of unit tests with XML deltas. + * Runtime env. dedicated to creating a temp. environment for a group of unit tests with XML deltas. * * @author Guillaume Lajarige * @since N°6097 2.7.10 3.0.4 3.1.1 @@ -68,7 +68,8 @@ class UnitTestRunTimeEnvironment extends RunTimeEnvironment // Prepare fake module name for delta $sDeltaName = preg_replace('/[^\d\w]/', '', $sDeltaFile); - $oDelta = new MFDeltaModule($sDeltaFile); + // Note: We can't use \MFDeltaModule as we can't specify the ID which leads to only 1 delta being applied... In the future we might introduce a new MFXXXModule, but in the meantime it feels alright (GLA / RQU) + $oDelta = new MFCoreModule($sDeltaName, $sDeltaName, $sDeltaFile); IssueLog::Debug('XML delta found for unit tests', static::class, [ 'Unit test class' => $sClass, diff --git a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.delta.xml b/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.delta.xml deleted file mode 100644 index b9a98184d..000000000 --- a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.delta.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - tested_attribute2 - - true - - - - - diff --git a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.php b/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.php deleted file mode 100644 index 7173aff4d..000000000 --- a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLA2Test.php +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - tested_attribute - - true - - - - - diff --git a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLATest.php b/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLATest.php deleted file mode 100644 index 7f0eea95a..000000000 --- a/tests/php-unit-tests/unitary-tests/core/TestGLA/TestGLATest.php +++ /dev/null @@ -1,34 +0,0 @@ -