savedGlobals = []; foreach (CACTI_OBJECT_HASH_TRACKING_GLOBALS as $name) { $this->savedGlobals[$name] = array_key_exists($name, $GLOBALS) ? [true, $GLOBALS[$name]] : [false, null]; } }); afterEach(function () : void { foreach ($this->savedGlobals as $name => $state) { [$existed, $value] = $state; if ($existed) { $GLOBALS[$name] = $value; } else { unset($GLOBALS[$name]); } } }); function cacti_object_hash_tracking_detail(PDO $conn) : array { return [ 'database_conn' => $conn, 'database_hostname' => 'localhost', 'database_username' => 'cacti', 'database_password' => 'secret', 'database_default' => 'cacti', 'database_type' => 'mysql', 'database_port' => 3306, 'database_retries' => 2, 'database_ssl' => false, 'database_ssl_key' => '', 'database_ssl_cert' => '', 'database_ssl_ca' => '', 'database_ssl_capath' => '', 'database_ssl_verify_server_cert' => false, ]; } it('matches a live connection against its registered details via cacti_object_hash', function () { $conn = new PDO('sqlite::memory:'); $GLOBALS['database_details'] = [ cacti_object_hash($conn) => cacti_object_hash_tracking_detail($conn), ]; $handle = $conn; // The reconnect check hashes every registered handle and compares it to // the one passed in; a match copies the stored credentials and the live // 'SELECT 1' probe then confirms the handle is still usable. expect(db_check_reconnect($handle, false))->toBeTrue(); }); it('resolves a registered connection while closing under DEBUG_SQL_CONNECT', function () { $conn = new PDO('sqlite::memory:'); $GLOBALS['config']['DEBUG_SQL_CONNECT'] = true; $GLOBALS['database_persist'] = true; $GLOBALS['database_details'] = [ cacti_object_hash($conn) => cacti_object_hash_tracking_detail($conn), ]; $handle = $conn; // db_close() hashes the handle to look up its registration before // disconnecting; persistence is forced on so no KILL CONNECTION is issued // against the SQLite handle. expect(db_close($handle))->toBeTrue() ->and($handle)->toBeNull(); }); it('logs a registered connection while executing under DEBUG_SQL_CONNECT', function () { $conn = new PDO('sqlite::memory:'); $conn->exec('CREATE TABLE host (id INTEGER PRIMARY KEY AUTOINCREMENT, hostname TEXT NOT NULL)'); $GLOBALS['config']['DEBUG_SQL_CONNECT'] = true; $GLOBALS['database_details'] = [ cacti_object_hash($conn) => cacti_object_hash_tracking_detail($conn), ]; // With DEBUG_SQL_CONNECT on, db_execute_prepared() hashes the handle to // find its registration for the debug log, then runs the statement and // records the affected-row count under the same hash. db_execute_prepared("INSERT INTO host (hostname) VALUES ('h1')", [], false, $conn); expect((int) db_affected_rows($conn))->toBe(1); });