Files
2meet-data-optimizer/tests/unit/HookBusBridgeTest.php
T
wpdev d36bb954d1 chore: initial snapshot of 2meet-data-optimizer v0.1.0
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 05:06:36 +08:00

113 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Unit tests for WPDO_Hook_Bus_Bridge — PR-3 feature-flag + conflict detection.
*
* @covers WPDO_Hook_Bus_Bridge
*/
class HookBusBridgeTest extends TestCase {
protected function setUp(): void {
WPDO_Hook_Bus_Bridge::reset_cache();
// Reset $GLOBALS['_wp_options'] for each test isolation.
$GLOBALS['_wp_options'] = array();
}
// ── is_enabled() ────────────────────────────────────────────────────────
public function test_is_enabled_default_true(): void {
// v2.5.4: Hook Bus is ON by default; option not set → true.
$this->assertTrue( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_string_one(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '1' );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertTrue( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_bool_true(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, true );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertTrue( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_zero(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '0' );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertFalse( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_caches_result(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '1' );
WPDO_Hook_Bus_Bridge::reset_cache();
$first = WPDO_Hook_Bus_Bridge::is_enabled();
// Mutate the option, but cache should retain previous value until reset.
update_option( WPDO_Hook_Bus_Bridge::OPTION, '0' );
$second = WPDO_Hook_Bus_Bridge::is_enabled();
$this->assertSame( $first, $second, 'Cache must be sticky within a request' );
// After explicit reset → new value visible.
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertFalse( WPDO_Hook_Bus_Bridge::is_enabled() );
}
// ── maybe_init_hook_bus() ───────────────────────────────────────────────
public function test_maybe_init_hook_bus_noop_when_disabled(): void {
// Disabled → must not throw even if WPDO_Hook_Bus class missing.
WPDO_Hook_Bus_Bridge::maybe_init_hook_bus();
$this->assertTrue( true );
}
// ── detect_intra_wpdo_conflicts() ───────────────────────────────────────
public function test_detect_intra_wpdo_conflicts_returns_empty_when_no_filters(): void {
// Stub bootstrap doesn't populate $wp_filter, so result is empty array.
$conflicts = WPDO_Hook_Bus_Bridge::detect_intra_wpdo_conflicts();
$this->assertIsArray( $conflicts );
$this->assertEmpty( $conflicts );
}
public function test_detect_intra_wpdo_conflicts_flags_multiple_wpdo_callbacks(): void {
// Simulate $wp_filter with two WPDO_* callbacks on the same hook.
$GLOBALS['wp_filter'] = array(
'update_post_metadata' => new class() {
public array $callbacks;
public function __construct() {
$this->callbacks = array(
10 => array(
array(
'function' => array(
new class() {
public function intercept_update() {}
},
'intercept_update',
),
),
),
8 => array(
array(
'function' => array(
new class() {
public function intercept_update() {}
},
'intercept_update',
),
),
),
);
}
},
);
// The anonymous classes won't have WPDO_ prefix → must not flag conflict.
$conflicts = WPDO_Hook_Bus_Bridge::detect_intra_wpdo_conflicts();
$this->assertEmpty( $conflicts, 'Non-WPDO classes must not be flagged' );
}
}