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:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_Admin_HivePress.
|
||||
*
|
||||
* @covers WPDO_Admin_HivePress
|
||||
*/
|
||||
class AdminHivePressTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
}
|
||||
|
||||
public function test_render_outputs_addon_catalog(): void {
|
||||
$out = self::capture( static function () {
|
||||
WPDO_Admin_HivePress::render();
|
||||
} );
|
||||
|
||||
$this->assertStringContainsString( 'HivePress', $out );
|
||||
$this->assertStringContainsString( 'hivepress', $out );
|
||||
$this->assertStringContainsString( 'wp wpdo hivepress', $out );
|
||||
}
|
||||
|
||||
public function test_render_shows_clean_when_no_conflicts(): void {
|
||||
$out = self::capture( static function () {
|
||||
WPDO_Admin_HivePress::render();
|
||||
} );
|
||||
|
||||
$this->assertStringContainsString( 'Conflict guard', $out );
|
||||
$this->assertStringContainsString( 'notice-success', $out );
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture echo output of a callable in a single buffer level.
|
||||
*
|
||||
* Avoids PHPUnit's "did not close output buffers" warning by using
|
||||
* the level-checked open/close pattern.
|
||||
*/
|
||||
private static function capture( callable $callable ): string {
|
||||
$start_level = ob_get_level();
|
||||
ob_start();
|
||||
try {
|
||||
$callable();
|
||||
} finally {
|
||||
$out = '';
|
||||
while ( ob_get_level() > $start_level ) {
|
||||
$out = (string) ob_get_clean() . $out;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user