From 6a7dbb06b00e63093606f3bd641dc2e6e72ea16e Mon Sep 17 00:00:00 2001 From: Molkobain Date: Fri, 6 Jan 2023 17:47:58 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B05608=20-=20Add=20methods=20to=20require?= =?UTF-8?q?=5Fonce=20an=20iTop=20or=20a=20unit=20test=20file=20to=20avoid?= =?UTF-8?q?=20crashes=20when=20tests=20dir=20is=20moved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/php-unit-tests/ItopTestCase.php | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/php-unit-tests/ItopTestCase.php b/tests/php-unit-tests/ItopTestCase.php index afc7c8a28..94407524d 100644 --- a/tests/php-unit-tests/ItopTestCase.php +++ b/tests/php-unit-tests/ItopTestCase.php @@ -65,6 +65,37 @@ class ItopTestCase extends TestCase } } + /** + * Require once an iTop file (core or extension) from its relative path to the iTop root dir. + * This ensure to always use the right absolute path, especially in {@see \Combodo\iTop\Test\UnitTest\ItopTestCase::RequireOnceUnitTestFile()} + * + * @param string $sFileRelPath Rel. path (from iTop root dir) of the iTop file (core or extension) to require (eg. 'core/attributedef.class.inc.php' for /core/attributedef.class.inc.php) + * + * @return void + * @since 2.7.9 3.0.3 3.1.0 N°5608 Add method after PHPUnit directory moving + */ + protected function RequireOnceItopFile(string $sFileRelPath): void + { + require_once APPROOT . $sFileRelPath; + } + + /** + * Require once a unit test file (eg. a mock class) from its relative path from the *current* dir. + * This ensure that required files don't crash when unit tests dir is moved in the iTop structure (see N°5608) + * + * @param string $sFileRelPath Rel. path (from the *current* dir) of the unit test file to require (eg. './WeeklyScheduledProcessMockConfig.php' for /tests/php-unit-tests/unitary-tests/core/WeeklyScheduledProcessMockConfig.php in Combodo\iTop\Test\UnitTest\Core\WeeklyScheduledProcessTest) + * + * @return void + * @since 2.7.9 3.0.3 3.1.0 N°5608 Add method after PHPUnit directory moving + */ + protected function RequireOnceUnitTestFile(string $sFileRelPath): void + { + $aStack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + $sCallerDirAbsPath = dirname($aStack[0]['file']); + + require_once $sCallerDirAbsPath . DIRECTORY_SEPARATOR . $sFileRelPath; + } + protected function debug($sMsg) { if (DEBUG_UNIT_TEST) {