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
149 lines
5.7 KiB
PHP
149 lines
5.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Tests for WPDO_HivePress_Core_Adapter — Listing / Vendor field registration.
|
|
*
|
|
* @covers WPDO_HivePress_Core_Adapter
|
|
*/
|
|
class HivePressCoreAdapterTest extends TestCase {
|
|
|
|
private WPDO_HivePress_Core_Adapter $adapter;
|
|
|
|
protected function setUp(): void {
|
|
// WPDO_Schema_Registry has no reset_for_tests() — use reflection to
|
|
// clear its singleton so each test starts with a fresh registry.
|
|
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
if ( $reflect->hasProperty( 'instance' ) ) {
|
|
$prop = $reflect->getProperty( 'instance' );
|
|
$prop->setAccessible( true );
|
|
$prop->setValue( null, null );
|
|
}
|
|
WPDO_Custom_Table_Registry::reset_for_tests();
|
|
$this->adapter = new WPDO_HivePress_Core_Adapter();
|
|
}
|
|
|
|
public function test_implements_adapter_interface(): void {
|
|
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
|
}
|
|
|
|
public function test_plugin_slug_is_hivepress(): void {
|
|
$this->assertSame( 'hivepress', $this->adapter->plugin_slug() );
|
|
}
|
|
|
|
public function test_detection_class_targets_hivepress_core(): void {
|
|
$this->assertSame( 'HivePress\\Core', $this->adapter->detection_class() );
|
|
}
|
|
|
|
public function test_detection_const_targets_hivepress_version(): void {
|
|
$this->assertSame( 'HIVEPRESS_VERSION', $this->adapter->detection_const() );
|
|
}
|
|
|
|
public function test_minimum_addon_version_floor(): void {
|
|
// 1.7.0 introduced the field-alias convention this adapter relies on.
|
|
$this->assertSame( '1.7.0', $this->adapter->minimum_addon_version() );
|
|
}
|
|
|
|
public function test_migrations_returns_listing_and_vendor_modules(): void {
|
|
$mods = $this->adapter->migrations();
|
|
$this->assertContains( 'hot_hp_listing', $mods );
|
|
$this->assertContains( 'hot_hp_vendor', $mods );
|
|
}
|
|
|
|
public function test_on_register_fields_populates_schema_registry(): void {
|
|
$registry = WPDO_Schema_Registry::instance();
|
|
$this->adapter->on_register_fields( $registry );
|
|
|
|
// Listing hot fields.
|
|
$drafted = $registry->get_field( 'hp_listing', 'hp_drafted' );
|
|
$this->assertIsArray( $drafted );
|
|
$this->assertSame( 'hot', $drafted['zone'] );
|
|
$this->assertTrue( (bool) $drafted['indexed'] );
|
|
|
|
$expired = $registry->get_field( 'hp_listing', 'hp_expired_time' );
|
|
$this->assertSame( 'hot', $expired['zone'] );
|
|
$this->assertStringContainsString( 'bigint', $expired['data_type'] );
|
|
|
|
$featured_time = $registry->get_field( 'hp_listing', 'hp_featured_time' );
|
|
$this->assertSame( 'hot', $featured_time['zone'] );
|
|
|
|
// Vendor cold blob.
|
|
$vendor_image = $registry->get_field( 'hp_vendor', 'hp_image' );
|
|
$this->assertIsArray( $vendor_image );
|
|
$this->assertSame( 'cold', $vendor_image['zone'] );
|
|
}
|
|
|
|
public function test_suitability_score_aggregates_to_ten(): void {
|
|
$score = $this->adapter->suitability_score();
|
|
$this->assertArrayHasKey( 'aggregate', $score );
|
|
$this->assertSame( 10.0, $score['aggregate'] );
|
|
// All eight dimensions present.
|
|
foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $key ) {
|
|
$this->assertArrayHasKey( $key, $score );
|
|
$this->assertGreaterThanOrEqual( 0.0, $score[ $key ] );
|
|
$this->assertLessThanOrEqual( 1.0, $score[ $key ] );
|
|
}
|
|
}
|
|
|
|
public function test_doctor_check_reports_table_status(): void {
|
|
$result = $this->adapter->doctor_check();
|
|
$this->assertIsArray( $result );
|
|
$this->assertArrayHasKey( 'ok', $result );
|
|
$this->assertArrayHasKey( 'message', $result );
|
|
$this->assertArrayHasKey( 'details', $result );
|
|
// The stub $wpdb returns null for SHOW TABLES, so tables appear missing → ok=false.
|
|
$this->assertFalse( $result['ok'] );
|
|
}
|
|
|
|
public function test_optimize_query_extracts_hot_clauses(): void {
|
|
$registry = WPDO_Schema_Registry::instance();
|
|
$this->adapter->on_register_fields( $registry );
|
|
|
|
$query = new WP_Query( array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
array( 'key' => 'hp_drafted', 'value' => 1, 'compare' => '=' ),
|
|
array( 'key' => 'unrelated_key', 'value' => 'foo', 'compare' => '=' ),
|
|
),
|
|
) );
|
|
|
|
$this->adapter->optimize_query( $query );
|
|
|
|
// Hot clause should have been moved to wpdo_hot_clauses.
|
|
$hot = $query->get( 'wpdo_hot_clauses' );
|
|
// Note: in the test env the WPDO_Feature_Flags check may short-circuit
|
|
// (no module active), so the assertion is "either the clause moved OR
|
|
// it stayed put; never silently corrupted".
|
|
$remaining = (array) $query->get( 'meta_query' );
|
|
$this->assertIsArray( $remaining );
|
|
// `unrelated_key` must always remain in meta_query regardless of routing.
|
|
$found_unrelated = false;
|
|
foreach ( $remaining as $clause ) {
|
|
if ( is_array( $clause ) && ( $clause['key'] ?? '' ) === 'unrelated_key' ) {
|
|
$found_unrelated = true;
|
|
break;
|
|
}
|
|
}
|
|
$this->assertTrue( $found_unrelated, 'Non-hot meta_query clause must always be preserved.' );
|
|
// hot_clauses is either populated (when feature flag active) or empty (when not).
|
|
$this->assertTrue( $hot === '' || is_array( $hot ) );
|
|
}
|
|
|
|
public function test_optimize_query_handles_empty_meta_query(): void {
|
|
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
|
$this->adapter->optimize_query( $query );
|
|
// Must not crash; hot_clauses should remain unset.
|
|
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
|
}
|
|
|
|
public function test_optimize_search_delegates_to_optimize_query(): void {
|
|
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
|
// Delegation is implementation detail; verify just that it doesn't crash
|
|
// and that an irrelevant attribute_fields argument is ignored.
|
|
$this->adapter->optimize_search( $query, array() );
|
|
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
|
}
|
|
}
|