Skip to content

Extend WP_CLI\Tests\TestCase and analyse tests with PHPStan - #705

Merged
swissspidy merged 2 commits into
mainfrom
claude/elegant-mendel-hm9kcu
Dec 4, 2026
Merged

swissspidy merged 2 commits into
mainfrom
claude/elegant-mendel-hm9kcu

Conversation

@swissspidy

@swissspidy swissspidy commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

The PHPStan rule added in wp-cli/wp-cli-tests#437 reports test classes that extend PHPUnit\Framework\TestCase directly. This switches the four unit tests here to WP_CLI\Tests\TestCase, which adds the PHPUnit Polyfills so the same tests run on every PHPUnit version the supported PHP versions need. This is the same change as wp-cli/wp-cli#6485. It also makes PHPStan analyse tests/, so the rule actually runs here.

Test case parent

  • tests/Block_Processor_HelperTest.php, tests/Compat/PolyfillsTest.php, tests/Compat/WP_Block_ProcessorTest.php and tests/Compat/WP_HTML_SpanTest.php now extend WP_CLI\Tests\TestCase.
  • The setUp() fixture in Block_Processor_HelperTest becomes set_up(), which the polyfilled test case calls on every PHPUnit version.

composer phpunit loads the class through the wp-cli-tests bootstrap, which then includes this package's own tests/bootstrap.php, so the polyfills under test still load.

PHPStan covers tests/

  • tests is added to paths. vendor/wp-cli/wp-cli-tests/tests/includes/TestCase.php is added to scanFiles because that class is not autoloaded.
  • The tests now assert that extracted blocks, parsed attributes and spans are not null before reading from them, and use assertTrue() where they compared against true.
  • tests/phpstan/WP_Block_Processor.stub corrects the return type of WP_Block_Processor::extract_full_block_and_advance(). WordPress documents it as a list of blocks (array[]|null), but it returns the single block it extracted, which is how Block_Processor_Helper uses it too.
  • Two ignoreErrors entries are scoped to single test files, following the existing entry for Block_Processor_Helper:
    • The WPCompat.methodNotAvailable reports for WP_Block_Processor in WP_Block_ProcessorTest. Those tests exercise the polyfill whatever the WordPress version is.
    • The no.private.class reports for new WP_HTML_Span in WP_HTML_SpanTest, which tests the polyfill of that class.

Testing

  • composer phpunit: 132 tests pass, before and after.
  • I ran the new rule from wp-cli-tests#437 over tests/: it reports all four classes before this change and none after.
  • composer phpstan: clean for tests/. Locally it also reports two unmatched @phpstan-ignore method.deprecated comments in src/User_Session_Command.php. That depends on the PHP version, and the PHPStan job on the default branch passes.
  • composer phpcs reports four indentation errors in a PHP block in features/option.feature. They are already there on the default branch and are not touched here.

https://claude.ai/code/session_01UWTd5n3ZupvgHr4fEBJCGp


Generated by Claude Code

Summary by CodeRabbit

  • Tests
    • Updated test setup and assertions to align with the project’s test framework and clarify checks for extracted block data.
  • Chores
    • Improved static analysis coverage for test files and added type information for block extraction results.

The new PHPStan rule in wp-cli-tests reports test classes that extend
PHPUnit's TestCase directly. WP_CLI\Tests\TestCase adds the PHPUnit
Polyfills, so the tests run on every PHPUnit version that the supported
PHP versions need. run-php-unit-tests loads it through the wp-cli-tests
bootstrap, which then includes this package's own bootstrap.

The fixture method becomes set_up(), which the polyfilled test case
calls on every PHPUnit version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5n3ZupvgHr4fEBJCGp
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The tests now use WP-CLI’s test case and add explicit null and type assertions. PHPStan now analyzes the tests, loads a WP_Block_Processor stub, and applies path-specific ignores.

Changes: Test and static-analysis updates

Layer / File(s) Summary
Test setup and assertion updates
tests/Block_Processor_HelperTest.php, tests/Compat/PolyfillsTest.php, tests/Compat/WP_Block_ProcessorTest.php, tests/Compat/WP_HTML_SpanTest.php
Test classes use WP_CLI\Tests\TestCase. Block processor tests check for non-null results and array attributes before accessing them.
PHPStan test coverage and stub
phpstan.neon.dist, tests/phpstan/WP_Block_Processor.stub
PHPStan analyzes tests, loads a stub for WP_Block_Processor::extract_full_block_and_advance(), and ignores specified reports in polyfill tests.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Suggested reviewers: schlessera

Merge Risk: Minimal · up to 667eb

This change only updates tests and static-analysis configuration, with no production behavior change. It looks safe to merge.

Security architecture risk: Minimal · up to 667eb

The compatibility tests adopt the WP-CLI test lifecycle, and PHPStan starts checking those tests. The block-extractor stub changes how PHPStan models the return value, not what the extractor returns at runtime.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names two changes: switching test classes to WP_CLI\Tests\TestCase and analysing tests with PHPStan. Both appear in the file summaries and objectives. The title is specific and matches the m…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names two changes: switching test classes to WP_CLI\Tests\TestCase and analysing tests with PHPStan. Both appear in the file summaries and objectives. The title is specific and matches the main changes, though it lists two changes rather than one.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 82e3297b-db08-43fd-9c22-7243ea5c433b
Commits

Reviewing files that changed from the base of the PR and between 2f2baa2 and 667ebb5.

Files selected for processing: 6

  • phpstan.neon.dist
  • tests/Block_Processor_HelperTest.php
  • tests/Compat/PolyfillsTest.php
  • tests/Compat/WP_Block_ProcessorTest.php
  • tests/Compat/WP_HTML_SpanTest.php
  • tests/phpstan/WP_Block_Processor.stub

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions github-actions Bot added the scope:testing Related to testing label Dec 4, 2026
Adds `tests` to the PHPStan paths, with the wp-cli-tests TestCase as a
scan file since it is not autoloaded, and fixes what that reports:

- Assert that extracted blocks, parsed attributes and spans are not null
  before reading from them.
- Add a stub for `WP_Block_Processor::extract_full_block_and_advance()`,
  which WordPress documents as returning a list of blocks although it
  returns the single block it extracted.
- Scope the WordPress-version and private-class reports for the polyfill
  tests to those test files, like the existing entry for the helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5n3ZupvgHr4fEBJCGp
@swissspidy swissspidy changed the title Extend WP_CLI\Tests\TestCase in the unit tests Extend WP_CLI\Tests\TestCase and analyse tests with PHPStan Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member Author

The failing PHP 8.6 / WP trunk / SQLite Behat job comes from a timing-dependent scenario, not from this PR, which only touches tests/ and phpstan.neon.dist.

features/post.feature:732 creates the Locked post without a --post_date, so it gets the current time. Whenever it is created at second 7 of a minute, wp post list --second=7 returns it as well, which is exactly what happened here (+Locked). Giving that post a fixed --post_date would make the scenario deterministic. That belongs in a separate change.

I've re-run the failed jobs once.


Generated by Claude Code

@swissspidy
swissspidy marked this pull request as ready for review December 4, 2026 21:26
@swissspidy
swissspidy requested a review from a team as a code owner December 4, 2026 21:26
Copilot AI balanced review requested due to automatic review settings December 4, 2026 21:26

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@swissspidy
swissspidy merged commit 3aaac41 into main Dec 4, 2026
133 of 134 checks passed
@swissspidy
swissspidy deleted the claude/elegant-mendel-hm9kcu branch December 4, 2026 22:13
@github-actions github-actions Bot added this to the 3.0.7 milestone Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants