Files
cacti/tests/Unit/Core/CactiObjectHashTest.php
T
d27c2a8779 fix: Fix php deprecation of spl_object_hash in php 8.6 (#8567)
* fix: Fix php deprecation of spl_object_hash in php 8.6

Replaces spl_object_hash() calls to address the PHP 8.6 deprecation.

Adds cacti_object_hash(), which uses spl_object_id() to produce a stable 32 character hash string, and migrates the database connection and affected-row tracking in lib/database.php to the helper. Boost already keys its per-connection packet-limit cache on spl_object_id() on develop, so no change is needed there.

Adds tests/Unit/CactiObjectHashTest.php covering hash format, determinism, uniqueness, and the non-object false return.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* tests: Cover cacti_object_hash connection-tracking paths in database.php

The changed-PHP-lines coverage gate fell below 80 percent because the cacti_object_hash() swaps in db_check_reconnect(), db_close(), and db_execute_prepared() (the DEBUG_SQL_CONNECT branches) were not exercised. Adds a unit test that drives those paths with a real PDO handle registered in \, raising changed-line coverage of lib/database.php to 7 of 8 lines (overall 10 of 11, 90.9 percent).

Every connection global these functions assign to is snapshotted and restored around each test so the shared connection state the rest of the suite relies on is not corrupted.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* tests: Move CactiObjectHashTest into the Core category folder

AGENTS.md requires unit tests to live in a category subfolder rather than the tests/Unit/ root. Moves the cacti_object_hash() helper test to tests/Unit/Core/ alongside the other core utility tests and updates its lib/functions.php include to dirname(__DIR__, 3) so it still resolves to the repository root.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: TheWitness <copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-02 15:54:54 +00:00

57 lines
2.2 KiB
PHP

<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
/*
* cacti_object_hash() replaces spl_object_hash(), which is deprecated starting
* in PHP 8.6. It must preserve the properties the database and boost layers
* relied on: a stable, 32 character hex string that is unique per live object
* and that is derived from spl_object_id(). Non-objects must yield false so
* callers never index a cache with a bogus key.
*/
require_once dirname(__DIR__, 3) . '/lib/functions.php';
test('a hash is a 32 character lowercase hex string derived from spl_object_id', function () {
$object = new stdClass();
$hash = cacti_object_hash($object);
expect($hash)->toBeString()
->and(strlen($hash))->toBe(32)
->and($hash)->toMatch('/^[0-9a-f]{32}$/')
->and($hash)->toBe(sprintf('%016x0000000000000000', spl_object_id($object)));
});
test('the same object always hashes to the same value', function () {
$object = new stdClass();
expect(cacti_object_hash($object))->toBe(cacti_object_hash($object));
});
test('two distinct live objects hash to different values', function () {
$first = new stdClass();
$second = new stdClass();
expect(cacti_object_hash($first))->not->toBe(cacti_object_hash($second));
});
test('non-object arguments return false', function () {
expect(cacti_object_hash('a string'))->toBeFalse()
->and(cacti_object_hash(42))->toBeFalse()
->and(cacti_object_hash(3.14))->toBeFalse()
->and(cacti_object_hash(array('an', 'array')))->toBeFalse()
->and(cacti_object_hash(null))->toBeFalse()
->and(cacti_object_hash(true))->toBeFalse();
});