Files
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

93 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Comment_Router.
*
* @covers WPDO_HivePress_Comment_Router
*/
class HivePressCommentRouterTest extends TestCase {
use WpdbMockTrait;
protected function setUp(): void {
$this->install_wpdb_mock();
WPDO_HivePress_Comment_Router::reset_for_tests();
// Default disabled.
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 0;
}
public function test_disabled_by_default(): void {
$this->assertFalse( WPDO_HivePress_Comment_Router::is_enabled() );
}
public function test_enabled_when_option_set(): void {
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
$this->assertTrue( WPDO_HivePress_Comment_Router::is_enabled() );
}
public function test_register_is_idempotent(): void {
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
WPDO_HivePress_Comment_Router::register();
WPDO_HivePress_Comment_Router::register(); // Second call must be no-op.
$this->assertTrue( true ); // No throw = pass
}
public function test_rewrite_skips_unknown_comment_type(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'comment' ); // not in shadow map
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
$this->assertSame( $clauses, $out );
}
public function test_rewrite_adds_left_join_for_hp_favorite(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'hp_favorite' );
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
$this->assertStringContainsString( 'LEFT JOIN', $out['join'] );
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $out['join'] );
$this->assertStringContainsString( 'AS wpdo_shadow', $out['join'] );
}
public function test_rewrite_promotes_recipient_for_hp_message(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'hp_message' );
$clauses = array(
'join' => '',
'where' => 'wp_comments.comment_karma = 42 AND comment_approved = 0',
);
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
// recipient lookup should be rewritten to use shadow column.
$this->assertStringContainsString( 'wpdo_shadow.recipient_id = 42', $out['where'] );
// Other predicates preserved.
$this->assertStringContainsString( 'comment_approved = 0', $out['where'] );
}
public function test_rewrite_handles_array_type(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => array( 'hp_review', 'comment' ) );
$clauses = array( 'join' => '', 'where' => '' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
// First element of type array should match (hp_review).
$this->assertStringContainsString( 'wpdo_comment_hp_review', $out['join'] );
}
public function test_rewrite_returns_unchanged_when_query_invalid(): void {
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, 'not an object' );
$this->assertSame( $clauses, $out );
}
}