Optimize tests execution time (no need for process isolation as long as we leave the premises clean)

This commit is contained in:
Romain Quetiez
2023-10-26 21:23:47 +02:00
parent 442721bcb5
commit 798cd10d6b
2 changed files with 49 additions and 22 deletions

View File

@@ -30,9 +30,6 @@ use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Dict; use Dict;
/**
* @runTestsInSeparateProcesses
*/
class dictApcuTest extends ItopTestCase class dictApcuTest extends ItopTestCase
{ {
private $sEnvName; private $sEnvName;
@@ -45,9 +42,16 @@ class dictApcuTest extends ItopTestCase
$this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php'); $this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php');
$this->sEnvName = time(); // This id will be used as path to the dictionary files
// It must be unique enough for the magic of Dict to operate (due to the use of require_once to load dictionaries)
$this->sEnvName = uniqid();
$_SESSION['itop_env'] = $this->sEnvName; $_SESSION['itop_env'] = $this->sEnvName;
// Preserve the dictionary for test that will be executed later on
static::BackupStaticProperties('Dict');
// Reset and prepare the dictionary
static::SetNonPublicStaticProperty('Dict', 'm_aData', []);
$this->oApcService = $this->createMock(\ApcService::class); $this->oApcService = $this->createMock(\ApcService::class);
Dict::SetApcService($this->oApcService); Dict::SetApcService($this->oApcService);
Dict::EnableCache('toto'); Dict::EnableCache('toto');
@@ -57,7 +61,8 @@ class dictApcuTest extends ItopTestCase
$this->InitDictionnaries(); $this->InitDictionnaries();
} }
private function InitDictionnaries(){ private function InitDictionnaries()
{
clearstatcache(); clearstatcache();
$this->sDictionaryFolder = APPROOT."env-$this->sEnvName" . DIRECTORY_SEPARATOR . "dictionaries"; $this->sDictionaryFolder = APPROOT."env-$this->sEnvName" . DIRECTORY_SEPARATOR . "dictionaries";
@mkdir($this->sDictionaryFolder, 0777, true); @mkdir($this->sDictionaryFolder, 0777, true);
@@ -82,7 +87,8 @@ STR;
clearstatcache(); clearstatcache();
} }
private function InitDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename, $sLabels){ private function InitDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename, $sLabels)
{
$sContent = <<<PHP $sContent = <<<PHP
<?php <?php
// //
@@ -95,7 +101,8 @@ PHP;
file_put_contents($sDictionaryFolder . DIRECTORY_SEPARATOR . "$sLanguageCodeInFilename.dict.php", $sContent); file_put_contents($sDictionaryFolder . DIRECTORY_SEPARATOR . "$sLanguageCodeInFilename.dict.php", $sContent);
} }
private function InitBrokenDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename){ private function InitBrokenDictionnary($sDictionaryFolder, $sLanguageCode, $sLanguageCodeInFilename)
{
$sContent = <<<PHP $sContent = <<<PHP
<?php <?php
// //
@@ -114,10 +121,13 @@ PHP;
rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries"); rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries");
rmdir(APPROOT."env-$this->sEnvName"); rmdir(APPROOT."env-$this->sEnvName");
static::RestoreStaticProperties('Dict');
parent::tearDown(); parent::tearDown();
} }
public function InitLangIfNeeded_NoApcProvider(){ public function InitLangIfNeeded_NoApcProvider()
{
return [ return [
'apcu mocked' => [ 'bApcuMocked' => true ], 'apcu mocked' => [ 'bApcuMocked' => true ],
'integration test - apcu service like in production - install php-apcu before' => [ 'bApcuMocked' => false ], 'integration test - apcu service like in production - install php-apcu before' => [ 'bApcuMocked' => false ],
@@ -127,7 +137,8 @@ PHP;
/** /**
* @dataProvider InitLangIfNeeded_NoApcProvider * @dataProvider InitLangIfNeeded_NoApcProvider
*/ */
public function testInitLangIfNeeded_NoApc($bApcuMocked){ public function testInitLangIfNeeded_NoApc($bApcuMocked)
{
if ($bApcuMocked) { if ($bApcuMocked) {
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
@@ -163,7 +174,8 @@ PHP;
$this->assertEquals('not_defined_label', Dict::S('not_defined_label')); $this->assertEquals('not_defined_label', Dict::S('not_defined_label'));
} }
public function testInitLangIfNeeded_Apc_LanguageMismatchDictionnary(){ public function testInitLangIfNeeded_Apc_LanguageMismatchDictionnary()
{
//language mismatch!! //language mismatch!!
$sLabels = <<<STR $sLabels = <<<STR
'label1' => 'de1', 'label1' => 'de1',
@@ -185,7 +197,8 @@ STR;
$this->assertEquals('label1', Dict::S('label1')); $this->assertEquals('label1', Dict::S('label1'));
} }
public function testInitLangIfNeeded_Apc_BrokenUserDictionnary(){ public function testInitLangIfNeeded_Apc_BrokenUserDictionnary()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
@@ -205,7 +218,8 @@ STR;
$this->assertEquals('ru1', Dict::S('label1')); $this->assertEquals('ru1', Dict::S('label1'));
} }
public function testInitLangIfNeeded_Apc_BrokenDictionnary_UserAndDefault(){ public function testInitLangIfNeeded_Apc_BrokenDictionnary_UserAndDefault()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru');
@@ -224,7 +238,8 @@ STR;
$this->assertEquals('en1', Dict::S('label1')); $this->assertEquals('en1', Dict::S('label1'));
} }
public function testInitLangIfNeeded_Apc_BrokenDictionnary_ALL(){ public function testInitLangIfNeeded_Apc_BrokenDictionnary_ALL()
{
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'DE DE', 'de-de');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'RU RU', 'ru-ru');
$this->InitBrokenDictionnary($this->sDictionaryFolder, 'EN US', 'en-us'); $this->InitBrokenDictionnary($this->sDictionaryFolder, 'EN US', 'en-us');
@@ -244,7 +259,8 @@ STR;
$this->assertEquals('label1', Dict::S('label1')); $this->assertEquals('label1', Dict::S('label1'));
} }
public function testInitLangIfNeeded_ApcFromCache_PropertyInUserDictionnary(){ public function testInitLangIfNeeded_ApcFromCache_PropertyInUserDictionnary()
{
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
->willReturn(true); ->willReturn(true);
@@ -262,7 +278,8 @@ STR;
$this->assertEquals('fr1', Dict::S('label1')); $this->assertEquals('fr1', Dict::S('label1'));
} }
public function testInitLangIfNeeded_ApcStore_PropertyInUserDictionnary(){ public function testInitLangIfNeeded_ApcStore_PropertyInUserDictionnary()
{
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
->willReturn(true); ->willReturn(true);
@@ -341,7 +358,8 @@ STR;
$this->assertEquals('en2', Dict::S('label2')); $this->assertEquals('en2', Dict::S('label2'));
} }
public function testInitLangIfNeeded_Apc_PropertyInDictDefaultLanguageDictionnary(){ public function testInitLangIfNeeded_Apc_PropertyInDictDefaultLanguageDictionnary()
{
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
->willReturn(true); ->willReturn(true);
@@ -362,7 +380,8 @@ STR;
$this->assertEquals('en3', Dict::S('label3')); $this->assertEquals('en3', Dict::S('label3'));
} }
public function testInitLangIfNeeded_ApcCorrupted_PropertyInDictDefaultLanguageDictionnary(){ public function testInitLangIfNeeded_ApcCorrupted_PropertyInDictDefaultLanguageDictionnary()
{
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
->willReturn(true); ->willReturn(true);
@@ -380,7 +399,8 @@ STR;
$this->assertEquals('label3', Dict::S('label3')); $this->assertEquals('label3', Dict::S('label3'));
} }
public function testInitLangIfNeeded_Apc_PropertyNotFound(){ public function testInitLangIfNeeded_Apc_PropertyNotFound()
{
$this->oApcService->expects($this->any()) $this->oApcService->expects($this->any())
->method('function_exists') ->method('function_exists')
->willReturn(true); ->willReturn(true);

View File

@@ -30,9 +30,6 @@ use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Dict; use Dict;
use Exception; use Exception;
/**
* @runClassInSeparateProcess
*/
class dictTest extends ItopTestCase class dictTest extends ItopTestCase
{ {
private $sEnvName; private $sEnvName;
@@ -42,7 +39,9 @@ class dictTest extends ItopTestCase
$this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php'); $this->RequireOnceItopFile('core'.DIRECTORY_SEPARATOR.'apc-service.class.inc.php');
$this->sEnvName = time(); // This id will be used as path to the dictionary files
// It must be unique enough for the magic of Dict to operate (due to the use of require_once to load dictionaries)
$this->sEnvName = uniqid();
$sDictionaryFolder = APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries"; $sDictionaryFolder = APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries";
@mkdir($sDictionaryFolder, 0777, true); @mkdir($sDictionaryFolder, 0777, true);
@@ -68,6 +67,12 @@ PHP;
file_put_contents($sDictionaryFolder.DIRECTORY_SEPARATOR."en-en.dict.php", $sContent); file_put_contents($sDictionaryFolder.DIRECTORY_SEPARATOR."en-en.dict.php", $sContent);
$_SESSION['itop_env'] = $this->sEnvName; $_SESSION['itop_env'] = $this->sEnvName;
// Preserve the dictionary for test that will be executed later on
static::BackupStaticProperties('Dict');
// Reset the dictionary
static::SetNonPublicStaticProperty('Dict', 'm_aData', []);
} }
protected function tearDown(): void protected function tearDown(): void
@@ -78,6 +83,8 @@ PHP;
rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries"); rmdir(APPROOT."env-$this->sEnvName".DIRECTORY_SEPARATOR."dictionaries");
rmdir(APPROOT."env-$this->sEnvName"); rmdir(APPROOT."env-$this->sEnvName");
static::RestoreStaticProperties('Dict');
parent::tearDown(); parent::tearDown();
} }