Merge remote-tracking branch 'origin/support/3.0' into support/3.1

This commit is contained in:
Pierre Goiffon
2023-11-23 15:55:33 +01:00
4 changed files with 89 additions and 5 deletions

View File

@@ -26,8 +26,10 @@ define('ITOP_DESIGN_LATEST_VERSION', '3.1');
define('ITOP_CORE_VERSION', '3.1.0');
/**
* @since 3.0.4 N°6274 Allow to test if PHPUnit is currently running. Starting with PHPUnit 9.5 we'll be able to replace it with $GLOBALS['phpunit_version']
* @var string
* @since 3.0.4 3.1.0 3.2.0 N°6274 Allow to test if PHPUnit is currently running. Starting with PHPUnit 9.5 we'll be able to replace it with $GLOBALS['phpunit_version']
* @since 3.0.4 3.1.1 3.2.0 N°6976 Fix constant name (DeprecatedCallsLog error handler was never set)
*/
define('ITOP_PHPUNIT_RUNNING_CONSTANT_NAME', 'ITOP_PHPUNIT_RUNNING');
const ITOP_PHPUNIT_RUNNING_CONSTANT_NAME = 'ITOP_PHPUNIT_RUNNING';
require_once APPROOT.'bootstrap.inc.php';

View File

@@ -1143,9 +1143,13 @@ class DeprecatedCallsLog extends LogAPI
parent::Enable($sTargetFile);
if (
(false === defined('ITOP_PHPUNIT_RUNNING_CONSTANT_NAME'))
(
(false === defined(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME))
|| (defined(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME) && (constant(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME) !== true))
)
&& static::IsLogLevelEnabledSafe(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD)
) {
IssueLog::Trace('Setting '.static::class.' error handler to catch DEPRECATED', static::ENUM_CHANNEL_PHP_LIBMETHOD);
set_error_handler([static::class, 'DeprecatedNoticesErrorHandler'], E_DEPRECATED | E_USER_DEPRECATED);
}
}

View File

@@ -7,6 +7,7 @@
namespace Combodo\iTop\Test\UnitTest;
use CMDBSource;
use DeprecatedCallsLog;
use MySQLTransactionNotClosedException;
use PHPUnit\Framework\TestCase;
use SetupUtils;
@@ -22,6 +23,12 @@ use const DEBUG_BACKTRACE_IGNORE_ARGS;
abstract class ItopTestCase extends TestCase
{
public const TEST_LOG_DIR = 'test';
/**
* @var bool
* @since 3.0.4 3.1.1 3.2.0 N°6976 Allow to enable/disable {@see DeprecatedCallsLog} error handler
*/
public const DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER = true;
public static $DEBUG_UNIT_TEST = false;
protected static $aBackupStaticProperties = [];
@@ -44,9 +51,10 @@ abstract class ItopTestCase extends TestCase
require_once static::GetAppRoot() . 'approot.inc.php';
if (false === defined('ITOP_PHPUNIT_RUNNING_CONSTANT_NAME')) {
if ((static::DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER)
&& (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);
define(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME, true);
}
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Test\UnitTest\Core\Log;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Config;
use DeprecatedCallsLog;
use FileLog;
use IssueLog;
use LogAPI;
use utils;
use const E_USER_DEPRECATED;
use const ITOP_PHPUNIT_RUNNING_CONSTANT_NAME;
class DeprecatedCallsLogErrorHandlerTest extends ItopTestCase {
public const DISABLE_DEPRECATEDCALLSLOG_ERRORHANDLER = false;
/**
* @covers DeprecatedCallsLog::DeprecatedNoticesErrorHandler
* @since 3.0.4 3.1.1 3.2.0 N°6976
*
* @runInSeparateProcess so that other tests won't set the constant !
*/
public function testPhpLibMethodNoticeCatched():void {
if (defined(ITOP_PHPUNIT_RUNNING_CONSTANT_NAME)) {
// Should not happen thanks to the process isolation !
$this->fail('Constant to disable error handler is set, so we cannot test :(');
}
$sNoticeMessage = __METHOD__.uniqid(' @trigger_error unique message - ', true);
// to check that error handler is really set
$oMockIssueLogFile = $this->createMock(FileLog::class);
$oMockIssueLogFile->expects($this->exactly(1))
->method(LogAPI::LEVEL_TRACE)
->with($this->stringContains(DeprecatedCallsLog::class), DeprecatedCallsLog::ENUM_CHANNEL_PHP_LIBMETHOD, []);
// to check the error handler is logging correctly
$oMockDeprecatedLogFile = $this->createMock(FileLog::class);
$oMockDeprecatedLogFile->expects($this->exactly(1))
->method(LogAPI::LEVEL_WARNING)
->with($this->stringContains($sNoticeMessage), DeprecatedCallsLog::ENUM_CHANNEL_PHP_LIBMETHOD, []);
$oMockConfig = $this->createMock(Config::class);
$oMockConfig
->method("Get")
->willReturnCallback(function ($sConfigParameterName) {
if ($sConfigParameterName==='log_level_min'){
return [
DeprecatedCallsLog::ENUM_CHANNEL_PHP_LIBMETHOD => LogAPI::LEVEL_TRACE
];
}
/** @noinspection NullPointerExceptionInspection */
return utils::GetConfig()->Get($sConfigParameterName);
});
$this->RequireOnceItopFile('core/log.class.inc.php');
IssueLog::Enable(APPROOT.'log/error.log'); // to get log when setting error handler
IssueLog::MockStaticObjects($oMockIssueLogFile, $oMockConfig);
DeprecatedCallsLog::Enable(); // will set error handler
DeprecatedCallsLog::MockStaticObjects($oMockDeprecatedLogFile, $oMockConfig);
@trigger_error($sNoticeMessage, E_USER_DEPRECATED);
}
}