fix: add missing WordPress function stubs to unit tests

- ContentAnalyzerTest: pass Cost_Tracker as 2nd constructor arg
- IdeaScorerTest: stub get_option for topic_exclusions
- CostTrackerTest: stub get_option for monthly_budget_usd
- OpenRouterPricingTest: stub get_transient with cached model data
- OpenRouterProviderTest: stub get_option for API key + get_transient

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-12 08:05:43 +07:00
parent ab8d38a9c8
commit ece032efe5
5 changed files with 118 additions and 47 deletions

View File

@@ -20,18 +20,43 @@ class ContentAnalyzerTest extends BaseTestCase {
*/
private $mock_provider;
/**
* @var \PHPUnit\Framework\MockObject\MockObject Mock cost tracker.
*/
private $mock_cost_tracker;
protected function setUp(): void {
parent::setUp();
// Create a mock that implements the LLM Provider Interface.
$this->mock_provider = $this->createMock( \PRAutoBlogger_LLM_Provider_Interface::class );
// Create a mock cost tracker — ContentAnalyzer requires both provider and tracker.
$this->mock_cost_tracker = $this->createMock( \PRAutoBlogger_Cost_Tracker::class );
// Stub WordPress functions used internally by ContentAnalyzer.
$this->stub_get_option( [
'prautoblogger_monthly_budget_usd' => '50.00',
] );
// Stub $wpdb for any database calls.
$wpdb = $this->create_mock_wpdb();
$wpdb->method( 'prepare' )->willReturn( 'prepared' );
$wpdb->method( 'get_var' )->willReturn( '0' );
$wpdb->method( 'get_results' )->willReturn( [] );
$GLOBALS['wpdb'] = $wpdb;
}
protected function tearDown(): void {
unset( $GLOBALS['wpdb'] );
parent::tearDown();
}
/**
* Test that ContentAnalyzer can be instantiated.
*/
public function test_content_analyzer_instantiation(): void {
$analyzer = new \PRAutoBlogger_Content_Analyzer( $this->mock_provider );
$analyzer = new \PRAutoBlogger_Content_Analyzer( $this->mock_provider, $this->mock_cost_tracker );
$this->assertInstanceOf( \PRAutoBlogger_Content_Analyzer::class, $analyzer );
}
@@ -53,7 +78,7 @@ class ContentAnalyzerTest extends BaseTestCase {
$this->mock_provider->method( 'get_available_models' )
->willReturn( [ 'model/test' ] );
$analyzer = new \PRAutoBlogger_Content_Analyzer( $this->mock_provider );
$analyzer = new \PRAutoBlogger_Content_Analyzer( $this->mock_provider, $this->mock_cost_tracker );
// analyze_recent_data takes no parameters.
$result = $analyzer->analyze_recent_data();

View File

@@ -25,6 +25,14 @@ class CostTrackerTest extends BaseTestCase {
$this->wpdb = $this->create_mock_wpdb();
$GLOBALS['wpdb'] = $this->wpdb;
// CostTracker methods call get_option for budget settings.
$this->stub_get_option( [
'prautoblogger_monthly_budget_usd' => '50.00',
] );
// Stub current_time used in date-based queries.
$this->stub_current_time( '2026-04-12 10:00:00' );
}
protected function tearDown(): void {

View File

@@ -14,6 +14,15 @@ use Brain\Monkey\Functions;
class IdeaScorerTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
// IdeaScorer::score_and_rank() calls get_option('prautoblogger_topic_exclusions', '[]').
$this->stub_get_option( [
'prautoblogger_topic_exclusions' => '[]',
] );
}
/**
* Test Idea Scorer can be instantiated.
*/