Repository navigation
Extend WP_CLI\Tests\TestCase and analyse tests with PHPStan - #705
Merged
Merged
Conversation
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
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
I've re-run the failed jobs once. Generated by Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The PHPStan rule added in wp-cli/wp-cli-tests#437 reports test classes that extend
PHPUnit\Framework\TestCasedirectly. This switches the four unit tests here toWP_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 analysetests/, so the rule actually runs here.Test case parent
tests/Block_Processor_HelperTest.php,tests/Compat/PolyfillsTest.php,tests/Compat/WP_Block_ProcessorTest.phpandtests/Compat/WP_HTML_SpanTest.phpnow extendWP_CLI\Tests\TestCase.setUp()fixture inBlock_Processor_HelperTestbecomesset_up(), which the polyfilled test case calls on every PHPUnit version.composer phpunitloads the class through the wp-cli-tests bootstrap, which then includes this package's owntests/bootstrap.php, so the polyfills under test still load.PHPStan covers
tests/testsis added topaths.vendor/wp-cli/wp-cli-tests/tests/includes/TestCase.phpis added toscanFilesbecause that class is not autoloaded.assertTrue()where they compared againsttrue.tests/phpstan/WP_Block_Processor.stubcorrects the return type ofWP_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 howBlock_Processor_Helperuses it too.ignoreErrorsentries are scoped to single test files, following the existing entry forBlock_Processor_Helper:WPCompat.methodNotAvailablereports forWP_Block_ProcessorinWP_Block_ProcessorTest. Those tests exercise the polyfill whatever the WordPress version is.no.private.classreports fornew WP_HTML_SpaninWP_HTML_SpanTest, which tests the polyfill of that class.Testing
composer phpunit: 132 tests pass, before and after.tests/: it reports all four classes before this change and none after.composer phpstan: clean fortests/. Locally it also reports two unmatched@phpstan-ignore method.deprecatedcomments insrc/User_Session_Command.php. That depends on the PHP version, and the PHPStan job on the default branch passes.composer phpcsreports four indentation errors in a PHP block infeatures/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