0a3b05789e
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
99 lines
3.1 KiB
PHP
99 lines
3.1 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
/**
|
||
* Tests for WPDO_HivePress_Adapter_Trait — score composition + helper sugar.
|
||
*
|
||
* Uses an anonymous adapter to exercise trait behaviour without requiring
|
||
* a concrete production adapter.
|
||
*
|
||
* @covers WPDO_HivePress_Adapter_Trait
|
||
*/
|
||
class HivePressAdapterTraitTest extends TestCase {
|
||
|
||
/**
|
||
* Build a fresh anonymous adapter with optional score override.
|
||
*/
|
||
private function make_adapter( array $score_override = array() ): object {
|
||
return new class( $score_override ) implements WPDO_HivePress_Adapter {
|
||
use WPDO_HivePress_Adapter_Trait;
|
||
|
||
private array $override;
|
||
|
||
public function __construct( array $override ) {
|
||
$this->override = $override;
|
||
}
|
||
|
||
public function plugin_slug(): string {
|
||
return 'test-adapter';
|
||
}
|
||
|
||
public function detection_class(): string {
|
||
return 'Test\\Adapter';
|
||
}
|
||
|
||
public function detection_const(): string {
|
||
return 'TEST_ADAPTER_VERSION';
|
||
}
|
||
|
||
public function suitability_score(): array {
|
||
return $this->compose_score( $this->override );
|
||
}
|
||
};
|
||
}
|
||
|
||
public function test_default_score_is_perfect_ten(): void {
|
||
$score = $this->make_adapter()->suitability_score();
|
||
$this->assertSame( 10.0, $score['aggregate'] );
|
||
foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $k ) {
|
||
$this->assertSame( 1.0, $score[ $k ] );
|
||
}
|
||
}
|
||
|
||
public function test_partial_override_lowers_aggregate(): void {
|
||
$score = $this->make_adapter( array( 'd5_hook_bus' => 0.5 ) )->suitability_score();
|
||
// 7 × 1.0 + 1 × 0.5 = 7.5; aggregate = 7.5 / 8 * 10 = 9.375 → rounded 9.38.
|
||
$this->assertSame( 9.38, $score['aggregate'] );
|
||
}
|
||
|
||
public function test_score_clamped_to_unit_interval(): void {
|
||
$score = $this->make_adapter( array(
|
||
'd1_meta_calls' => 1.5, // → clamped to 1.0
|
||
'd2_meta_sql' => -0.3, // → clamped to 0.0
|
||
) )->suitability_score();
|
||
$this->assertSame( 1.0, $score['d1_meta_calls'] );
|
||
$this->assertSame( 0.0, $score['d2_meta_sql'] );
|
||
}
|
||
|
||
public function test_default_implementations_are_no_op(): void {
|
||
$adapter = $this->make_adapter();
|
||
|
||
// All optional methods should be safe to invoke on a minimal adapter.
|
||
$this->assertSame( array(), $adapter->migrations() );
|
||
$this->assertSame( '', $adapter->minimum_addon_version() );
|
||
|
||
$result = $adapter->doctor_check();
|
||
$this->assertTrue( $result['ok'] );
|
||
|
||
// Hook handlers must not throw on a no-op adapter.
|
||
$adapter->on_register_query_hooks();
|
||
$adapter->on_register_event_hooks();
|
||
$adapter->on_register_entity_fields();
|
||
$this->assertTrue( true ); // reaching here = no exception
|
||
}
|
||
|
||
public function test_aggregate_rounding_is_two_decimals(): void {
|
||
$score = $this->make_adapter( array(
|
||
'd1_meta_calls' => 0.85,
|
||
'd2_meta_sql' => 0.92,
|
||
'd3_table_coverage' => 0.78,
|
||
) )->suitability_score();
|
||
|
||
// All other dimensions stay at 1.0 → sum = 0.85+0.92+0.78+5*1.0 = 7.55.
|
||
// aggregate = 7.55 / 8 * 10 = 9.4375 → round(2) = 9.44.
|
||
$this->assertSame( 9.44, $score['aggregate'] );
|
||
}
|
||
}
|