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

80 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_REST.
*
* @covers WPDO_HivePress_REST
*/
class HivePressRestTest extends TestCase {
private WPDO_HivePress_REST $rest;
protected function setUp(): void {
WPDO_HivePress_Bootstrap::reset_for_tests();
WPDO_HivePress_Detector::reset_for_tests();
// Default: caller is admin so permission check passes in unit env.
$GLOBALS['_wp_current_user_can']['manage_options'] = true;
$this->rest = new WPDO_HivePress_REST();
}
public function test_namespace_constant(): void {
$this->assertSame( 'wpdo/v1', WPDO_HivePress_REST::NAMESPACE );
}
public function test_register_routes_does_not_throw(): void {
// register_rest_route is stubbed in tests/bootstrap.php → returns true.
$this->rest->register_routes();
$this->assertTrue( true );
}
public function test_check_permission_returns_true_for_admin(): void {
$this->assertTrue( $this->rest->check_permission() );
}
public function test_check_permission_returns_error_for_non_admin(): void {
$GLOBALS['_wp_current_user_can']['manage_options'] = false;
$result = $this->rest->check_permission();
$this->assertInstanceOf( WP_Error::class, $result );
}
public function test_get_status_returns_addon_catalog(): void {
$response = $this->rest->get_status();
$this->assertInstanceOf( WP_REST_Response::class, $response );
$data = $response->get_data();
$this->assertArrayHasKey( 'addons', $data );
$this->assertArrayHasKey( 'conflicts', $data );
$this->assertArrayHasKey( 'totals', $data );
// Catalog should list all 13 known addons regardless of detection state.
$this->assertSame( 13, $data['totals']['catalog'] );
// Without HivePress installed, detected = 0.
$this->assertSame( 0, $data['totals']['detected'] );
}
public function test_get_score_returns_report_structure(): void {
$response = $this->rest->get_score();
$this->assertInstanceOf( WP_REST_Response::class, $response );
$data = $response->get_data();
$this->assertArrayHasKey( 'aggregate', $data );
$this->assertArrayHasKey( 'adapter_count', $data );
$this->assertArrayHasKey( 'per_dimension', $data );
$this->assertArrayHasKey( 'adapters', $data );
}
public function test_get_health_returns_ok_when_no_adapters_active(): void {
$response = $this->rest->get_health();
$this->assertInstanceOf( WP_REST_Response::class, $response );
$data = $response->get_data();
$this->assertArrayHasKey( 'ok', $data );
$this->assertArrayHasKey( 'probes', $data );
// No active adapters → vacuously ok.
$this->assertTrue( $data['ok'] );
$this->assertSame( array(), $data['probes'] );
}
}