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' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user