query(). */ public static string $last_query = ''; /** Last $wpdb->delete() call args: [table, where, formats]. */ public static array $last_delete = []; /** Stub return for $wpdb->get_var(). */ public static mixed $get_var_return = null; protected function setUp(): void { self::$last_query = ''; self::$last_delete = []; self::$get_var_return = null; $this->setup_wpdb_mock(); } private function setup_wpdb_mock(): void { global $wpdb; $wpdb = new class { public string $prefix = 'wp_'; public function prepare( string $sql, mixed ...$args ): string { $i = 0; return preg_replace_callback( '/%([sd])/', static function ( $m ) use ( &$i, $args ) { $val = $args[ $i++ ] ?? ''; return $m[1] === 'd' ? (string) (int) $val : "'" . addslashes( (string) $val ) . "'"; }, $sql ); } public function query( string $sql ): int { ZoneRouterTest::$last_query = $sql; return 1; } public function get_var( string $sql ): mixed { ZoneRouterTest::$last_query = $sql; return ZoneRouterTest::$get_var_return; } public function insert( string $table, array $data, mixed $formats = null ): int { ZoneRouterTest::$last_query = "INSERT INTO `{$table}`"; return 1; } public function delete( string $table, array $where, array $formats ): void { ZoneRouterTest::$last_delete = [ $table, $where, $formats ]; } public function get_row( string $sql, string $output = OBJECT ): mixed { ZoneRouterTest::$last_query = $sql; return null; } }; } // ── module_name ─────────────────────────────────────────────────────────── public function test_module_name_hot_prepends_zone(): void { $this->assertSame( 'hot_hp_listing', WPDO_Zone_Router::module_name( 'hot', 'hp_listing' ) ); } public function test_module_name_cold_prepends_zone(): void { $this->assertSame( 'cold_hp_listing', WPDO_Zone_Router::module_name( 'cold', 'hp_listing' ) ); } public function test_module_name_warm_ignores_post_type(): void { $this->assertSame( 'warm', WPDO_Zone_Router::module_name( 'warm', 'hp_listing' ) ); } public function test_module_name_archive_ignores_post_type(): void { $this->assertSame( 'archive', WPDO_Zone_Router::module_name( 'archive', 'hp_listing' ) ); } public function test_module_name_unknown_zone_returns_unknown(): void { $this->assertSame( 'unknown', WPDO_Zone_Router::module_name( 'other', 'hp_listing' ) ); } public function test_module_name_sanitizes_post_type(): void { // sanitize_key strips spaces and lowercases; 'HP Listing' → 'hplisting'. $this->assertSame( 'hot_hplisting', WPDO_Zone_Router::module_name( 'hot', 'HP Listing' ) ); } // ── read ───────────────────────────────────────────────────────────────── public function test_read_hot_calls_zone_hot_get(): void { $field = array( 'zone' => 'hot', 'column' => 'hp_price' ); WPDO_Zone_Router::read( $field, 42, 'hp_listing', 'hp_price' ); $this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query ); $this->assertStringContainsString( 'hp_price', self::$last_query ); $this->assertStringContainsString( '42', self::$last_query ); } public function test_read_warm_calls_zone_warm_get(): void { $field = array( 'zone' => 'warm' ); WPDO_Zone_Router::read( $field, 7, 'hp_listing', 'views' ); $this->assertStringContainsString( 'wpdo_warm', self::$last_query ); } public function test_read_cold_calls_zone_cold_get(): void { $field = array( 'zone' => 'cold' ); WPDO_Zone_Router::read( $field, 5, 'hp_listing', 'hp_desc' ); $this->assertStringContainsString( 'wpdo_cold_hp_listing', self::$last_query ); } public function test_read_unknown_zone_returns_null(): void { $field = array( 'zone' => 'other' ); $result = WPDO_Zone_Router::read( $field, 1, 'hp_listing', 'key' ); $this->assertNull( $result ); } // ── write ───────────────────────────────────────────────────────────────── public function test_write_hot_calls_zone_hot_set(): void { $field = array( 'zone' => 'hot', 'column' => 'hp_price' ); WPDO_Zone_Router::write( $field, 42, 'hp_listing', 'hp_price', '100' ); $this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query ); $this->assertStringContainsString( 'hp_price', self::$last_query ); } public function test_write_warm_calls_zone_warm_set(): void { $field = array( 'zone' => 'warm', 'ttl' => null ); WPDO_Zone_Router::write( $field, 7, 'hp_listing', 'views', '5' ); $this->assertStringContainsString( 'wpdo_warm', self::$last_query ); } public function test_write_warm_json_encodes_non_string_value(): void { $field = array( 'zone' => 'warm', 'ttl' => null ); WPDO_Zone_Router::write( $field, 7, 'hp_listing', 'data', array( 'a' => 1 ) ); $this->assertStringContainsString( 'wpdo_warm', self::$last_query ); } public function test_write_unknown_zone_is_noop(): void { $field = array( 'zone' => 'other' ); WPDO_Zone_Router::write( $field, 1, 'hp_listing', 'key', 'val' ); $this->assertSame( '', self::$last_query ); } // ── delete_field ────────────────────────────────────────────────────────── public function test_delete_field_hot_nulls_column(): void { $field = array( 'zone' => 'hot', 'column' => 'hp_price' ); WPDO_Zone_Router::delete_field( $field, 42, 'hp_listing', 'hp_price' ); $this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query ); // Hot delete sets the column to NULL via an UPDATE. $this->assertStringContainsString( 'NULL', self::$last_query ); } public function test_delete_field_warm_calls_delete(): void { $field = array( 'zone' => 'warm' ); WPDO_Zone_Router::delete_field( $field, 7, 'hp_listing', 'views' ); // Table name includes $wpdb->prefix ('wp_') + 'wpdo_warm'. $this->assertSame( 'wp_wpdo_warm', self::$last_delete[0] ?? '' ); } public function test_delete_field_unknown_zone_is_noop(): void { $field = array( 'zone' => 'other' ); WPDO_Zone_Router::delete_field( $field, 1, 'hp_listing', 'key' ); $this->assertSame( '', self::$last_query ); $this->assertSame( [], self::$last_delete ); } // ── delete_post ─────────────────────────────────────────────────────────── public function test_delete_post_skips_hot_when_no_hot_columns(): void { $sr = new ReflectionClass( WPDO_Schema_Registry::class ); $sr->getProperty( 'instance' )->setValue( null, null ); $captured = array(); global $wpdb; $wpdb = new class( $captured ) { public string $prefix = 'wp_'; public array $log; public function __construct( array &$ref ) { $this->log = &$ref; } public function prepare( string $s, mixed ...$a ): string { return $s; } public function query( string $s ): int { return 1; } public function get_var( string $s ): mixed { return null; } public function insert( string $t, array $d, mixed $f = null ): int { return 1; } public function delete( string $table, array $where, array $formats ): void { $this->log[] = $table; } }; WPDO_Zone_Router::delete_post( 1, 'hp_listing' ); $this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured, 'Hot should be skipped when no columns registered' ); $this->assertContains( 'wp_wpdo_warm', $captured ); $this->assertContains( 'wp_wpdo_archive', $captured ); } public function test_delete_post_deletes_hot_when_columns_registered(): void { $sr = new ReflectionClass( WPDO_Schema_Registry::class ); $sr->getProperty( 'instance' )->setValue( null, null ); WPDO_Schema_Registry::instance()->register( 'test', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'zone' => 'hot', 'column' => 'hp_price', 'type' => 'decimal', ) ); $captured = array(); global $wpdb; $wpdb = new class( $captured ) { public string $prefix = 'wp_'; public array $log; public function __construct( array &$ref ) { $this->log = &$ref; } public function prepare( string $s, mixed ...$a ): string { return $s; } public function query( string $s ): int { return 1; } public function get_var( string $s ): mixed { return null; } public function insert( string $t, array $d, mixed $f = null ): int { return 1; } public function delete( string $table, array $where, array $formats ): void { $this->log[] = $table; } }; WPDO_Zone_Router::delete_post( 10, 'hp_listing' ); $this->assertContains( 'wp_wpdo_hot_hp_listing', $captured ); } public function test_delete_post_always_clears_warm_and_archive(): void { $sr = new ReflectionClass( WPDO_Schema_Registry::class ); $sr->getProperty( 'instance' )->setValue( null, null ); $captured = array(); global $wpdb; $wpdb = new class( $captured ) { public string $prefix = 'wp_'; public array $log; public function __construct( array &$ref ) { $this->log = &$ref; } public function prepare( string $s, mixed ...$a ): string { return $s; } public function query( string $s ): int { return 1; } public function get_var( string $s ): mixed { return null; } public function insert( string $t, array $d, mixed $f = null ): int { return 1; } public function delete( string $table, array $where, array $formats ): void { $this->log[] = $table; } }; WPDO_Zone_Router::delete_post( 5, 'hp_listing' ); $this->assertContains( 'wp_wpdo_warm', $captured ); $this->assertContains( 'wp_wpdo_archive', $captured ); } }