Files
2meet-data-optimizer-hivepr…/tests/unit/WpdbMockTrait.php
T
wpdev 5777210184 refactor(2meet-data-optimizer-hivepress-addon): remove comment mirror writes to never-created tables
- Favorites/Messages adapters reduced to detection only; Requests keeps hot_hp_request fields, drops hp_offer mirror
- Delete TMDO_HivePress_Comment_Router (read path to wpdo_comment_hp_* tables that no installer creates)
- Verify: addon unit 141 OK, integration 52 OK; phpcs 41E/57W → 35E/48W
- PR: PR-013
2026-09-25 22:58:34 +08:00

80 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* Shared $wpdb mock helper for HivePress adapter tests.
*
* Several existing wpdo tests (ZoneHotTest, SyncBridgeTest, etc.) replace the
* global $wpdb with their own anonymous-class mock and never restore it. When
* our adapter tests run after those, $wpdb lacks the query/get_col/delete
* methods our cron-optimizer code paths call, causing
* cross-test pollution failures.
*
* Trait reinstalls a known $wpdb mock + clears the wpdo_features option so
* Feature_Flags reports idle — the safe default state.
*/
trait WpdbMockTrait {
/**
* Install a fresh $wpdb mock with the methods adapter code paths call.
*
* Idempotent — call from setUp() in test classes that touch $wpdb.
*/
protected function install_wpdb_mock(): void {
global $wpdb;
$wpdb = new class {
public string $prefix = 'wp_';
public string $comments = 'wp_comments';
public string $postmeta = 'wp_postmeta';
public string $posts = 'wp_posts';
public string $options = 'wp_options';
/** @var array<int,string> */
public array $queries = array();
public function prepare( string $sql, ...$args ): string {
$i = 0;
return (string) preg_replace_callback(
'/%[sd]/',
static function () use ( &$i, $args ) {
return $args[ $i++ ] ?? '?';
},
$sql
);
}
public function query( string $sql ): int|bool {
$this->queries[] = $sql;
return 1;
}
public function get_var( string $sql ): ?string {
return null;
}
public function get_col( string $sql ): array {
return array();
}
public function get_results( string $sql, $output = OBJECT ): array {
return array();
}
public function insert( string $table, array $data, $format = null ): int|false {
return 1;
}
public function update( string $table, array $data, array $where, $format = null, $where_format = null ): int|false {
return 1;
}
public function delete( string $table, array $where, $format = null ): int|false {
return 1;
}
};
// Reset Feature_Flags state so is_query_active() returns false in tests.
$GLOBALS['_wp_options']['wpdo_features'] = array();
}
}