refactor(zones): 回填 Zone_Router + Routing_Predicate(PR-C)
A v3.0.1/v3.4.0 的架構深化,B 完全缺席:
新增
- includes/zones/class-tmdo-zone-router.php:module_name / read / write /
delete_field / delete_post 五個 static 分派點
- includes/class-tmdo-routing-predicate.php:entity_bridge_owns(含
request-level cache,B 原本沒有)+ should_{write,read,query}_from_zone
+ flush_cache
改寫
- Sync_Bridge 改用兩者,移除 5 個 private wrapper(get_zone_module /
read_from_zone / write_to_zone / delete_from_zone / is_owned_by_entity_bridge)
與 44 行 inline cleanup_post → Zone_Router::delete_post 一行(400→269 行)
- Query_Router 抽出 extract_hot_clauses() public static,pre_get_posts 變薄殼
- 收斂 5 處重複的 'hot_'/'cold_' . sanitize_key()(rest-api ×3、
hot/cold-migration ×2)
- back-compat 補 WPDO_Zone_Router / WPDO_Routing_Predicate alias
測試
- 移植 ZoneRouterTest(250 行)+ RoutingPredicateTest(85 行)
- SyncBridgeEntityGuardTest 的 setUp/tearDownAfterClass 補 flush_cache(),
否則 entity_bridge_cache 會跨測試污染(A v3.4.6 踩過同一個坑)
unit 409 / integration 398 GREEN
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for WPDO_Routing_Predicate — zone ownership and activation predicates.
|
||||
*/
|
||||
class RoutingPredicateTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_Routing_Predicate::flush_cache();
|
||||
|
||||
// Reset Feature Flags request cache.
|
||||
$ff = new ReflectionClass( WPDO_Feature_Flags::class );
|
||||
$ff->getProperty( 'cache' )->setValue( null, null );
|
||||
|
||||
// Reset Schema Registry singleton.
|
||||
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$sr->getProperty( 'instance' )->setValue( null, null );
|
||||
|
||||
$GLOBALS['_wp_options'] = array();
|
||||
}
|
||||
|
||||
// ── entity_bridge_owns ────────────────────────────────────────────────────
|
||||
|
||||
public function test_entity_bridge_owns_returns_false_when_mode_manager_absent(): void {
|
||||
// WPDO_Mode_Manager is not defined in unit test bootstrap → returns false.
|
||||
$this->assertFalse( WPDO_Routing_Predicate::entity_bridge_owns( 'hp_price' ) );
|
||||
}
|
||||
|
||||
public function test_entity_bridge_owns_caches_result(): void {
|
||||
$first = WPDO_Routing_Predicate::entity_bridge_owns( 'hp_featured' );
|
||||
$second = WPDO_Routing_Predicate::entity_bridge_owns( 'hp_featured' );
|
||||
$this->assertSame( $first, $second );
|
||||
}
|
||||
|
||||
public function test_flush_cache_clears_entity_bridge_cache(): void {
|
||||
WPDO_Routing_Predicate::entity_bridge_owns( 'some_key' );
|
||||
WPDO_Routing_Predicate::flush_cache();
|
||||
|
||||
$ref = new ReflectionClass( WPDO_Routing_Predicate::class );
|
||||
$cache = $ref->getProperty( 'entity_bridge_cache' )->getValue( null );
|
||||
$this->assertEmpty( $cache );
|
||||
}
|
||||
|
||||
// ── should_write_to_zone ──────────────────────────────────────────────────
|
||||
|
||||
public function test_should_write_to_zone_returns_false_when_module_idle(): void {
|
||||
$this->assertFalse( WPDO_Routing_Predicate::should_write_to_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
public function test_should_write_to_zone_returns_true_when_dual_write(): void {
|
||||
WPDO_Feature_Flags::set( 'hot_hp_listing', 'dual_write' );
|
||||
$this->assertTrue( WPDO_Routing_Predicate::should_write_to_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
public function test_should_write_to_zone_returns_false_when_only_cold_active(): void {
|
||||
WPDO_Feature_Flags::set( 'cold_hp_listing', 'dual_write' );
|
||||
$this->assertFalse( WPDO_Routing_Predicate::should_write_to_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
// ── should_read_from_zone ─────────────────────────────────────────────────
|
||||
|
||||
public function test_should_read_from_zone_returns_false_when_not_cutover(): void {
|
||||
WPDO_Feature_Flags::set( 'hot_hp_listing', 'dual_write' );
|
||||
$this->assertFalse( WPDO_Routing_Predicate::should_read_from_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
public function test_should_read_from_zone_returns_true_when_cutover(): void {
|
||||
WPDO_Feature_Flags::set( 'hot_hp_listing', 'cutover' );
|
||||
$this->assertTrue( WPDO_Routing_Predicate::should_read_from_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
// ── should_query_from_zone ────────────────────────────────────────────────
|
||||
|
||||
public function test_should_query_from_zone_returns_false_when_idle(): void {
|
||||
$this->assertFalse( WPDO_Routing_Predicate::should_query_from_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
|
||||
public function test_should_query_from_zone_returns_true_when_cutover(): void {
|
||||
WPDO_Feature_Flags::set( 'hot_hp_listing', 'cutover' );
|
||||
$this->assertTrue( WPDO_Routing_Predicate::should_query_from_zone( 'hp_listing', 'hot' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for WPDO_Zone_Router — central zone dispatch and module-name resolution.
|
||||
*/
|
||||
class ZoneRouterTest extends TestCase {
|
||||
|
||||
/** Last SQL passed to $wpdb->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 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user