diff --git a/core/log.class.inc.php b/core/log.class.inc.php index 9c9fbc396..c41b4e31f 100644 --- a/core/log.class.inc.php +++ b/core/log.class.inc.php @@ -15,6 +15,7 @@ // // You should have received a copy of the GNU Affero General Public License // along with iTop. If not, see +use Combodo\iTop\Test\UnitTest\ItopTestCase; /** @@ -1071,16 +1072,19 @@ class DeprecatedCallsLog extends LogAPI * @uses \set_error_handler() to catch deprecated notices * * @since 3.0.0 N°3002 logs deprecated notices in called code + * @since 3.0.4 N°6274 do not set handler when in PHPUnit context (otherwise PHP notices won't be caught) */ - public static function Enable($sTargetFile = null): void - { + public static function Enable($sTargetFile = null): void { if (empty($sTargetFile)) { $sTargetFile = APPROOT.'log/deprecated-calls.log'; } parent::Enable($sTargetFile); - if (static::IsLogLevelEnabledSafe(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD)) { - set_error_handler([static::class, 'DeprecatedNoticesErrorHandler']); + if ( + (false === defined(ItopTestCase::ITOP_PHPUNIT_RUNNING_CONSTANT_NAME)) + && static::IsLogLevelEnabledSafe(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD) + ) { + set_error_handler([static::class, 'DeprecatedNoticesErrorHandler'], E_DEPRECATED | E_USER_DEPRECATED); } } diff --git a/tests/php-unit-tests/ItopTestCase.php b/tests/php-unit-tests/ItopTestCase.php index 633e8884f..5dec2a274 100644 --- a/tests/php-unit-tests/ItopTestCase.php +++ b/tests/php-unit-tests/ItopTestCase.php @@ -32,13 +32,20 @@ use SetupUtils; define('DEBUG_UNIT_TEST', true); -class ItopTestCase extends TestCase -{ - const TEST_LOG_DIR = 'test'; +class ItopTestCase extends TestCase { + /** + * @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'] + */ + public const ITOP_PHPUNIT_RUNNING_CONSTANT_NAME = 'ITOP_PHPUNIT_RUNNING'; + + public const TEST_LOG_DIR = 'test'; + + protected function setUp(): void { + if (false === defined(static::ITOP_PHPUNIT_RUNNING_CONSTANT_NAME)) { + // setUp might be called multiple times, so protecting the define() call ! + define(static::ITOP_PHPUNIT_RUNNING_CONSTANT_NAME, true); + } - /** @noinspection UsingInclusionOnceReturnValueInspection avoid errors for approot includes */ - protected function setUp(): void - { $sAppRootRelPath = 'approot.inc.php'; $sDepthSeparator = '../'; for ($iDepth = 0; $iDepth < 8; $iDepth++) { diff --git a/tests/php-unit-tests/unitary-tests/core/Log/DeprecatedCallsLogTest.php b/tests/php-unit-tests/unitary-tests/core/Log/DeprecatedCallsLogTest.php new file mode 100644 index 000000000..6d01ec173 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/Log/DeprecatedCallsLogTest.php @@ -0,0 +1,41 @@ +expectNotice(); + + $aArray = []; + if ('toto' === $aArray['tutu']) { + //Do nothing, just raising a undefined offset warning + } + } + + /** + * The error handler set by DeprecatedCallsLog during startup was causing PHPUnit to miss PHP notices like "undefined offset" + * + * The error handler is now disabled when running PHPUnit + * + * @since 3.0.4 N°6274 + * @covers DeprecatedCallsLog::DeprecatedNoticesErrorHandler + */ + public function testPhpNoticeWithDeprecatedCallsLog(): void { + $this->RequireOnceItopFile('core/log.class.inc.php'); + DeprecatedCallsLog::Enable(); // will set error handler + $this->expectNotice(); + + $aArray = []; + if ('toto' === $aArray['tutu']) { + //Do nothing, just raising a undefined offset warning + } + } +}