Skip to content

Add unit tests for OpenAiProvider - #53

Open
noruzzamans wants to merge 7 commits into
WordPress:trunkfrom
noruzzamans:test/openai-provider-unit-tests
Open

noruzzamans wants to merge 7 commits into
WordPress:trunkfrom
noruzzamans:test/openai-provider-unit-tests

Conversation

@noruzzamans

Copy link
Copy Markdown

Description

This PR introduces a dedicated unit test suite for the OpenAiProvider class (WordPress\OpenAiAiProvider\Provider\OpenAiProvider).

OpenAiProvider is the central entry class for the AI Provider for OpenAI. It handles:

  • Base URL and endpoint construction (url()).
  • Provider metadata creation (metadata()) including ID, name, cloud type, API key authentication, localized descriptions, and SVG logo path.
  • Model directory instantiation (modelMetadataDirectory()) and provider availability checking (availability()).
  • Factory dispatching for all supported model capabilities:
    • Text generation (OpenAiTextGenerationModel)
    • Image generation (OpenAiImageGenerationModel)
    • Embedding generation (OpenAiEmbeddingGenerationModel)
    • Text-to-speech conversion (OpenAiTextToSpeechConversionModel)
  • Exception handling for unsupported capabilities (RuntimeException).

While individual model and directory classes had unit tests, OpenAiProvider itself previously lacked dedicated test coverage.


Test Coverage Summary

The newly added test suite (tests/unit/Provider/OpenAiProviderTest.php) contains 10 tests and 25 assertions covering:

  1. Base URL & Endpoint Construction: Asserts url() resolves https://api.openai.com/v1 and correctly formats subpaths with or without leading slashes.
  2. Provider Metadata Verification: Confirms all provider metadata properties (id, name, cloud type, credentials URL, authentication method, localized description, logo path).
  3. Directory & Availability Instantiation: Asserts proper class instances and verifies static caching across consecutive calls.
  4. Model Factory Capabilities: Tests that text, image, embedding, and text-to-speech capabilities correctly resolve to their corresponding model instances.
  5. Unsupported Capabilities & Error Handling: Asserts RuntimeException is thrown with expected error messages for unknown or empty capability sets.

Verification & Testing

All local test suites and linters pass cleanly:

# Isolated unit tests
vendor/bin/phpunit tests/unit/Provider/OpenAiProviderTest.php

# Full unit test suite (60 tests, 243 assertions)
vendor/bin/phpunit

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

# Static analysis (Level MAX)
composer phpstan

Use of AI Tools

AI assistance: Yes
Model(s): Gemini 3.8 Flash High
Used for: Authoring PHPUnit unit tests for OpenAiProvider and drafting PR documentation. Code was manually reviewed, locally executed, and verified.

@noruzzamans
noruzzamans marked this pull request as ready for review September 20, 2026 08:39
@noruzzamans
noruzzamans requested a review from a team September 20, 2026 08:39
@github-actions

github-actions Bot commented Sep 20, 2026 •

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 <noruzzaman@git.wordpress.org>
Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

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

@jeffpaul jeffpaul added this to the 1.3.0 milestone Sep 21, 2026

@saarnilauri saarnilauri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid looking PR with a nice test addition for a class that genuinely had no direct coverage.

I added few comments that might be good additions to the PR.

Comment thread tests/unit/Provider/OpenAiProviderTest.php Outdated
Comment thread tests/unit/Provider/OpenAiProviderTest.php Outdated
Comment thread tests/unit/Provider/OpenAiProviderTest.php Outdated
noruzzamans and others added 4 commits September 22, 2026 12:31
Co-authored-by: Lauri Saarni <36833033+saarnilauri@users.noreply.github.com>
Co-authored-by: Lauri Saarni <36833033+saarnilauri@users.noreply.github.com>
Co-authored-by: Lauri Saarni <36833033+saarnilauri@users.noreply.github.com>
@noruzzamans

Copy link
Copy Markdown
Author

Solid looking PR with a nice test addition for a class that genuinely had no direct coverage.

I added few comments that might be good additions to the PR.

Thank you @saarnilauri for the helpful review and guidance!

I have accepted all 3 suggestions and updated the tests to cover the capability-skipping and precedence paths you mentioned. All tests and linters pass cleanly.

Could you please take another look when you get a chance?

@saarnilauri saarnilauri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The first round changes landed well. See the comments for few more followup changes I suggest.

Comment thread tests/unit/Provider/OpenAiProviderTest.php Outdated
Comment thread tests/unit/Provider/OpenAiProviderTest.php Outdated

@saarnilauri saarnilauri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again. Now approving.

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.

3 participants