N°6274 DeprecatedCallsLogTest : fix expected exception mismatch in PHP 8.0+

Undefined offset notice was changed to a warning in PHP 8.0... Also message was changed :(
This commit is contained in:
Pierre Goiffon
2023-05-05 09:13:03 +02:00
parent 1884596ecd
commit 959ac7e3be

View File

@@ -9,10 +9,30 @@ namespace Combodo\iTop\Test\UnitTest\Core\Log;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use DeprecatedCallsLog;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\Error\Warning;
class DeprecatedCallsLogTest extends ItopTestCase {
/**
* We are testing for a undefined offset error. This was throwing a Notice, but starting with PHP 8.0 it was converted to a Warning ! Also the message was changed :(
*
* @link https://www.php.net/manual/en/migration80.incompatible.php check "A number of notices have been converted into warnings:"
*/
private function SetUndefinedOffsetExceptionToExpect(): void {
if (version_compare(phpversion(), '8.0', '>=')) {
$sUndefinedOffsetExceptionClass = Warning::class;
$sUndefinedOffsetExceptionMessage = 'Undefined array key "tutu"';
}
else {
$sUndefinedOffsetExceptionClass = Notice::class;
$sUndefinedOffsetExceptionMessage = 'Undefined index: tutu';
}
$this->expectException($sUndefinedOffsetExceptionClass);
$this->expectExceptionMessage($sUndefinedOffsetExceptionMessage);
}
public function testPhpNoticeWithoutDeprecatedCallsLog(): void {
$this->expectNotice();
$this->SetUndefinedOffsetExceptionToExpect();
$aArray = [];
if ('toto' === $aArray['tutu']) {
@@ -31,7 +51,7 @@ class DeprecatedCallsLogTest extends ItopTestCase {
public function testPhpNoticeWithDeprecatedCallsLog(): void {
$this->RequireOnceItopFile('core/log.class.inc.php');
DeprecatedCallsLog::Enable(); // will set error handler
$this->expectNotice();
$this->SetUndefinedOffsetExceptionToExpect();
$aArray = [];
if ('toto' === $aArray['tutu']) {