Files
2meet-data-optimizer-hivepr…/tests/unit/DetectorTest.php
T
wpdev 0a3b05789e test: 建立 phpunit 測試基建並移植 25 個 HivePress 測試
AddOn 先前 tests=0。新增:
- composer.json(dev 依賴 + phpunit/phpcs script)
- phpunit.xml(failOnWarning=true)
- tests/bootstrap.php:直接 require 核心 plugin 的 unit bootstrap,避免複製
  ~700 行 WP stub,再載入本 AddOn 的類別;補 trait wrapper 與自動 WPDO_ alias
  (includes/back-compat-aliases.php 掛在 plugins_loaded:7,PHPUnit 下不會跑)
- tests/unit/ 25 個測試 + WpdbMockTrait(自 A 移植)

生產碼一併修對外契約:interface-tmdo-hp-adapter.php 改為先宣告空的
WPDO_HivePress_Adapter、再讓 TMDO_HivePress_Adapter extends 它(PHP 無法
class_alias 介面,只有繼承能讓 instanceof WPDO_HivePress_Adapter 對
implements TMDO_ 名稱的 adapter 成立)。與核心 interface-entity-adapter.php
同一模式。這也讓第三方自訂 adapter 用舊介面名仍可通過核心檢查。

145 tests / 357 assertions GREEN

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

80 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Detector — addon detection + caching.
*
* @covers WPDO_HivePress_Detector
*/
class HivePressDetectorTest extends TestCase {
protected function setUp(): void {
WPDO_HivePress_Detector::reset_for_tests();
// Clear transient cache (in-memory in test bootstrap).
unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] );
}
public function test_returns_empty_array_when_hivepress_core_absent(): void {
// HivePress\Core is not declared in the test environment.
$result = WPDO_HivePress_Detector::detect();
$this->assertSame( array(), $result );
}
public function test_short_circuit_caches_negative_result(): void {
WPDO_HivePress_Detector::detect();
// Second call should hit memo cache; verify identical.
$this->assertSame( array(), WPDO_HivePress_Detector::detect() );
}
public function test_catalog_returns_thirteen_known_addons(): void {
$catalog = WPDO_HivePress_Detector::catalog();
$this->assertCount( 13, $catalog );
$this->assertArrayHasKey( 'hivepress', $catalog );
$this->assertArrayHasKey( 'hivepress-bookings', $catalog );
$this->assertArrayHasKey( 'hivepress-marketplace', $catalog );
$this->assertArrayHasKey( 'hivepress-statistics', $catalog );
}
public function test_is_known_recognises_supported_addons(): void {
$this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress' ) );
$this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress-reviews' ) );
$this->assertFalse( WPDO_HivePress_Detector::is_known( 'hivepress-frobnicator' ) );
$this->assertFalse( WPDO_HivePress_Detector::is_known( '' ) );
}
public function test_bust_cache_clears_memo_and_transient(): void {
WPDO_HivePress_Detector::detect();
// Seed transient with bogus data to verify bust clears it.
$GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] = array( 'fake' => '9.9.9' );
WPDO_HivePress_Detector::bust_cache();
$this->assertArrayNotHasKey( '_transient_wpdo_hivepress_detector_cache', $GLOBALS['_wp_options'] ?? array() );
}
public function test_detects_addon_via_active_plugin_signal(): void {
// HivePress addons publish no class/const — they register via the
// `hivepress/v1/extensions` filter and are detected purely by being
// active plugins. Seed active_plugins with core + one addon and assert
// both are detected even though no HivePress classes are declared.
$GLOBALS['_wp_options']['active_plugins'] = array(
'hivepress/hivepress.php',
'hivepress-messages/hivepress-messages.php',
);
WPDO_HivePress_Detector::reset_for_tests();
unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] );
$result = WPDO_HivePress_Detector::detect();
$this->assertArrayHasKey( 'hivepress', $result, 'core detected via active plugin' );
$this->assertArrayHasKey( 'hivepress-messages', $result, 'addon detected via active plugin' );
$this->assertArrayNotHasKey( 'hivepress-reviews', $result, 'inactive addon not detected' );
// Cleanup so other tests observe no active plugins. bust_cache() clears
// BOTH the memo and the seeded transient (reset_for_tests clears only
// the memo) — otherwise the cached detection leaks into later suites.
unset( $GLOBALS['_wp_options']['active_plugins'] );
WPDO_HivePress_Detector::bust_cache();
}
}