-
-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.0: Move "is unit test" utility functions to dedicated trait #1960
Conversation
It seems to me that, in both cases, the current status of what this helper does is not truthful. If we want to cover both cases, a new helper would need to be created which will account for the scope of the unit test, and a check should be added in the current helper that would exclude the case if it's in a unit case, but just cover the part where we check if the token is in a test method. If I'm not mistaken, this could be useful if anonymous functions or classes are used in a method within a test class. Don't ask me how I know of this case, let's just say that integration tests in WordPress require some level of imagination π |
Well, as the method is not used in WPCS at this time, this discussion is theoretical. Maybe we should just remove the method completely until such time that WPCS needs it again. More than anything, it would be better for custom rulesets to exclude the test directories completely for these sniffs. This can't be done from within WPCS as we don't know what the name of the test directory is for individual projects. |
As this is going to be for a major release, I'm not against the behaviour here being clarified in a particular way, even if there is no extra helper method to help answer the other question. |
@dingo-d @GaryJones What can I do to move this PR forward ? I'm happy to adjust it, but at this moment there doesn't seem to be consensus on if and how it should be adjusted. Would it be possible to continue the above discussion ? |
I'm fine with removing it, especially since it's not being used at the moment. We can easily return it back (it's in the commits). |
I'm in agreement here. |
546c018
to
fe2bf3e
Compare
Ok, in that case, the method is hereby gone. I've added a new commit at the start of the stack and removed the "improve the method" commit. The changes have been annotated in the above PR description. |
This method is currently unused in WPCS and it is not 100% clear what the method _should_ do, so removing the method for now. If needs be it can be brought back later if/when there is more clarity on what the method is supposed to solve.
β¦ait` The "is this test code" related utilities are only used by a small set of sniffs, so are better placed in a dedicated trait. This moves the (`public`) `$custom_test_class_whitelist` and the `$custom_test_class_whitelist` properties as well as the `is_test_class` method to a new `WordPressCS\WordPress\Helpers\IsUnitTestTrait` and starts using that trait in the relevant sniffs. The trait has been made stand-alone, i.e. it does not presume that a sniff implementing it extends the `WordPressCS\WordPress\Sniff` class. This makes it easier to (re-)use the traits, even when a sniff extends an abstract sniff class from PHPCS itself or from PHPCSUtils. Related to 1465
This implements PHPCSUtils alternatives for PHPCS native methods and other PHPCSUtils utilities in the `IsUnitTestTrait::is_test_class()` code. The functionality is unchanged (just less buggy).
The protected property `$test_class_whitelist` has been renamed to `$known_test_classes` as a step towards removing whitelist/blacklist terminology.
The public property `$custom_test_class_whitelist` has been renamed to `$custom_test_classes` as a step towards removing whitelist/blacklist terminology. Includes adjusting the property name in all tests which included the property. To do before release: * Annotate this change in the wiki custom properties list
The function local variable `$whitelist` has been renamed to `$known_test_classes` as a step towards removing whitelist/blacklist terminology.
Add two more test cases which were added to `/tests/phpunit/includes/`. Note: WP Core contains more test cases, but those are in the `tests/phpunit/tests` directory, while the test cases in `/tests/phpunit/includes/` are supposed to be the "public test API", which is why those are the only ones in this list.
fe2bf3e
to
0248285
Compare
Rebased now the GH actions PR has been merged. Would appreciate a merge if the build passes, |
* upstream/develop: (106 commits) PHP/NoSilencedErrors: add `imagecreatefromwebp` PHP/NoSilencedErrors: add libxml_disable_entity_loader() GH Actions: turn display_errors on Bug report template: various improvements QA: import all used classes QA: use fully qualified global constant references QA: remove unnecessary assignment CS: get rid of "commented out code" warnings OneObjectStructurePerFile: move from `Extra` to `Core` GH Actions: don't use cs2pr in quicktest GH Actions: report CS violations in the PR GH Actions: get the tests running on PHP 8.1 (nightly) PHP 8.1 compatibility: fix deprecation notice [2] PHP 8.1 compatibility: fix deprecation notice [1] GH Actions: don't test against PHPCS 4.x (yet) Security/EscapeOutput: bug fix - allow for basename() being fully qualified 3.0: Move "is unit test" utility functions to dedicated trait (WordPress#1960) Move from TravisCI to GitHub Actions (WordPress#1965) Travis: add build against PHP 8.0 PHP/RestrictedPHPFunctions: update error message ...
This PR will be easiest to review by looking through the individual commits.
Related to #1877
Commit Details
β Sniff: remove the
is_token_in_test_method()
method πThis method is currently unused in WPCS and it is not 100% clear what the method should do, so removing the method for now.
If needs be it can be brought back later if/when there is more clarity on what the method is supposed to solve.
Move "is this test code" related utilities to dedicated
IsUnitTestTrait
The "is this test code" related utilities are only used by a small set of sniffs, so are better placed in a dedicated trait.
This moves the (
public
)$custom_test_class_whitelist
and the$test_class_whitelist
properties as well asthetheis_token_in_test_method()
andis_test_class
method to a newWordPressCS\WordPress\Helpers\IsUnitTestTrait
and starts using that trait in the relevant sniffs.The trait has been made stand-alone, i.e. it does not presume that a sniff implementing it extends the
WordPressCS\WordPress\Sniff
class.This makes it easier to (re-)use the traits, even when a sniff extends an abstract sniff class from PHPCS itself or from PHPCSUtils.
Note: theis_token_in_test_method()
method is currently unused in WPCS, but seems useful enough to keep in place.Related to #1465
IsUnitTestTrait::is_test_class(): use PHPCSUtils
This implements PHPCSUtils alternatives for PHPCS native methods and other PHPCSUtils utilities in the
IsUnitTestTrait::is_test_class()
code.The functionality is unchanged (just less buggy).
β
IsUnitTestTrait::is_token_in_test_method(): implement PHPCSUtils/improve handling nested functionsIsUnitTestTrait: rename protected property
The protected property
$test_class_whitelist
has been renamed to$known_test_classes
as a step towards removing whitelist/blacklist terminology.IsUnitTestTrait: rename public property
The public property
$custom_test_class_whitelist
has been renamed to$custom_test_classes
as a step towards removing whitelist/blacklist terminology.Includes adjusting the property name in all tests which included the property.
Related to #1915
To do before release:
IsUnitTestTrait: rename local variable
The function local variable
$whitelist
has been renamed to$known_test_classes
as a step towards removing whitelist/blacklist terminology.IsUnitTestTrait: add missing WP Core test case classes
Add two more test cases which were added to
/tests/phpunit/includes/
.Note: WP Core contains more test cases, but those are in the
tests/phpunit/tests
directory, while the test cases in/tests/phpunit/includes/
are supposed to be the "public test API", which is why those are the only ones in this list.IsUnitTestTrait: improve documentation