Skip to content

Commit 6cb08ba

Browse files
authored
Add few APIs to ease tests (#788)
* add few APIs to ease tests * code style * log testname via IssueLog only through ItopDataTestCase * code style * ci: phpunit fix fatal error
1 parent f9db405 commit 6cb08ba

File tree

9 files changed

+140
-140
lines changed

9 files changed

+140
-140
lines changed

tests/php-static-analysis/config/php-includes/set-php-version-from-process.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
2+
23
/*
34
* @copyright Copyright (C) 2010-2023 Combodo SARL
45
* @license http://opensource.org/licenses/AGPL-3.0
56
*/
67

7-
declare(strict_types = 1);
8+
declare(strict_types=1);
89

910
/**
1011
* This file is only here to allow setting a specific PHP version to run the analysis for without
@@ -21,4 +22,4 @@
2122
$config = [];
2223
$config['parameters']['phpVersion'] = PHP_VERSION_ID;
2324

24-
return $config;
25+
return $config;

tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public function testCompileOperation()
2828
$iLastCompilation = filemtime(APPROOT.'env-production');
2929

3030
// When
31-
$sOutput = $this->CallItopUrl(
32-
"/pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
31+
$sOutput = $this->CallItopUri(
32+
"pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php",
3333
[
3434
'auth_user' => $sLogin,
3535
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
3636
'operation' => "compile",
3737
'authent' => self::AUTHENTICATION_TOKEN,
38-
]
38+
],
3939
);
4040

4141
// Then
@@ -53,26 +53,4 @@ public function testCompileOperation()
5353
clearstatcache();
5454
$this->assertGreaterThan($iLastCompilation, filemtime(APPROOT.'env-production'), 'The env-production directory should have been rebuilt');
5555
}
56-
57-
protected function CallItopUrl($sUri, ?array $aPostFields = null, bool $bXDebugEnabled = false)
58-
{
59-
$ch = curl_init();
60-
if ($bXDebugEnabled) {
61-
curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=phpstorm');
62-
}
63-
64-
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
65-
var_dump($sUrl);
66-
curl_setopt($ch, CURLOPT_URL, $sUrl);
67-
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
68-
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
69-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
70-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
71-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
72-
$sOutput = curl_exec($ch);
73-
//echo "$sUrl error code:".curl_error($ch);
74-
curl_close($ch);
75-
76-
return $sOutput;
77-
}
7856
}
Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (C) 2013-2024 Combodo SAS
45
*
@@ -24,72 +25,55 @@
2425
require_once('../../../approot.inc.php');
2526
require_once(APPROOT.'application/startup.inc.php');
2627

27-
2828
$sEnvironment = MetaModel::GetEnvironmentId();
29-
$aEntries = array();
29+
$aEntries = [];
3030
$aCacheUserData = apc_cache_info_compat();
31-
if (is_array($aCacheUserData) && isset($aCacheUserData['cache_list']))
32-
{
31+
if (is_array($aCacheUserData) && isset($aCacheUserData['cache_list'])) {
3332
$sPrefix = 'itop-'.$sEnvironment.'-query-cache-';
3433

35-
foreach($aCacheUserData['cache_list'] as $i => $aEntry)
36-
{
34+
foreach ($aCacheUserData['cache_list'] as $i => $aEntry) {
3735
$sEntryKey = array_key_exists('info', $aEntry) ? $aEntry['info'] : $aEntry['key'];
38-
if (strpos($sEntryKey, $sPrefix) === 0)
39-
{
36+
if (strpos($sEntryKey, $sPrefix) === 0) {
4037
$aEntries[] = $sEntryKey;
4138
}
4239
}
4340
}
4441

4542
echo "<pre>";
4643

47-
if (empty($aEntries))
48-
{
44+
if (empty($aEntries)) {
4945
echo "No Data";
5046
return;
5147
}
5248

5349
$sKey = $aEntries[0];
5450
$result = apc_fetch($sKey);
55-
if (!is_object($result))
56-
{
51+
if (!is_object($result)) {
5752
return;
5853
}
5954
$oSQLQuery = $result;
6055

6156
echo "NB Tables before;NB Tables after;";
62-
foreach($oSQLQuery->m_aContextData as $sField => $oValue)
63-
{
57+
foreach ($oSQLQuery->m_aContextData as $sField => $oValue) {
6458
echo $sField.';';
6559
}
6660
echo "\n";
6761

6862
sort($aEntries);
6963

70-
foreach($aEntries as $sKey)
71-
{
64+
foreach ($aEntries as $sKey) {
7265
$result = apc_fetch($sKey);
73-
if (is_object($result))
74-
{
66+
if (is_object($result)) {
7567
$oSQLQuery = $result;
76-
if (isset($oSQLQuery->m_aContextData))
77-
{
68+
if (isset($oSQLQuery->m_aContextData)) {
7869
echo $oSQLQuery->m_iOriginalTableCount.";".$oSQLQuery->CountTables().';';
79-
foreach($oSQLQuery->m_aContextData as $oValue)
80-
{
81-
if (is_array($oValue))
82-
{
70+
foreach ($oSQLQuery->m_aContextData as $oValue) {
71+
if (is_array($oValue)) {
8372
$sVal = json_encode($oValue);
84-
}
85-
else
86-
{
87-
if (empty($oValue))
88-
{
73+
} else {
74+
if (empty($oValue)) {
8975
$sVal = '';
90-
}
91-
else
92-
{
76+
} else {
9377
$sVal = $oValue;
9478
}
9579
}
@@ -101,4 +85,3 @@
10185
}
10286

10387
echo "</pre>";
104-

tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CMDBObject;
1919
use CMDBSource;
2020
use Combodo\iTop\Service\Events\EventService;
21+
use Config;
2122
use Contact;
2223
use DBObject;
2324
use DBObjectSet;
@@ -65,6 +66,9 @@ abstract class ItopDataTestCase extends ItopTestCase
6566
private $aCreatedObjects = [];
6667
private $aEventListeners = [];
6768

69+
protected ?string $sConfigTmpBackupFile = null;
70+
protected ?Config $oiTopConfig = null;
71+
6872
/**
6973
* @var bool When testing with silo, there are some cache we need to update on tearDown. Doing it all the time will cost too much, so it's opt-in !
7074
* @see tearDown
@@ -119,6 +123,8 @@ protected function setUp(): void
119123
{
120124
parent::setUp();
121125

126+
\IssueLog::Error($this->getName());
127+
122128
$this->PrepareEnvironment();
123129

124130
if (static::USE_TRANSACTION) {
@@ -185,6 +191,8 @@ protected function tearDown(): void
185191

186192
CMDBObject::SetCurrentChange(null);
187193

194+
$this->RestoreConfiguration();
195+
188196
parent::tearDown();
189197
}
190198

@@ -1434,4 +1442,35 @@ protected static function StopStopwatchInTheFuture(DBObject $oObject, string $sS
14341442
$oObject->Set($sStopwatchAttCode, $oStopwatch);
14351443
}
14361444

1445+
protected function BackupConfiguration(): void
1446+
{
1447+
$sConfigPath = MetaModel::GetConfig()->GetLoadedFile();
1448+
clearstatcache();
1449+
echo sprintf("rights via ls on %s:\n %s \n", $sConfigPath, exec("ls -al $sConfigPath"));
1450+
$sFilePermOutput = substr(sprintf('%o', fileperms('/etc/passwd')), -4);
1451+
echo sprintf("rights via fileperms on %s:\n %s \n", $sConfigPath, $sFilePermOutput);
1452+
1453+
$this->sConfigTmpBackupFile = tempnam(sys_get_temp_dir(), "config_");
1454+
MetaModel::GetConfig()->WriteToFile($this->sConfigTmpBackupFile);
1455+
$this->oiTopConfig = new Config($sConfigPath);
1456+
}
1457+
1458+
protected function RestoreConfiguration(): void
1459+
{
1460+
if (is_null($this->sConfigTmpBackupFile) || ! is_file($this->sConfigTmpBackupFile)) {
1461+
return;
1462+
}
1463+
1464+
if (is_null($this->oiTopConfig)) {
1465+
return;
1466+
}
1467+
1468+
//put config back
1469+
$sConfigPath = $this->oiTopConfig->GetLoadedFile();
1470+
@chmod($sConfigPath, 0770);
1471+
$oConfig = new Config($this->sConfigTmpBackupFile);
1472+
$oConfig->WriteToFile($sConfigPath);
1473+
@chmod($sConfigPath, 0440);
1474+
@unlink($this->sConfigTmpBackupFile);
1475+
}
14371476
}

tests/php-unit-tests/src/BaseTestCase/ItopTestCase.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use CMDBSource;
1111
use DeprecatedCallsLog;
1212
use MySQLTransactionNotClosedException;
13-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1413
use ReflectionMethod;
1514
use SetupUtils;
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1616
use Symfony\Component\HttpKernel\KernelInterface;
1717

1818
use const DEBUG_BACKTRACE_IGNORE_ARGS;
@@ -28,6 +28,7 @@
2828
abstract class ItopTestCase extends KernelTestCase
2929
{
3030
public const TEST_LOG_DIR = 'test';
31+
protected array $aFileToClean = [];
3132

3233
/**
3334
* @var bool
@@ -36,7 +37,7 @@ abstract class ItopTestCase extends KernelTestCase
3637
public const DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER = true;
3738
public static $DEBUG_UNIT_TEST = false;
3839
protected static $aBackupStaticProperties = [];
39-
40+
public ?array $aLastCurlGetInfo = null;
4041
/**
4142
* @link https://docs.phpunit.de/en/9.6/annotations.html#preserveglobalstate PHPUnit `preserveGlobalState` annotation documentation
4243
*
@@ -174,6 +175,15 @@ protected function tearDown(): void
174175
}
175176
throw new MySQLTransactionNotClosedException('Some DB transactions were opened but not closed ! Fix the code by adding ROLLBACK or COMMIT statements !', []);
176177
}
178+
179+
foreach ($this->aFileToClean as $sPath) {
180+
if (is_file($sPath)) {
181+
@unlink($sPath);
182+
continue;
183+
}
184+
185+
SetupUtils::tidydir($sPath);
186+
}
177187
}
178188

179189
/**
@@ -631,4 +641,52 @@ protected static function ReadTail($sFilename, $iLines = 1)
631641
fclose($handle);
632642
return array_reverse($aLines);
633643
}
644+
645+
/**
646+
* @param $sUrl
647+
* @param array|null $aPostFields
648+
* @param array|null $aCurlOptions
649+
* @param $bXDebugEnabled
650+
* @return string
651+
*/
652+
protected function CallItopUrl($sUrl, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
653+
{
654+
$ch = curl_init();
655+
if ($bXDebugEnabled) {
656+
curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_SESSION=phpstorm");
657+
}
658+
659+
curl_setopt($ch, CURLOPT_URL, $sUrl);
660+
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
661+
if (!is_array($aPostFields)) {
662+
var_dump($aPostFields);
663+
}
664+
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
665+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
666+
// Force disable of certificate check as most of dev / test env have a self-signed certificate
667+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
668+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
669+
670+
var_dump($aCurlOptions);
671+
curl_setopt_array($ch, $aCurlOptions);
672+
673+
$sOutput = curl_exec($ch);
674+
//\IssueLog::Info("$sUrl error code:", null, ['error' => curl_error($ch)]);
675+
676+
$info = curl_getinfo($ch);
677+
$this->aLastCurlGetInfo = $info;
678+
$sErrorMsg = curl_error($ch);
679+
$iErrorCode = curl_errno($ch);
680+
curl_close($ch);
681+
682+
\IssueLog::Info(__METHOD__, null, ['url' => $sUrl, 'error' => $sErrorMsg, 'error_code' => $iErrorCode, 'post_fields' => $aPostFields, 'info' => $info]);
683+
684+
return $sOutput;
685+
}
686+
687+
protected function CallItopUri(string $sUri, ?array $aPostFields = [], ?array $aCurlOptions = [], $bXDebugEnabled = false): string
688+
{
689+
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
690+
return $this->CallItopUrl($sUrl, $aPostFields, $aCurlOptions, $bXDebugEnabled);
691+
}
634692
}

tests/php-unit-tests/unitary-tests/application/LoginTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class LoginTest extends ItopDataTestCase
99
{
10-
protected $sConfigTmpBackupFile;
1110
protected $sConfigPath;
1211
protected $sLoginMode;
1312

tests/php-unit-tests/unitary-tests/application/query/QueryTest.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,34 +172,11 @@ private function CallExportService(Query $oQuery)
172172
{
173173
// compute request url
174174
$url = $oQuery->GetExportUrl();
175-
176-
// open curl
177-
$curl = curl_init();
178-
179-
// curl options
180-
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
181-
curl_setopt($curl, CURLOPT_USERPWD, self::USER.':'.self::PASSWORD);
182-
curl_setopt($curl, CURLOPT_URL, $url);
183-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
184-
// Force disable of certificate check as most of dev / test env have a self-signed certificate
185-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
186-
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
187-
188-
// execute curl
189-
$result = curl_exec($curl);
190-
if (curl_errno($curl)) {
191-
$info = curl_getinfo($curl);
192-
var_export($info);
193-
var_dump([
194-
'url' => $url,
195-
'app_root_url:' => MetaModel::GetConfig()->Get('app_root_url'),
196-
'GetAbsoluteUrlAppRoot:' => \utils::GetAbsoluteUrlAppRoot(),
197-
]);
198-
}
199-
// close curl
200-
curl_close($curl);
201-
202-
return $result;
175+
$aCurlOptions = [
176+
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
177+
CURLOPT_USERPWD => self::USER.':'.self::PASSWORD,
178+
];
179+
return $this->CallItopUrl($url, [], $aCurlOptions);
203180
}
204181

205182
/** @inheritDoc */

0 commit comments

Comments
 (0)