diff --git a/core/DbConnectionWrapper.php b/core/DbConnectionWrapper.php index 2b25128ab..6f352bc2e 100644 --- a/core/DbConnectionWrapper.php +++ b/core/DbConnectionWrapper.php @@ -56,12 +56,29 @@ class DbConnectionWrapper } /** - * Use this to register a mock that will handle {@see mysqli::query()} + * mysqli object is really hard to mock as it contains lots of attributes & methods ! Thought we need to mock it to test transactions ! * - * @param \mysqli|null $oMysqli + * To solve this, a new attribute exists and is only used in specific use cases, so there are just few things to mock. + * + * This object adds more readability than previous model with 2 attributes in {@see CMDBSource}. + * + * @used-by \CMDBSource + * + * @since 3.0.0 N°4325 Object creation + * This wrapper handles the 2 {@mysqli myqsli} attributes that were previously in {@see CMDBSource} + * To allow testing we added a second mysqli object (N°3513 in 2.7.5) and code became a bit confusing :/ + * With this wrapper everything is in the same place, and we can express the intention more clearly ! + * @since 3.1.0-4 N°6848 backport of restoring cnx on null parameter value */ - public static function SetDbConnectionMockForQuery(?mysqli $oMysqli): void + + public static function SetDbConnectionMockForQuery(?mysqli $oMysqli = null): void { - static::$oDbCnxMockableForQuery = $oMysqli; + if (is_null($oMysqli)) { + // Reset to standard connection + static::$oDbCnxMockableForQuery = static::$oDbCnxStandard; + } + else { + static::$oDbCnxMockableForQuery = $oMysqli; + } } } \ No newline at end of file diff --git a/tests/php-unit-tests/unitary-tests/core/CMDBSource/TransactionsTest.php b/tests/php-unit-tests/unitary-tests/core/CMDBSource/TransactionsTest.php index 5ee8ca63a..b30bca12a 100644 --- a/tests/php-unit-tests/unitary-tests/core/CMDBSource/TransactionsTest.php +++ b/tests/php-unit-tests/unitary-tests/core/CMDBSource/TransactionsTest.php @@ -276,6 +276,7 @@ class TransactionsTest extends ItopTestCase protected function tearDown(): void { try { + DbConnectionWrapper::SetDbConnectionMockForQuery(); // Else will throw error on PHP 8.1+ (see N°6848) parent::tearDown(); } catch (MySQLTransactionNotClosedException $e) {