Files
2meet-data-optimizer-hivepr…/tests/unit/AttributeBridgeTest.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

99 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Attribute_Bridge.
*
* @covers WPDO_HivePress_Attribute_Bridge
*/
class HivePressAttributeBridgeTest extends TestCase {
private WPDO_HivePress_Attribute_Bridge $bridge;
protected function setUp(): void {
$this->bridge = new WPDO_HivePress_Attribute_Bridge();
}
public function test_discover_returns_empty_when_no_attributes_registered(): void {
// apply_filters stub returns the value unchanged → empty array.
$this->assertSame( array(), $this->bridge->discover_attributes() );
}
public function test_suggest_zone_configs_returns_empty_when_no_attributes(): void {
$this->assertSame( array(), $this->bridge->suggest_zone_configs() );
}
/**
* Verify the SQL type mapping is sensible for known HivePress field types.
*
* Indirect test via reflection — the public API only returns full configs
* but we want fast feedback if the type table drifts.
*/
public function test_sql_type_mapping_via_reflection(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'sql_type_for' );
$method->setAccessible( true );
$this->assertStringContainsString( 'int', $method->invoke( $this->bridge, 'number' ) );
$this->assertStringContainsString( 'decimal', $method->invoke( $this->bridge, 'price' ) );
$this->assertStringContainsString( 'tinyint', $method->invoke( $this->bridge, 'checkbox' ) );
$this->assertStringContainsString( 'date', $method->invoke( $this->bridge, 'date' ) );
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'text' ) );
// Unknown type → fallback to varchar.
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'unknown_type' ) );
}
/**
* Verify build_record normalises HP attribute config correctly.
*/
public function test_build_record_normalises_attribute_config(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'build_record' );
$method->setAccessible( true );
$record = $method->invoke( $this->bridge, 'listing', 'beds', array(
'edit_field' => array( 'type' => 'integer' ),
'filterable' => true,
'sortable' => false,
'searchable' => true,
) );
$this->assertSame( 'listing', $record['model'] );
$this->assertSame( 'beds', $record['slug'] );
$this->assertSame( 'hp_listing_beds', $record['meta_key'] );
$this->assertSame( 'integer', $record['type'] );
$this->assertTrue( $record['filterable'] );
$this->assertFalse( $record['sortable'] );
}
/**
* Promotion candidate logic: filterable OR sortable triggers promotion.
*/
public function test_promotion_candidate_logic(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'is_promotion_candidate' );
$method->setAccessible( true );
// Filterable but not sortable → promote.
$this->assertTrue( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => true, 'sortable' => false, 'searchable' => true,
) ) );
// Sortable but not filterable → promote.
$this->assertTrue( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => false, 'sortable' => true, 'searchable' => false,
) ) );
// Searchable only (no filter, no sort) → don't promote (full-text would
// be a different optimization).
$this->assertFalse( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => false, 'sortable' => false, 'searchable' => true,
) ) );
}
}