Skip to content

Add unit tests for ErrorMessageExtractor - #297

Open
noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:test/error-message-extractor-unit-tests
Open

noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:test/error-message-extractor-unit-tests

Conversation

@noruzzamans

Copy link
Copy Markdown

Description

This PR introduces a comprehensive PHPUnit unit test suite for the ErrorMessageExtractor utility class (WordPress\AiClient\Providers\Http\Util\ErrorMessageExtractor).

ErrorMessageExtractor centralizes the parsing and extraction of human-readable error messages from various API response shapes returned by generative AI providers (such as OpenAI, Google Gemini, Anthropic) when HTTP errors occur (ClientException and ServerException).

While the sibling ResponseUtilTest exercises high-level HTTP status code handling, ErrorMessageExtractor itself previously lacked a dedicated unit test suite. This contribution fills that gap by verifying all supported response structures, format precedence, and edge-case payload validation.


Test Coverage Summary

The newly added test suite (tests/unit/Providers/Http/Util/ErrorMessageExtractorTest.php) contains 40 tests and 42 assertions covering:

  1. Non-Array Input Handling: Asserts that extractFromResponseData safely returns null when input is not an array (null, empty string, non-empty string, integer, float, boolean, object).
  2. Array-Wrapped Error Objects: Verifies extraction from [ [ 'error' => [ 'message' => '...' ] ] ] (Google Gemini / batch error format).
  3. Nested Associative Error Object: Verifies extraction from [ 'error' => [ 'message' => '...' ] ] (OpenAI format).
  4. Simple Error String: Verifies extraction from [ 'error' => '...' ].
  5. Direct Message String: Verifies extraction from [ 'message' => '...' ].
  6. Format Precedence: Confirms resolution hierarchy when multiple candidate error formats are present in the same payload.
  7. Malformed & Unrecognized Data Structures: Asserts null is safely returned for empty arrays, missing keys, non-string messages (integers, booleans, arrays, nulls), and non-array array elements.

Verification & Testing

All local test suites and linters pass cleanly:

# Isolated unit test
vendor/bin/phpunit tests/unit/Providers/Http/Util/ErrorMessageExtractorTest.php

# Full unit test suite (1242 tests, 4408 assertions)
composer test:unit

# Coding standards (PER / PSR-12)
composer phpcs

# Static analysis
composer phpstan

Use of AI Tools

AI assistance: Yes
Model(s): Gemini 3.8 Flash High
Used for: Authoring PHPUnit unit tests, data providers, and test documentation for ErrorMessageExtractor. Code reviewed and locally validated by human contributor.

@codecov

codecov Bot commented Sep 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.03%. Comparing base (77a5995) to head (b5f0ee1).

Additional details and impacted files
@@             Coverage Diff              @@
##              trunk     #297      +/-   ##
============================================
+ Coverage     86.58%   87.03%   +0.44%     
  Complexity     1383     1383              
============================================
  Files            69       69              
  Lines          4449     4449              
============================================
+ Hits           3852     3872      +20     
+ Misses          597      577      -20     
Flag Coverage Δ
unit 87.03% <ø> (+0.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@noruzzamans
noruzzamans marked this pull request as ready for review September 20, 2026 08:15
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant