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
This commit is contained in:
2026-07-31 09:19:01 +08:00
parent b96b041445
commit 0a3b05789e
32 changed files with 2574 additions and 59 deletions
+98
View File
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Benchmark.
*
* @covers WPDO_HivePress_Benchmark
*/
class HivePressBenchmarkTest extends TestCase {
public function test_default_iterations_is_fifty(): void {
$this->assertSame( 50, WPDO_HivePress_Benchmark::DEFAULT_ITERATIONS );
}
public function test_run_returns_mean_milliseconds(): void {
$ms = WPDO_HivePress_Benchmark::run( static function () {
usleep( 1000 ); // 1 ms
}, 5 );
// Allow generous slack for slow CI runners; we just need it to be > 0.
$this->assertGreaterThan( 0.0, $ms );
}
public function test_compare_returns_speedup_ratio(): void {
$result = WPDO_HivePress_Benchmark::compare(
'sample',
static function () { usleep( 4000 ); }, // ~4 ms baseline
static function () { usleep( 1000 ); }, // ~1 ms target
3
);
$this->assertSame( 'sample', $result['name'] );
$this->assertGreaterThan( 0.0, $result['baseline_ms'] );
$this->assertGreaterThan( 0.0, $result['target_ms'] );
// Baseline is roughly 4× target — ratio should be > 1 (faster).
$this->assertGreaterThan( 1.0, $result['ratio'] );
$this->assertSame( 3, $result['iterations'] );
}
public function test_compare_handles_zero_target_time(): void {
// If target is suspiciously fast, ratio falls back to 0 (no division).
$result = WPDO_HivePress_Benchmark::compare(
'instant',
static function () { /* noop */ },
static function () { /* noop */ },
1
);
// Either ratio > 0 (both nonzero) or 0 (target_ms was 0); never NaN/inf.
$this->assertIsFloat( $result['ratio'] );
$this->assertFalse( is_nan( $result['ratio'] ) );
$this->assertFalse( is_infinite( $result['ratio'] ) );
}
public function test_run_suite_aggregates_samples(): void {
$report = WPDO_HivePress_Benchmark::run_suite( array(
array(
'name' => 'a',
'baseline' => static function () { usleep( 2000 ); },
'target' => static function () { usleep( 500 ); },
'iterations' => 2,
),
array(
'name' => 'b',
'baseline' => static function () { usleep( 4000 ); },
'target' => static function () { usleep( 1000 ); },
'iterations' => 2,
),
) );
$this->assertSame( 2, $report['sample_count'] );
$this->assertGreaterThan( 0.0, $report['geomean_ratio'] );
}
public function test_run_suite_skips_malformed_sample(): void {
$report = WPDO_HivePress_Benchmark::run_suite( array(
array( 'name' => 'incomplete' ), // missing baseline + target
array(
'name' => 'ok',
'baseline' => static function () { usleep( 100 ); },
'target' => static function () { usleep( 100 ); },
'iterations' => 1,
),
) );
$this->assertSame( 1, $report['sample_count'] );
}
public function test_geometric_mean_with_empty_values_returns_zero(): void {
$this->assertSame( 0.0, WPDO_HivePress_Benchmark::geometric_mean( array() ) );
}
public function test_geometric_mean_resists_outliers(): void {
// Geomean of (2, 8) = sqrt(16) = 4 (vs arithmetic mean 5).
$this->assertSame( 4.0, WPDO_HivePress_Benchmark::geometric_mean( array( 2.0, 8.0 ) ) );
}
}