N°6658 - Boost PHPUnit tests execution

This commit is contained in:
Romain Quetiez
2023-08-04 14:06:44 +02:00
parent d951d3b872
commit 65bb76b9e3
74 changed files with 690 additions and 416 deletions

View File

@@ -24,23 +24,53 @@ define('DEBUG_UNIT_TEST', true);
abstract class ItopTestCase extends TestCase
{
public const TEST_LOG_DIR = 'test';
public static $DEBUG_UNIT_TEST = false;
/**
* Override the default value to disable the backup of globals in case of tests run in a separate process
*/
protected $preserveGlobalState = false;
/**
* This method is called before the first test of this test class is run (in the current process).
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::$DEBUG_UNIT_TEST = getenv('DEBUG_UNIT_TEST');
require_once static::GetAppRoot() . 'approot.inc.php';
if (false === defined('ITOP_PHPUNIT_RUNNING_CONSTANT_NAME')) {
// setUp might be called multiple times, so protecting the define() call !
define('ITOP_PHPUNIT_RUNNING_CONSTANT_NAME', true);
}
}
/**
* This method is called after the last test of this test class is run (in the current process).
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
if (method_exists('utils', 'GetConfig')) {
// Reset the config by forcing the load from disk
$oConfig = \utils::GetConfig(true);
if (method_exists('MetaModel', 'SetConfig')) {
\MetaModel::SetConfig($oConfig);
}
}
if (method_exists('Dict', 'SetUserLanguage')) {
\Dict::SetUserLanguage();
}
}
protected function setUp(): void {
$sAppRootRelPath = 'approot.inc.php';
$sDepthSeparator = '../';
for ($iDepth = 0; $iDepth < 8; $iDepth++) {
if (file_exists($sAppRootRelPath)) {
require_once $sAppRootRelPath;
break;
}
parent::setUp();
$sAppRootRelPath = $sDepthSeparator.$sAppRootRelPath;
}
if (false === defined(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME)) {
// setUp might be called multiple times, so protecting the define() call !
define(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME, true);
}
$this->debug("\n----------\n---------- ".$this->getName()."\n----------\n");
$this->LoadRequiredItopFiles();
$this->LoadRequiredTestFiles();
@@ -56,10 +86,38 @@ abstract class ItopTestCase extends TestCase
if (CMDBSource::IsInsideTransaction()) {
// Nested transactions were opened but not finished !
// Rollback to avoid side effects on next tests
while (CMDBSource::IsInsideTransaction()) {
CMDBSource::Query('ROLLBACK');
}
throw new MySQLTransactionNotClosedException('Some DB transactions were opened but not closed ! Fix the code by adding ROLLBACK or COMMIT statements !', []);
}
}
/** Helper than can be called in the context of a data provider */
public static function GetAppRoot()
{
if (defined('APPROOT')) {
return APPROOT;
}
$sSearchPath = __DIR__;
for ($iDepth = 0; $iDepth < 8; $iDepth++) {
if (file_exists($sSearchPath.'/approot.inc.php')) {
break;
}
$iOffsetSep = strrpos($sSearchPath, '/');
if ($iOffsetSep === false) {
$iOffsetSep = strrpos($sSearchPath, '\\');
if ($iOffsetSep === false) {
// Do not throw an exception here as PHPUnit will not show it clearly when determing the list of test to perform
return 'Could not find the approot file in '.$sSearchPath;
}
}
$sSearchPath = substr($sSearchPath, 0, $iOffsetSep);
}
return $sSearchPath.'/';
}
/**
* Overload this method to require necessary files through {@see \Combodo\iTop\Test\UnitTest\ItopTestCase::RequireOnceItopFile()}
*
@@ -93,7 +151,7 @@ abstract class ItopTestCase extends TestCase
*/
protected function RequireOnceItopFile(string $sFileRelPath): void
{
require_once APPROOT . $sFileRelPath;
require_once $this->GetAppRoot() . $sFileRelPath;
}
/**
@@ -161,7 +219,7 @@ abstract class ItopTestCase extends TestCase
/**
* @since 2.7.4 3.0.0
*/
public function InvokeNonPublicStaticMethod($sObjectClass, $sMethodName, $aArgs)
public function InvokeNonPublicStaticMethod($sObjectClass, $sMethodName, $aArgs = [])
{
return $this->InvokeNonPublicMethod($sObjectClass, $sMethodName, null, $aArgs);
}
@@ -178,7 +236,7 @@ abstract class ItopTestCase extends TestCase
*
* @since 2.7.4 3.0.0
*/
public function InvokeNonPublicMethod($sObjectClass, $sMethodName, $oObject, $aArgs)
public function InvokeNonPublicMethod($sObjectClass, $sMethodName, $oObject, $aArgs = [])
{
$class = new \ReflectionClass($sObjectClass);
$method = $class->getMethod($sMethodName);