fix: rewrite all unit tests to match actual class signatures

All 14 test files updated to use correct constructor patterns (array-based
value objects, dependency injection for ContentAnalyzer, etc.) and verified
method names against source.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-12 01:43:02 +07:00
parent 83567dcb1f
commit ab8d38a9c8
15 changed files with 1375 additions and 970 deletions

View File

@@ -11,59 +11,116 @@ use PRAutoBlogger\Tests\BaseTestCase;
class AnalysisResultTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-analysis-result.php';
/**
* Test construction with array.
*/
public function test_constructor_with_array(): void {
$data = $this->get_analysis_result_fixture();
$result = new \PRAutoBlogger_Analysis_Result( $data );
$this->assertSame( 1, $result->get_id() );
$this->assertSame( 'trending_topic', $result->get_analysis_type() );
$this->assertSame( 'Test Topic', $result->get_topic() );
$this->assertSame( 'This is a summary.', $result->get_summary() );
$this->assertSame( 10, $result->get_frequency() );
$this->assertSame( 0.85, $result->get_relevance_score() );
$this->assertSame( [ 1, 2, 3 ], $result->get_source_ids() );
$this->assertSame( '2026-04-12 10:00:00', $result->get_analyzed_at() );
$this->assertSame( [ 'key' => 'value' ], $result->get_metadata() );
}
/**
* Test construction with valid analysis data.
* Test getters return correct types.
*/
public function test_constructor_sets_all_properties(): void {
public function test_getters_return_correct_types(): void {
$result = new \PRAutoBlogger_Analysis_Result(
'peptide-research',
[ 'BPC-157', 'healing' ],
8,
'High relevance to target audience.',
[ 'regenerative medicine', 'peptide therapy' ]
$this->get_analysis_result_fixture()
);
$this->assertSame( 'peptide-research', $result->get_category() );
$this->assertSame( [ 'BPC-157', 'healing' ], $result->get_keywords() );
$this->assertSame( 8, $result->get_relevance_score() );
$this->assertSame( 'High relevance to target audience.', $result->get_summary() );
$this->assertSame( [ 'regenerative medicine', 'peptide therapy' ], $result->get_topics() );
$this->assertIsInt( $result->get_id() );
$this->assertIsString( $result->get_analysis_type() );
$this->assertIsString( $result->get_topic() );
$this->assertIsString( $result->get_summary() );
$this->assertIsInt( $result->get_frequency() );
$this->assertIsFloat( $result->get_relevance_score() );
$this->assertIsArray( $result->get_source_ids() );
$this->assertIsString( $result->get_analyzed_at() );
}
/**
* Test relevance score is clamped between 0 and 10.
* Test nullable summary field.
*/
public function test_relevance_score_boundaries(): void {
$high = new \PRAutoBlogger_Analysis_Result( 'cat', [], 10, 'Max score.', [] );
$this->assertSame( 10, $high->get_relevance_score() );
public function test_nullable_summary(): void {
$data = $this->get_analysis_result_fixture();
$data['summary'] = null;
$low = new \PRAutoBlogger_Analysis_Result( 'cat', [], 0, 'Min score.', [] );
$this->assertSame( 0, $low->get_relevance_score() );
$result = new \PRAutoBlogger_Analysis_Result( $data );
$this->assertNull( $result->get_summary() );
}
/**
* Test to_array returns complete representation.
* Test nullable metadata field.
*/
public function test_to_array_completeness(): void {
public function test_nullable_metadata(): void {
$data = $this->get_analysis_result_fixture();
$data['metadata'] = null;
$result = new \PRAutoBlogger_Analysis_Result( $data );
$this->assertNull( $result->get_metadata() );
}
/**
* Test to_db_row returns array.
*/
public function test_to_db_row_returns_array(): void {
$result = new \PRAutoBlogger_Analysis_Result(
'news',
[ 'test' ],
5,
'Summary text.',
[ 'topic1' ]
$this->get_analysis_result_fixture()
);
$array = $result->to_array();
$this->assertIsArray( $array );
$this->assertArrayHasKey( 'category', $array );
$this->assertArrayHasKey( 'keywords', $array );
$this->assertArrayHasKey( 'relevance_score', $array );
$this->assertArrayHasKey( 'summary', $array );
$this->assertArrayHasKey( 'topics', $array );
$row = $result->to_db_row();
$this->assertIsArray( $row );
$this->assertArrayHasKey( 'analysis_type', $row );
$this->assertArrayHasKey( 'topic', $row );
$this->assertArrayHasKey( 'frequency', $row );
}
/**
* Test with minimal data.
*/
public function test_with_minimal_data(): void {
$data = [
'id' => 1,
'analysis_type' => 'topic',
'topic' => 'Test',
'summary' => null,
'frequency' => 0,
'relevance_score' => 0.0,
'source_ids' => [],
'analyzed_at' => '2026-04-12 10:00:00',
'metadata' => null,
];
$result = new \PRAutoBlogger_Analysis_Result( $data );
$this->assertSame( 1, $result->get_id() );
$this->assertSame( 0, $result->get_frequency() );
$this->assertEmpty( $result->get_source_ids() );
}
/**
* Test source_ids is always an array.
*/
public function test_source_ids_always_array(): void {
$data = $this->get_analysis_result_fixture();
$data['source_ids'] = [];
$result = new \PRAutoBlogger_Analysis_Result( $data );
$this->assertIsArray( $result->get_source_ids() );
$this->assertEmpty( $result->get_source_ids() );
}
}

View File

@@ -11,63 +11,81 @@ use PRAutoBlogger\Tests\BaseTestCase;
class ArticleIdeaTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-article-idea.php';
/**
* Test construction with array.
*/
public function test_constructor_with_array(): void {
$data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $data );
$this->assertSame( 'Test Article Topic', $idea->get_topic() );
$this->assertSame( 'guide', $idea->get_article_type() );
$this->assertSame( 'Ultimate Guide to Test Topic', $idea->get_suggested_title() );
$this->assertSame( 'A comprehensive guide to test topic.', $idea->get_summary() );
$this->assertSame( 0.92, $idea->get_score() );
$this->assertSame( 1, $idea->get_analysis_id() );
$this->assertSame( [ 1, 2 ], $idea->get_source_ids() );
$this->assertSame( [ 'Point 1', 'Point 2', 'Point 3' ], $idea->get_key_points() );
$this->assertSame( [ 'test', 'keyword', 'example' ], $idea->get_target_keywords() );
}
/**
* Test construction with full data.
* Test getters return correct types.
*/
public function test_constructor_sets_all_properties(): void {
public function test_getters_return_correct_types(): void {
$idea = new \PRAutoBlogger_Article_Idea(
'The Future of BPC-157 Research',
'An in-depth look at recent clinical trials.',
[ 'BPC-157', 'clinical trials', 'peptides' ],
'peptide-research',
85.5
$this->get_article_idea_fixture()
);
$this->assertSame( 'The Future of BPC-157 Research', $idea->get_title() );
$this->assertSame( 'An in-depth look at recent clinical trials.', $idea->get_description() );
$this->assertSame( [ 'BPC-157', 'clinical trials', 'peptides' ], $idea->get_keywords() );
$this->assertSame( 'peptide-research', $idea->get_category() );
$this->assertSame( 85.5, $idea->get_score() );
$this->assertIsString( $idea->get_topic() );
$this->assertIsString( $idea->get_article_type() );
$this->assertIsString( $idea->get_suggested_title() );
$this->assertIsString( $idea->get_summary() );
$this->assertIsFloat( $idea->get_score() );
$this->assertIsInt( $idea->get_analysis_id() );
$this->assertIsArray( $idea->get_source_ids() );
$this->assertIsArray( $idea->get_key_points() );
$this->assertIsArray( $idea->get_target_keywords() );
}
/**
* Test to_array returns all fields.
* Test with empty arrays.
*/
public function test_to_array_returns_complete_data(): void {
$idea = new \PRAutoBlogger_Article_Idea(
'Test Title',
'Test description.',
[ 'kw1' ],
'general',
50.0
);
public function test_with_empty_arrays(): void {
$data = $this->get_article_idea_fixture();
$data['source_ids'] = [];
$data['key_points'] = [];
$data['target_keywords'] = [];
$array = $idea->to_array();
$this->assertIsArray( $array );
$this->assertArrayHasKey( 'title', $array );
$this->assertArrayHasKey( 'description', $array );
$this->assertArrayHasKey( 'keywords', $array );
$this->assertArrayHasKey( 'category', $array );
$this->assertArrayHasKey( 'score', $array );
$idea = new \PRAutoBlogger_Article_Idea( $data );
$this->assertEmpty( $idea->get_source_ids() );
$this->assertEmpty( $idea->get_key_points() );
$this->assertEmpty( $idea->get_target_keywords() );
}
/**
* Test score can be zero.
* Test with zero score.
*/
public function test_allows_zero_score(): void {
$idea = new \PRAutoBlogger_Article_Idea(
'Low Score Idea',
'Not great.',
[],
'general',
0.0
);
public function test_with_zero_score(): void {
$data = $this->get_article_idea_fixture();
$data['score'] = 0.0;
$idea = new \PRAutoBlogger_Article_Idea( $data );
$this->assertSame( 0.0, $idea->get_score() );
}
/**
* Test with high score.
*/
public function test_with_high_score(): void {
$data = $this->get_article_idea_fixture();
$data['score'] = 1.0;
$idea = new \PRAutoBlogger_Article_Idea( $data );
$this->assertSame( 1.0, $idea->get_score() );
}
}

View File

@@ -11,60 +11,103 @@ use PRAutoBlogger\Tests\BaseTestCase;
class ContentRequestTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-content-request.php';
}
/**
* Test construction with required fields.
* Test construction with Article Idea and parameters.
*/
public function test_constructor_sets_all_properties(): void {
public function test_constructor_with_idea(): void {
$idea_data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $idea_data );
$request = new \PRAutoBlogger_Content_Request(
'Write about BPC-157 benefits',
'single_pass',
'google/gemini-2.0-flash-001',
[ 'tone' => 'informative', 'length' => 'long' ]
$idea,
'auto',
'professional',
1000,
2500,
'Technology and innovation',
[ 'politics', 'religion' ]
);
$this->assertSame( 'Write about BPC-157 benefits', $request->get_prompt() );
$this->assertSame( 'single_pass', $request->get_pipeline_mode() );
$this->assertSame( 'google/gemini-2.0-flash-001', $request->get_model() );
$this->assertSame( 'informative', $request->get_option( 'tone' ) );
$this->assertSame( 'long', $request->get_option( 'length' ) );
$this->assertSame( $idea, $request->get_idea() );
$this->assertSame( 'auto', $request->get_pipeline_mode() );
$this->assertSame( 'professional', $request->get_tone() );
$this->assertSame( 1000, $request->get_min_word_count() );
$this->assertSame( 2500, $request->get_max_word_count() );
$this->assertSame( 'Technology and innovation', $request->get_niche_description() );
$this->assertSame( [ 'politics', 'religion' ], $request->get_topic_exclusions() );
}
/**
* Test get_option returns default for missing keys.
* Test getters return correct types.
*/
public function test_get_option_returns_default_for_missing_key(): void {
public function test_getters_return_correct_types(): void {
$idea_data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $idea_data );
$request = new \PRAutoBlogger_Content_Request(
'prompt',
'single_pass',
'model/name',
$idea,
'auto',
'casual',
500,
1500,
'Niche',
[]
);
$this->assertNull( $request->get_option( 'nonexistent' ) );
$this->assertSame( 'fallback', $request->get_option( 'nonexistent', 'fallback' ) );
$this->assertInstanceOf( \PRAutoBlogger_Article_Idea::class, $request->get_idea() );
$this->assertIsString( $request->get_pipeline_mode() );
$this->assertIsString( $request->get_tone() );
$this->assertIsInt( $request->get_min_word_count() );
$this->assertIsInt( $request->get_max_word_count() );
$this->assertIsString( $request->get_niche_description() );
$this->assertIsArray( $request->get_topic_exclusions() );
}
/**
* Test to_array completeness.
* Test with empty topic exclusions.
*/
public function test_to_array_returns_all_fields(): void {
public function test_with_empty_exclusions(): void {
$idea_data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $idea_data );
$request = new \PRAutoBlogger_Content_Request(
'test prompt',
'multi_step',
'model/x',
[ 'key' => 'val' ]
$idea,
'auto',
'professional',
1000,
2500,
'Tech',
[]
);
$array = $request->to_array();
$this->assertIsArray( $array );
$this->assertArrayHasKey( 'prompt', $array );
$this->assertArrayHasKey( 'pipeline_mode', $array );
$this->assertArrayHasKey( 'model', $array );
$this->assertArrayHasKey( 'options', $array );
$this->assertEmpty( $request->get_topic_exclusions() );
}
/**
* Test with different pipeline modes.
*/
public function test_different_pipeline_modes(): void {
$idea_data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $idea_data );
$auto = new \PRAutoBlogger_Content_Request( $idea, 'auto', 'professional', 1000, 2500, 'niche', [] );
$this->assertSame( 'auto', $auto->get_pipeline_mode() );
$manual = new \PRAutoBlogger_Content_Request( $idea, 'manual', 'casual', 1000, 2500, 'niche', [] );
$this->assertSame( 'manual', $manual->get_pipeline_mode() );
}
/**
* Test with different tones.
*/
public function test_different_tones(): void {
$idea_data = $this->get_article_idea_fixture();
$idea = new \PRAutoBlogger_Article_Idea( $idea_data );
$professional = new \PRAutoBlogger_Content_Request( $idea, 'auto', 'professional', 1000, 2500, 'niche', [] );
$this->assertSame( 'professional', $professional->get_tone() );
$casual = new \PRAutoBlogger_Content_Request( $idea, 'auto', 'casual', 1000, 2500, 'niche', [] );
$this->assertSame( 'casual', $casual->get_tone() );
}
}

View File

@@ -11,52 +11,129 @@ use PRAutoBlogger\Tests\BaseTestCase;
class ContentScoreTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-content-score.php';
/**
* Test construction with array.
*/
public function test_constructor_with_array(): void {
$data = $this->get_content_score_fixture();
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 1, $score->get_id() );
$this->assertSame( 123, $score->get_post_id() );
$this->assertSame( 500, $score->get_pageviews() );
$this->assertSame( 3.5, $score->get_avg_time_on_page() );
$this->assertSame( 0.25, $score->get_bounce_rate() );
$this->assertSame( 15, $score->get_comment_count() );
$this->assertSame( 0.82, $score->get_composite_score() );
$this->assertIsArray( $score->get_score_factors() );
$this->assertSame( '2026-04-12 10:00:00', $score->get_measured_at() );
}
/**
* Test construction with scoring dimensions.
* Test getters return correct types.
*/
public function test_constructor_sets_all_dimensions(): void {
public function test_getters_return_correct_types(): void {
$score = new \PRAutoBlogger_Content_Score(
8.0, // relevance
7.5, // quality
9.0, // originality
6.5, // seo
7.75 // overall
$this->get_content_score_fixture()
);
$this->assertSame( 8.0, $score->get_relevance() );
$this->assertSame( 7.5, $score->get_quality() );
$this->assertSame( 9.0, $score->get_originality() );
$this->assertSame( 6.5, $score->get_seo() );
$this->assertSame( 7.75, $score->get_overall() );
$this->assertIsInt( $score->get_id() );
$this->assertIsInt( $score->get_post_id() );
$this->assertIsInt( $score->get_pageviews() );
$this->assertIsFloat( $score->get_avg_time_on_page() );
$this->assertIsFloat( $score->get_bounce_rate() );
$this->assertIsInt( $score->get_comment_count() );
$this->assertIsFloat( $score->get_composite_score() );
$this->assertIsArray( $score->get_score_factors() );
$this->assertIsString( $score->get_measured_at() );
}
/**
* Test boundary scores (0 and 10).
* Test with zero pageviews.
*/
public function test_boundary_scores(): void {
$min = new \PRAutoBlogger_Content_Score( 0.0, 0.0, 0.0, 0.0, 0.0 );
$this->assertSame( 0.0, $min->get_overall() );
public function test_with_zero_pageviews(): void {
$data = $this->get_content_score_fixture();
$data['pageviews'] = 0;
$max = new \PRAutoBlogger_Content_Score( 10.0, 10.0, 10.0, 10.0, 10.0 );
$this->assertSame( 10.0, $max->get_overall() );
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 0, $score->get_pageviews() );
}
/**
* Test to_array completeness.
* Test with zero composite score.
*/
public function test_to_array_returns_all_dimensions(): void {
$score = new \PRAutoBlogger_Content_Score( 5.0, 5.0, 5.0, 5.0, 5.0 );
public function test_with_zero_composite_score(): void {
$data = $this->get_content_score_fixture();
$data['composite_score'] = 0.0;
$array = $score->to_array();
$this->assertArrayHasKey( 'relevance', $array );
$this->assertArrayHasKey( 'quality', $array );
$this->assertArrayHasKey( 'originality', $array );
$this->assertArrayHasKey( 'seo', $array );
$this->assertArrayHasKey( 'overall', $array );
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 0.0, $score->get_composite_score() );
}
/**
* Test with perfect composite score.
*/
public function test_with_perfect_composite_score(): void {
$data = $this->get_content_score_fixture();
$data['composite_score'] = 1.0;
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 1.0, $score->get_composite_score() );
}
/**
* Test with zero bounce rate.
*/
public function test_with_zero_bounce_rate(): void {
$data = $this->get_content_score_fixture();
$data['bounce_rate'] = 0.0;
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 0.0, $score->get_bounce_rate() );
}
/**
* Test with high bounce rate.
*/
public function test_with_high_bounce_rate(): void {
$data = $this->get_content_score_fixture();
$data['bounce_rate'] = 0.95;
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertSame( 0.95, $score->get_bounce_rate() );
}
/**
* Test with empty score factors.
*/
public function test_with_empty_score_factors(): void {
$data = $this->get_content_score_fixture();
$data['score_factors'] = [];
$score = new \PRAutoBlogger_Content_Score( $data );
$this->assertEmpty( $score->get_score_factors() );
}
/**
* Test to_db_row returns array.
*/
public function test_to_db_row_returns_array(): void {
$score = new \PRAutoBlogger_Content_Score(
$this->get_content_score_fixture()
);
$row = $score->to_db_row();
$this->assertIsArray( $row );
$this->assertArrayHasKey( 'post_id', $row );
$this->assertArrayHasKey( 'pageviews', $row );
$this->assertArrayHasKey( 'composite_score', $row );
}
}

View File

@@ -11,60 +11,105 @@ use PRAutoBlogger\Tests\BaseTestCase;
class EditorialReviewTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-editorial-review.php';
}
/**
* Test construction with approval.
* Test construction with array.
*/
public function test_approved_review(): void {
$review = new \PRAutoBlogger_Editorial_Review(
true,
'Content meets quality standards.',
8.5,
[ 'Good structure', 'Clear language' ],
[]
);
public function test_constructor_with_array(): void {
$data = $this->get_editorial_review_fixture();
$this->assertTrue( $review->is_approved() );
$this->assertSame( 'Content meets quality standards.', $review->get_feedback() );
$this->assertSame( 8.5, $review->get_quality_score() );
$this->assertSame( [ 'Good structure', 'Clear language' ], $review->get_strengths() );
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 'approved', $review->get_verdict() );
$this->assertSame( 'Article is well-structured.', $review->get_notes() );
$this->assertNull( $review->get_revised_content() );
$this->assertSame( 0.88, $review->get_quality_score() );
$this->assertSame( 0.92, $review->get_seo_score() );
$this->assertEmpty( $review->get_issues() );
}
/**
* Test construction with rejection and issues.
* Test getters return correct types.
*/
public function test_rejected_review_with_issues(): void {
public function test_getters_return_correct_types(): void {
$review = new \PRAutoBlogger_Editorial_Review(
false,
'Content needs improvement.',
4.0,
[],
[ 'Too short', 'Missing citations', 'Unclear conclusion' ]
$this->get_editorial_review_fixture()
);
$this->assertFalse( $review->is_approved() );
$this->assertCount( 3, $review->get_issues() );
$this->assertSame( 4.0, $review->get_quality_score() );
$this->assertIsString( $review->get_verdict() );
$this->assertIsString( $review->get_notes() );
$this->assertIsFloat( $review->get_quality_score() );
$this->assertIsFloat( $review->get_seo_score() );
$this->assertIsArray( $review->get_issues() );
}
/**
* Test to_array completeness.
* Test with revision notes.
*/
public function test_to_array_returns_all_fields(): void {
$review = new \PRAutoBlogger_Editorial_Review(
true, 'OK', 7.0, [ 'Good' ], []
);
public function test_with_revised_content(): void {
$data = $this->get_editorial_review_fixture();
$data['revised_content'] = 'Here is the revised article content.';
$array = $review->to_array();
$this->assertArrayHasKey( 'approved', $array );
$this->assertArrayHasKey( 'feedback', $array );
$this->assertArrayHasKey( 'quality_score', $array );
$this->assertArrayHasKey( 'strengths', $array );
$this->assertArrayHasKey( 'issues', $array );
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 'Here is the revised article content.', $review->get_revised_content() );
}
/**
* Test with issues.
*/
public function test_with_issues(): void {
$data = $this->get_editorial_review_fixture();
$data['verdict'] = 'revision_requested';
$data['issues'] = [ 'Grammar issue on line 5', 'Missing citation for claim' ];
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 'revision_requested', $review->get_verdict() );
$this->assertCount( 2, $review->get_issues() );
$this->assertStringContainsString( 'Grammar', $review->get_issues()[0] );
}
/**
* Test with rejected verdict.
*/
public function test_with_rejected_verdict(): void {
$data = $this->get_editorial_review_fixture();
$data['verdict'] = 'rejected';
$data['quality_score'] = 0.35;
$data['seo_score'] = 0.25;
$data['issues'] = [ 'Content is off-topic', 'Poor quality' ];
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 'rejected', $review->get_verdict() );
$this->assertLessThan( 0.5, $review->get_quality_score() );
}
/**
* Test with perfect scores.
*/
public function test_with_perfect_scores(): void {
$data = $this->get_editorial_review_fixture();
$data['quality_score'] = 1.0;
$data['seo_score'] = 1.0;
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 1.0, $review->get_quality_score() );
$this->assertSame( 1.0, $review->get_seo_score() );
}
/**
* Test with zero scores.
*/
public function test_with_zero_scores(): void {
$data = $this->get_editorial_review_fixture();
$data['quality_score'] = 0.0;
$data['seo_score'] = 0.0;
$review = new \PRAutoBlogger_Editorial_Review( $data );
$this->assertSame( 0.0, $review->get_quality_score() );
$this->assertSame( 0.0, $review->get_seo_score() );
}
}

View File

@@ -11,83 +11,125 @@ use PRAutoBlogger\Tests\BaseTestCase;
class GenerationLogTest extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-generation-log.php';
/**
* Test construction with array.
*/
public function test_constructor_with_array(): void {
$data = $this->get_generation_log_fixture();
$log = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 1, $log->get_id() );
$this->assertSame( 123, $log->get_post_id() );
$this->assertSame( 'run_abc123', $log->get_run_id() );
$this->assertSame( 'analysis', $log->get_stage() );
$this->assertSame( 'openrouter', $log->get_provider() );
$this->assertSame( 'openai/gpt-4', $log->get_model() );
$this->assertSame( 1000, $log->get_prompt_tokens() );
$this->assertSame( 500, $log->get_completion_tokens() );
$this->assertSame( 0.02, $log->get_estimated_cost() );
$this->assertSame( '{}', $log->get_request_json() );
$this->assertSame( 'success', $log->get_response_status() );
$this->assertNull( $log->get_error_message() );
$this->assertSame( '2026-04-12 10:00:00', $log->get_created_at() );
}
/**
* Test construction with all fields.
* Test getters return correct types.
*/
public function test_constructor_sets_all_properties(): void {
public function test_getters_return_correct_types(): void {
$log = new \PRAutoBlogger_Generation_Log(
'google/gemini-2.0-flash-001',
'content_generation',
500,
1200,
0.0034,
2.45,
true,
''
$this->get_generation_log_fixture()
);
$this->assertSame( 'google/gemini-2.0-flash-001', $log->get_model() );
$this->assertSame( 'content_generation', $log->get_step() );
$this->assertSame( 500, $log->get_prompt_tokens() );
$this->assertSame( 1200, $log->get_completion_tokens() );
$this->assertSame( 0.0034, $log->get_cost() );
$this->assertSame( 2.45, $log->get_duration() );
$this->assertTrue( $log->is_success() );
$this->assertSame( '', $log->get_error() );
$this->assertIsInt( $log->get_id() );
$this->assertIsInt( $log->get_post_id() );
$this->assertIsString( $log->get_run_id() );
$this->assertIsString( $log->get_stage() );
$this->assertIsString( $log->get_provider() );
$this->assertIsString( $log->get_model() );
$this->assertIsInt( $log->get_prompt_tokens() );
$this->assertIsInt( $log->get_completion_tokens() );
$this->assertIsFloat( $log->get_estimated_cost() );
$this->assertIsString( $log->get_request_json() );
$this->assertIsString( $log->get_response_status() );
$this->assertIsString( $log->get_created_at() );
}
/**
* Test failed generation log.
* Test with error message.
*/
public function test_failed_generation_log(): void {
$log = new \PRAutoBlogger_Generation_Log(
'anthropic/claude-3-haiku',
'analysis',
200,
0,
0.0,
0.0,
false,
'Rate limit exceeded'
);
public function test_with_error_message(): void {
$data = $this->get_generation_log_fixture();
$data['response_status'] = 'error';
$data['error_message'] = 'API rate limit exceeded';
$this->assertFalse( $log->is_success() );
$this->assertSame( 'Rate limit exceeded', $log->get_error() );
$log = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 'error', $log->get_response_status() );
$this->assertSame( 'API rate limit exceeded', $log->get_error_message() );
}
/**
* Test with zero tokens.
*/
public function test_with_zero_tokens(): void {
$data = $this->get_generation_log_fixture();
$data['prompt_tokens'] = 0;
$data['completion_tokens'] = 0;
$log = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 0, $log->get_prompt_tokens() );
$this->assertSame( 0, $log->get_completion_tokens() );
}
/**
* Test total tokens calculation.
* Test with zero cost.
*/
public function test_get_total_tokens(): void {
$log = new \PRAutoBlogger_Generation_Log(
'model/x', 'step', 300, 700, 0.01, 1.0, true, ''
);
public function test_with_zero_cost(): void {
$data = $this->get_generation_log_fixture();
$data['estimated_cost'] = 0.0;
$this->assertSame( 1000, $log->get_total_tokens() );
$log = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 0.0, $log->get_estimated_cost() );
}
/**
* Test to_array completeness.
* Test to_db_row returns array.
*/
public function test_to_array_returns_all_fields(): void {
public function test_to_db_row_returns_array(): void {
$log = new \PRAutoBlogger_Generation_Log(
'model', 'step', 100, 200, 0.001, 0.5, true, ''
$this->get_generation_log_fixture()
);
$array = $log->to_array();
$this->assertArrayHasKey( 'model', $array );
$this->assertArrayHasKey( 'step', $array );
$this->assertArrayHasKey( 'prompt_tokens', $array );
$this->assertArrayHasKey( 'completion_tokens', $array );
$this->assertArrayHasKey( 'cost', $array );
$this->assertArrayHasKey( 'duration', $array );
$this->assertArrayHasKey( 'success', $array );
$this->assertArrayHasKey( 'error', $array );
$row = $log->to_db_row();
$this->assertIsArray( $row );
$this->assertArrayHasKey( 'post_id', $row );
$this->assertArrayHasKey( 'run_id', $row );
$this->assertArrayHasKey( 'stage', $row );
$this->assertArrayHasKey( 'provider', $row );
$this->assertArrayHasKey( 'model', $row );
}
/**
* Test different response statuses.
*/
public function test_different_response_statuses(): void {
$data = $this->get_generation_log_fixture();
$data['response_status'] = 'success';
$success = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 'success', $success->get_response_status() );
$data['response_status'] = 'error';
$error = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 'error', $error->get_response_status() );
$data['response_status'] = 'timeout';
$timeout = new \PRAutoBlogger_Generation_Log( $data );
$this->assertSame( 'timeout', $timeout->get_response_status() );
}
}

View File

@@ -12,62 +12,138 @@ use PRAutoBlogger\Tests\BaseTestCase;
class SourceDataTest extends BaseTestCase {
/**
* Require the class under test.
* Test construction with minimal required fields.
*/
protected function setUp(): void {
parent::setUp();
require_once PRAB_PLUGIN_DIR . 'includes/models/class-prab-source-data.php';
public function test_constructor_with_array(): void {
$this->stub_current_time( '2026-04-12 10:00:00' );
$data = [
'id' => 1,
'source_type' => 'reddit',
'source_id' => 'post_123',
'subreddit' => 'test_subreddit',
'title' => 'Test Post Title',
'content' => 'This is the post content.',
'author' => 'test_author',
'score' => 100,
'comment_count' => 50,
'permalink' => '/r/test/comments/123',
'collected_at' => '2026-04-12 10:00:00',
'metadata' => [ 'key' => 'value' ],
];
$source = new \PRAutoBlogger_Source_Data( $data );
$this->assertSame( 1, $source->get_id() );
$this->assertSame( 'reddit', $source->get_source_type() );
$this->assertSame( 'post_123', $source->get_source_id() );
$this->assertSame( 'test_subreddit', $source->get_subreddit() );
$this->assertSame( 'Test Post Title', $source->get_title() );
$this->assertSame( 'This is the post content.', $source->get_content() );
$this->assertSame( 'test_author', $source->get_author() );
$this->assertSame( 100, $source->get_score() );
$this->assertSame( 50, $source->get_comment_count() );
$this->assertSame( '/r/test/comments/123', $source->get_permalink() );
$this->assertSame( '2026-04-12 10:00:00', $source->get_collected_at() );
$this->assertSame( [ 'key' => 'value' ], $source->get_metadata() );
}
/**
* Test construction with all required fields.
* Test getters return correct types.
*/
public function test_constructor_sets_all_properties(): void {
$data = new \PRAutoBlogger_Source_Data(
'Test Title',
'https://example.com/article',
'This is the content body.',
'rss'
public function test_getters_return_correct_types(): void {
$this->stub_current_time( '2026-04-12 10:00:00' );
$source = new \PRAutoBlogger_Source_Data(
$this->get_source_data_fixture()
);
$this->assertSame( 'Test Title', $data->get_title() );
$this->assertSame( 'https://example.com/article', $data->get_url() );
$this->assertSame( 'This is the content body.', $data->get_content() );
$this->assertSame( 'rss', $data->get_source_type() );
$this->assertIsInt( $source->get_id() );
$this->assertIsString( $source->get_source_type() );
$this->assertIsString( $source->get_source_id() );
$this->assertIsString( $source->get_title() );
$this->assertIsString( $source->get_content() );
$this->assertIsInt( $source->get_score() );
$this->assertIsInt( $source->get_comment_count() );
$this->assertIsString( $source->get_collected_at() );
}
/**
* Test that to_array returns all fields.
* Test nullable fields return null when absent.
*/
public function test_to_array_returns_complete_representation(): void {
$data = new \PRAutoBlogger_Source_Data(
'Array Test',
'https://example.com',
'Content here.',
'manual'
);
public function test_nullable_fields_return_null(): void {
$this->stub_current_time( '2026-04-12 10:00:00' );
$array = $data->to_array();
$data = [
'id' => 1,
'source_type' => 'reddit',
'source_id' => 'post_123',
'subreddit' => null,
'title' => null,
'content' => null,
'author' => null,
'score' => 0,
'comment_count' => 0,
'permalink' => null,
'collected_at' => '2026-04-12 10:00:00',
'metadata' => null,
];
$this->assertIsArray( $array );
$this->assertArrayHasKey( 'title', $array );
$this->assertArrayHasKey( 'url', $array );
$this->assertArrayHasKey( 'content', $array );
$this->assertArrayHasKey( 'source_type', $array );
$this->assertSame( 'Array Test', $array['title'] );
$source = new \PRAutoBlogger_Source_Data( $data );
$this->assertNull( $source->get_subreddit() );
$this->assertNull( $source->get_title() );
$this->assertNull( $source->get_content() );
$this->assertNull( $source->get_author() );
$this->assertNull( $source->get_permalink() );
$this->assertNull( $source->get_metadata() );
}
/**
* Test construction with empty content is allowed.
* Test to_db_row returns array representation.
*/
public function test_allows_empty_content(): void {
$data = new \PRAutoBlogger_Source_Data(
'Title Only',
'https://example.com',
'',
'rss'
public function test_to_db_row_returns_array(): void {
$this->stub_current_time( '2026-04-12 10:00:00' );
$source = new \PRAutoBlogger_Source_Data(
$this->get_source_data_fixture()
);
$this->assertSame( '', $data->get_content() );
$row = $source->to_db_row();
$this->assertIsArray( $row );
$this->assertArrayHasKey( 'source_type', $row );
$this->assertArrayHasKey( 'source_id', $row );
$this->assertArrayHasKey( 'title', $row );
$this->assertArrayHasKey( 'content', $row );
}
/**
* Test with empty/zero values.
*/
public function test_with_empty_values(): void {
$this->stub_current_time( '2026-04-12 10:00:00' );
$data = [
'id' => 0,
'source_type' => '',
'source_id' => '',
'subreddit' => '',
'title' => '',
'content' => '',
'author' => '',
'score' => 0,
'comment_count' => 0,
'permalink' => '',
'collected_at' => '2026-04-12 10:00:00',
'metadata' => [],
];
$source = new \PRAutoBlogger_Source_Data( $data );
$this->assertSame( 0, $source->get_id() );
$this->assertSame( 0, $source->get_score() );
$this->assertSame( 0, $source->get_comment_count() );
$this->assertIsArray( $source->get_metadata() );
}
}